FileDocCategorySizeDatePackage
StringParser.javaAPI DocApache Tomcat 6.0.148665Fri Jul 20 04:20:36 BST 2007org.apache.catalina.util

StringParser

public final class StringParser extends Object
Utility class for string parsing that is higher performance than StringParser for simple delimited text cases. Parsing is performed by setting the string, and then using the findXxxx() and skipXxxx() families of methods to remember significant offsets. To retrieve the parsed substrings, call the extract() method with the appropriate saved offset values.
author
Craig R. McClanahan
version
$Revision: 467222 $ $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $

Fields Summary
private char[]
chars
The characters of the current string, as a character array. Stored when the string is first specified to speed up access to characters being compared during parsing.
private int
index
The zero-relative index of the current point at which we are positioned within the string being parsed. NOTE: the value of this index can be one larger than the index of the last character of the string (i.e. equal to the string length) if you parse off the end of the string. This value is useful for extracting substrings that include the end of the string.
private int
length
The length of the String we are currently parsing. Stored when the string is first specified to avoid repeated recalculations.
private String
string
The String we are currently parsing.
Constructors Summary
public StringParser()
Construct a string parser with no preset string to be parsed.


        this(null);

    
public StringParser(String string)
Construct a string parser that is initialized to parse the specified string.

param
string The string to be parsed


        super();
        setString(string);

    
Methods Summary
public voidadvance()
Advance the current parsing position by one, if we are not already past the end of the string.


        if (index < length)
            index++;

    
public java.lang.Stringextract(int start)
Extract and return a substring that starts at the specified position, and extends to the end of the string being parsed. If this is not possible, a zero-length string is returned.

param
start Starting index, zero relative, inclusive


        if ((start < 0) || (start >= length))
            return ("");
        else
            return (string.substring(start));

    
public java.lang.Stringextract(int start, int end)
Extract and return a substring that starts at the specified position, and ends at the character before the specified position. If this is not possible, a zero-length string is returned.

param
start Starting index, zero relative, inclusive
param
end Ending index, zero relative, exclusive


        if ((start < 0) || (start >= end) || (end > length))
            return ("");
        else
            return (string.substring(start, end));

    
public intfindChar(char ch)
Return the index of the next occurrence of the specified character, or the index of the character after the last position of the string if no more occurrences of this character are found. The current parsing position is updated to the returned value.

param
ch Character to be found


        while ((index < length) && (ch != chars[index]))
            index++;
        return (index);

    
public intfindText()
Return the index of the next occurrence of a non-whitespace character, or the index of the character after the last position of the string if no more non-whitespace characters are found. The current parsing position is updated to the returned value.


        while ((index < length) && isWhite(chars[index]))
            index++;
        return (index);

    
public intfindWhite()
Return the index of the next occurrence of a whitespace character, or the index of the character after the last position of the string if no more whitespace characters are found. The current parsing position is updated to the returned value.


        while ((index < length) && !isWhite(chars[index]))
            index++;
        return (index);

    
public intgetIndex()
Return the zero-relative index of our current parsing position within the string being parsed.



    // ------------------------------------------------------------- Properties


                       
       

        return (this.index);

    
public intgetLength()
Return the length of the string we are parsing.


        return (this.length);

    
public java.lang.StringgetString()
Return the String we are currently parsing.


        return (this.string);

    
protected booleanisWhite(char ch)
Is the specified character considered to be whitespace?

param
ch Character to be checked


        if ((ch == ' ") || (ch == '\t") || (ch == '\r") || (ch == '\n"))
            return (true);
        else
            return (false);

    
public voidreset()
Reset the current state of the parser to the beginning of the current string being parsed.


        index = 0;

    
public voidsetString(java.lang.String string)
Set the String we are currently parsing. The parser state is also reset to begin at the start of this string.

param
string The string to be parsed.


        this.string = string;
        if (string != null) {
            this.length = string.length();
            chars = this.string.toCharArray();
        } else {
            this.length = 0;
            chars = new char[0];
        }
        reset();

    
public intskipChar(char ch)
Advance the current parsing position while it is pointing at the specified character, or until it moves past the end of the string. Return the final value.

param
ch Character to be skipped


        while ((index < length) && (ch == chars[index]))
            index++;
        return (index);

    
public intskipText()
Advance the current parsing position while it is pointing at a non-whitespace character, or until it moves past the end of the string. Return the final value.


        while ((index < length) && !isWhite(chars[index]))
            index++;
        return (index);

    
public intskipWhite()
Advance the current parsing position while it is pointing at a whitespace character, or until it moves past the end of the string. Return the final value.


        while ((index < length) && isWhite(chars[index]))
            index++;
        return (index);