/**
 * Filename    : String.js
 * Author      : Robert Cerny
 * Created     : 2004-10-27
 * Last Change : 2006-11-26
 *
 * Description:
 *   Adds some useful methods for the String prototype.
 *
 * History:
 *   2004-10-27 Created.
 *
 * License:
 */

CERNY.js.String = {};

CERNY.require("CERNY.js.Number");

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
 */
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;
}

function isNonEmptyString(s) {
    return !isNull(s) && isString(s) && s.trim().length > 0;
}

