// Evol Script // Author: Gumi // safe string manipulation functions // ** does not require PCRE // str() // returns whatever is passed, converted to string function script str { return "" + getarg(0); } // startswith("", "") // returns true if begins with function script startswith { return substr(getarg(0), 0, getstrlen(getarg(1)) - 1) == getarg(1); } // endswith("", "") // returns true if ends with function script endswith { .@t = getstrlen(getarg(0)); // total length .@n = getstrlen(getarg(1)); // substring length return substr(getarg(0), .@t - .@n, .@t - 1) == getarg(1); } // capitalize("") // returns with its first letter capitalized function script capitalize { return setchar(getarg(0), strtoupper(charat(getarg(0), 0)), 0); } // titlecase("" {, "" {, }}) // returns with the first letter of each word capitalized // if is true, the string is joined in a camelCase fashion function script titlecase { .@delimiter$ = getarg(1, " "); .@c = getarg(2, 0); explode(.@words$, getarg(0), .@delimiter$); for (.@i = (.@c ? 1 : 0); .@i < 255; ++.@i) { if (.@words$[.@i] == "") { break; } .@words$[.@i] = setchar(.@words$[.@i], strtoupper(charat(.@words$[.@i], 0)), 0); } return implode(.@words$, (.@c ? "" : .@delimiter$)); } // camelcase(""}) function script camelcase { return titlecase(getarg(0), getarg(1, " "), true); } // zfill("" {, {, ""}}) // returns padded to the left with up to width function script zfill { .@str$ = getarg(0); .@width = getarg(1, 8); .@padding$ = getarg(2, "0"); for (.@s = getstrlen(.@str$); .@s < .@width; ++.@s) { .@str$ = .@padding$ + .@str$; } return .@str$; } // format_number( {, ""}) // formats a number properly function script format_number { .@number$ = str(getarg(0)); .@len = getstrlen(.@number$); .@separator$ = getarg(1, ","); if (getargcount() < 2 && playerattached()) { // get from user language switch (Lang) { case LANG_FR: .@separator$ = " "; break; // French case LANG_DE: .@separator$ = "."; break; // Germanic case LANG_PTBR: .@separator$ = "."; break; // Brazilian default: .@separator$ = ","; // English (default) } } for (.@i = .@len - 3; .@i > 0; .@i -= 3) { .@number$ = insertchar(.@number$, .@separator$, .@i); } return .@number$; } // fnum() // alias for format_number function script fnum { return format_number(getarg(0)); } // strip("") // removes spaces at the start and end function script strip { .@s$ = getarg(0); if (.@s$ == "") { return ""; } .@start = 0; .@end = getstrlen(.@s$) - 1; for (.@i = .@start; .@i < .@end; .@i++) { if (charat(.@s$, .@i) != " ") { break; } else { .@start++; } } for (.@i = .@end; .@i >= .@start; .@i--) { if (charat(.@s$, .@i) != " ") { break; } else { .@end--; } } //debugmes "STRIP.DEBUG MODE ENABLED BY JESUSALVA. PASSING SUBSTRING PARAMS"; //debugmes "String \""+.@s$+"\" from "+str(.@start)+" to "+str(.@end); return substr(.@s$, .@start, .@end); } // reverse("") // returns reversed function script reverse { .@str$ = getarg(0); .@len = getstrlen(.@str$); for (.@i = 0; .@i < (.@len / 2); ++.@i) { .@tmp$ = charat(.@str$, .@i); .@str$ = setchar(.@str$, charat(.@str$, (.@len - 1 - .@i)), .@i); // a <= b .@str$ = setchar(.@str$, .@tmp$, (.@len - 1 - .@i)); // b <= a } return .@str$; } // repeat("", ) // repeats many times and returns it function script repeat { .@mul = getarg(1); for (.@i = 0; .@i < .@mul; ++.@i) { .@str$ += getarg(0); } return .@str$; } // shuffle("") // returns shuffled function script shuffle { .@str$ = getarg(0); for (.@len = getstrlen(.@str$); .@len > 0; --.@len) { .@rnd = rand(.@len); .@out$ += charat(.@str$, .@rnd); .@str$ = delchar(.@str$, .@rnd); } return .@out$; }