/**********************************************************
file:	trim.js
date:	122899
author:	Rob Barlow

dependencies: none

description:
============
Contains functions for trimming whitespace from a
JavaScript string.
**********************************************************/


/*
FUNCTION: isSpace(char)
	Returns true if c is a white space character (space,
	tab or newline).
*/
function isSpace(c)
{ return ( (c == ' ') || (c == '\r') || (c == '\n') || (c == '\t') ); }


/*
FUNCTION: trimLeft(str)
	Trims leading white space from the beginning of a string.
	This function does not change the passed string, but
	returns a trimmed COPY of the passed string.
*/
function trimLeft(str)
{
	var i = 0, len = str.length;
	var newStr = "";

	while ( (i < len) && isSpace( str.charAt(i) ) )
		i++;

	// could probably use SubStr(?) here
	while (i < len)
		newStr += str.charAt(i++);

	return newStr;
}


/*
FUNCTION: trimRight(str)
	Much like TrimLeft(), but removes ending white space
	characters. str is not changed.  The function returns
	a trimmed COPY of str.
*/
function trimRight(str)
{
	var i = str.length - 1;
	var newStr = "";
	
	while ( (i >= 0) && isSpace( str.charAt(i) ) )
		i--;

	// could probably use SubStr(?) here
	while (i >= 0)
		newStr = str.charAt(i--) + newStr;

	return newStr;
}


/*
FUNCTION: trim(str)
	Uses the trimRight and trimLeft functions to trim
	leading and ending white space.
*/
function trim(str)
{ return trimRight( trimLeft(str) ); }