a580e12000-09-27Fredrik Hübinette (Hubbe) #pike __REAL_VERSION__
a20af62000-09-26Fredrik Hübinette (Hubbe) 
c2a4061997-02-06Fredrik Hübinette (Hubbe) #define BEGIN 32
97e2492001-03-08Per Hedbor constant Buffer = __builtin.Buffer;
8cee9e2000-12-12Henrik Grubbström (Grubba) //! @decl string count(string haystack, string needle) //! //! This function counts the number of times the @[needle] //! can be found in @[haystack]. //! //! @note //! Intersections between needles are not counted, ie //! @tt{count("....","..")@} is @tt{2@}. //!
2aec871999-05-25Mirar (Pontus Hagland) constant count=__builtin.string_count;
8cee9e2000-12-12Henrik Grubbström (Grubba)  //! @decl int width(string s) //! //! Returns the width in bits (8, 16 or 32) of the widest character //! in @[s]. //!
d46f731999-09-06Fredrik Hübinette (Hubbe) constant width=__builtin.string_width;
2aec871999-05-25Mirar (Pontus Hagland) 
c2a4061997-02-06Fredrik Hübinette (Hubbe) /* * Implode an array of strings to an english 'list'
0662a92000-07-12Henrik Grubbström (Grubba)  * ie. ({"foo","bar","gazonk"}) becomes "foo, bar and gazonk"
c2a4061997-02-06Fredrik Hübinette (Hubbe)  */
8cee9e2000-12-12Henrik Grubbström (Grubba)  //! This function implodes a list of words to a readable string. //! If the separator is omitted, the default is <tt>"and"</tt>. //! If the words are numbers they are converted to strings first. //! //! @seealso //! @[`*()] //! string implode_nicely(array(string|int|float) foo, string|void separator)
c2a4061997-02-06Fredrik Hübinette (Hubbe) {
8cee9e2000-12-12Henrik Grubbström (Grubba)  if(!separator) separator="and";
d0d8b81999-07-07Henrik Wallin  foo=(array(string))foo;
c2a4061997-02-06Fredrik Hübinette (Hubbe)  switch(sizeof(foo)) { case 0: return "";
0662a92000-07-12Henrik Grubbström (Grubba)  case 1: return ([array(string)]foo)[0];
8cee9e2000-12-12Henrik Grubbström (Grubba)  default: return foo[0..sizeof(foo)-2]*", "+" "+separator+" "+foo[-1];
c2a4061997-02-06Fredrik Hübinette (Hubbe)  } }
8cee9e2000-12-12Henrik Grubbström (Grubba) //! Convert the first character in @[str] to upper case, and return the //! new string. //! //! @seealso //! @[lower_case()], @[upper_case()] //! string capitalize(string str)
d5190b1997-02-10Fredrik Hübinette (Hubbe) {
8cee9e2000-12-12Henrik Grubbström (Grubba)  return upper_case(str[0..0])+str[1..sizeof(str)];
d5190b1997-02-10Fredrik Hübinette (Hubbe) }
8cee9e2000-12-12Henrik Grubbström (Grubba) //! Convert the first character in each word (separated by spaces) in //! @[str] to upper case, and return the new string. //! string sillycaps(string str)
bcf9f61998-09-29Johan Schön {
8cee9e2000-12-12Henrik Grubbström (Grubba)  return Array.map(str/" ",capitalize)*" ";
bcf9f61998-09-29Johan Schön }
8cee9e2000-12-12Henrik Grubbström (Grubba) //! This function multiplies @[s] by @[num]. The return value is the same //! as appending @[s] to an empty string @[num] times. //! //! @note //! This function is obsolete, since this functionality has been incorporated //! into @[`*()]. //! //! @seealso //! @[`*()] //!
c2a4061997-02-06Fredrik Hübinette (Hubbe) string strmult(string str, int num) { #if 1 num*=strlen(str); while(strlen(str) < num) str+=str; return str[0..num-1]; #endif #if 0 return sprintf("%~n",str,strlen(str)*num); #endif }
11a6ca1999-09-15Fredrik Noring /* * string common_prefix(array(string) strs) * { * if(!sizeof(strs)) * return ""; * * for(int n = 0; n < sizeof(strs[0]); n++) * for(int i = 1; i < sizeof(strs); i++) * if(sizeof(strs[i]) <= n || strs[i][n] != strs[0][n]) * return strs[0][0..n-1]; * * return strs[0]; * } * * This function is a slightly optimised version based on the code * above (which is far more suitable for an implementation in C). */
8cee9e2000-12-12Henrik Grubbström (Grubba)  //! Find the longest common prefix from an array of strings. //!
11a6ca1999-09-15Fredrik Noring string common_prefix(array(string) strs) { if(!sizeof(strs)) return ""; string strs0 = strs[0]; int n, i; catch { for(n = 0; n < sizeof(strs0); n++) for(i = 1; i < sizeof(strs); i++) if(strs[i][n] != strs0[n]) return strs0[0..n-1]; }; return strs0[0..n-1]; }
c2a4061997-02-06Fredrik Hübinette (Hubbe) class String_buffer {
bcb2be2000-02-19Fredrik Noring  array(string) buffer=allocate(BEGIN);
c2a4061997-02-06Fredrik Hübinette (Hubbe)  int ptr=0; static void fix() { string tmp=buffer*""; buffer=allocate(strlen(tmp)/128+BEGIN); buffer[0]=tmp; ptr=1; } string get_buffer() { if(ptr != 1) fix(); return buffer[0]; } void append(string s) { if(ptr==sizeof(buffer)) fix(); buffer[ptr++]=s; } mixed cast(string to) { if(to=="string") return get_buffer(); return 0; } void flush() { buffer=allocate(BEGIN); ptr=0; } };
3b319f1998-07-28David Hedbor  // Do a fuzzy matching between two different strings and return a // "similarity index". The higher, the closer the strings match. static int low_fuzzymatch(string str1, string str2) { string tmp1, tmp2; int offset, length; int fuzz; fuzz = 0; while(strlen(str1) && strlen(str2)) { /* Now we will look for the first character of tmp1 in tmp2 */ if((offset = search(str2, str1[0..0])) != -1) {
74b1c01998-07-30Fredrik Hübinette (Hubbe)  tmp2 = str2[offset..];
3b319f1998-07-28David Hedbor  /* Ok, so we have found one character, let's check how many more */ tmp1 = str1; length = 1; while(1) { //*(++tmp1)==*(++tmp2) && *tmp1 if(length < strlen(tmp1) && length < strlen(tmp2) && tmp1[length] == tmp2[length]) length++; else break; } if(length >= offset) { fuzz += length;
74b1c01998-07-30Fredrik Hübinette (Hubbe)  str1 = str1[length..]; str2 = str2[length + offset..];
3b319f1998-07-28David Hedbor  continue; } } if(strlen(str1))
74b1c01998-07-30Fredrik Hübinette (Hubbe)  str1 = str1[1..];
3b319f1998-07-28David Hedbor  } return fuzz; }
8cee9e2000-12-12Henrik Grubbström (Grubba) //! This function compares two strings using a fuzzy matching //! routine. The higher the resulting value, the better the strings match. //! //! @seealso //! @[Array.diff()], @[Array.diff_compare_table()] //! @[Array.diff_longest_sequence()] //!
3b319f1998-07-28David Hedbor int fuzzymatch(string a, string b) { int fuzz; if(a == b) { fuzz = 100; } else { fuzz = low_fuzzymatch(a, b); fuzz += low_fuzzymatch(b, a); fuzz = fuzz*100/(strlen(a)+strlen(b)); } return fuzz; }
dac6262000-02-17Martin Nilsson 
8cee9e2000-12-12Henrik Grubbström (Grubba) //! Trim leading and trailing spaces and tabs from the string @[s]. //! string trim_whites(string s)
dac6262000-02-17Martin Nilsson {
8cee9e2000-12-12Henrik Grubbström (Grubba)  if (stringp(s)) { sscanf(s, "%*[ \t]%s", s); string rev = reverse(s);
dac6262000-02-17Martin Nilsson  sscanf(rev, "%*[ \t]%s", rev);
8cee9e2000-12-12Henrik Grubbström (Grubba)  return s[..strlen(rev) - 1];
dac6262000-02-17Martin Nilsson  }
8cee9e2000-12-12Henrik Grubbström (Grubba)  return s;
dac6262000-02-17Martin Nilsson }
8cee9e2000-12-12Henrik Grubbström (Grubba) //! Trim leading and trailing white spaces characters (@tt{" \t\r\n"@}) from //! the string @[s]. //! string trim_all_whites(string s)
dac6262000-02-17Martin Nilsson {
8cee9e2000-12-12Henrik Grubbström (Grubba)  if (stringp(s)) { sscanf(s, "%*[ \t\r\n]%s", s); string rev = reverse(s);
dac6262000-02-17Martin Nilsson  sscanf(rev, "%*[ \t\r\n]%s", rev);
8cee9e2000-12-12Henrik Grubbström (Grubba)  return s[..strlen(rev) - 1];
dac6262000-02-17Martin Nilsson  }
8cee9e2000-12-12Henrik Grubbström (Grubba)  return s;
dac6262000-02-17Martin Nilsson }