public class ParseText extends Object
ParseText object contains a string of text to
be examined (in the form of a CharSequence), an index denoting the current parse
position and a start index pointing to the start of the last matched or skipped characters.
The class includes a number of "match" operations, which return true if
the text matches the parameter(s) to the operation. On a successful match, these operations
set the start index to the position of the first character matched, and increment the index
past the matched characters. If the match is not successful, the index and start index are
not modified.
For use following a successful match operation, there are several "getResult"
operations that will extract the matched characters from the text in a variety of forms
(int, long, String etc.).
The class takes text in the form of a CharSequence rather than a String,
and some implementations of CharSequence allow modification of the contents. The
results of any method of this class are undefined in the case of concurrent modification of
the text.
| Constructor and Description |
|---|
ParseText(CharSequence text)
Construct a
ParseText object with the given text and an initial index value 0f 0. |
ParseText(CharSequence text,
int index)
Construct a
ParseText object with the given text and initial index value. |
| Modifier and Type | Method and Description |
|---|---|
Appendable |
appendResultTo(Appendable a)
Copy the result character sequence to an
Appendable. |
StringBuilder |
appendResultTo(StringBuilder sb)
Copy the result character sequence to a
StringBuilder. |
boolean |
available(int len)
Test whether the text has at least the specified number of characters left after the
index.
|
ParseText |
back(int n)
Decrement the index by n.
|
char |
charAt(int index)
Get a specific character from the text.
|
int |
convertDecDigit(char ch)
Convert a decimal digit to the integer value of the digit.
|
int |
convertHexDigit(char ch)
Convert a hexadecimal digit to the integer value of the digit.
|
boolean |
equals(Object o)
Compare the
ParseText with another object for equality. |
char |
getChar()
Get the character at the current index and increment the index.
|
int |
getCodePoint()
Get the Unicode code point at the current character index and increment the index past
the code point.
|
int |
getHexInt(int from,
int to)
Get an
int from the text, treating the digits as hexadecimal. |
long |
getHexLong(int from,
int to)
Get a
long from the text, treating the digits as hexadecimal. |
int |
getIndex()
Get the current index (the offset within the text).
|
int |
getInt(int from,
int to)
Get an
int from the text. |
long |
getLong(int from,
int to)
Get a
long from the text. |
char |
getResultChar()
Get the result of the last match operation (or the first character of a longer match) as
a single character.
|
int |
getResultHexInt()
Get the result of the last match operation as an
int, treating the digits as
hexadecimal. |
long |
getResultHexLong()
Get the result of the last match operation as a
long, treating the digits as
hexadecimal. |
int |
getResultInt()
Get the result of the last match operation as an
int. |
int |
getResultLength()
Get the length of the result of the last match operation.
|
long |
getResultLong()
Get the result of the last match operation as a
long. |
CharSequence |
getResultSequence()
Get the result of the last match operation as a
CharSequence. |
String |
getResultString()
Get the result of the last match operation as a
String. |
int |
getStart()
Get the start index (the index of the start of the last matched sequence).
|
String |
getString(int from,
int to)
Extract a string from the text, bounded by the given start and end offsets.
|
CharSequence |
getText()
Get the entire text from the
ParseText object (as a CharSequence). |
int |
getTextLength()
Get the length of the entire text.
|
int |
hashCode()
Compute the hash code for this object (for completeness).
|
boolean |
isDigit(char ch)
Test whether the given character is a digit.
|
boolean |
isExhausted()
Test whether the
ParseText object is exhausted (the index has reached the end of
the text). |
boolean |
isHexDigit(char ch)
Test whether the given character is a hexadecimal digit.
|
boolean |
isNameContinuation(char ch)
Test whether the given character is a continuation character of a name.
|
boolean |
isNameStart(char ch)
Test whether the given character is the start character of a name.
|
boolean |
isSpace(char ch)
Test whether the given character is a space.
|
int |
length()
Get the length of the entire text.
|
boolean |
match(char ch)
Match the current character in the text against a given character.
|
boolean |
match(CharSequence target)
|
boolean |
match(int cp)
Match the current character in the text against a given Unicode code point.
|
boolean |
matchAnyOf(char... array)
Match the current character in the text against any of the characters in a given array.
|
boolean |
matchAnyOf(CharSequence... array)
Match the characters at the index against any of an array of
CharSequence
(String, StringBuilder etc.) objects. |
boolean |
matchAnyOf(Collection<? extends CharSequence> collection)
Match the characters at the index against any of a
Collection of
CharSequence (String, StringBuilder etc.) objects. |
boolean |
matchAnyOf(String str)
Match the current character in the text against any of the characters in a given
String. |
boolean |
matchDec()
Match the characters at the index as decimal digits, with a minimum of 1 digit and no
maximum.
|
boolean |
matchDec(int maxDigits)
Match the characters at the index as decimal digits, with a minimum of 1 digit and an
optional maximum.
|
boolean |
matchDec(int maxDigits,
int minDigits)
Match the characters at the index as decimal digits, with a given minimum number of
digits and an optional maximum.
|
boolean |
matchDecFixed(int numDigits)
Match the characters at the index as a fixed number of decimal digits.
|
boolean |
matchHex()
Match the characters at the index as hexadecimal digits, with a minimum of 1 digit and no
maximum.
|
boolean |
matchHex(int maxDigits)
Match the characters at the index as hexadecimal digits, with a minimum of 1 digit and an
optional maximum.
|
boolean |
matchHex(int maxDigits,
int minDigits)
Match the characters at the index as hexadecimal digits, with a given minimum number of
digits and an optional maximum.
|
boolean |
matchHexFixed(int numDigits)
Match the characters at the index as a fixed number of hexadecimal digits.
|
boolean |
matchIgnoreCase(char ch)
Match the current character in the text against a given character, ignoring case.
|
boolean |
matchIgnoreCase(CharSequence target)
Match the characters at the index against a given
CharSequence (String,
StringBuilder etc.), ignoring case. |
boolean |
matchName()
Match the characters at the index as a name, where a name is defined as starting with a
character that matches
isNameStart(char), followed by zero or more characters
that match isNameContinuation(char). |
boolean |
matchName(CharSequence target)
Match the characters at the index against a given
CharSequence (String,
StringBuilder etc.), checking that the character following the match is not part
of a name. |
boolean |
matchRange(char from,
char to)
Match the current character in the text against a given character range.
|
boolean |
matchSpaces()
Match the characters at the index as spaces.
|
protected boolean |
matchSuccess(int i)
Indicate successful match.
|
ParseText |
reset()
Reset the index to the start of the text.
|
ParseText |
revert()
Undo the effect of the last match operation.
|
ParseText |
setIndex(int index)
Set the index to a specified value.
|
ParseText |
setStart(int start)
Set the start index (the index of the start of the last matched sequence).
|
ParseText |
setText(CharSequence text)
Set the text.
|
ParseText |
setText(CharSequence text,
int index)
Set the text and the index within the text.
|
ParseText |
skip(int n)
Increment the index by n.
|
ParseText |
skipPast(IntPredicate comparison)
Increment the index past any characters matching a given comparison function.
|
ParseText |
skipSpaces()
Increment the index past zero or more spaces.
|
ParseText |
skipTo(char ch)
Increment the index to the next occurrence of the given character.
|
ParseText |
skipTo(CharSequence target)
Increment the index to the next occurrence of the stopper sequence.
|
ParseText |
skipTo(IntPredicate comparison)
Increment the index past any characters matching a given comparison function.
|
ParseText |
skipToAnyOf(char... array)
Increment the index to the next occurrence of any of the given characters.
|
ParseText |
skipToAnyOf(CharSequence stoppers)
Increment the index to the next occurrence of any of the given characters.
|
ParseText |
skipToEnd()
Increment the index directly to the end of the text.
|
ParseText |
skipToSpace()
Increment the index to the next space.
|
String |
toString()
Create a
String representation of the ParseText object. |
String |
unescape(CharUnmapper charUnmapper,
char stopper)
Unescape a string from the index, using the provided
CharUnmapper and stopping on
the given character stopper value. |
public ParseText(CharSequence text, int index)
ParseText object with the given text and initial index value.text - the textindex - the initial indexNullPointerException - if the text is nullStringIndexOutOfBoundsException - if the index is negative or beyond the end of
the textpublic ParseText(CharSequence text)
ParseText object with the given text and an initial index value 0f 0.text - the textNullPointerException - if the text is nullpublic ParseText setText(CharSequence text, int index)
text - the textindex - the indexParseText object (for chaining purposes)NullPointerException - if the text is nullStringIndexOutOfBoundsException - if the index is outside the bounds of the textpublic ParseText setText(CharSequence text)
text - the textParseText object (for chaining purposes)NullPointerException - if the text is nullpublic CharSequence getText()
ParseText object (as a CharSequence).public int getTextLength()
public boolean isExhausted()
ParseText object is exhausted (the index has reached the end of
the text).true if the index has reached the end of the textpublic int getIndex()
public ParseText setIndex(int index)
index - the new indexParseText object (for chaining purposes)StringIndexOutOfBoundsException - if the index is outside the bounds of the textprotected boolean matchSuccess(int i)
i - the index at end of matchtrue to indicate successful matchpublic char getChar()
StringIndexOutOfBoundsException - if the index is at or beyond end of stringpublic int getCodePoint()
StringIndexOutOfBoundsException - if the index is at or beyond end of stringpublic String getString(int from, int to)
from - the start offsetto - the end offsetIndexOutOfBoundsException - if the start and end offsets are invalidpublic int getStart()
public ParseText setStart(int start)
start - the new start indexParseText object (for chaining purposes)StringIndexOutOfBoundsException - if the start index is invalidpublic int getResultInt()
int.int (always positive)NumberFormatException - if the start and end indices do not describe a valid
intpublic int getInt(int from,
int to)
int from the text.from - the start offsetto - the end offsetint (always positive)NumberFormatException - if the start and end indices do not describe a valid
intIndexOutOfBoundsException - if the start and end indices are not contained within
the textpublic long getResultLong()
long.long (always positive)NumberFormatException - if the start and end indices do not describe a valid
longpublic long getLong(int from,
int to)
long from the text.from - the start offsetto - the end offsetlong (always positive)NumberFormatException - if the start and end indices do not describe a valid
longIndexOutOfBoundsException - if the start and end indices are not contained within
the textpublic int convertDecDigit(char ch)
isDigit(char) as well.ch - the decimal digitNumberFormatException - if the digit is not validpublic int getResultHexInt()
int, treating the digits as
hexadecimal.int (always positive)NumberFormatException - if the start and end indices do not describe a valid
intpublic int getHexInt(int from,
int to)
int from the text, treating the digits as hexadecimal.from - the start offsetto - the end offsetint (always positive)NumberFormatException - if the start and end indices do not describe a valid
intIndexOutOfBoundsException - if the start and end indices are not contained within
the textpublic long getResultHexLong()
long, treating the digits as
hexadecimal.long (always positive)NumberFormatException - if the start and end indices do not describe a valid
longpublic long getHexLong(int from,
int to)
long from the text, treating the digits as hexadecimal.from - the start offsetto - the end offsetlong (always positive)NumberFormatException - if the start and end indices do not describe a valid
longIndexOutOfBoundsException - if the start and end indices are not contained within
the textpublic int convertHexDigit(char ch)
isHexDigit(char) as well.ch - the hexadecimal digitNumberFormatException - if the digit is not validpublic int getResultLength()
public char getResultChar()
public CharSequence getResultSequence()
CharSequence.public String getResultString()
String.public StringBuilder appendResultTo(StringBuilder sb)
StringBuilder. This is equivalent to but
more efficient than
sb.append(xxx.getResultSequence());
because it avoids the creation of an intermediate object.sb - the StringBuilderStringBuilder (for chaining purposes)public Appendable appendResultTo(Appendable a) throws IOException
Appendable. This is equivalent to but
more efficient than
a.append(xxx.getResultSequence());
because it avoids the creation of an intermediate object.a - the AppendableAppendable (for chaining purposes)IOException - if thrown by the Appendablepublic boolean available(int len)
len - the number of characters requiredtrue if that number of characters are availablepublic int length()
public char charAt(int index)
index - the index of the characterIndexOutOfBoundsException - if the index is negative or beyond the end of the textpublic boolean match(int cp)
cp - the code point to match againsttrue if the code point in the text matches the given characterpublic boolean match(char ch)
ch - the character to match againsttrue if the character in the text matches the given characterpublic boolean matchIgnoreCase(char ch)
ch - the character to match againsttrue if the character in the text matches the given characterpublic boolean matchRange(char from,
char to)
from - the low character in the range to match againstto - the high character in the range to match against (inclusive)true if the character in the text falls in the given rangepublic boolean matchAnyOf(String str)
String. Following a successful match the start index will point to the matched
character and the index will be incremented past it.str - the characters to match against (as a String)true if the character in the text matches any of the characters in
the stringpublic boolean matchAnyOf(char... array)
array - the characters to match against (as an array or varargs list)true if the character in the text matches any of the characters in
the arrayIllegalArgumentException - if the array is emptypublic boolean match(CharSequence target)
CharSequence (String,
StringBuilder etc.). Following a successful match the start index will point to
the first character of the matched sequence and the index will be incremented past it.target - the target CharSequencetrue if the characters in the text at the index match the targetpublic boolean matchName(CharSequence target)
CharSequence (String,
StringBuilder etc.), checking that the character following the match is not part
of a name. Following a successful match the start index will point to
the first character of the matched sequence and the index will be incremented past it.target - the target CharSequencetrue if the characters in the text at the index match the targetpublic boolean matchAnyOf(CharSequence... array)
CharSequence
(String, StringBuilder etc.) objects. Following a successful match the
start index will point to the first character of the matched sequence and the index will
be incremented past it.array - the array (or varargs list) of CharSequencetrue if the characters in the text at the index match any of the
entries in the arrayIllegalArgumentException - if the array is emptypublic boolean matchAnyOf(Collection<? extends CharSequence> collection)
Collection of
CharSequence (String, StringBuilder etc.) objects. Following a
successful match the start index will point to the first character of the matched
sequence and the index will be incremented past it.collection - the Collection of CharSequencestrue if the characters in the text at the index match any of
the entries in the collectionpublic boolean matchIgnoreCase(CharSequence target)
CharSequence (String,
StringBuilder etc.), ignoring case. Following a successful match the start index
will point to the first character of the matched sequence and the index will be
incremented past it.target - the target CharSequencetrue if the characters in the text at the index match the targetpublic boolean matchDec(int maxDigits,
int minDigits)
maxDigits - the maximum number digits to match (or 0 to indicate no limit)minDigits - the minimum number digits for a successful matchtrue if the characters in the text at the index are decimal
digits (subject to the specified minimum and maximum number of digits)public boolean matchDec(int maxDigits)
maxDigits - the maximum number digits to match (or 0 to indicate no limit)true if the characters in the text at the index are decimal
digits (subject to the specified maximum number of digits)public boolean matchDec()
true if the characters in the text at the index are decimal digitspublic boolean matchDecFixed(int numDigits)
numDigits - the number of digits expectedtrue if the characters in the text at the index are decimal
digitspublic boolean matchHex(int maxDigits,
int minDigits)
maxDigits - the maximum number digits to match (or 0 to indicate no limit)minDigits - the minimum number digits for a successful matchtrue if the characters in the text at the index are hexadecimal
digits (subject to the specified minimum and maximum number of digits)public boolean matchHex(int maxDigits)
maxDigits - the maximum number digits to match (or 0 to indicate no limit)true if the characters in the text at the index are hexadecimal
digits (subject to the specified maximum number of digits)public boolean matchHex()
true if the characters in the text at the index are hexadecimal digitspublic boolean matchHexFixed(int numDigits)
numDigits - the number of digits expectedtrue if the characters in the text at the index are hexadecimal
digitspublic ParseText revert()
ParseText object (for chaining purposes)public ParseText reset()
ParseText object (for chaining purposes)public ParseText skip(int n)
n - the amount to add to the indexParseText object (for chaining purposes)StringIndexOutOfBoundsException - if the result index would be beyond the end
of the textpublic ParseText back(int n)
n - the amount to subtract from the indexParseText object (for chaining purposes)StringIndexOutOfBoundsException - if the result index would be negativepublic ParseText skipTo(char ch)
ch - the stopper characterParseText object (for chaining purposes)public ParseText skipToAnyOf(char... array)
array - the array (or varargs list) of possible stopper charactersParseText object (for chaining purposes)IllegalArgumentException - if the array is emptypublic ParseText skipToAnyOf(CharSequence stoppers)
stoppers - a CharSequence of possible stopper charactersParseText object (for chaining purposes)IllegalArgumentException - if the CharSequence is emptypublic ParseText skipTo(CharSequence target)
target - the stopper sequenceParseText object (for chaining purposes)public boolean matchSpaces()
true if the characters in the text at the index are one or more spacespublic ParseText skipPast(IntPredicate comparison)
comparison - the comparison functionParseText object (for chaining purposes)public ParseText skipSpaces()
ParseText object (for chaining purposes)public ParseText skipTo(IntPredicate comparison)
comparison - the comparison functionParseText object (for chaining purposes)public ParseText skipToSpace()
ParseText object (for chaining purposes)public ParseText skipToEnd()
ParseText object (for chaining purposes)public boolean matchName()
isNameStart(char), followed by zero or more characters
that match isNameContinuation(char).true if the characters in the text at the index constitute a namepublic String unescape(CharUnmapper charUnmapper, char stopper)
CharUnmapper and stopping on
the given character stopper value. The index is left positioned at the stopper character
so that the caller can test whether the stopper was present. A call to
getResultString() or getResultSequence() following this method will
return the section of the text prior to unescaping.charUnmapper - the CharUnmapperstopper - the stopper character (e.g. the closing quote)IllegalArgumentException - if thrown by the CharUnmapperpublic boolean isSpace(char ch)
ch - the charactertrue if the character is a spacepublic boolean isDigit(char ch)
convertDecDigit(char) as well.ch - the charactertrue if the character is a digitpublic boolean isHexDigit(char ch)
convertHexDigit(char) as well.ch - the charactertrue if the character is a hexadecimal digitpublic boolean isNameStart(char ch)
ch - the charactertrue if the character is a name start characterpublic boolean isNameContinuation(char ch)
ch - the charactertrue if the character is a name continuation characterpublic String toString()
String representation of the ParseText object. This is
primarily intended for debugging purposes; the resulting string consists of the text
enclosed in square brackets, with a "~" indicating the start position and
a "^" for the index.public boolean equals(Object o)
ParseText with another object for equality.Copyright © 2020. All rights reserved.