/**
 * Filename    : String.js
 * Author      : Robert Cerny
 * Created     : 2004-10-27
 * Last Change : 2007-03-17
 *
 * Description:
 *   Adds some useful methods for the String prototype.
 *
 * History:
 *   2007-03-15 Added signatures.
 *   2007-01-06 Added interception.
 *   2007-01-05 Fixed bug in require statement.
 *   2004-10-27 Created.
 */

CERNY.js.String = {};

CERNY.require("CERNY.js.String");

String.prototype.logger = CERNY.Logger("CERNY.js.String");

String.prototype.entityify = function() {
    return this.replace(/&/g, "&amp;").replace(/</g,"&lt;").replace(/>/g, "&gt;");
}

String.prototype.trim = function () {
    return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
}

/**
 * Pad a string.
 *
 * padChr - the character which should be used for padding
 * length - how many characters should the string have after padding
 * front - if true, padding is added before the string
 * return - the padded string
 */
String.prototype.pad = function(padChr, length, front) {
    padChr = padChr.substring(0,1);
    if (!isBoolean(front)) {
        front = true;
    }
    var padSize = length - this.length;
    if (padSize > 0) {
        var padStr = "";
        for (var i = 0; i < padSize; i++) {
            padStr += padChr;
        }
        if (front) {
            return padStr + this;
        }
        return this + padStr;
    }
    return "" + this;
}
CERNY.signature(String.prototype.pad, "string", "string", "number", ["undefined", "boolean"]);


function isNonEmptyString(s) {
    return !isNull(s) && isString(s) && s.trim().length > 0;
}

CERNY.intercept(String.prototype, "entityify", "trim", "pad");
