
/* -- strip -- */


function presente(s, a) {
	for (var i = 0; i < a.length; i++) {
		if (a[i] == s)
			return true;
	}
	return false;
}


var CARATTERI_VUOTI = [' ', '\n', '\r', '\t'];


/* Cancella gli spazi a sinistra */
String.prototype.lstrip = function(caratteri_da_togliere) {
	if (typeof caratteri_da_togliere == "undefined")
		caratteri_da_togliere = CARATTERI_VUOTI;
	for (var i1 = 0; i1 < this.length; i1++)
		if (! presente(this.charAt(i1), caratteri_da_togliere))
			break;
	if (i1 >= this.length)
		return "";
	return this.substr(i1);
}


/* Cancella gli spazi a destra */
String.prototype.rstrip = function(caratteri_da_togliere) {
	if (typeof caratteri_da_togliere == "undefined")
		caratteri_da_togliere = CARATTERI_VUOTI;
	for (var i2 = this.length - 1; i2 > 0; i2--)
		if (! presente(this.charAt(i2), caratteri_da_togliere))
			break;
	if (i2 < 0)
		return "";
	return this.substr(0, i2 + 1);
}


/* Cancella gli spazi iniziali e finali */
String.prototype.strip = function(caratteri_da_togliere) {
	return this.lstrip(caratteri_da_togliere).rstrip(caratteri_da_togliere);
}


/* --- */


String.prototype.lettera_maiuscola = function() {
	var p = this.substr(0, 1).toUpperCase();
	return p + this.substr(1);
}


String.prototype.vuota = function() {
	return (this.strip() == "");
}


/* Conta il numero di occorrenze del carattere c nella stringa */
String.prototype.conta = function(c, idx_inizio, idx_fine) {
	if (typeof idx_inizio == "undefined")
		idx_inizio = 0;
	if (typeof idx_fine == "undefined")
		idx_fine = this.length - 1;
	var n = 0;
	for (var i = idx_inizio; i <= idx_fine; i++) 
		if (this.charAt(i) == c)
			n++;
	return n;
}


/* Aggiunge ... alla fine se la lunghezza supera max_len */
String.prototype.ellipsize = function(max_len) {
	if (this.length <= max_len)
		return this;
	return this.substr(0, max_len - 3) + "...";
}


/* Ritorna la sottostringa fino al catattere c1 (escluso), se c1 non � presente ritorna tutta la stringa. */
String.prototype.fino_a = function(c1) {
	var p1 = this.indexOf(c1);
	if (p1 < 0)
		return this;
	else
		return this.substr(0, p1);
}


/* Ritorna la sottostringa fino al catattere c1 (escluso), se c1 non � presente ritorna stringa vuota. */
String.prototype.fino_a_se_presente = function(c1) {
	var p1 = this.indexOf(c1);
	if (p1 < 0)
		return "";
	else
		return this.substr(0, p1);
}


/* Ritorna la sottostringa dopo il catattere c1, stringa vuota se c1 non � presente. */
String.prototype.dopo = function(c1) {
	var p1 = this.indexOf(c1);
	if (p1 < 0)
		return "";
	else
		return this.substr(p1 + 1);
}


/* Ritorna gli ultimi n caratteri della stringa */
String.prototype.r_substr = function(n) {
	var len = this.length;
	if (n <= len)
		return this.substr(len - n, n);
	else
		return this;
}


/* Ritorna la stringa senza gli ultimi n caratteri */
String.prototype.togli_ultimi = function(n) {
	var len = this.length;
	if (n <= len)
		return this.substr(0, len - n);
	else
		return "";
}


/* Ritorna true se la stringa inizia con il valore in s. */
String.prototype.inizia_con = function(s) {
	return s == this.substr(0, s.length);
}


/* Ritorna true se la stringa finisce con il valore in s. */
String.prototype.finisce_con = function(s) {
	return s == this.r_substr(s.length);
}


/* Split con lo stesso comportamento della versione Ruby */
String.prototype.split_max = function(separatore, quanti) {
	if ((typeof quanti == "undefined") || (quanti < 1))
		return this.split(separatore);
	var a = [];
	var inizio = 0;
	var fine = 0;
	for (var i = 1; i < quanti; i++) {
		fine = this.indexOf(separatore, inizio);
		if (fine == -1)
			break;
		a.push(this.substr(inizio, fine - inizio));
		inizio = fine + 1;
	}
	a.push(this.substr(inizio));
	return a;
}


/* --- split_regexp --- */


var ie = navigator.appName.indexOf("Internet Explorer") >= 0;


Array.prototype.match_last_index = function() {
	if (ie)
		return this.lastIndex;
	else
		return this.index + this[0].length;
}


function prova_match(s, inizia_da, a_regexp) {
	var d = [];
	for (var i = 0; i < a_regexp.length; i++) {
		var s1 = s.substr(inizia_da);
		if (s1 == null)
			return null;
		var re = new RegExp("^" + a_regexp[i], "mi");
		var m = re.exec(s1);
		if (m == null)
			return null;
		d.push(inizia_da + m.index);
		inizia_da += m.match_last_index();
	}
	d.push(inizia_da);
	return d;
}


function lista_match(s, inizia_da, regexp) {
	var d = [];
	var re = new RegExp(regexp, "mi");
	while (true) {
		var s1 = s.substr(inizia_da);
		if (s1 == null)
			return d;
		var m = re.exec(s1);
		if (m == null)
			return d;
		d.push(inizia_da + m.index);
		inizia_da += m.match_last_index();
	}
	return d;
}


function trova_match(s, inizia_da, a_regexp) {
	var possibili_inizi = lista_match(s, inizia_da, a_regexp[0]);
	for (var i = 0; i < possibili_inizi.length; i++) {
		var dati_match = prova_match(s, possibili_inizi[i], a_regexp);
		if (dati_match != null)
			return dati_match;
	}
	return null;
}


String.prototype.dividi = function(d) {
	var a = new Array();
	for (var i = 0; i < d.length - 1; i++)
		a.push(this.substr(d[i], d[i + 1] - d[i]));
	a.push(this.substr(d[d.length - 1]));
	return a;
}


String.prototype.split_regexp_v = function(a_regexp) {
	var a_limiti = [0];
	var inizia_da = 0;
	var limiti_match;
	while ((limiti_match = trova_match(this, inizia_da, a_regexp)) != null) {
		a_limiti = a_limiti.concat(limiti_match);
		inizia_da = limiti_match[limiti_match.length - 1];
	}
	return this.dividi(a_limiti);
}


String.prototype.split_regexp = function() {
	var a_regexp = [];
	for (var i = 0; i < arguments.length; i++)
		a_regexp.push(arguments[i]);
	return this.split_regexp_v(a_regexp);
}


