- 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 aString
or better a sequence of characters (char[]
) together with aposition
in that sequence.
It has various useful methods for scanning the sequence. This scanner is designed to be fast on long sequences and therefore internallyconverts
String
s 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 void
appendSubstring(StringBuffer appendable, int start, int end)
Deprecated.void
appendSubstring(StringBuilder appendable, int start, int end)
char
charAt(int index)
protected boolean
expectRestWithLookahead(char[] stopChars, boolean ignoreCase, Runnable appender, boolean skip)
boolean
expectStrict(String expected, boolean ignoreCase)
This method acts asCharStreamScanner.expect(String, boolean)
but if the expected String is NOT completely present, no character isconsumed
and the state of the scanner remains unchanged.
Attention:
This method requires lookahead.char
forceNext()
LikeCharStreamScanner.next()
this method reads the current character and increments the index.char
forcePeek()
This method reads the current character without incrementing the index.int
getCurrentIndex()
This method gets the current position in the stream to scan.int
getLength()
String
getOriginalString()
This method gets the original string to parse.String
getReplaced(String substitute, int start, int end)
This method gets theoriginal string
where thesubstring
specified bystart
andend
is replaced bysubstitute
.protected String
getTail()
This method gets the tail of this scanner without changing the state.protected String
getTail(int maximum)
This method gets the tail of this scanner limited (truncated) to the givenmaximum
number of characters without changing the state.boolean
hasNext()
This method determines if there is at least one more character available.char
next()
This method reads the current character and increments the index stepping to the next character.char
peek()
This method reads the current character without incrementing the index.String
peek(int count)
This method peeks the number ofnext characters
given bycount
and returns them as string.String
readUntil(CharFilter filter, boolean acceptEot)
This method reads allnext characters
until the first characteraccepted
by the givenfilter
or the end is reached.String
readWhile(CharFilter filter, int max)
void
require(String expected, boolean ignoreCase)
This method verifies that theexpected
string gets consumed from this scanner with respect toignoreCase
.void
setCurrentIndex(int index)
This method sets thecurrent index
.void
stepBack()
This method decrements theindex
by one.String
substring(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 thestring
to scan.
-
CharSequenceScanner
public CharSequenceScanner(String string)
The constructor.- Parameters:
string
- is thestring
to 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
(typically0
to start at the beginning of the array).length
- is thenumber of characters
to scan fromcharacters
starting 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 string
where thesubstring
specified bystart
andend
is 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 string
with 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 thesubstring
specified bystart
andend
to 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 to0
and less or equal togetLength()
.
-
hasNext
public boolean hasNext()
Description copied from interface:CharStreamScanner
This method determines if there is at least one more character available.- Specified by:
hasNext
in interfaceCharStreamScanner
- Overrides:
hasNext
in classAbstractCharStreamScanner
- Returns:
true
if there is at least one character available,false
if the end of text (EOT) has been reached.
-
next
public char next()
Description copied from interface:CharStreamScanner
This method reads the current character and increments the index stepping to the next character. You need tocheck
if a character is available before calling this method.- Specified by:
next
in interfaceCharStreamScanner
- Overrides:
next
in classAbstractCharStreamScanner
- Returns:
- the current character.
-
forceNext
public char forceNext()
Description copied from interface:CharStreamScanner
LikeCharStreamScanner.next()
this method reads the current character and increments the index. If there is no characteravailable
this method will do nothing but return'\0'
(the NULL character and NOT'0'
).- Specified by:
forceNext
in interfaceCharStreamScanner
- Overrides:
forceNext
in classAbstractCharStreamScanner
- Returns:
- the current character or
0
if none isavailable
.
-
peek
public char peek()
Description copied from interface:CharStreamScanner
This method reads the current character without incrementing the index. You need tocheck
if a character is available before calling this method.- Specified by:
peek
in interfaceCharStreamScanner
- Overrides:
peek
in classAbstractCharStreamScanner
- Returns:
- the current character.
-
forcePeek
public char forcePeek()
Description copied from interface:CharStreamScanner
This method reads the current character without incrementing the index. If there is no characteravailable
this method will return0
(the NULL character and NOT'0'
).- Specified by:
forcePeek
in interfaceCharStreamScanner
- Overrides:
forcePeek
in classAbstractCharStreamScanner
- Returns:
- the current character or
0
if none isavailable
.
-
peek
public String peek(int count)
This method peeks the number ofnext characters
given bycount
and returns them as string. If there are less charactersavailable
the returned string will be shorter thancount
and 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_VALUE
to 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 isavailable
at all. - Since:
- 3.0.0
-
stepBack
public void stepBack()
-
readUntil
public String readUntil(CharFilter filter, boolean acceptEot)
Description copied from interface:CharStreamScanner
This method reads allnext characters
until the first characteraccepted
by the givenfilter
or the end is reached.
After the call of this method, the current index will point to the firstaccepted
stop character or to the end if NO such character exists.- Specified by:
readUntil
in interfaceCharStreamScanner
- Overrides:
readUntil
in classAbstractCharStreamScanner
- Parameters:
filter
- is used todecide
where to stop.acceptEot
- iftrue
ifEOT
should be treated like thestop
character and the rest of the text will be returned,false
otherwise (to returnnull
ifEOT
was reached and the scanner has been consumed).- Returns:
- the string with all read characters not
accepted
by the givenCharFilter
ornull
if there was noaccepted
character andacceptEot
isfalse
.
-
expectRestWithLookahead
protected boolean expectRestWithLookahead(char[] stopChars, boolean ignoreCase, Runnable appender, boolean skip)
- Specified by:
expectRestWithLookahead
in classAbstractCharStreamScanner
- Parameters:
stopChars
- the stopString
aschar[]
. IfignoreCase
istrue
in lower case.ignoreCase
- -true
to (also) compare chars inlower case
,false
otherwise.appender
- an optional lambda torun
before shifting buffers to append data.skip
- -true
to update buffers and offset such that on success this scanner points after the expected stopString
,false
otherwise (to not consume any character in any case).- Returns:
true
if the stopString
(stopChars
) was found and consumed,false
otherwise (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:CharStreamScanner
This method acts asCharStreamScanner.expect(String, boolean)
but if the expected String is NOT completely present, no character isconsumed
and the state of the scanner remains unchanged.
Attention:
This method requires lookahead. For implementations that are backed by an underlying stream (or reader) thelength
of the expectedString
shall not exceed the available lookahead size (buffer capacity given at construction time). Otherwise the method may fail.- Parameters:
expected
- is the expected string.ignoreCase
- - iftrue
the case of the characters is ignored when compared.- Returns:
true
if theexpected
string was successfully consumed from this scanner,false
otherwise.
-
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 givenmaximum
number 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:CharStreamScanner
This method verifies that theexpected
string 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:
require
in interfaceCharStreamScanner
- Overrides:
require
in classAbstractCharStreamScanner
- Parameters:
expected
- is the expected string.ignoreCase
- - iftrue
the case of the characters is ignored during comparison.
-
readWhile
public String readWhile(CharFilter filter, int max)
Description copied from interface:CharStreamScanner
This method reads allnext characters
that areaccepted
by the givenfilter
.
After the call of this method, the current index will point to the next character that was NOTaccepted
by the givenfilter
. If the nextmax
characters or the characters left until theend
of this scanner areaccepted
, only that amount of characters are skipped.- Specified by:
readWhile
in interfaceCharStreamScanner
- Overrides:
readWhile
in classAbstractCharStreamScanner
- Parameters:
filter
- is used todecide
which characters should be accepted.max
- is the maximum number of characters that should be read.- Returns:
- a string with all characters
accepted
by the givenfilter
limited to the length ofmax
and theend
of 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)
-
-