package to.etc.util;

/**
 * Small helper class to scan text strings for expected tokens.
 * @author jal
 * Created on May 21, 2004
 */
public class TextScanner {
	/** The string being scanned */
	private String	m_text;

	/** The length of the input string */
	private int		m_len;

	/** The current position within the string */
	private int		m_ix;

	private long	m_lastint;

	public TextScanner(String s) {
		setString(s);
	}

	public long getLastInt() {
		return m_lastint;
	}

	public void setString(String s) {
		m_text = s;
		m_ix = 0;
		m_len = s.length();
	}

	public long getInt() {
		return m_lastint;
	}

	/**
	 * Skips whitespace at the current position. Returns TRUE if whitespace was
	 * found.
	 * @return
	 */
	public boolean skipWS() {
		int six = m_ix;
		while(m_ix < m_len) {
			char c = m_text.charAt(m_ix);
			if(!Character.isWhitespace(c))
				break;
			m_ix++;
		}
		return m_ix != six;
	}

	public boolean eof() {
		return m_ix >= m_len;
	}

	/**
	 * Scans for an integer number at the current location. Returns T if a
	 * number was found. The value for that number can be obtained with a
	 * call to getLastInt()
	 * @return
	 */
	public boolean scanInt() {
		long res = 0;
		int ndig = 0;
		while(m_ix < m_len) {
			char c = m_text.charAt(m_ix);
			if(!Character.isDigit(c))
				break;
			res = res * 10 + (c - '0');
			ndig++;
			m_ix++;
		}
		if(ndig == 0)
			return false; // No digits -> no number
		m_lastint = res;
		return true;
	}

	/**
	 * Scans for a generic "word", which is defined as a sequence of
	 * characters starting with a letter, followed by letters and/or
	 * digits and the special characters '$' and '_'.
	 * @return The scanned string, or null if no string found here.
	 */
	public String scanWord() {
		int six = m_ix; // Start index for word
		while(m_ix < m_len) {
			char c = m_text.charAt(m_ix);
			if(six == m_ix) // First letter?
			{
				if(!Character.isLetter(c))
					return null;
			} else {
				if(!Character.isLetterOrDigit(c) && c != '$' && c != '_')
					break;
			}
			m_ix++;
		}
		return m_text.substring(six, m_ix);
	}

	/**
	 * Scans for a word consisting only of letters.
	 * @return
	 */
	public String scanLetters() {
		int six = m_ix; // Start index for word
		while(m_ix < m_len) {
			char c = m_text.charAt(m_ix);
			if(!Character.isLetterOrDigit(c) && c != '$' && c != '_')
				break;
			m_ix++;
		}
		if(six == m_ix)
			return null;
		return m_text.substring(six, m_ix);
	}

	public char currentChar() {
		return m_ix >= m_len ? 0 : m_text.charAt(m_ix);
	}

	public void inc() {
		m_ix++;
	}
}

@@ -369,16 +369,43 @@
 stint;%0A%0A
+%09public TextScanner() %7B%0A%09%7D%0A
 %09public 
@@ -2465,32 +2465,360 @@
 six, m_ix);%0A%09%7D%0A%0A
+%09/**%0A%09 * Return the next char and advance the ptr.%0A%09 * @return%0A%09 */%0A%09public int nextChar() %7B%0A%09%09if(m_ix %3E= m_len)%0A%09%09%09return -1;%0A%09%09return m_text.charAt(m_ix++) & 0xffff;%0A%09%7D%0A%0A%09public boolean skip(char c) %7B%0A%09%09if(m_ix %3E= m_len)%0A%09%09%09return false;%0A%09%09if((c & 0xffff) != m_text.charAt(m_ix))%0A%09%09%09return false;%0A%09%09m_ix++;%0A%09%09return true;%0A%09%7D%0A%0A
 %09public char cur

Make TextScanner more usable