/*
 * Classe StringBuffer usada para acelerar a concatenação
*/
function StringBuffer() {
    this.buffer = []; 
};

StringBuffer.prototype.append = function append(string) {      
    this.buffer.push(string);
    return this; 
}; 

StringBuffer.prototype.toString = function toString() {     
    return this.buffer.join(""); 
};

StringBuffer.prototype.length = function length()
{
    return this.buffer.length;
}

/*
 * Funções extras para strings
*/
String.prototype.trim = function()
{
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.toDate = function()
{
    if (!this.match('^[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]$'))
        throw "Data inválida"
    var dia = parseInt(this.substr(0,2), 10);
    var mes = parseInt(this.substr(3,2), 10);
    var ano = parseInt(this.substr(6,4), 10);
    if (dia < 0) throw "Data inválida";
    if (mes < 0 || mes > 12) throw "Data inválida";
    if (ano < 0) throw "Data inválida";
    
    var diasFev = ((ano % 4 == 0 && ano % 100 != 0 && ano % 400 != 0) ? 29 : 28);
    var diasMeses = [31, diasFev, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    if (dia > diasMeses[mes - 1]) throw "Data inválida";
    
    return new Date(ano, mes, dia);
}
String.prototype.toNumber = function()
{
    var nValue = new Number(this.replace(",","."));
    if (isNaN(nValue)) throw "Valor inválido";
    return nValue;
}
