- All Superinterfaces:
AutoCloseable
,TextFormatProcessor
,TextPosition
- All Known Implementing Classes:
AbstractCharStreamScanner
,CharReaderScanner
,CharSequenceScanner
This is the interface for a scanner that can be used to parse a stream or sequence of characters.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final char
The NULL character'\0'
used to indicate the end of stream (EOS).
ATTENTION: Do not confuse and mix'\0'
with'0'
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
default boolean
This method determines if the givenexpected
String
is completely present at the current position.default boolean
This method determines if the givenexpected
String
is completely present at the current position.default boolean
This method determines if the givenexpected
String
is completely present at the current position.boolean
This method determines if the givenexpected
String
is completely present at the current position.default boolean
This method determines if the givenexpected
String
is completely present at the current position.default boolean
expectOne
(char expected) This method checks if thenext character
is equal to the givenexpected
character.boolean
expectOne
(char expected, boolean warning) This method checks if thenext character
is equal to the givenexpected
character.default boolean
expectOne
(CharFilter expected) default boolean
expectUnsafe
(String expected) This method skips allnext characters
as long as they equal to the according character of theexpected
String
.boolean
expectUnsafe
(String expected, boolean ignoreCase) This method skips allnext characters
as long as they equal to the according character of theexpected
string.int
boolean
hasNext()
This method determines if there is at least one more character available.char
next()
This method reads the current character from the stream and increments the index stepping to the next character.char
peek()
This method reads the current character withoutconsuming
characters and will therefore not change the state of this scanner.char
peek
(int lookaheadOffset) peekString
(int count) default String
peekUntil
(CharFilter stopFilter, int maxLen) peekWhile
(CharFilter filter, int maxLen) read
(int count) This method reads the number ofnext characters
given bycount
and returns them as string.void
read
(int count, StringBuilder builder) This method reads the number ofnext characters
given bycount
andappends
them to the givenStringBuilder
.default Boolean
Reads aBoolean
value from this scanner if available.default Boolean
readBoolean
(boolean ignoreCase) Reads aBoolean
value from this scanner if available.default Boolean
readBoolean
(boolean ignoreCase, boolean acceptYesNo) Reads aBoolean
value from this scanner if available.default int
This method reads thenext character
if it is a digit.int
readDigit
(int radix) This method reads thenext character
if it is a digit within the givenradix
.default Double
This method reads the double value (decimal number) starting at the current position by reading as many matching characters as available and returns itsparsed
value.readDouble
(CharScannerRadixHandler radixMode) This method reads the double value (decimal number) starting at the current position by reading as many matching characters as available and returns itsparsed
value.default Float
readFloat
(CharScannerRadixHandler radixMode) default Integer
readInteger
(CharScannerRadixHandler radixMode) default Character
Reads and parses a JavaCharacter
literal value according to JLS 3.10.6.readJavaCharLiteral
(TextFormatMessageType severity) Reads and parses a JavaCharacter
literal value according to JLS 3.10.6.Reads a JavaNumber
literal (e.g.default String
Reads and parses a JavaString
literal value according to JLS 3.10.6.readJavaStringLiteral
(TextFormatMessageType severity) Reads and parses a JavaString
literal value according to JLS 3.10.6.default String
readLine()
readLine
(boolean trim) default Long
readLong()
readLong
(CharScannerRadixHandler radixMode) void
readNumber
(CharScannerNumberParser numberParser) Generic way to read and parse any kind ofNumber
.long
readUnsignedLong
(int maxDigits) This method reads the long starting at the current position by reading as many Latin digits as available but at maximum the givenmaxDigits
and returns itsparsed
value.readUntil
(char stop, boolean acceptEnd) This method reads allnext characters
until the givenstop
character or the end is reached.readUntil
(char stop, boolean acceptEnd, char escape) This method reads allnext characters
until the given (un-escaped)stop
character or the end is reached.readUntil
(char stop, boolean acceptEnd, CharScannerSyntax syntax) This method reads allnext characters
until the givenstop
character or the end of the string to parse is reached.readUntil
(CharFilter filter, boolean acceptEnd) This method reads allnext characters
until the first characteraccepted
by the givenfilter
or the end is reached.readUntil
(CharFilter filter, boolean acceptEnd, CharScannerSyntax syntax) This method reads allnext characters
until the givenCharFilter
accepts
the current character as stop character or the end of data is reached.default String
readUntil
(CharFilter filter, boolean acceptEnd, String stop) This method reads allnext characters
until the first characteraccepted
by the givenfilter
, the givenstop
String
or the end is reached.default String
readUntil
(CharFilter filter, boolean acceptEnd, String stop, boolean ignoreCase) This method reads allnext characters
until the first characteraccepted
by the givenfilter
, the givenstop
String
or the end is reached.readUntil
(CharFilter filter, boolean acceptEnd, String stop, boolean ignoreCase, boolean trim) This method reads allnext characters
until the first characteraccepted
by the givenfilter
, the givenstop
String
or the end is reached.default String
readUntil
(CharFilter stopFilter, int min, int max) default String
readWhile
(CharFilter filter) readWhile
(CharFilter filter, int min, int max) default int
require
(CharFilter filter, int min) default int
require
(CharFilter filter, int min, int max) void
This method verifies that theexpected
string gets consumed from this scanner with respect toignoreCase
.default void
requireOne
(char expected) This method verifies that thenext character
is equal to the givenexpected
character.default int
requireOne
(CharFilter filter) default int
requireOneOrMore
(CharFilter filter) int
skip
(int count) This method skips the number ofnext characters
given bycount
.int
default boolean
This method reads allnext characters
until the givensubstring
has been detected.default boolean
This method reads allnext characters
until the givensubstring
has been detected.boolean
skipOver
(String substring, boolean ignoreCase, CharFilter stopFilter) This method consumes allnext characters
until the givensubstring
has been detected, a character wasaccepted
by the givenCharFilter
or the end of data was reached.
After the call of this method this scanner will point to the next character after the first occurrence ofsubstring
, to the stop character or to end of data.boolean
skipUntil
(char stop) This method skips allnext characters
until the givenstop
character or the end is reached.boolean
skipUntil
(char stop, char escape) This method reads allnext characters
until the givenstop
character or the end of the string to parse is reached.int
skipWhile
(char c) This method reads allnext characters
that are identical to the character given byc
.default int
skipWhile
(CharFilter filter) int
skipWhile
(CharFilter filter, int max) default char
skipWhileAndPeek
(CharFilter filter) Behaves like the following code:default char
skipWhileAndPeek
(CharFilter filter, int max) Behaves like the following code:Methods inherited from interface io.github.mmm.base.text.TextFormatProcessor
addError, addInfo, addMessage, addMessage, addWarning, getMessages
Methods inherited from interface io.github.mmm.base.text.TextPosition
getColumn, getLine
-
Field Details
-
EOS
static final char EOSThe NULL character'\0'
used to indicate the end of stream (EOS).
ATTENTION: Do not confuse and mix'\0'
with'0'
.- See Also:
-
-
Method Details
-
hasNext
boolean hasNext()This method determines if there is at least one more character available.- Returns:
true
if there is at least one character available,false
if the end of data has been reached.
-
next
char next()This method reads the current character from the stream and increments the index stepping to the next character. You shouldcheck
if a character is available before calling this method. Otherwise if your stream may contain the NUL character ('\0') you can not distinguish if the end of the stream was reached or you actually read the NUL character. -
peek
char peek()This method reads the current character withoutconsuming
characters and will therefore not change the state of this scanner. -
peek
char peek(int lookaheadOffset) Likepeek()
but with further lookahead.
Attention:
This method requires lookahead. For implementations that are backed by an underlying stream (or reader) the givenlookaheadOffset
shall not exceed the available lookahead size (buffer capacity given at construction time). Otherwise the method may fail. -
peekString
This method peeks the number ofnext characters
given bycount
and returns them asString
. If there are less charactersavailable
the returnedString
will be shorter thancount
and only contain the available characters. Unlikeread(int)
this method does notconsume
the characters and will therefore not change the state of this scanner.
Attention:
This method requires lookahead. For implementations that are backed by an underlying stream (or reader) the givencount
shall not exceed the available lookahead size (buffer capacity given at construction time). Otherwise the method may fail.- 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.
-
peekWhile
- Parameters:
filter
- theCharFilter
accepting
only the characters to peek.maxLen
- the maximum number of characters to peek (to get as lookahead without modifying this stream).- Returns:
- a
String
with thepeeked
characters of the givenmaxLen
or less if a character was hit that is notaccepted
by the givenfilter
or the end-of-text has been reached before. The state of this stream remains unchanged. - See Also:
-
peekUntil
- Parameters:
stopFilter
- theCharFilter
that decides which characters toaccept
as stop characters.maxLen
- the maximum number of characters to peek (get as lookahead without modifying this stream).- Returns:
- a
String
with thepeeked
characters of the givenmaxLen
or less if a stop character was hit or the end-of-text has been reached before. The state of this stream remains unchanged. - See Also:
-
read
This method reads 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.- Parameters:
count
- is the number of characters to read. You may useInteger.MAX_VALUE
to read until the end of data 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.
-
read
This method reads the number ofnext characters
given bycount
andappends
them to the givenStringBuilder
. If there are less charactersavailable
then only the remaining characters will be appended resulting in less characters thancount
.- Parameters:
count
- is the number of characters to read. You may useInteger.MAX_VALUE
to read until the end of data if the data-size is suitable.builder
- theStringBuilder
where toappend
the characters to read.
-
getPosition
int getPosition()- Returns:
- the position in the sequence to scan or in other words the number of characters that have been read. Will
initially be
0
. Please note that this API is designed for scanning textual content (for parsers). Therefore we consider 2.1 terabyte as a suitablelimit
.
-
readUntil
This method reads allnext characters
until the givenstop
character or the end is reached.
After the call of this method, the current index will point to the next character after the (first)stop
character or to the end if NO such character exists.- Parameters:
stop
- is the character to read until.acceptEnd
- iftrue
the end of data will be treated asstop
, too.- Returns:
- the string with all read characters excluding the
stop
character ornull
if there was nostop
character andacceptEnd
isfalse
.
-
readUntil
This method reads allnext characters
until the given (un-escaped)stop
character or the end is reached.
In advance toreadUntil(char, boolean)
, this method allows that thestop
character may be used in the input-string by adding the givenescape
character. After the call of this method, the current index will point to the next character after the (first)stop
character or to the end if NO such character exists.
This method is especially useful when quoted strings should be parsed. E.g.:CharStreamScanner
scanner = getScanner(); doSomething(); char c = scanner.next()
; if ((c == '"') || (c == '\'')) { char escape = c; // may also be something like '\\' String quote = scanner.readUntil
(c, false, escape) } else { doOtherThings(); }- Parameters:
stop
- is the character to read until.acceptEnd
- iftrue
the end of data will be treated asstop
, too.escape
- is the character used to escape thestop
character. To add an occurrence of theescape
character it has to be duplicated (occur twice). Theescape
character may also be equal to thestop
character. If other regular characters are escaped theescape
character is simply ignored.- Returns:
- the string with all read characters excluding the
stop
character ornull
if there was nostop
character andacceptEnd
isfalse
.
-
readUntil
This method reads allnext characters
until the givenstop
character or the end of the string to parse is reached. In advance toreadUntil(char, boolean)
, this method will scan the input using the givensyntax
which e.g. allows toescape
the stop character.
After the call of this method, the current index will point to the next character after the (first)stop
character or to the end of the string if NO such character exists.- Parameters:
stop
- is the character to read until.acceptEnd
- iftrue
the end of data will be treated asstop
, too.syntax
- contains the characters specific for the syntax to read.- Returns:
- the string with all read characters excluding the
stop
character ornull
if there was nostop
character. - See Also:
-
readUntil
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.- Parameters:
filter
- is used todecide
where to stop.acceptEnd
- iftrue
if end of data should be treated like thestop
character and the rest of the text will be returned,false
otherwise (to returnnull
if the end of data 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 andacceptEnd
isfalse
.
-
readUntil
This method reads allnext characters
until the first characteraccepted
by the givenfilter
, the givenstop
String
or the end is reached.
After the call of this method, the current index will point to the firstaccepted
stop character, or to the first character of the givenstop
String
or to the end if NO such character exists.- Parameters:
filter
- is used todecide
where to stop.acceptEnd
- iftrue
if the end of data should be treated like thestop
character and the rest of the text will be returned,false
otherwise (to returnnull
if end of data was reached and the scanner has been consumed).stop
- theString
where to stop consuming data. Should be at least two characters long (otherwise accept byCharFilter
instead).- Returns:
- the string with all read characters not
accepted
by the givenCharFilter
or until the givenstop
String
was detected. If end of data was reached without a stop signal the entire rest of the data is returned ornull
ifacceptEnd
isfalse
.
-
readUntil
This method reads allnext characters
until the first characteraccepted
by the givenfilter
, the givenstop
String
or the end is reached.
After the call of this method, the current index will point to the firstaccepted
stop character, or to the first character of the givenstop
String
or to the end if NO such character exists.- Parameters:
filter
- is used todecide
where to stop.acceptEnd
- iftrue
if the end of data should be treated like thestop
character and the rest of the text will be returned,false
otherwise (to returnnull
if the end of data was reached and the scanner has been consumed).stop
- theString
where to stop consuming data. Should be at least two characters long (otherwise accept byCharFilter
instead).ignoreCase
- - iftrue
the case of the characters is ignored when compared with characters fromstop
String
.- Returns:
- the string with all read characters not
accepted
by the givenCharFilter
or until the givenstop
String
was detected. If the end of data was reached without a stop signal the entire rest of the data is returned ornull
ifacceptEnd
isfalse
.
-
readUntil
String readUntil(CharFilter filter, boolean acceptEnd, String stop, boolean ignoreCase, boolean trim) This method reads allnext characters
until the first characteraccepted
by the givenfilter
, the givenstop
String
or the end is reached.
After the call of this method, the current index will point to the firstaccepted
stop character, or to the first character of the givenstop
String
or to the end if NO such character exists.- Parameters:
filter
- is used todecide
where to stop.acceptEnd
- iftrue
if the end of data should be treated like thestop
character and the rest of the text will be returned,false
otherwise (to returnnull
if the end of data was reached and the scanner has been consumed).stop
- theString
where to stop consuming data. Should be at least two characters long (otherwise accept byCharFilter
instead).ignoreCase
- - iftrue
the case of the characters is ignored when compared with characters fromstop
String
.trim
- -true
if the result should betrimmed
,false
otherwise.- Returns:
- the string with all read characters not
accepted
by the givenCharFilter
or until the givenstop
String
was detected. If the end of data was reached without hittingstop
the entire rest of the data is returned ornull
ifacceptEnd
isfalse
. Thre result will betrimmed
iftrim
istrue
.
-
readUntil
This method reads allnext characters
until the givenCharFilter
accepts
the current character as stop character or the end of data is reached. In advance toreadUntil(char, boolean)
, this method will scan the input using the givensyntax
which e.g. allows toescape
the stop character.
After the call of this method, the current index will point to the firstaccepted
stop character or to the end of the string if NO such character exists.- Parameters:
filter
- is used todecide
where to stop.acceptEnd
- iftrue
the end of data will be treated asstop
, too.syntax
- contains the characters specific for the syntax to read.- Returns:
- the string with all read characters excluding the
stop
character ornull
if there was nostop
character. - See Also:
-
readUntil
- Parameters:
stopFilter
- theCharFilter
that decides which characters toaccept
as stop characters.min
- the minimum number of characters expected.max
- the (maximum) length of the characters to consume.- Returns:
- the
String
with all consumed characters excluding the stop character. If nostop
character was found untilmaxLength
characters have been consumed, this method behaves likeread(maxLength)
. - Throws:
IllegalStateException
- if less than the minimum number of characters have beenrejected
.- See Also:
-
readWhile
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
or to the end if NO such character exists. -
readWhile
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.- Parameters:
filter
- used todecide
which characters should be accepted.min
- the minimum number of characters expected.max
- 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. - Throws:
IllegalStateException
- if less than the minimum number of characters have beenaccepted
.- See Also:
-
readLine
-
readLine
-
readBoolean
Reads aBoolean
value from this scanner if available. -
readBoolean
Reads aBoolean
value from this scanner if available. -
readBoolean
Reads aBoolean
value from this scanner if available.- Parameters:
ignoreCase
- - iftrue
the case of the characters is ignored when compared,false
otherwise (only lower case is accepted).acceptYesNo
- - iftrue
also "yes" is accepted fortrue
and "no" forfalse
,false
otherwise.- Returns:
- the consumed
Boolean
value ornull
if no such value was available and theposition
remains unchanged.
-
readNumber
Generic way to read and parse any kind ofNumber
.- Parameters:
numberParser
- theCharScannerNumberParser
. Can decide if sign, digits, radix, exponent, or even specials are
-
readDouble
This method reads the double value (decimal number) starting at the current position by reading as many matching characters as available and returns itsparsed
value.- Returns:
- the parsed
double
number ornull
if the current current position does not point to a number. - Throws:
NumberFormatException
- if the number at the current position could not be parsed.
-
readDouble
This method reads the double value (decimal number) starting at the current position by reading as many matching characters as available and returns itsparsed
value.- Parameters:
radixMode
- theCharScannerRadixHandler
- e.g.CharScannerRadixMode.ALL
.- Returns:
- the parsed
double
number ornull
if the current current position does not point to a number. - Throws:
NumberFormatException
- if the number at the current position could not be parsed.
-
readFloat
This method reads aFloat
value from the current positionconsuming
as many matching characters as available.- Returns:
- the parsed
Float
value ornull
if the current current position does not point to aFloat
number. - Throws:
NumberFormatException
- if the number at the current position could not be parsed.
-
readFloat
This method reads aFloat
value from the current positionconsuming
as many matching characters as available.- Parameters:
radixMode
- theCharScannerRadixHandler
- e.g.CharScannerRadixMode.ALL
.- Returns:
- the parsed
Float
value ornull
if the current current position does not point to aFloat
number. - Throws:
NumberFormatException
- if the number at the current position could not be parsed.
-
readLong
- Returns:
- the consumed
Long
value ornull
if no number was present and theposition
remains unchanged. - Throws:
NumberFormatException
- if the current current position points to a number that is not aLong
value.
-
readLong
- Parameters:
radixMode
- theCharScannerRadixHandler
- e.g.CharScannerRadixMode.ALL
.- Returns:
- the consumed
Long
value ornull
if no such value was present and theposition
remains unchanged. - Throws:
NumberFormatException
- if the current current position points to a number that is not aLong
value.
-
readInteger
- Returns:
- the consumed
Integer
value ornull
if no such value was present and theposition
remains unchanged. - Throws:
NumberFormatException
- if the current current position does not point to aInteger
value.
-
readInteger
- Parameters:
radixMode
- theCharScannerRadixHandler
- e.g.CharScannerRadixMode.ALL
.- Returns:
- the consumed
Integer
value ornull
if no such value was present and theposition
remains unchanged. - Throws:
NumberFormatException
- if the current current position does not point to aLong
value.
-
readJavaNumberLiteral
Number readJavaNumberLiteral()Reads a JavaNumber
literal (e.g. "1L" or "1.3F").- Returns:
- the consumed
Number
ornull
if no number literal was found and theposition
remains unchainged. - Throws:
NumberFormatException
- if a number literal was found that has an illegal format.
-
readDigit
default int readDigit()This method reads thenext character
if it is a digit. Else the state remains unchanged.- Returns:
- the numeric value of the next Latin digit (e.g.
0
if'0'
) or-1
if thenext character
is no Latin digit.
-
readDigit
int readDigit(int radix) This method reads thenext character
if it is a digit within the givenradix
. Else the state remains unchanged.- Parameters:
radix
- the radix that defines the range of the digits. SeeInteger.parseInt(String, int)
. E.g.10
to read any Latin digit (seereadDigit()
),8
to read octal digit,16
to read hex decimal digits.- Returns:
- the numeric value of the next digit within the given
radix
or-1
if thenext character
is no such digit.
-
readUnsignedLong
This method reads the long starting at the current position by reading as many Latin digits as available but at maximum the givenmaxDigits
and returns itsparsed
value.
ATTENTION:
This method does NOT treat signs (+
or-
) to do so, scan them yourself before and negate the result as needed.- Parameters:
maxDigits
- is the maximum number of digits that will be read. The value has to be positive (greater than zero). Should not be greater than19
as this will exceed the range oflong
.- Returns:
- the parsed number.
- Throws:
NumberFormatException
- if the number at the current position could not be parsed.
-
readJavaStringLiteral
-
readJavaStringLiteral
Reads and parses a JavaString
literal value according to JLS 3.10.6.
As a complex example for the input "Hi \"\176\477\579•∑\"\n" this scanner would return theString
outputHi "~'7/9•∑"
followed by a newline character.- Parameters:
severity
- theTextFormatMessageType
to use to report invalid escape sequences or missing terminating quotation.- Returns:
- the parsed Java
String
literal value ornull
if not pointing to aString
literal.
-
readJavaCharLiteral
Reads and parses a JavaCharacter
literal value according to JLS 3.10.6.
Examples are given in the following table:literal result comment 'a'
a regular char '\''
' escaped char '\176'
~ escaped octal representation '•'
• escaped unicode representation -
readJavaCharLiteral
Reads and parses a JavaCharacter
literal value according to JLS 3.10.6.
Examples are given in the following table:literal result comment 'a'
a regular char '\''
' escaped char '\176'
~ escaped octal representation '•'
• escaped unicode representation - Parameters:
severity
- theTextFormatMessageType
to use to report invalid escape sequences or missing terminating quotation.- Returns:
- the parsed Java
Character
literal value ornull
if not pointing to aCharacter
literal.
-
expect
This method determines if the givenexpected
String
is completely present at the current position. It will onlyconsume
characters and change the state if theexpected
String
was found (entirely).
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.- Returns:
true
if theexpected
string was successfully consumed from this scanner,false
otherwise.- See Also:
-
expect
This method determines if the givenexpected
String
is completely present at the current position. It will onlyconsume
characters and change the state if theexpected
String
was found (entirely).
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. -
expect
This method determines if the givenexpected
String
is completely present at the current position. It will onlyconsume
characters and change the state iflookahead
isfalse
and theexpected
String
was found (entirely).
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
- the expectedString
to search for.ignoreCase
- - iftrue
the case of the characters is ignored when compared,false
otherwise.lookahead
- - iftrue
the state of the scanner remains unchanged even if the expectedString
has been found,false
otherwise (expectedString
is consumed on match).- Returns:
true
if theexpected
string was successfully found,false
otherwise.
-
expect
This method determines if the givenexpected
String
is completely present at the current position. It will onlyconsume
characters and change the state iflookahead
isfalse
and theexpected
String
was found (entirely).
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
- the expectedString
to search for.ignoreCase
- - iftrue
the case of the characters is ignored when compared,false
otherwise.lookahead
- - iftrue
the state of the scanner remains unchanged even if the expectedString
has been found,false
otherwise (expectedString
is consumed on match).offset
- the number of characters that have already beenpeeked
and after which the givenString
is expected. Will typically be0
. Iflookahead
isfalse
and the expectedString
was found these characters will beskipped
together with the expectedString
.- Returns:
true
if theexpected
string was successfully found,false
otherwise.
-
expect
default boolean expect(String expected, boolean ignoreCase, boolean lookahead, int offset, boolean warning) This method determines if the givenexpected
String
is completely present at the current position. It will onlyconsume
characters and change the state iflookahead
isfalse
and theexpected
String
was found (entirely).
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
- the expectedString
to search for.ignoreCase
- - iftrue
the case of the characters is ignored when compared,false
otherwise.lookahead
- - iftrue
the state of the scanner remains unchanged even if the expectedString
has been found,false
otherwise (expectedString
is consumed on match).offset
- the number of characters that have already beenpeeked
and after which the givenString
is expected. Will typically be0
. Iflookahead
isfalse
and the expectedString
was found these characters will beskipped
together with the expectedString
.warning
-true
toadd a warning
in case the expectedString
was not found,false
otherwise.- Returns:
true
if theexpected
string was successfully found,false
otherwise.
-
expectOne
default boolean expectOne(char expected) This method checks if thenext character
is equal to the givenexpected
character.
If the character matched with theexpected
character, the parser points to the next character. Otherwise its position will remain unchanged.- Parameters:
expected
- is the expected character.- Returns:
true
if the current character is the same asexpected
,false
otherwise.
-
expectOne
boolean expectOne(char expected, boolean warning) This method checks if thenext character
is equal to the givenexpected
character.
If the character matched with theexpected
character, the parser points to the next character. Otherwise its position will remain unchanged.- Parameters:
expected
- the character to expect asnext
in this stream.warning
-true
toadd a warning
in case the expected character was not present,false
otherwise.- Returns:
true
if the expected character was found and consumer,false
otherwise (and this stream remains unchanged).
-
expectOne
This method checks that thenext character
isaccepted
by the givenCharFilter
.
If the current character was as expected, the parser points to the next character. Otherwise its position will remain unchanged.- Parameters:
expected
- is theCharFilter
accepting
the expected chars.- Returns:
true
if the current character isaccepted
,false
otherwise.
-
expectUnsafe
This method skips allnext characters
as long as they equal to the according character of theexpected
String
.
If a character differs this method stops and the parser points to the first character that differs fromexpected
. Except for the latter circumstance, this method behaves similar to the following code:read
(expected.length).equals(expected)
In most cases you want to preferexpect(String)
instead of using this method. Only in specific cases and for highly optimized performance it may make sense to use it. In such case be careful and consider to combine withgetPosition()
to be able to determine whether characters have been consumed iffalse
was returned (e.g. otherwise when doingexpectUnsafe
("false") and else doingexpectUnsafe
("true") to parse aboolean
literal your code could accept "falstrue" as "true").- Parameters:
expected
- is the expected string.- Returns:
true
if theexpected
string was successfully consumed from this scanner,false
otherwise.- See Also:
-
expectUnsafe
This method skips allnext characters
as long as they equal to the according character of theexpected
string.
If a character differs this method stops and the parser points to the first character that differs fromexpected
. Except for the latter circumstance, this method behaves similar to the following code:read
(expected.length).equals[IgnoreCase](expected)
In most cases you want to preferexpect(String, boolean)
instead of using this method. SeeexpectUnsafe(String)
for details.- 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.- See Also:
-
requireOne
This method verifies that thenext character
is equal to the givenexpected
character.
If the current character was as expected, the parser points to the next character. Otherwise an exception is thrown indicating the problem.- Parameters:
expected
- is the expected character.- Throws:
IllegalStateException
- if the required character was not found.
-
require
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.
expectUnsafe
(expected, ignoreCase)) { throw newIllegalStateException
(...); }- Parameters:
expected
- is the expected string.ignoreCase
- - iftrue
the case of the characters is ignored during comparison.
-
requireOne
- Parameters:
filter
- theCharFilter
accepting
the expected characters toskip
.- Returns:
- the actual number of characters that have been skipped.
- Throws:
IllegalStateException
- if less than1
or more than1000
accepted
characters have been consumed.
-
requireOneOrMore
- Parameters:
filter
- theCharFilter
accepting
the expected characters toskip
.- Returns:
- the actual number of characters that have been skipped.
- Throws:
IllegalStateException
- if less than1
or more than1000
accepted
characters have been consumed.
-
require
- Parameters:
filter
- theCharFilter
accepting
the expected characters toskip
.min
- the minimum required number of skipped characters.- Returns:
- the actual number of characters that have been skipped.
- Throws:
IllegalStateException
- if less thanmin
or more than1000
accepted
characters have been consumed.
-
require
- Parameters:
filter
- theCharFilter
accepting
the expected characters toskip
.min
- the minimum required number of skipped characters.max
- the maximum number of skipped characters.- Returns:
- the actual number of characters that have been skipped.
- Throws:
IllegalStateException
- if less thanmin
or more thanmax
accepted
characters have been consumed.
-
skipUntil
boolean skipUntil(char stop) This method skips allnext characters
until the givenstop
character or the end is reached. If thestop
character was reached, this scanner will point to the next character afterstop
when this method returns.- Parameters:
stop
- is the character to read until.- Returns:
true
if the first occurrence of the givenstop
character has been passed,false
if there is no such character.
-
skipUntil
boolean skipUntil(char stop, char escape) This method reads allnext characters
until the givenstop
character or the end of the string to parse is reached. In advance toskipUntil(char)
, this method will read over thestop
character if it is escaped with the givenescape
character.- Parameters:
stop
- is the character to read until.escape
- is the character used to escape the stop character (e.g. '\').- Returns:
true
if the first occurrence of the givenstop
character has been passed,false
if there is no such character.
-
skip
int skip(int count) This method skips the number ofnext characters
given bycount
.- Parameters:
count
- is the number of characters to skip. You may useInteger.MAX_VALUE
to read until the end of data if the data-size is suitable.- Returns:
- a to total number of characters that have been skipped. Typically equal to
count
. Will be less in case the end of data was reached.
-
skipNewLine
int skipNewLine()- Returns:
0
if thenext characeter
is not a newline and the stream remains unchanged,1
if thenext characeter
was '\n' and has beenskipped
, or2
if thenext characeters
have been '\r' and '\n' and have beenskipped
.
-
skipOver
This method reads allnext characters
until the givensubstring
has been detected.
After the call of this method, the current index will point to the next character after the first occurrence ofsubstring
or to the end of data if the givensubstring
was NOT found.- Parameters:
substring
- is the substring to search and skip over starting at the current index.- Returns:
true
if the givensubstring
occurred and has been passed andfalse
if the end of the string has been reached without any occurrence of the givensubstring
.
-
skipOver
This method reads allnext characters
until the givensubstring
has been detected.
After the call of this method, the current index will point to the next character after the first occurrence ofsubstring
or to the end of data if the givensubstring
was NOT found.- Parameters:
substring
- is the substring to search and skip over starting at the current index.ignoreCase
- - iftrue
the case of the characters is ignored when compared with characters fromsubstring
.- Returns:
true
if the givensubstring
occurred and has been passed andfalse
if the end of the string has been reached without any occurrence of the givensubstring
.
-
skipOver
This method consumes allnext characters
until the givensubstring
has been detected, a character wasaccepted
by the givenCharFilter
or the end of data was reached.
After the call of this method this scanner will point to the next character after the first occurrence ofsubstring
, to the stop character or to end of data.- Parameters:
substring
- is the substring to search and skip over starting at the current index.ignoreCase
- - iftrue
the case of the characters is ignored when compared with characters fromsubstring
.stopFilter
- is the filter used todetect
stop characters. If such character was detected, the skip is stopped and the parser points to the character after the stop character. Thesubstring
should NOT contain astop character
.- Returns:
true
if the givensubstring
occurred and has been passed andfalse
if a stop character has been detected or the end of the string has been reached without any occurrence of the givensubstring
or stop character.
-
skipWhile
int skipWhile(char c) This method reads allnext characters
that are identical to the character given byc
.
E.g. usereadWhile(' ')
to skip all blanks from the current index. After the call of this method, the current index will point to the next character that is different to the given characterc
or to the end if NO such character exists.- Parameters:
c
- is the character to read over.- Returns:
- the number of characters that have been skipped.
-
skipWhile
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
or to the end if NO such character exists. -
skipWhile
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.- Parameters:
filter
- is used todecide
which characters should be accepted.max
- is the maximum number of characters that may be skipped.- Returns:
- the number of skipped characters.
- See Also:
-
skipWhileAndPeek
- Parameters:
filter
- is used todecide
which characters should be accepted.- Returns:
- the first character that was not
accepted
by the givenCharFilter
. Only theaccepted
characters have been consumed, this scanner still points to the returned character.
-
skipWhileAndPeek
- Parameters:
filter
- is used todecide
which characters should be accepted.max
- is the maximum number of characters that may be skipped.- Returns:
- the first character that was not
accepted
by the givenCharFilter
. Only theaccepted
characters have been consumed, this scanner still points to the returned character.
-
getBufferParsed
String getBufferParsed()- Returns:
- the
String
with the characters that have already been parsed but are still available in the underlying buffer. May be used for debugging or error messages.
-
getBufferToParse
String getBufferToParse()- Returns:
- the
String
with the characters that have not yet been parsed but are available in the underlying buffer. May be used for debugging or error messages.
-
close
void close()- Specified by:
close
in interfaceAutoCloseable
-