
/**
 * Returns a copy of string without leading and trailing whitespace.
 * Whitespace is defined as {char c | c <= ' '}.
 *
 * Note: We avoid regular expressions in implementation to support older user agents.
 *
 * @author Andrew Cottrell
 */
function StringUtil_trim(string) {

    /* Lookup the LENGTH of string. */
    var LENGTH = string.length;

    /* Find the first character that is not whitespace. */
    var beginIndex = 0;
    while (beginIndex != LENGTH && string.charAt(beginIndex) <= ' ') {
        beginIndex++;
    }

    /* If all characters are whitespace then return the empty String. */
    if (beginIndex == LENGTH) {return "";}

    /* Find the last character that is not whitespace. */
    var endIndex = LENGTH;
    while (endIndex != beginIndex && string.charAt(endIndex - 1) <= ' ') {
        endIndex--;
    }

    /*
     * If no leading or trailing whitespace found then return the string,
     * otherwise return the substring without leading and trailing whitespace.
     */
    return ((beginIndex == 0) && (endIndex == LENGTH)) ?
        string : string.substring(beginIndex, endIndex);
}

