a580e12000-09-27Fredrik Hübinette (Hubbe) #pike __REAL_VERSION__
a20af62000-09-26Fredrik Hübinette (Hubbe) 
c2a4061997-02-06Fredrik Hübinette (Hubbe) #define BEGIN 32
bfb5a62001-02-04Per Hedbor  constant Buffer = __builtin.Buffer;
2aec871999-05-25Mirar (Pontus Hagland) constant count=__builtin.string_count;
d46f731999-09-06Fredrik Hübinette (Hubbe) constant width=__builtin.string_width;
269ef02001-04-18Martin Stjernholm constant trim_whites = __builtin.string_trim_whites; constant trim_all_whites = __builtin.string_trim_all_whites;
fa306e2001-05-17Henrik Grubbström (Grubba) constant Iterator = __builtin.string_iterator; constant SplitIterator = __builtin.string_split_iterator;
fed7de2001-06-28Henrik Grubbström (Grubba) constant Replace = __builtin.multi_string_replace;
0c4ad02001-07-05Henrik Grubbström (Grubba) constant SingleReplace = __builtin.single_string_replace;
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.
63dd472001-07-26Martin Nilsson //! If the separator is omitted, the default is @tt{"and"@}.
8cee9e2000-12-12Henrik Grubbström (Grubba) //! 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()] //!
63dd472001-07-26Martin Nilsson int(0..100) fuzzymatch(string a, string b)
3b319f1998-07-28David Hedbor { 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 
7628552001-03-13Martin Nilsson //! Returns the soundex value of @[word] according to //! the original Soundex algorithm, patented by Margaret O´Dell //! and Robert C. Russel in 1918. The method is based on the phonetic //! classification of sounds by how they are made. string soundex(string word) { word = upper_case(word); string first = word[0..0]; word = word[1..] - "A" - "E" - "H" - "I" - "O" - "U" - "W" - "Y"; word = replace(word, ([ "B":"1", "F":"1", "P":"1", "V":"1", "C":"2", "G":"2", "J":"2", "K":"2", "Q":"2", "S":"2", "X":"2", "Z":"2", "D":"3", "T":"3", "L":"4", "M":"5", "N":"5", "R":"6" ]) ); word = replace(word, ({"11", "22", "33", "44", "55", "66" }), ({"", "", "", "", "", "", })); word+="000"; return first + word[..2]; }
f079cb2001-10-20Mirar (Pontus Hagland)  // ---------------------------------------------------------------- static private class _Elite { // neccesary translation (in order) array(array(string|array(string))) elite_short= ({ ({" you are"," your"}), ({" you'r"," your"}), ({" what the"," wt"}),
8667992001-10-21Mirar (Pontus Hagland)  ({" wt fuck"," wt\1001"}), ({" wt\1001?"," wt\1001!"}),
f079cb2001-10-20Mirar (Pontus Hagland)  ({" download"," d/l"}), ({" upload"," u/l"}), ({"picture","pic"}), ({" pornography"," pr0n"}), ({" porn"," pr0n"}),
cc7ab72001-10-20Mirar (Pontus Hagland)  ({"cool","kewl"}),
5102362001-10-20Mirar (Pontus Hagland)  ({" elite "," 1337 "}),
8667992001-10-21Mirar (Pontus Hagland)  ({"qu","kw"}),
682d9d2001-10-21Mirar (Pontus Hagland)  ({" too "," 2 "}), ({" too."," 2."}), ({" too!"," 2!"}),
8667992001-10-21Mirar (Pontus Hagland)  ({"japanese",".jp"}), ({"japan",".jp"}), ({"swedish",".se"}), ({"sweden",".se"}), ({"chinese",".cn"}), ({"china",".cn"}), ({"france",".fr"}),
f079cb2001-10-20Mirar (Pontus Hagland)  }); // optional recursive translation (in order) array(array(string|array(string))) elite_trans= ({ ({"you",({"u","j00"})}), ({" are "," r "}), ({" ok "," k "}),
8667992001-10-21Mirar (Pontus Hagland)  ({" any "," ne "}),
f079cb2001-10-20Mirar (Pontus Hagland)  ({"dude","dood"}), ({"newbie","noob"}),
8667992001-10-21Mirar (Pontus Hagland)  ({"fuck","\1001-"}), ({"girl","gurl"}), ({"choose","pick"}),
f079cb2001-10-20Mirar (Pontus Hagland) 
5102362001-10-20Mirar (Pontus Hagland)  ({"one","1"}), ({"two","2"}), ({"ate","8"}), ({"twi","2"}),
8667992001-10-21Mirar (Pontus Hagland)  ({" to"," 2"}),
5102362001-10-20Mirar (Pontus Hagland)  ({"ight","8t"}), ({"ite","eet"}),
99e9fb2001-10-20Mirar (Pontus Hagland)  ({"four","4"}), ({"fore","4"}), ({"for","4"}),
5102362001-10-20Mirar (Pontus Hagland) 
8667992001-10-21Mirar (Pontus Hagland)  ({"ea","ee"}), ({"nn","n"}), ({"ff","\1002h"}), ({"f","\1002h"}), ({"cks",({"xor"})}),
f079cb2001-10-20Mirar (Pontus Hagland)  ({"cks",({"x","xx","xz"})}), ({"cs ",({"kz ","cz "})}), ({"ks",({"x","xz"})}), ({"cs",({"x","xz"})}),
8667992001-10-21Mirar (Pontus Hagland)  ({"ck ",({"xer "})}),
f079cb2001-10-20Mirar (Pontus Hagland)  ({"ck",({"k","cc"})}), ({"ers ",({"ors "})}), ({"er ",({"or "})}), ({"ed ",({"edz "})}),
8667992001-10-21Mirar (Pontus Hagland)  ({"me ",({"mez "})}),
5102362001-10-20Mirar (Pontus Hagland)  ({" el",({" l"})}),
f079cb2001-10-20Mirar (Pontus Hagland)  ({"s ",({"z "})}),
8667992001-10-21Mirar (Pontus Hagland)  ({"s!",({"z!"})}), ({"s.",({"z."})}),
f079cb2001-10-20Mirar (Pontus Hagland)  ({"s,",({"z,"})}),
8667992001-10-21Mirar (Pontus Hagland)  ({"s",({"z"})}), ({". ","! "}), ({"! ","!! "}), ({"? ",({"?? ","!? "})}),
f079cb2001-10-20Mirar (Pontus Hagland)  }); // optional one-time character translation mapping(string:array(string)) elite_char= ([
682d9d2001-10-21Mirar (Pontus Hagland)  "a":({"4","@"}),
f079cb2001-10-20Mirar (Pontus Hagland)  "b":({"8"}), "c":({"("}),
2f85002001-10-21Mirar (Pontus Hagland)  "d":({"|)","|]"}),
f079cb2001-10-20Mirar (Pontus Hagland)  "e":({"3"}), "f":({}), "g":({"6"}),
8667992001-10-21Mirar (Pontus Hagland)  "h":({"|-|"}),
f079cb2001-10-20Mirar (Pontus Hagland)  "i":({"1","|"}),
99e9fb2001-10-20Mirar (Pontus Hagland)  "j":({}),
f079cb2001-10-20Mirar (Pontus Hagland)  "k":({"|<"}), "l":({"1","|_"}), "m":({"|V|","|\\/|"}), "n":({"|\\|"}), "o":({"0","()"}),
8667992001-10-21Mirar (Pontus Hagland)  "p":({"I=","p"}),
f079cb2001-10-20Mirar (Pontus Hagland)  "q":({}), "r":({}), "s":({"5"}), "t":({"+","7"}),
8667992001-10-21Mirar (Pontus Hagland)  "u":({"|_|"}),
f079cb2001-10-20Mirar (Pontus Hagland)  "v":({"\\/"}),
8667992001-10-21Mirar (Pontus Hagland)  "w":({"\\/\\/"}),
f079cb2001-10-20Mirar (Pontus Hagland)  "x":({"><"}), "y":({}),
8667992001-10-21Mirar (Pontus Hagland)  "z":({}),
f079cb2001-10-20Mirar (Pontus Hagland)  "0":({"()"}), "1":({"|","l"}), "2":({}), "3":({"E"}), "4":({}), "5":({"S"}), "6":({"G"}), "7":({}), "8":({}), "9":({}), "0":({"()","O"}), ]);
2f85002001-10-21Mirar (Pontus Hagland) // 8 bit variants mapping(string:array(string)) elite_char8= ([
682d9d2001-10-21Mirar (Pontus Hagland)  "!":"¡!"/1,
2f85002001-10-21Mirar (Pontus Hagland)  "?":"¿"/1, "0":"º"/1, "1":"¹"/1, "2":"²"/1, "3":"³"/1, "a":"àáâãäåª"/1, "b":"þß"/1, "c":"碩"/1, "d":"ð"/1, "e":"èéêë"/1, "f":"£"/1,
682d9d2001-10-21Mirar (Pontus Hagland)  "i":"ìíîï"/1,
2f85002001-10-21Mirar (Pontus Hagland)  "n":"ñ"/1, "o":"òóôõöøº"/1, "p":"þ"/1, "r":"®"/1, "s":"§"/1, "u":"ùúûüµ"/1, "x":"÷"/1, "y":"ýÿ¥"/1, "A":"ÀÁÂÃÄŪ"/1, "B":"Þß"/1, "C":"Ç¢©"/1, "D":"Ð"/1, "E":"ÈÉÊË"/1, "F":"£"/1, "I":"ÌÍÎÏ "/1, "N":"Ñ"/1, "O":"ÒÓÔÕÖغ"/1, "P":"Þ"/1, "R":"®"/1, "S":"§"/1, "U":"ÙÚÛܵ"/1, "X":"÷"/1, "Y":"Ýÿ¥"/1, ]);
f079cb2001-10-20Mirar (Pontus Hagland) // how do I mark up this correctly? It's String.Elite.foobar. // translates one word to 1337. The optional // argument leetp is the maximum percentage of // leetness (100=max leet, 0=no leet). // elite_word only do character-based translation, // for instance from "k" to "|<", but no language // translation (no "cool" to "kewl").
2f85002001-10-21Mirar (Pontus Hagland)  string elite_word(string in,void|int leetp,void|int eightbit)
f079cb2001-10-20Mirar (Pontus Hagland)  { if (zero_type(leetp)) leetp=50; // aim for 50% leetness
8667992001-10-21Mirar (Pontus Hagland)  else if (!leetp) return replace(in,"\1001\1002\1003"/1,"fpl"/1);
f079cb2001-10-20Mirar (Pontus Hagland) 
2f85002001-10-21Mirar (Pontus Hagland)  array v; switch (eightbit) { case 0: v=rows(elite_char, lower_case(in)/1); break; case 2: v=rows(elite_char8, lower_case(in)/1); break; case 1: v=map(in/1, lambda(string s) { return ( elite_char[s]||({}) ) | ( elite_char8[s]||({}) ); }); break; default: error("argument 3: illegal value (expected eightbit 0..2)\n"); }
f079cb2001-10-20Mirar (Pontus Hagland)  multiset leet=(<>); multiset unleet=(<>); foreach (v;int i;array(string) d) if (!d || !sizeof(d)) unleet[i]=1; else leet[i]=1; // lower leet level to target leetness while (100*sizeof(leet)/sizeof(in)>leetp) { int z=((array)leet)[random(sizeof(leet))]; leet[z]=0; unleet[z]=1; } string res=""; foreach (v;int i;array(string) d) if (leet[i]) { res+=d[random(sizeof(d))]; }
cc7ab72001-10-20Mirar (Pontus Hagland)  else
8667992001-10-21Mirar (Pontus Hagland)  { string s=in[i..i]; if ( (<"\1001","\1002">)[s] ) s=replace(s,"\1001\1002\1003"/1,"fpl"/1); if (leetp>50) // add random caps res+=(i&1?upper_case:lower_case)(s); else res+=s; }
f079cb2001-10-20Mirar (Pontus Hagland)  return res; } // translates a string to 1337. The optional // argument leetp is the maximum percentage of // leetness (100=max leet, 0=no leet). // // The translation is performed in three steps, // first the neccesary elite translations (picture -> pic, // cool->kewl etc), then optional translations // (ok->k, dude->dood, -ers -> -orz), then // calls elite_word on the resulting words.
2f85002001-10-21Mirar (Pontus Hagland)  string elite_string(string in,void|int leetp,void|int eightbit)
f079cb2001-10-20Mirar (Pontus Hagland)  { if (zero_type(leetp)) leetp=50; // aim for 50% leetness in=" "+in+" "; foreach (elite_short;;[string what,array(string)|string dest]) { string res=""; int i; while ((i=search(lower_case(in),what))!=-1) { if (arrayp(dest)) dest=dest[random(sizeof(dest))]; res+=in[..i-1]+dest; in=in[i+strlen(what)..]; } in=res+in; } in=" "+in+" "; foreach (elite_trans;;[string what,array(string)|string dest]) { string res=""; int i; while ((i=search(lower_case(in),what))!=-1) { string r; if (dest && random(100)<leetp) { if (!arrayp(dest)) r=dest; else r=dest[random(sizeof(dest))]; } else r=what; res+=in[..i-1]+r; in=in[i+strlen(what)..]; } in=res+in; }
2f85002001-10-21Mirar (Pontus Hagland)  in=map(in/" "-({""}),elite_word,leetp,eightbit)*" ";
f079cb2001-10-20Mirar (Pontus Hagland)  return in; } }; _Elite Elite=_Elite();