- java.lang.Object
-
- io.github.mmm.scanner.AbstractCharStreamScanner
-
- io.github.mmm.scanner.CharSequenceScanner
-
- All Implemented Interfaces:
CharStreamScanner
public class CharSequenceScanner extends AbstractCharStreamScanner
This class represents aStringor better a sequence of characters (char[]) together with apositionin that sequence.
It has various useful methods for scanning the sequence. This scanner is designed to be fast on long sequences and therefore internallyconvertsStrings to a char array instead of frequently callingString.charAt(int).
ATTENTION:
This implementation is NOT and has no intention to be thread-safe.
-
-
Field Summary
-
Fields inherited from class io.github.mmm.scanner.AbstractCharStreamScanner
buffer, limit, offset
-
-
Constructor Summary
Constructors Constructor Description CharSequenceScanner(char[] characters)The constructor.CharSequenceScanner(char[] characters, int offset, int length)The constructor.CharSequenceScanner(CharSequence charSequence)The constructor.CharSequenceScanner(String string)The constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidappendSubstring(StringBuffer appendable, int start, int end)Deprecated.voidappendSubstring(StringBuilder appendable, int start, int end)charcharAt(int index)protected booleanexpectRestWithLookahead(char[] stopChars, boolean ignoreCase, Runnable appender, boolean skip)booleanexpectStrict(String expected, boolean ignoreCase)This method acts asCharStreamScanner.expect(String, boolean)but if the expected String is NOT completely present, no character isconsumedand the state of the scanner remains unchanged.
Attention:
This method requires lookahead.charforceNext()LikeCharStreamScanner.next()this method reads the current character and increments the index.charforcePeek()This method reads the current character without incrementing the index.intgetCurrentIndex()This method gets the current position in the stream to scan.intgetLength()StringgetOriginalString()This method gets the original string to parse.StringgetReplaced(String substitute, int start, int end)This method gets theoriginal stringwhere thesubstringspecified bystartandendis replaced bysubstitute.protected StringgetTail()This method gets the tail of this scanner without changing the state.protected StringgetTail(int maximum)This method gets the tail of this scanner limited (truncated) to the givenmaximumnumber of characters without changing the state.booleanhasNext()This method determines if there is at least one more character available.charnext()This method reads the current character and increments the index stepping to the next character.charpeek()This method reads the current character without incrementing the index.Stringpeek(int count)This method peeks the number ofnext charactersgiven bycountand returns them as string.StringreadUntil(CharFilter filter, boolean acceptEot)This method reads allnext charactersuntil the first characteracceptedby the givenfilteror the end is reached.StringreadWhile(CharFilter filter, int max)voidrequire(String expected, boolean ignoreCase)This method verifies that theexpectedstring gets consumed from this scanner with respect toignoreCase.voidsetCurrentIndex(int index)This method sets thecurrent index.voidstepBack()This method decrements theindexby one.Stringsubstring(int start, int end)-
Methods inherited from class io.github.mmm.scanner.AbstractCharStreamScanner
append, builder, consumeDecimal, eot, expect, expect, fill, getAppended, isEob, isEos, isEot, read, readDigit, readJavaCharLiteral, readJavaStringLiteral, readLine, readLong, readUntil, readUntil, readUntil, readUntil, readUntil, require, reset, skip, skipOver, skipUntil, skipUntil, skipWhile, skipWhile, toString, verifyLookahead
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface io.github.mmm.scanner.CharStreamScanner
expect, expectStrict, readDigit, readDouble, readFloat, readJavaCharLiteral, readJavaStringLiteral, readLine, readUntil, readUntil, readWhile, skipOver, skipOver, skipWhile, skipWhileAndPeek, skipWhileAndPeek
-
-
-
-
Constructor Detail
-
CharSequenceScanner
public CharSequenceScanner(CharSequence charSequence)
The constructor.- Parameters:
charSequence- is thestringto scan.
-
CharSequenceScanner
public CharSequenceScanner(String string)
The constructor.- Parameters:
string- is thestringto parse.
-
CharSequenceScanner
public CharSequenceScanner(char[] characters)
The constructor.- Parameters:
characters- is an array containing the characters to scan.
-
CharSequenceScanner
public CharSequenceScanner(char[] characters, int offset, int length)The constructor.- Parameters:
characters- is an array containing the characters to scan.offset- is the index of the first char to scan incharacters(typically0to start at the beginning of the array).length- is thenumber of charactersto scan fromcharactersstarting atoffset(typicallycharacters.length - offset).
-
-
Method Detail
-
charAt
public char charAt(int index)
- Parameters:
index- is the index of the requested character.- Returns:
- the character at the given
index. - See Also:
CharSequence.charAt(int)
-
getLength
public int getLength()
- Returns:
- the total length of the
string to parse. - See Also:
CharSequence.length()
-
substring
public String substring(int start, int end)
- Parameters:
start- the start index, inclusive.end- the end index, exclusive.- Returns:
- the specified substring.
- See Also:
String.substring(int, int),appendSubstring(StringBuffer, int, int)
-
getReplaced
public String getReplaced(String substitute, int start, int end)
This method gets theoriginal stringwhere thesubstringspecified bystartandendis replaced bysubstitute.- Parameters:
substitute- is the string used as replacement.start- is the inclusive start index of the substring to replace.end- is the exclusive end index of the substring to replace.- Returns:
- the
original stringwith the specified substring replaced bysubstitute.
-
appendSubstring
@Deprecated public void appendSubstring(StringBuffer appendable, int start, int end)
Deprecated.- Parameters:
appendable- is the buffer where to append the substring to.start- the start index, inclusive.end- the end index, exclusive.
-
appendSubstring
public void appendSubstring(StringBuilder appendable, int start, int end)
This method appends thesubstringspecified bystartandendto the givenbuffer.
This avoids the overhead of creating a new string and copying the char array.- Parameters:
appendable- is the buffer where to append the substring to.start- the start index, inclusive.end- the end index, exclusive.- Since:
- 7.5.0
-
getCurrentIndex
public int getCurrentIndex()
This method gets the current position in the stream to scan. It will initially be0. In other words this method returns the number of characters that have already beenconsumed.- Returns:
- the current index position.
-
setCurrentIndex
public void setCurrentIndex(int index)
This method sets thecurrent index.- Parameters:
index- is the next index position to set. The value has to be greater or equal to0and less or equal togetLength().
-
hasNext
public boolean hasNext()
Description copied from interface:CharStreamScannerThis method determines if there is at least one more character available.- Specified by:
hasNextin interfaceCharStreamScanner- Overrides:
hasNextin classAbstractCharStreamScanner- Returns:
trueif there is at least one character available,falseif the end of text (EOT) has been reached.
-
next
public char next()
Description copied from interface:CharStreamScannerThis method reads the current character and increments the index stepping to the next character. You need tocheckif a character is available before calling this method.- Specified by:
nextin interfaceCharStreamScanner- Overrides:
nextin classAbstractCharStreamScanner- Returns:
- the current character.
-
forceNext
public char forceNext()
Description copied from interface:CharStreamScannerLikeCharStreamScanner.next()this method reads the current character and increments the index. If there is no characteravailablethis method will do nothing but return'\0'(the NULL character and NOT'0').- Specified by:
forceNextin interfaceCharStreamScanner- Overrides:
forceNextin classAbstractCharStreamScanner- Returns:
- the current character or
0if none isavailable.
-
peek
public char peek()
Description copied from interface:CharStreamScannerThis method reads the current character without incrementing the index. You need tocheckif a character is available before calling this method.- Specified by:
peekin interfaceCharStreamScanner- Overrides:
peekin classAbstractCharStreamScanner- Returns:
- the current character.
-
forcePeek
public char forcePeek()
Description copied from interface:CharStreamScannerThis method reads the current character without incrementing the index. If there is no characteravailablethis method will return0(the NULL character and NOT'0').- Specified by:
forcePeekin interfaceCharStreamScanner- Overrides:
forcePeekin classAbstractCharStreamScanner- Returns:
- the current character or
0if none isavailable.
-
peek
public String peek(int count)
This method peeks the number ofnext charactersgiven bycountand returns them as string. If there are less charactersavailablethe returned string will be shorter thancountand only contain the available characters. UnlikeAbstractCharStreamScanner.read(int)this method does NOT consume the characters and will therefore NOT change the state of this scanner.- Parameters:
count- is the number of characters to peek. You may useInteger.MAX_VALUEto peek until the end of text (EOT) if the data-size is suitable.- Returns:
- a string with the given number of characters or all available characters if less than
count. Will be the empty string if no character isavailableat all. - Since:
- 3.0.0
-
stepBack
public void stepBack()
-
readUntil
public String readUntil(CharFilter filter, boolean acceptEot)
Description copied from interface:CharStreamScannerThis method reads allnext charactersuntil the first characteracceptedby the givenfilteror the end is reached.
After the call of this method, the current index will point to the firstacceptedstop character or to the end if NO such character exists.- Specified by:
readUntilin interfaceCharStreamScanner- Overrides:
readUntilin classAbstractCharStreamScanner- Parameters:
filter- is used todecidewhere to stop.acceptEot- iftrueifEOTshould be treated like thestopcharacter and the rest of the text will be returned,falseotherwise (to returnnullifEOTwas reached and the scanner has been consumed).- Returns:
- the string with all read characters not
acceptedby the givenCharFilterornullif there was noacceptedcharacter andacceptEotisfalse.
-
expectRestWithLookahead
protected boolean expectRestWithLookahead(char[] stopChars, boolean ignoreCase, Runnable appender, boolean skip)- Specified by:
expectRestWithLookaheadin classAbstractCharStreamScanner- Parameters:
stopChars- the stopStringaschar[]. IfignoreCaseistruein lower case.ignoreCase- -trueto (also) compare chars inlower case,falseotherwise.appender- an optional lambda torunbefore shifting buffers to append data.skip- -trueto update buffers and offset such that on success this scanner points after the expected stopString,falseotherwise (to not consume any character in any case).- Returns:
trueif the stopString(stopChars) was found and consumed,falseotherwise (and no data consumed).- See Also:
CharStreamScanner.readUntil(CharFilter, boolean, String, boolean),AbstractCharStreamScanner.skipOver(String, boolean, CharFilter)
-
expectStrict
public boolean expectStrict(String expected, boolean ignoreCase)
Description copied from interface:CharStreamScannerThis method acts asCharStreamScanner.expect(String, boolean)but if the expected String is NOT completely present, no character isconsumedand the state of the scanner remains unchanged.
Attention:
This method requires lookahead. For implementations that are backed by an underlying stream (or reader) thelengthof the expectedStringshall not exceed the available lookahead size (buffer capacity given at construction time). Otherwise the method may fail.- Parameters:
expected- is the expected string.ignoreCase- - iftruethe case of the characters is ignored when compared.- Returns:
trueif theexpectedstring was successfully consumed from this scanner,falseotherwise.
-
getTail
protected String getTail()
This method gets the tail of this scanner without changing the state.- Returns:
- the tail of this scanner.
-
getTail
protected String getTail(int maximum)
This method gets the tail of this scanner limited (truncated) to the givenmaximumnumber of characters without changing the state.- Parameters:
maximum- is the maximum number of characters to return from thetail.- Returns:
- the tail of this scanner.
-
require
public void require(String expected, boolean ignoreCase)
Description copied from interface:CharStreamScannerThis method verifies that theexpectedstring gets consumed from this scanner with respect toignoreCase. Otherwise an exception is thrown indicating the problem.
This method behaves functionally equivalent to the following code:if (!scanner.
expectStrict(expected, ignoreCase)) { throw newIllegalStateException(...); }- Specified by:
requirein interfaceCharStreamScanner- Overrides:
requirein classAbstractCharStreamScanner- Parameters:
expected- is the expected string.ignoreCase- - iftruethe case of the characters is ignored during comparison.
-
readWhile
public String readWhile(CharFilter filter, int max)
Description copied from interface:CharStreamScannerThis method reads allnext charactersthat areacceptedby the givenfilter.
After the call of this method, the current index will point to the next character that was NOTacceptedby the givenfilter. If the nextmaxcharacters or the characters left until theendof this scanner areaccepted, only that amount of characters are skipped.- Specified by:
readWhilein interfaceCharStreamScanner- Overrides:
readWhilein classAbstractCharStreamScanner- Parameters:
filter- is used todecidewhich characters should be accepted.max- is the maximum number of characters that should be read.- Returns:
- a string with all characters
acceptedby the givenfilterlimited to the length ofmaxand theendof this scanner. Will be the empty string if no character was accepted. - See Also:
CharStreamScanner.skipWhile(char)
-
getOriginalString
public String getOriginalString()
This method gets the original string to parse.- Returns:
- the original string.
- See Also:
CharSequenceScanner(String)
-
-