/*
    File:   string.js
    Author: remco
    Date:   Sat Nov 16 13:29:12 PDT 2002

    Description:
      Some extra string functions.

    Copyright (c) 2002-2003, Remco Teunen.  All Rights Reserved.
*/


/*---------------------------------------------------------------------------*
 * trim
 *
 * Removes leading and trailing spaces, \r, \t, and \n from the string.
 * The string object itself is unchanged.
 *
 * Params:  None.
 *
 * Returns:  A new string without leading/trailing spaces.
 *---------------------------------------------------------------------------*/
function trim()
{
    var new_start = -1;
    var new_end = -1;
    var white_spaces = " \n\r\t";

    // Search for the first non-white-space character in the string.
    for (var i = 0; i < this.length; i++) {

	// Get the current character.
	var cur_char = this.charAt(i);

	// White-space?
	if (white_spaces.indexOf(cur_char) < 0) {
	    new_start = i;
	    break;
	}
    }
    // Found a non-white-space char? If not, we're done.
    if (new_start < 0) {
	return "";
    }    
    // Search for the first non-white-space character in the string.
    for (var i = this.length - 1; i >= 0; i--) {

	// Get the current character.
	var cur_char = this.charAt(i);

	// White-space?
	if (white_spaces.indexOf(cur_char) < 0) {
	    new_end = i;
	    break;
	}
    }
    // Sanity check.
    if (new_end < 0 || new_end < new_start) {
	throw "Bug in string.trim() code.";
    }  
    // Return the sub-string.
    return this.substring(new_start, new_end + 1);

}   /* end trim */


/*===========================================================================*
 * "main"
 *
 * Add the new properties to the string class.
 *===========================================================================*/
String.prototype.trim = trim;


/* end string.js */

