/
ColdWeb-0.2/
ColdWeb-0.2/root/
parent $root
object $string

var $string alphabet "abcdefghijklmnopqrstuvwxyz"
var $string numbers "1234567890"
var $string non_alphanumeric "!@#$%^&*()_+-=~`'{}[]|/?\"\\,.<>;: "

method left
    arg str, width, [fchar];

    // will NOT chop off 'str' if it is longer than width, use pad() for that.
    if (fchar)
        return str + (strlen(str) < width ? pad("", width - strlen(str), fchar[1]) | "");
    else
        return str + (strlen(str) < width ? pad("", width - strlen(str)) | "");
.

method center
    arg text, len, [args];
    var lfill, rfill, textlen, padlen;
    
    // args[1] == string to center
    // args[2] == integer of width to center in
    // args[3] <op> == what to fill the left|right side with.
    // args[4] <op> == what to fill the right side with.
    lfill = listlen(args) >= 1 && args[1] || " ";
    rfill = listlen(args) >= 2 ? args[2] | lfill == " " ? "" | lfill;
    textlen = strlen(text);
    padlen = (len - textlen) / 2;
    if (textlen < len)
        return .fill(padlen, lfill) + text + (rfill ? .fill(padlen, rfill) | "");
    else
        return len > 0 ? text | pad(text, len);
.

method trim
    arg string, [args];
    var rl, chars, type;
    
    // remove leading and trailing characters.
    // if args includes a string, it takes that as the strip string.
    // args can also include symbols of what edge to trim: 'left 'right (or both)
    // left and right defaults on.
    rl = [];
    while (args) {
        type = type(args[1]);
        if (type == 'string)
            chars = args[1];
        else if (type == 'symbol)
            rl = [@rl, args[1]];
        args = sublist(args, 2);
    }
    if (!chars)
        chars = " ";
    if (!rl)
        rl = ['left, 'right];
    if ('left in rl) {
        // strip from left
        while (string && string[1] in chars)
            string = substr(string, 2);
    }
    if ('right in rl) {
        // strip from right
        while (string && string[strlen(string)] in chars)
            string = substr(string, 1, strlen(string) - 1);
    }
    return string;
.

method to_list
    arg str, [sep];
    var result, list;
    
    // separate a string into a list of strings, breaking wherever 'sep' appears.
    // if not provided, sep defaults to a comma.
    // One word of warning.  sep should not contain an asterisk.  If it does,
    // this routine will separate the string oddly, most likely losing bits.
    if (!str)
        return [];
    sep = "*" + (sep ? sep[1] | ",") + "*";
    list = [];
    while (1) {
        result = match_pattern(sep, str);
        if (result) {
            list = list + [result[1]];
            str = result[2];
        } else {
            return list + [str];
        }
    }
.

method right
    arg str, width, [fchar];
    
    // will not chop off 'str' if it is longer than width (unlike pad())
    if (fchar)
        return pad("", width - strlen(str), fill_char[1]) + str;
    else
        return pad("", width - strlen(str)) + str;
.

method alphabet
    return alphabet;
.

method numbers
    return numbers;
.

method cap
    arg string;
    
    // Capitalizes the first character of a word.
    return uppercase(string[1]) + substr(string, 2);
.

method is_numeric
    arg string;
    
    return toint(string) || string == "0";
.

method a_or_an
    arg string;
    
    if (lowercase(string[1]) in "aeiou")
        return "an";
    return "a";
.

method strip
    arg string, strip;
    var new_str, char;
    
    // strips all of "strip" characters from the string
    // if "strip" is -1 it will use .non_alphanumeric()
    if (type(string) != 'string || type(strip) != 'string && strip != -1)
        throw(~type, "First argument must be a string, second can be -1");
    new_str = "";
    if (strip == -1)
        new_str = non_alphanumeric;
    for char in [1 .. strlen(string)] {
        if (!(string[char] in strip))
            new_str = new_str + string[char];
    }
    return new_str;
.

method non_alphanumeric
    return non_alphanumeric;
.

method chop
    arg str, len, [end];
    
    // chops string off strlen(end) characters before len and appends len
    end = [@end, "..."][1];
    if (strlen(str) < len)
        return str;
    if (strlen(str) < strlen(end))
        return str;
    return pad(str, len - strlen(end)) + end;
.

method explode
    arg [args];
    
    return (> explode(@args) <);
.

method match_template
    arg [args];
    
    return (> match_template(@args) <);
.

method match_pattern
    arg [args];
    
    return (> match_pattern(@args) <);
.

method match_regexp
    arg [args];
    
    return (> match_regexp(@args) <);
.

method toliteral
    arg [args];
    
    return (> toliteral(@args) <);
.

method last
    arg str;
    
    return str[strlen(str)];
.

method explode_quoted
    arg str;
    var out, result;
    
    out = [];
    while (str) {
        result = match_pattern("*\"*\"*", str);
        if (result) {
            out = [@out, @explode(result[1]), result[2].trim()];
            str = result[3];
        } else {
            out = [@out, @explode(str)];
            str = "";
        }
    }
    return out;
.

method rindex
    arg str, c;
    var i;

    i = strlen(str);
    while (i) {
        if (str[i] == c)
            return i;
        i = i - 1;
    }
    return 0;
.