1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
#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" 
 */ 
 
//! 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(!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]*", "+" "+separator+" "+foo[-1]; 
  } 
} 
 
//! 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(str[0..0])+str[1..sizeof(str)]; 
} 
 
//! 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(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 
} 
 
/* 
 * 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). 
 */ 
 
//! 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 
  { 
    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]; 
} 
 
class String_buffer { 
  array(string) buffer=allocate(BEGIN); 
  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; 
    } 
}; 
 
 
// 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) 
    { 
      tmp2 = str2[offset..]; 
      /* 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; 
        str1 = str1[length..]; 
        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; 
} 
 
//! Trim leading and trailing spaces and tabs from the string @[s]. 
//! 
string trim_whites(string s) 
{ 
  if (stringp(s)) { 
    sscanf(s, "%*[ \t]%s", s); 
    string rev = reverse(s); 
    sscanf(rev, "%*[ \t]%s", rev); 
    return s[..strlen(rev) - 1]; 
  } 
 
  return s; 
} 
 
//! Trim leading and trailing white spaces characters (@tt{" \t\r\n"@}) from 
//! the string @[s]. 
//! 
string trim_all_whites(string s) 
{ 
  if (stringp(s)) { 
    sscanf(s, "%*[ \t\r\n]%s", s); 
    string rev = reverse(s); 
    sscanf(rev, "%*[ \t\r\n]%s", rev); 
    return s[..strlen(rev) - 1]; 
  } 
 
  return s; 
}