Top Description Fields Constructors Methods
org.glassfish.grizzly.http.server.util

public final Class StringParser

extends Object
Class Inheritance

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: 1.3 $ $Date: 2007/06/18 14:17:08 $

Field Summary

Modifier and TypeField and Description
private char[]
chars

The characters of the current string, as a character array.

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.

private int
length

The length of the String we are currently parsing.

private String
string

The String we are currently parsing.

Constructor Summary

AccessConstructor and Description
public
StringParser()

Construct a string parser with no preset string to be parsed.

public
StringParser(String
The string to be parsed
string
)

Construct a string parser that is initialized to parse the specified string.

Method Summary

Modifier and TypeMethod and Description
public void
advance()

Advance the current parsing position by one, if we are not already past the end of the string.

public String
extract(int
Starting index, zero relative, inclusive
start
)

Extract and return a substring that starts at the specified position, and extends to the end of the string being parsed.

public String
extract(int
Starting index, zero relative, inclusive
start
,
int
Ending index, zero relative, exclusive
end
)

Extract and return a substring that starts at the specified position, and ends at the character before the specified position.

public int
findChar(char
Character to be found
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.

public int
findText()

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.

public int
findWhite()

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.

public int
getIndex()

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

public int
getLength()

Return the length of the string we are parsing.

public String
getString()

Return the String we are currently parsing.

protected boolean
isWhite(char
Character to be checked
ch
)

Is the specified character considered to be whitespace?

public void
reset()

Reset the current state of the parser to the beginning of the current string being parsed.

public void
setString(String
The string to be parsed.
string
)

Set the String we are currently parsing.

public int
skipChar(char
Character to be skipped
ch
)

Advance the current parsing position while it is pointing at the specified character, or until it moves past the end of the string.

public int
skipText()

Advance the current parsing position while it is pointing at a non-whitespace character, or until it moves past the end of the string.

public int
skipWhite()

Advance the current parsing position while it is pointing at a whitespace character, or until it moves past the end of the string.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

charsback to 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.

indexback to summary
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.

lengthback to summary
private int length

The length of the String we are currently parsing. Stored when the string is first specified to avoid repeated recalculations.

stringback to summary
private String string

The String we are currently parsing.

Constructor Detail

StringParserback to summary
public StringParser()

Construct a string parser with no preset string to be parsed.

StringParserback to summary
public StringParser(String string)

Construct a string parser that is initialized to parse the specified string.

Parameters
string:String

The string to be parsed

Method Detail

advanceback to summary
public void advance()

Advance the current parsing position by one, if we are not already past the end of the string.

extractback to summary
public String extract(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.

Parameters
start:int

Starting index, zero relative, inclusive

extractback to summary
public String extract(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.

Parameters
start:int

Starting index, zero relative, inclusive

end:int

Ending index, zero relative, exclusive

findCharback to summary
public int findChar(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.

Parameters
ch:char

Character to be found

findTextback to summary
public int findText()

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.

findWhiteback to summary
public int findWhite()

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.

getIndexback to summary
public int getIndex()

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

getLengthback to summary
public int getLength()

Return the length of the string we are parsing.

getStringback to summary
public String getString()

Return the String we are currently parsing.

isWhiteback to summary
protected boolean isWhite(char ch)

Is the specified character considered to be whitespace?

Parameters
ch:char

Character to be checked

resetback to summary
public void reset()

Reset the current state of the parser to the beginning of the current string being parsed.

setStringback to summary
public void setString(String string)

Set the String we are currently parsing. The parser state is also reset to begin at the start of this string.

Parameters
string:String

The string to be parsed.

skipCharback to summary
public int skipChar(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.

Parameters
ch:char

Character to be skipped

skipTextback to summary
public int skipText()

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.

skipWhiteback to summary
public int skipWhite()

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.