Package net.pwall.text
Class TextMatcher
- java.lang.Object
-
- net.pwall.text.TextMatcher
-
public class TextMatcher extends Object
A text matching class to help with parsing text strings. It maintains a current pointer within a string and updates this pointer on a successful match.The
TextMatcherhas four main types of functions:Matchfunctions- These test the characters at the current index, and if successful, update the start index and the current index to reflect the matched characters
Skipfunctions- These advance the current pointer past one or more or a specified type of character (e.g. whitespace)
GetResultfunctions- These get the result of the last match in a number of forms
- General
Getfunctions - These get arbitrary data from the string
- Author:
- Peter Wall
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTextMatcher.CharSeqAn implementation ofCharSequenceto return data fromTextMatcher.
-
Constructor Summary
Constructors Constructor Description TextMatcher(String text)Construct aTextMatcherwith the specified text.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static intconvertDecDigit(char ch)Convert a decimal digit to the integer value of the digit.static intconvertHexDigit(char ch)Convert a hexadecimal digit to the integer value of the digit.chargetChar(int index)Get the character at the nominated index.CharSequencegetCharSeq(int start, int end)Get a substring of the text as aCharSequence.intgetHexInt(int from, int to)Get an unsignedintfrom the text, treating the digits as hexadecimal.longgetHexLong(int from, int to)Get an unsignedlongfrom the text, treating the digits as hexadecimal.intgetIndex()Get the current index (the offset within the text).intgetInt(int from, int to)Get a positiveintfrom the text.intgetInt(int from, int to, boolean negative)Get a signedintfrom the text.intgetLength()Get the length of the entire text.longgetLong(int from, int to)Get a positivelongfrom the text.longgetLong(int from, int to, boolean negative)Get a signedlongfrom the text.StringgetResult()Get the result of the last match operation as aString.chargetResultChar()Get the result of the last match operation (or the first character of a longer match) as a single character.CharSequencegetResultCharSeq()Get the result of the last match operation as aCharSequence.intgetResultHexInt()Get the result of the last match operation as an unsignedint, treating the digits as hexadecimal.longgetResultHexLong()Get the result of the last match operation as an unsignedlong, treating the digits as hexadecimal.intgetResultInt()Get the result of the last match operation as anint.intgetResultInt(boolean negative)Get the result of the last match operation as anint.intgetResultLength()Get the length of the result of the last match operation.longgetResultLong()Get the result of the last match operation as along.longgetResultLong(boolean negative)Get the result of the last match operation as along.intgetStart()Get the start index (the index of the start of the last matched sequence).StringgetString(int start, int end)Get a substring of the text.booleanisAtEnd()Test whether theTextMatcherobject is exhausted (the index has reached the end of the text).static booleanisDigit(char ch)Test whether the given character is a digit.static booleanisHexDigit(char ch)Test whether the given character is a hexadecimal digit.booleanmatch(char ch)Match the current character in the text against a given character.booleanmatch(CharSequence target)booleanmatch(CharPredicate comparison)Match the current character in the text using the specified comparison function.booleanmatchAny(String any)Match the current character in the text against any of the characters in a givenString.booleanmatchContinue(int maxChars, int minChars, CharPredicate comparison)Match the characters at the index as a continuation using the specified comparison function, with a given minimum number of characters and an optional maximum, but do not set the start index on success, and on fail, set the index back to the start index (as if the original match had failed).booleanmatchContinue(int maxChars, CharPredicate comparison)Match the characters at the index as a continuation using the specified comparison function, with no minimum number of characters and an optional maximum, but do not set the start index on success, and on fail, set the index back to the start index (as if the original match had failed).booleanmatchContinue(CharPredicate comparison)Match the characters at the index as a continuation using the specified comparison function, with no minimum or maximum number of characters, but do not set the start index on success, and on fail, set the index back to the start index (as if the original match had failed).booleanmatchDec()Match the characters at the index as decimal digits, with a minimum of 1 digit and no maximum.booleanmatchDec(int maxDigits)Match the characters at the index as decimal digits, with a minimum of 1 digit and an optional maximum.booleanmatchDec(int maxDigits, int minDigits)Match the characters at the index as decimal digits, with a given minimum number of digits and an optional maximum.booleanmatchHex()Match the characters at the index as hexadecimal digits, with a minimum of 1 digit and no maximum.booleanmatchHex(int maxDigits)Match the characters at the index as hexadecimal digits, with a minimum of 1 digit and an optional maximum.booleanmatchHex(int maxDigits, int minDigits)Match the characters at the index as hexadecimal digits, with a given minimum number of digits and an optional maximum.booleanmatchSeq(int maxChars, int minChars, CharPredicate comparison)Match the characters at the index using the specified comparison function, with a given minimum number of characters and an optional maximum.booleanmatchSeq(int maxChars, CharPredicate comparison)Match the characters at the index using the specified comparison function, with a minimum of 1 character and an optional maximum.booleanmatchSeq(CharPredicate comparison)Match the characters at the index using the specified comparison function, with a minimum of 1 character and no maximum.charnextChar()Get the character at the current index and increment the index.voidrevert()Undo the effect of the last match operation.voidsetIndex(int index)Set the current index.voidsetStart(int start)Set the start index.voidskip(CharPredicate comparison)Increment the index past any characters matching a given comparison function.voidskipAny(String any)Increment the index past any of the characters in a given string.voidskipFixed(int n)Increment the index by a fixed amount.voidskipToEnd()Increment the index directly to the end of the text.
-
-
-
Constructor Detail
-
TextMatcher
public TextMatcher(String text)
Construct aTextMatcherwith the specified text.- Parameters:
text- the text- Throws:
NullPointerException- if the text isnull
-
-
Method Detail
-
getChar
public char getChar(int index)
Get the character at the nominated index.- Parameters:
index- the index- Returns:
- the character at that index
- Throws:
IndexOutOfBoundsException- if the index is invalid
-
getLength
public int getLength()
Get the length of the entire text.- Returns:
- the text length
-
getStart
public int getStart()
Get the start index (the index of the start of the last matched sequence).- Returns:
- the start index
-
setStart
public void setStart(int start)
Set the start index. If the current index is less than the new start index, make them equal.- Parameters:
start- the new start index- Throws:
IndexOutOfBoundsException- if the new start index is less than 0 or greater than the text length
-
getIndex
public int getIndex()
Get the current index (the offset within the text).- Returns:
- the index
-
setIndex
public void setIndex(int index)
Set the current index. If the new current index is less than the start index, make them equal.- Parameters:
index- the new current index- Throws:
IndexOutOfBoundsException- if the new current index is less than 0 or greater than the text length
-
isAtEnd
public boolean isAtEnd()
Test whether theTextMatcherobject is exhausted (the index has reached the end of the text).- Returns:
trueif the index has reached the end of the text
-
revert
public void revert()
Undo the effect of the last match operation.
-
match
public boolean match(char ch)
Match the current character in the text against a given character. Following a successful match the start index will point to the matched character and the index will be incremented past it.- Parameters:
ch- the character to match against- Returns:
trueif the character in the text matches the given character
-
match
public boolean match(CharSequence target)
Match the characters at the index against a givenCharSequence(String,StringBuilderetc.). 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.- Parameters:
target- the targetCharSequence- Returns:
trueif the characters in the text at the index match the target
-
match
public boolean match(CharPredicate comparison)
Match the current character in the text using the specified comparison function. Following a successful match the start index will point to the matched character and the index will be incremented past it.- Parameters:
comparison- the comparison function- Returns:
trueif the character in the text matches using the comparison function
-
matchAny
public boolean matchAny(String any)
Match the current character in the text against any of the characters in a givenString. Following a successful match the start index will point to the matched character and the index will be incremented past it.- Parameters:
any- the characters to match against (as aString)- Returns:
trueif the character in the text at the index matches any of the characters in the string
-
matchSeq
public boolean matchSeq(int maxChars, int minChars, CharPredicate comparison)Match the characters at the index using the specified comparison function, with a given minimum number of characters and an optional maximum. To match a fixed number of characters, the maximum and minimum should be set to the same value.- Parameters:
maxChars- the maximum number of characters to match (or 0 to indicate no limit)minChars- the minimum number of characters for a successful matchcomparison- the comparison function- Returns:
trueif the characters in the text at the index satisfy the comparison function (subject to the specified minimum and maximum number of characters)
-
matchSeq
public boolean matchSeq(int maxChars, CharPredicate comparison)Match the characters at the index using the specified comparison function, with a minimum of 1 character and an optional maximum.- Parameters:
maxChars- the maximum number of characters to match (or 0 to indicate no limit)comparison- the comparison function- Returns:
trueif one or more characters in the text at the index satisfy the comparison function (subject to the specified maximum number of characters)
-
matchSeq
public boolean matchSeq(CharPredicate comparison)
Match the characters at the index using the specified comparison function, with a minimum of 1 character and no maximum.- Parameters:
comparison- the comparison function- Returns:
trueif one or more characters in the text at the index satisfy the comparison function
-
matchDec
public 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. To match a fixed number of digits, the maximum and minimum should be set to the same value.- Parameters:
maxDigits- the maximum number of digits to match (or 0 to indicate no limit)minDigits- the minimum number of digits for a successful match- Returns:
trueif the characters in the text at the index are decimal digits (subject to the specified minimum and maximum number of digits)
-
matchDec
public boolean matchDec(int maxDigits)
Match the characters at the index as decimal digits, with a minimum of 1 digit and an optional maximum.- Parameters:
maxDigits- the maximum number of digits to match (or 0 to indicate no limit)- Returns:
trueif one or more characters in the text at the index are decimal digits (subject to the specified maximum number of digits)
-
matchDec
public boolean matchDec()
Match the characters at the index as decimal digits, with a minimum of 1 digit and no maximum.- Returns:
trueif one or more characters in the text at the index are decimal digits
-
matchHex
public 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. To match a fixed number of digits, the maximum and minimum should be set to the same value.- Parameters:
maxDigits- the maximum number of digits to match (or 0 to indicate no limit)minDigits- the minimum number of digits for a successful match- Returns:
trueif the characters in the text at the index are hexadecimal digits (subject to the specified minimum and maximum number of digits)
-
matchHex
public boolean matchHex(int maxDigits)
Match the characters at the index as hexadecimal digits, with a minimum of 1 digit and an optional maximum.- Parameters:
maxDigits- the maximum number of digits to match (or 0 to indicate no limit)- Returns:
trueif one or more characters in the text at the index are hexadecimal digits (subject to the specified maximum number of digits)
-
matchHex
public boolean matchHex()
Match the characters at the index as hexadecimal digits, with a minimum of 1 digit and no maximum.- Returns:
trueif one or more characters in the text at the index are hexadecimal digits
-
matchContinue
public boolean matchContinue(int maxChars, int minChars, CharPredicate comparison)Match the characters at the index as a continuation using the specified comparison function, with a given minimum number of characters and an optional maximum, but do not set the start index on success, and on fail, set the index back to the start index (as if the original match had failed). To match a fixed number of characters, the maximum and minimum should be set to the same value.- Parameters:
maxChars- the maximum number of characters to match (or 0 to indicate no limit)minChars- the minimum number of characters for a successful matchcomparison- the comparison function- Returns:
trueif the characters in the text at the index satisfy the comparison function (subject to the specified minimum and maximum number of characters)
-
matchContinue
public boolean matchContinue(int maxChars, CharPredicate comparison)Match the characters at the index as a continuation using the specified comparison function, with no minimum number of characters and an optional maximum, but do not set the start index on success, and on fail, set the index back to the start index (as if the original match had failed).- Parameters:
maxChars- the maximum number of characters to match (or 0 to indicate no limit)comparison- the comparison function- Returns:
true(with a minimum of zero the function can not fail; the only effect is to set the index past the matching characters)
-
matchContinue
public boolean matchContinue(CharPredicate comparison)
Match the characters at the index as a continuation using the specified comparison function, with no minimum or maximum number of characters, but do not set the start index on success, and on fail, set the index back to the start index (as if the original match had failed).- Parameters:
comparison- the comparison function- Returns:
true(with a minimum of zero the function can not fail; the only effect is to set the index past any matching characters)
-
skipAny
public void skipAny(String any)
Increment the index past any of the characters in a given string.- Parameters:
any- the characters to be skipped, as aString
-
skip
public void skip(CharPredicate comparison)
Increment the index past any characters matching a given comparison function.- Parameters:
comparison- the comparison function
-
skipToEnd
public void skipToEnd()
Increment the index directly to the end of the text.
-
skipFixed
public void skipFixed(int n)
Increment the index by a fixed amount.- Parameters:
n- the number of characters to skip (must be positive)- Throws:
IllegalArgumentException- if the increment is negativeStringIndexOutOfBoundsException- if the incremented index is beyond end of string
-
nextChar
public char nextChar()
Get the character at the current index and increment the index.- Returns:
- the current character
- Throws:
StringIndexOutOfBoundsException- if the index is at or beyond end of string
-
getString
public String getString(int start, int end)
Get a substring of the text.- Parameters:
start- the start offsetend- the end offset (exclusive)- Returns:
- the substring
- Throws:
StringIndexOutOfBoundsException- if the start offset is lees than zero, the end offset is less than the start offset, or the end offset is greater than the length
-
getCharSeq
public CharSequence getCharSeq(int start, int end)
Get a substring of the text as aCharSequence. This will be slightly more efficient than getting aString, for those cases where aCharSequenceis just as useful.- Parameters:
start- the start offsetend- the end offset (exclusive)- Returns:
- the
CharSequence - Throws:
IndexOutOfBoundsException- if the start offset is lees than zero, the end offset is less than the start offset, or the end offset is greater than the length
-
getResultChar
public char getResultChar()
Get the result of the last match operation (or the first character of a longer match) as a single character.- Returns:
- the first character of the result of the last match
- Throws:
IndexOutOfBoundsException- if the start index at or beyond the end of the text
-
getResult
public String getResult()
Get the result of the last match operation as aString.- Returns:
- the result of the last match
-
getResultCharSeq
public CharSequence getResultCharSeq()
Get the result of the last match operation as aCharSequence. This will be slightly more efficient than getting aString, for those cases where aCharSequenceis just as useful.- Returns:
- the result of the last match
-
getResultLength
public int getResultLength()
Get the length of the result of the last match operation.- Returns:
- the length of the result of the last match
-
getResultInt
public int getResultInt()
Get the result of the last match operation as anint.- Returns:
- the result of the last match as an
int(always positive) - Throws:
NumberFormatException- if the start and end indices do not describe a validint
-
getResultInt
public int getResultInt(boolean negative)
Get the result of the last match operation as anint.- Parameters:
negative-trueto indicate that the value must be negated- Returns:
- the result of the last match as an
int(always positive) - Throws:
NumberFormatException- if the start and end indices do not describe a validint
-
getInt
public int getInt(int from, int to)Get a positiveintfrom the text.- Parameters:
from- the start offsetto- the end offset (exclusive)- Returns:
- the
int(always positive) - Throws:
NumberFormatException- if the start and end indices do not describe a validintIndexOutOfBoundsException- if the start and end indices are not contained within the text
-
getInt
public int getInt(int from, int to, boolean negative)Get a signedintfrom the text.- Parameters:
from- the start offsetto- the end offset (exclusive)negative-trueto indicate that the value must be negated- Returns:
- the
int - Throws:
NumberFormatException- if the start and end indices do not describe a validintIndexOutOfBoundsException- if the start and end indices are not contained within the text
-
getResultLong
public long getResultLong()
Get the result of the last match operation as along.- Returns:
- the result of the last match as a
long(always positive) - Throws:
NumberFormatException- if the start and end indices do not describe a validlong
-
getResultLong
public long getResultLong(boolean negative)
Get the result of the last match operation as along.- Parameters:
negative-trueto indicate that the value must be negated- Returns:
- the result of the last match as a
long(always positive) - Throws:
NumberFormatException- if the start and end indices do not describe a validlong
-
getLong
public long getLong(int from, int to)Get a positivelongfrom the text.- Parameters:
from- the start offsetto- the end offset (exclusive)- Returns:
- the
long(always positive) - Throws:
NumberFormatException- if the start and end indices do not describe a validlongIndexOutOfBoundsException- if the start and end indices are not contained within the text
-
getLong
public long getLong(int from, int to, boolean negative)Get a signedlongfrom the text.- Parameters:
from- the start offsetto- the end offset (exclusive)negative-trueto indicate that the value must be negated- Returns:
- the
long - Throws:
NumberFormatException- if the start and end indices do not describe a validlongIndexOutOfBoundsException- if the start and end indices are not contained within the text
-
getResultHexInt
public int getResultHexInt()
Get the result of the last match operation as an unsignedint, treating the digits as hexadecimal.- Returns:
- the result of the last match as an
int - Throws:
NumberFormatException- if the start and end indices do not describe a validint
-
getHexInt
public int getHexInt(int from, int to)Get an unsignedintfrom the text, treating the digits as hexadecimal.- Parameters:
from- the start offsetto- the end offset (exclusive)- Returns:
- the hexadecimal
int - Throws:
NumberFormatException- if the start and end indices do not describe a validintIndexOutOfBoundsException- if the start and end indices are not contained within the text
-
getResultHexLong
public long getResultHexLong()
Get the result of the last match operation as an unsignedlong, treating the digits as hexadecimal.- Returns:
- the result of the last match as a
long - Throws:
NumberFormatException- if the start and end indices do not describe a validlong
-
getHexLong
public long getHexLong(int from, int to)Get an unsignedlongfrom the text, treating the digits as hexadecimal.- Parameters:
from- the start offsetto- the end offset (exclusive)- Returns:
- the hexadecimal
long - Throws:
NumberFormatException- if the start and end indices do not describe a validlongIndexOutOfBoundsException- if the start and end indices are not contained within the text
-
isDigit
public static boolean isDigit(char ch)
Test whether the given character is a digit.- Parameters:
ch- the character- Returns:
trueif the character is a digit
-
isHexDigit
public static boolean isHexDigit(char ch)
Test whether the given character is a hexadecimal digit.- Parameters:
ch- the character- Returns:
trueif the character is a hexadecimal digit
-
convertDecDigit
public static int convertDecDigit(char ch)
Convert a decimal digit to the integer value of the digit.- Parameters:
ch- the decimal digit- Returns:
- the integer value (0 - 9)
- Throws:
NumberFormatException- if the digit is not valid
-
convertHexDigit
public static int convertHexDigit(char ch)
Convert a hexadecimal digit to the integer value of the digit.- Parameters:
ch- the hexadecimal digit- Returns:
- the integer value (0 - 15)
- Throws:
NumberFormatException- if the digit is not valid
-
-