pike.git / lib / modules / String.pmod

version» Context lines:

pike.git/lib/modules/String.pmod:1:   #pike __REAL_VERSION__      #define BEGIN 32    + //! @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@}. + //!   constant count=__builtin.string_count; -  +  + //! @decl int width(string s) + //! + //! Returns the width in bits (8, 16 or 32) of the widest character + //! in @[s]. + //!   constant width=__builtin.string_width;      /*    * Implode an array of strings to an english 'list'    * ie. ({"foo","bar","gazonk"}) becomes "foo, bar and gazonk"    */ - string implode_nicely(array(string|int|float) foo, string|void and) +  + //! 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)   { -  if(!and) and="and"; +  if(!separator) separator="and";    foo=(array(string))foo;    switch(sizeof(foo))    {    case 0: return "";    case 1: return ([array(string)]foo)[0]; -  default: return foo[0..sizeof(foo)-2]*", "+" "+and+" "+foo[-1]; +  default: return foo[0..sizeof(foo)-2]*", "+" "+separator+" "+foo[-1];    }   }    - string capitalize(string s) + //! Convert the first character in @[str] to upper case, and return the + //! new string. + //! + //! @seealso + //! @[lower_case()], @[upper_case()] + //! + string capitalize(string str)   { -  return upper_case(s[0..0])+s[1..sizeof(s)]; +  return upper_case(str[0..0])+str[1..sizeof(str)];   }    - string sillycaps(string s) + //! Convert the first character in each word (separated by spaces) in + //! @[str] to upper case, and return the new string. + //! + string sillycaps(string str)   { -  return Array.map(s/" ",capitalize)*" "; +  return Array.map(str/" ",capitalize)*" ";   }    -  + //! 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 + //! @[`*()] + //!   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
pike.git/lib/modules/String.pmod:53:    * 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).    */ +  + //! Find the longest common prefix from an array of strings. + //!   string common_prefix(array(string) strs)   {    if(!sizeof(strs))    return "";       string strs0 = strs[0];    int n, i;       catch    {
pike.git/lib/modules/String.pmod:151:    str2 = str2[length + offset..];    continue;    }    }    if(strlen(str1))    str1 = str1[1..];    }    return fuzz;   }    + //! 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()] + //!   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;   }    - string trim_whites( string what ) + //! Trim leading and trailing spaces and tabs from the string @[s]. + //! + string trim_whites(string s)   { -  if (stringp (what)) { -  sscanf(what, "%*[ \t]%s", what); -  string rev = reverse(what); +  if (stringp(s)) { +  sscanf(s, "%*[ \t]%s", s); +  string rev = reverse(s);    sscanf(rev, "%*[ \t]%s", rev); -  return what[..strlen(rev) - 1]; +  return s[..strlen(rev) - 1];    }    -  return what; +  return s;   }    - string trim_all_whites( string what ) + //! Trim leading and trailing white spaces characters (@tt{" \t\r\n"@}) from + //! the string @[s]. + //! + string trim_all_whites(string s)   { -  if (stringp (what)) { -  sscanf(what, "%*[ \t\r\n]%s", what); -  string rev = reverse(what); +  if (stringp(s)) { +  sscanf(s, "%*[ \t\r\n]%s", s); +  string rev = reverse(s);    sscanf(rev, "%*[ \t\r\n]%s", rev); -  return what[..strlen(rev) - 1]; +  return s[..strlen(rev) - 1];    }    -  return what; +  return s;   }