c2a4061997-02-06Fredrik Hübinette (Hubbe) #define BEGIN 32 /* * Implode an array of strings to an english 'list' * ie. ({"foo","bar","gazonk"}) beomces "foo, bar and gazonk" */ string implode_nicely(string *foo, string|void and) { if(!and) and="and"; switch(sizeof(foo)) { case 0: return ""; case 1: return foo[0]; default: return foo[0..sizeof(foo)-2]*", "+" "+and+" "+foo[-1]; } }
d5190b1997-02-10Fredrik Hübinette (Hubbe) string capitalize(string s) { return upper_case(s[0..0])+s[1..sizeof(s)]; }
bcf9f61998-09-29Johan Schön string sillycaps(string s) { return Array.map(s/" ",capitalize)*" "; }
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 } class String_buffer { 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; } };
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; } 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; }