Class StringMarcher
java.lang.Object
com.end0katz.blobj.StringMarcher
- All Implemented Interfaces:
CharSequence
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclass
Exception to indicate the current scope is insuccesful / has error and to back up until something to handle the error - ie exit/reset the scope - is reached. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected StringBuilder
The string to march.The sections ofdata
that have been matched.The scopes fromenterScope()
&exitScope()
-
Constructor Summary
ConstructorsConstructorDescriptionStringMarcher
(CharSequence data) Construct a StringMarcher marching through aCharSequence
(e.g. aString
);StringMarcher
(StringBuilder data) Construct a StringMarcher marching through aStringBuilder
. -
Method Summary
Modifier and TypeMethodDescriptionMark this scope as successful and discarding the previous scopechar
charAt
(int index) Save the current data to a scope.Match and discard the entire data.boolean
Exit this scope and restore the previous.expect()
Ensure the last unmatched operation obtained something.Query the underlyingStringBuilder
.getData()
Query the marchers data.int
hashCode()
int
length()
Match the literal, or fail if the marcher does not start with the literal.next()
Match the next character. fail if the marcher is empty.next
(int value) Match the nextvalue
characters. fail if the marcher's length is less thanvalue
.peek()
Return the result of the last operation.regexGreedy
(String regex) Try to match portions ofdata
starting with the entire content and shrinking down to the empty string.regexNonGreedy
(String regex) Try to match portions ofdata
starting with the empty string and growing up to the entire content.Reset this scope to the previous without exiting.result()
Return the result of the last operation.subSequence
(int start, int end) boolean
success()
Return if the last unchecked operation succeeded. this will consume the result.toString()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.CharSequence
chars, codePoints, isEmpty
-
Field Details
-
data
The string to march. -
matched
-
scopes
The scopes fromenterScope()
&exitScope()
-
-
Constructor Details
-
StringMarcher
Construct a StringMarcher marching through aCharSequence
(e.g. aString
);- Parameters:
data
- the sequence to march through.
-
StringMarcher
Construct a StringMarcher marching through aStringBuilder
. Changes in the StringMarcher will be reflected in the builder, and vice versa. To prevent this, usenew StringMarcher(yourBuilder.toString());
- Parameters:
data
- the builder to march through;- Throws:
NullPointerException
- if data is null;
-
-
Method Details
-
length
public int length()- Specified by:
length
in interfaceCharSequence
-
charAt
public char charAt(int index) - Specified by:
charAt
in interfaceCharSequence
-
subSequence
- Specified by:
subSequence
in interfaceCharSequence
-
toString
- Specified by:
toString
in interfaceCharSequence
- Overrides:
toString
in classObject
-
hashCode
-
equals
-
getBuilder
Query the underlyingStringBuilder
. Any modifications to this builder will be reflected in the StringMarcher.- Returns:
data
-
getData
Query the marchers data.- Returns:
- the portion of the string that has not been marched through.
-
enterScope
Save the current data to a scope. You can then parse the data, discover it does not fit into pattern a, callresetScope()
, and check for pattern b.- Returns:
this
- See Also:
-
exitScope
Exit this scope and restore the previous.- Returns:
this
- Throws:
NoSuchElementException
- if there are no saved scopes.- See Also:
-
resetScope
Reset this scope to the previous without exiting.- Returns:
this
- Throws:
NoSuchElementException
- if there are no saved scopes to reset to.- See Also:
-
acceptScope
Mark this scope as successful and discarding the previous scope- Returns:
this
- Throws:
NoSuchElementException
- See Also:
-
result
Return the result of the last operation. If the last operation has been obtained, use the run before that.- Returns:
- the result of the last operation whose value has not been obtained.
- Throws:
NoSuchElementException
- if no operations have taken place or their results have already been obtained.- See Also:
-
success
Return if the last unchecked operation succeeded. this will consume the result.- Returns:
- if the last operation consumed any portion of string, even empty.
- Throws:
NoSuchElementException
- if there are no unmatched operations.- See Also:
-
peek
Return the result of the last operation. If the last operation has been obtained, use the run before that. This does not remove the result.- Returns:
- the result of the last operation whose value has not been obtained.
- Throws:
NoSuchElementException
- if no operations have taken place or their results have already been obtained.- See Also:
-
expect
Ensure the last unmatched operation obtained something.- Returns:
this
- Throws:
NoSuchElementException
- if there are no unmatched operations.StringMarcher.Backup
- if the last unchecked operation failed.- See Also:
-
literal
Match the literal, or fail if the marcher does not start with the literal.- Parameters:
value
- the value to try and match.- Returns:
this
-
next
Match the next character. fail if the marcher is empty.- Returns:
this
- See Also:
-
next
Match the nextvalue
characters. fail if the marcher's length is less thanvalue
.- Parameters:
value
- the number of characters to match- Returns:
this
- See Also:
-
regexGreedy
Try to match portions ofdata
starting with the entire content and shrinking down to the empty string. If any portions match, remove the largest one. Fail if the regex matches neither a substring starting at 0 nor the empty string.- Parameters:
regex
- the regex to try and match.- Returns:
this
- See Also:
-
regexNonGreedy
Try to match portions ofdata
starting with the empty string and growing up to the entire content. If any portions match, remove the smallest one. Fail if the regex matches neither a substring starting at 0 nor the empty string.- Parameters:
regex
- the regex to try and match.- Returns:
this
- See Also:
-
entireString
Match and discard the entire data. This operation is guaranteed not to fail.- Returns:
this
-