/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
// Conventions:
//    Words in multi-word function names will be delimited by '_'.
//    Words in multi-word function names will be unabbreviated.
//    The full string will be last in multi-parameter functions.

// NOTE: These functions don't modify their arguments directly.
// You have to return the function into the argument.  For example,
// trim_right(s); won't change s.  You have to do:
// s = trim_right(s);

// Contents:
//    count_ss(match, s) // counts instances of match in s
//    del_after_line(n, s) // deletes all lines after the n-th.
//    left_equals(match, s) // true if s begins with match
//    minus_word1(s) // returns s without the first word
//    remove_after(at, s) // trims off rear after at off of s
//    start_after(mark, s) // trims off front until mark
//		substitute(news, olds, s)
//    trim_ends(s) // removes whitespace from both ends
//    trim_left(s) // removes whitespace from front
//    trim_right(s) // removes whitespace from rear
//    word_number(n, s) // returns word number n from s
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
function substitute(news, olds, s)
{
	var at = 0;
	var front;
	var rear;
	var result;
	var delta = news.length - olds.length;
	while((at = s.indexOf(olds, at)) != -1)
	{
// These methods fail e.g. when news=\\ and olds=\
//		front = remove_after(olds, s);
		front = s.substring(0, at);
//		rear = start_after(olds, s);
		rear = s.substring(at + olds.length, s.length);
		s = front + news + rear;
		at += delta + 1;
	}
	return(s);
}
function count_ss(match, s)
{
	var at = 0;
	var count = 0;
	while((at = s.indexOf(match, at)) != -1)
	{
		count++;
		at += match.length;
	}
	return(count);
}
function remove_after(at, s)
{
	var t1 = s.indexOf(at);
	if (t1 != -1)
		s = s.substring(0, t1);
	return(s);
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
function start_after(mark, s)
{
	i = s.indexOf(mark);
	if (i == -1)
		return ("");
	return (s.substring(i + mark.length));
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
function left_equals(match, s)
{
//	alert("LEFTEQ:\n" + match + "\n" + s + "\n" + s.substring(0, match.length));
	if (s.substring(0, match.length).toLowerCase() == match.toLowerCase())
		return true;
	return false;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
function trim_ends(s) // remove whitespace at both ends
{
	s = trim_left(s);
	return(trim_right(s));
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
function trim_left(s)
{
	s0 = 0;
	while (s.charAt(s0) == ' ' || s.charAt(s0) == '\t')
		s0++;
	return(s.substring(s0));
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
function trim_right(s)
{
	s1 = s.length - 1;
	while (s.charAt(s1) == ' ' || s.charAt(s1) == '\t')
		s1--;
	return(s.substring(0, s1 + 1));
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
function del_after_line(n, s)
{
	// find n'th \n
	var ret = 0;
	for (i=1; i <= n; i++)
	{
		ret = s.indexOf("\n", ret);
		if (ret == -1)
			return(s);
		ret++;
	}
	// cut off from n'th \n
	return(s.substring(0, ret));
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* THESE FUNCTIONS WERE CHECKED FOR PROPER FUNCTION BUT WERE NOT NEEDED
IN THE FINAL VERSION OF LCS2 */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*
function minus_word1(s)
{
	s = trim_left(s);
	s = start_after(" ", s);
	return (trim_left(s));
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function word_number(n, s) // get word number n from s; if none, return ""
{
	s = trim_ends(s);
	s = s + " ";
	var w0 = 0;
	for (i = 1; i <= n; i++)
	{
		w1 = w0;
		var w1 = s.indexOf(" ", w0);
		if (w1 == -1)
			return ("");
		if (i == n)
			break;
		while (s.length > (w1 + 1))
		{
			if (s.charAt(w1 + 1) == " " || s.charAt(w1 + 1) == "\t")
				w1++; // skip multiple consecutive spaces
			else break;
		}
		if (s.length <= (w1 + 1))
			return ("");
		w0 = w1 + 1;
	}
	return s.substring(w0, w1);
}
*/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
