- 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
FieldsModifier and TypeFieldDescriptionstatic final charThe NULL character'\0'used to indicate the end of stream (EOS).
ATTENTION: Do not confuse and mix'\0'with'0'. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()default booleanThis method determines if the givenexpectedStringis completely present at the current position.default booleanThis method determines if the givenexpectedStringis completely present at the current position.default booleanThis method determines if the givenexpectedStringis completely present at the current position.booleanThis method determines if the givenexpectedStringis completely present at the current position.default booleanThis method determines if the givenexpectedStringis completely present at the current position.default booleanexpectOne(char expected) This method checks if thenext characteris equal to the givenexpectedcharacter.booleanexpectOne(char expected, boolean warning) This method checks if thenext characteris equal to the givenexpectedcharacter.default booleanexpectOne(CharFilter expected) default booleanexpectUnsafe(String expected) This method skips allnext charactersas long as they equal to the according character of theexpectedString.booleanexpectUnsafe(String expected, boolean ignoreCase) This method skips allnext charactersas long as they equal to the according character of theexpectedstring.intbooleanhasNext()This method determines if there is at least one more character available.charnext()This method reads the current character from the stream and increments the index stepping to the next character.charpeek()This method reads the current character withoutconsumingcharacters and will therefore not change the state of this scanner.charpeek(int lookaheadOffset) peekString(int count) default StringpeekUntil(CharFilter stopFilter, int maxLen) peekWhile(CharFilter filter, int maxLen) read(int count) This method reads the number ofnext charactersgiven bycountand returns them as string.voidread(int count, StringBuilder builder) This method reads the number ofnext charactersgiven bycountandappendsthem to the givenStringBuilder.default BooleanReads aBooleanvalue from this scanner if available.default BooleanreadBoolean(boolean ignoreCase) Reads aBooleanvalue from this scanner if available.default BooleanreadBoolean(boolean ignoreCase, boolean acceptYesNo) Reads aBooleanvalue from this scanner if available.default intThis method reads thenext characterif it is a digit.intreadDigit(int radix) This method reads thenext characterif it is a digit within the givenradix.default DoubleThis method reads the double value (decimal number) starting at the current position by reading as many matching characters as available and returns itsparsedvalue.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 itsparsedvalue.default FloatreadFloat(CharScannerRadixHandler radixMode) default IntegerreadInteger(CharScannerRadixHandler radixMode) default CharacterReads and parses a JavaCharacterliteral value according to JLS 3.10.6.readJavaCharLiteral(TextFormatMessageType severity) Reads and parses a JavaCharacterliteral value according to JLS 3.10.6.Reads a JavaNumberliteral (e.g.default StringReads and parses a JavaStringliteral value according to JLS 3.10.6.readJavaStringLiteral(TextFormatMessageType severity) Reads and parses a JavaStringliteral value according to JLS 3.10.6.default StringreadLine()readLine(boolean trim) default LongreadLong()readLong(CharScannerRadixHandler radixMode) voidreadNumber(CharScannerNumberParser numberParser) Generic way to read and parse any kind ofNumber.longreadUnsignedLong(int maxDigits) This method reads the long starting at the current position by reading as many Latin digits as available but at maximum the givenmaxDigitsand returns itsparsedvalue.readUntil(char stop, boolean acceptEnd) This method reads allnext charactersuntil the givenstopcharacter or the end is reached.readUntil(char stop, boolean acceptEnd, char escape) This method reads allnext charactersuntil the given (un-escaped)stopcharacter or the end is reached.readUntil(char stop, boolean acceptEnd, CharScannerSyntax syntax) This method reads allnext charactersuntil the givenstopcharacter or the end of the string to parse is reached.readUntil(CharFilter filter, boolean acceptEnd) This method reads allnext charactersuntil the first characteracceptedby the givenfilteror the end is reached.readUntil(CharFilter filter, boolean acceptEnd, CharScannerSyntax syntax) This method reads allnext charactersuntil the givenCharFilteracceptsthe current character as stop character or the end of data is reached.default StringreadUntil(CharFilter filter, boolean acceptEnd, String stop) This method reads allnext charactersuntil the first characteracceptedby the givenfilter, the givenstopStringor the end is reached.default StringreadUntil(CharFilter filter, boolean acceptEnd, String stop, boolean ignoreCase) This method reads allnext charactersuntil the first characteracceptedby the givenfilter, the givenstopStringor the end is reached.readUntil(CharFilter filter, boolean acceptEnd, String stop, boolean ignoreCase, boolean trim) This method reads allnext charactersuntil the first characteracceptedby the givenfilter, the givenstopStringor the end is reached.default StringreadUntil(CharFilter stopFilter, int min, int max) default StringreadWhile(CharFilter filter) readWhile(CharFilter filter, int min, int max) default intrequire(CharFilter filter, int min) default intrequire(CharFilter filter, int min, int max) voidThis method verifies that theexpectedstring gets consumed from this scanner with respect toignoreCase.default voidrequireOne(char expected) This method verifies that thenext characteris equal to the givenexpectedcharacter.default intrequireOne(CharFilter filter) default intrequireOneOrMore(CharFilter filter) intskip(int count) This method skips the number ofnext charactersgiven bycount.intdefault booleanThis method reads allnext charactersuntil the givensubstringhas been detected.default booleanThis method reads allnext charactersuntil the givensubstringhas been detected.booleanskipOver(String substring, boolean ignoreCase, CharFilter stopFilter) This method consumes allnext charactersuntil the givensubstringhas been detected, a character wasacceptedby the givenCharFilteror 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.booleanskipUntil(char stop) This method skips allnext charactersuntil the givenstopcharacter or the end is reached.booleanskipUntil(char stop, char escape) This method reads allnext charactersuntil the givenstopcharacter or the end of the string to parse is reached.intskipWhile(char c) This method reads allnext charactersthat are identical to the character given byc.default intskipWhile(CharFilter filter) intskipWhile(CharFilter filter, int max) default charskipWhileAndPeek(CharFilter filter) Behaves like the following code:default charskipWhileAndPeek(CharFilter filter, int max) Behaves like the following code:Methods inherited from interface io.github.mmm.base.text.TextFormatProcessor
addError, addInfo, addMessage, addMessage, addWarning, getMessagesMethods 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:
trueif there is at least one character available,falseif 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 shouldcheckif 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 withoutconsumingcharacters 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 givenlookaheadOffsetshall not exceed the available lookahead size (buffer capacity given at construction time). Otherwise the method may fail. -
peekString
This method peeks the number ofnext charactersgiven bycountand returns them asString. If there are less charactersavailablethe returnedStringwill be shorter thancountand only contain the available characters. Unlikeread(int)this method does notconsumethe 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 givencountshall 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_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.
-
peekWhile
- Parameters:
filter- theCharFilteracceptingonly the characters to peek.maxLen- the maximum number of characters to peek (to get as lookahead without modifying this stream).- Returns:
- a
Stringwith thepeekedcharacters of the givenmaxLenor less if a character was hit that is notacceptedby the givenfilteror the end-of-text has been reached before. The state of this stream remains unchanged. - See Also:
-
peekUntil
- Parameters:
stopFilter- theCharFilterthat decides which characters toacceptas stop characters.maxLen- the maximum number of characters to peek (get as lookahead without modifying this stream).- Returns:
- a
Stringwith thepeekedcharacters of the givenmaxLenor 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 charactersgiven bycountand returns them as string. If there are less charactersavailablethe returned string will be shorter thancountand only contain the available characters.- Parameters:
count- is the number of characters to read. You may useInteger.MAX_VALUEto 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 isavailableat all.
-
read
This method reads the number ofnext charactersgiven bycountandappendsthem to the givenStringBuilder. If there are less charactersavailablethen 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_VALUEto read until the end of data if the data-size is suitable.builder- theStringBuilderwhere toappendthe 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 charactersuntil the givenstopcharacter or the end is reached.
After the call of this method, the current index will point to the next character after the (first)stopcharacter or to the end if NO such character exists.- Parameters:
stop- is the character to read until.acceptEnd- iftruethe end of data will be treated asstop, too.- Returns:
- the string with all read characters excluding the
stopcharacter ornullif there was nostopcharacter andacceptEndisfalse.
-
readUntil
This method reads allnext charactersuntil the given (un-escaped)stopcharacter or the end is reached.
In advance toreadUntil(char, boolean), this method allows that thestopcharacter may be used in the input-string by adding the givenescapecharacter. After the call of this method, the current index will point to the next character after the (first)stopcharacter or to the end if NO such character exists.
This method is especially useful when quoted strings should be parsed. E.g.:CharStreamScannerscanner = 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- iftruethe end of data will be treated asstop, too.escape- is the character used to escape thestopcharacter. To add an occurrence of theescapecharacter it has to be duplicated (occur twice). Theescapecharacter may also be equal to thestopcharacter. If other regular characters are escaped theescapecharacter is simply ignored.- Returns:
- the string with all read characters excluding the
stopcharacter ornullif there was nostopcharacter andacceptEndisfalse.
-
readUntil
This method reads allnext charactersuntil the givenstopcharacter or the end of the string to parse is reached. In advance toreadUntil(char, boolean), this method will scan the input using the givensyntaxwhich e.g. allows toescapethe stop character.
After the call of this method, the current index will point to the next character after the (first)stopcharacter or to the end of the string if NO such character exists.- Parameters:
stop- is the character to read until.acceptEnd- iftruethe 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
stopcharacter ornullif there was nostopcharacter. - See Also:
-
readUntil
This 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.- Parameters:
filter- is used todecidewhere to stop.acceptEnd- iftrueif end of data should be treated like thestopcharacter and the rest of the text will be returned,falseotherwise (to returnnullif the end of data was reached and the scanner has been consumed).- Returns:
- the string with all read characters not
acceptedby the givenCharFilterornullif there was noacceptedcharacter andacceptEndisfalse.
-
readUntil
This method reads allnext charactersuntil the first characteracceptedby the givenfilter, the givenstopStringor the end is reached.
After the call of this method, the current index will point to the firstacceptedstop character, or to the first character of the givenstopStringor to the end if NO such character exists.- Parameters:
filter- is used todecidewhere to stop.acceptEnd- iftrueif the end of data should be treated like thestopcharacter and the rest of the text will be returned,falseotherwise (to returnnullif end of data was reached and the scanner has been consumed).stop- theStringwhere to stop consuming data. Should be at least two characters long (otherwise accept byCharFilterinstead).- Returns:
- the string with all read characters not
acceptedby the givenCharFilteror until the givenstopStringwas detected. If end of data was reached without a stop signal the entire rest of the data is returned ornullifacceptEndisfalse.
-
readUntil
This method reads allnext charactersuntil the first characteracceptedby the givenfilter, the givenstopStringor the end is reached.
After the call of this method, the current index will point to the firstacceptedstop character, or to the first character of the givenstopStringor to the end if NO such character exists.- Parameters:
filter- is used todecidewhere to stop.acceptEnd- iftrueif the end of data should be treated like thestopcharacter and the rest of the text will be returned,falseotherwise (to returnnullif the end of data was reached and the scanner has been consumed).stop- theStringwhere to stop consuming data. Should be at least two characters long (otherwise accept byCharFilterinstead).ignoreCase- - iftruethe case of the characters is ignored when compared with characters fromstopString.- Returns:
- the string with all read characters not
acceptedby the givenCharFilteror until the givenstopStringwas detected. If the end of data was reached without a stop signal the entire rest of the data is returned ornullifacceptEndisfalse.
-
readUntil
String readUntil(CharFilter filter, boolean acceptEnd, String stop, boolean ignoreCase, boolean trim) This method reads allnext charactersuntil the first characteracceptedby the givenfilter, the givenstopStringor the end is reached.
After the call of this method, the current index will point to the firstacceptedstop character, or to the first character of the givenstopStringor to the end if NO such character exists.- Parameters:
filter- is used todecidewhere to stop.acceptEnd- iftrueif the end of data should be treated like thestopcharacter and the rest of the text will be returned,falseotherwise (to returnnullif the end of data was reached and the scanner has been consumed).stop- theStringwhere to stop consuming data. Should be at least two characters long (otherwise accept byCharFilterinstead).ignoreCase- - iftruethe case of the characters is ignored when compared with characters fromstopString.trim- -trueif the result should betrimmed,falseotherwise.- Returns:
- the string with all read characters not
acceptedby the givenCharFilteror until the givenstopStringwas detected. If the end of data was reached without hittingstopthe entire rest of the data is returned ornullifacceptEndisfalse. Thre result will betrimmediftrimistrue.
-
readUntil
This method reads allnext charactersuntil the givenCharFilteracceptsthe 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 givensyntaxwhich e.g. allows toescapethe stop character.
After the call of this method, the current index will point to the firstacceptedstop character or to the end of the string if NO such character exists.- Parameters:
filter- is used todecidewhere to stop.acceptEnd- iftruethe 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
stopcharacter ornullif there was nostopcharacter. - See Also:
-
readUntil
- Parameters:
stopFilter- theCharFilterthat decides which characters toacceptas stop characters.min- the minimum number of characters expected.max- the (maximum) length of the characters to consume.- Returns:
- the
Stringwith all consumed characters excluding the stop character. If nostopcharacter was found untilmaxLengthcharacters 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 charactersthat areacceptedby the givenfilter.
After the call of this method, the current index will point to the next character that was NOTacceptedby the givenfilteror to the end if NO such character exists. -
readWhile
This 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.- Parameters:
filter- used todecidewhich 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
acceptedby the givenfilterlimited to the length ofmaxand theendof 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 aBooleanvalue from this scanner if available. -
readBoolean
Reads aBooleanvalue from this scanner if available. -
readBoolean
Reads aBooleanvalue from this scanner if available.- Parameters:
ignoreCase- - iftruethe case of the characters is ignored when compared,falseotherwise (only lower case is accepted).acceptYesNo- - iftruealso "yes" is accepted fortrueand "no" forfalse,falseotherwise.- Returns:
- the consumed
Booleanvalue ornullif no such value was available and thepositionremains 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 itsparsedvalue.- Returns:
- the parsed
doublenumber ornullif 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 itsparsedvalue.- Parameters:
radixMode- theCharScannerRadixHandler- e.g.CharScannerRadixMode.ALL.- Returns:
- the parsed
doublenumber ornullif 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 aFloatvalue from the current positionconsumingas many matching characters as available.- Returns:
- the parsed
Floatvalue ornullif the current current position does not point to aFloatnumber. - Throws:
NumberFormatException- if the number at the current position could not be parsed.
-
readFloat
This method reads aFloatvalue from the current positionconsumingas many matching characters as available.- Parameters:
radixMode- theCharScannerRadixHandler- e.g.CharScannerRadixMode.ALL.- Returns:
- the parsed
Floatvalue ornullif the current current position does not point to aFloatnumber. - Throws:
NumberFormatException- if the number at the current position could not be parsed.
-
readLong
- Returns:
- the consumed
Longvalue ornullif no number was present and thepositionremains unchanged. - Throws:
NumberFormatException- if the current current position points to a number that is not aLongvalue.
-
readLong
- Parameters:
radixMode- theCharScannerRadixHandler- e.g.CharScannerRadixMode.ALL.- Returns:
- the consumed
Longvalue ornullif no such value was present and thepositionremains unchanged. - Throws:
NumberFormatException- if the current current position points to a number that is not aLongvalue.
-
readInteger
- Returns:
- the consumed
Integervalue ornullif no such value was present and thepositionremains unchanged. - Throws:
NumberFormatException- if the current current position does not point to aIntegervalue.
-
readInteger
- Parameters:
radixMode- theCharScannerRadixHandler- e.g.CharScannerRadixMode.ALL.- Returns:
- the consumed
Integervalue ornullif no such value was present and thepositionremains unchanged. - Throws:
NumberFormatException- if the current current position does not point to aLongvalue.
-
readJavaNumberLiteral
Number readJavaNumberLiteral()Reads a JavaNumberliteral (e.g. "1L" or "1.3F").- Returns:
- the consumed
Numberornullif no number literal was found and thepositionremains unchainged. - Throws:
NumberFormatException- if a number literal was found that has an illegal format.
-
readDigit
default int readDigit()This method reads thenext characterif it is a digit. Else the state remains unchanged.- Returns:
- the numeric value of the next Latin digit (e.g.
0if'0') or-1if thenext characteris no Latin digit.
-
readDigit
int readDigit(int radix) This method reads thenext characterif 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.10to read any Latin digit (seereadDigit()),8to read octal digit,16to read hex decimal digits.- Returns:
- the numeric value of the next digit within the given
radixor-1if thenext characteris 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 givenmaxDigitsand returns itsparsedvalue.
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 than19as 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 JavaStringliteral value according to JLS 3.10.6.
As a complex example for the input "Hi \"\176\477\579•∑\"\n" this scanner would return theStringoutputHi "~'7/9•∑"followed by a newline character.- Parameters:
severity- theTextFormatMessageTypeto use to report invalid escape sequences or missing terminating quotation.- Returns:
- the parsed Java
Stringliteral value ornullif not pointing to aStringliteral.
-
readJavaCharLiteral
Reads and parses a JavaCharacterliteral 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 JavaCharacterliteral 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- theTextFormatMessageTypeto use to report invalid escape sequences or missing terminating quotation.- Returns:
- the parsed Java
Characterliteral value ornullif not pointing to aCharacterliteral.
-
expect
This method determines if the givenexpectedStringis completely present at the current position. It will onlyconsumecharacters and change the state if theexpectedStringwas found (entirely).
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.- Returns:
trueif theexpectedstring was successfully consumed from this scanner,falseotherwise.- See Also:
-
expect
This method determines if the givenexpectedStringis completely present at the current position. It will onlyconsumecharacters and change the state if theexpectedStringwas found (entirely).
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. -
expect
This method determines if the givenexpectedStringis completely present at the current position. It will onlyconsumecharacters and change the state iflookaheadisfalseand theexpectedStringwas found (entirely).
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- the expectedStringto search for.ignoreCase- - iftruethe case of the characters is ignored when compared,falseotherwise.lookahead- - iftruethe state of the scanner remains unchanged even if the expectedStringhas been found,falseotherwise (expectedStringis consumed on match).- Returns:
trueif theexpectedstring was successfully found,falseotherwise.
-
expect
This method determines if the givenexpectedStringis completely present at the current position. It will onlyconsumecharacters and change the state iflookaheadisfalseand theexpectedStringwas found (entirely).
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- the expectedStringto search for.ignoreCase- - iftruethe case of the characters is ignored when compared,falseotherwise.lookahead- - iftruethe state of the scanner remains unchanged even if the expectedStringhas been found,falseotherwise (expectedStringis consumed on match).offset- the number of characters that have already beenpeekedand after which the givenStringis expected. Will typically be0. Iflookaheadisfalseand the expectedStringwas found these characters will beskippedtogether with the expectedString.- Returns:
trueif theexpectedstring was successfully found,falseotherwise.
-
expect
default boolean expect(String expected, boolean ignoreCase, boolean lookahead, int offset, boolean warning) This method determines if the givenexpectedStringis completely present at the current position. It will onlyconsumecharacters and change the state iflookaheadisfalseand theexpectedStringwas found (entirely).
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- the expectedStringto search for.ignoreCase- - iftruethe case of the characters is ignored when compared,falseotherwise.lookahead- - iftruethe state of the scanner remains unchanged even if the expectedStringhas been found,falseotherwise (expectedStringis consumed on match).offset- the number of characters that have already beenpeekedand after which the givenStringis expected. Will typically be0. Iflookaheadisfalseand the expectedStringwas found these characters will beskippedtogether with the expectedString.warning-truetoadd a warningin case the expectedStringwas not found,falseotherwise.- Returns:
trueif theexpectedstring was successfully found,falseotherwise.
-
expectOne
default boolean expectOne(char expected) This method checks if thenext characteris equal to the givenexpectedcharacter.
If the character matched with theexpectedcharacter, the parser points to the next character. Otherwise its position will remain unchanged.- Parameters:
expected- is the expected character.- Returns:
trueif the current character is the same asexpected,falseotherwise.
-
expectOne
boolean expectOne(char expected, boolean warning) This method checks if thenext characteris equal to the givenexpectedcharacter.
If the character matched with theexpectedcharacter, the parser points to the next character. Otherwise its position will remain unchanged.- Parameters:
expected- the character to expect asnextin this stream.warning-truetoadd a warningin case the expected character was not present,falseotherwise.- Returns:
trueif the expected character was found and consumer,falseotherwise (and this stream remains unchanged).
-
expectOne
This method checks that thenext characterisacceptedby 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 theCharFilteracceptingthe expected chars.- Returns:
trueif the current character isaccepted,falseotherwise.
-
expectUnsafe
This method skips allnext charactersas long as they equal to the according character of theexpectedString.
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:
ATTENTION: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 iffalsewas returned (e.g. otherwise when doingexpectUnsafe("false") and else doingexpectUnsafe("true") to parse abooleanliteral your code could accept "falstrue" as "true").- Parameters:
expected- is the expected string.- Returns:
trueif theexpectedstring was successfully consumed from this scanner,falseotherwise.- See Also:
-
expectUnsafe
This method skips allnext charactersas long as they equal to the according character of theexpectedstring.
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:
ATTENTION: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- - iftruethe case of the characters is ignored when compared.- Returns:
trueif theexpectedstring was successfully consumed from this scanner,falseotherwise.- See Also:
-
requireOne
This method verifies that thenext characteris equal to the givenexpectedcharacter.
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 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.
expectUnsafe(expected, ignoreCase)) { throw newIllegalStateException(...); }- Parameters:
expected- is the expected string.ignoreCase- - iftruethe case of the characters is ignored during comparison.
-
requireOne
- Parameters:
filter- theCharFilteracceptingthe expected characters toskip.- Returns:
- the actual number of characters that have been skipped.
- Throws:
IllegalStateException- if less than1or more than1000acceptedcharacters have been consumed.
-
requireOneOrMore
- Parameters:
filter- theCharFilteracceptingthe expected characters toskip.- Returns:
- the actual number of characters that have been skipped.
- Throws:
IllegalStateException- if less than1or more than1000acceptedcharacters have been consumed.
-
require
- Parameters:
filter- theCharFilteracceptingthe 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 thanminor more than1000acceptedcharacters have been consumed.
-
require
- Parameters:
filter- theCharFilteracceptingthe 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 thanminor more thanmaxacceptedcharacters have been consumed.
-
skipUntil
boolean skipUntil(char stop) This method skips allnext charactersuntil the givenstopcharacter or the end is reached. If thestopcharacter was reached, this scanner will point to the next character afterstopwhen this method returns.- Parameters:
stop- is the character to read until.- Returns:
trueif the first occurrence of the givenstopcharacter has been passed,falseif there is no such character.
-
skipUntil
boolean skipUntil(char stop, char escape) This method reads allnext charactersuntil the givenstopcharacter or the end of the string to parse is reached. In advance toskipUntil(char), this method will read over thestopcharacter if it is escaped with the givenescapecharacter.- Parameters:
stop- is the character to read until.escape- is the character used to escape the stop character (e.g. '\').- Returns:
trueif the first occurrence of the givenstopcharacter has been passed,falseif there is no such character.
-
skip
int skip(int count) This method skips the number ofnext charactersgiven bycount.- Parameters:
count- is the number of characters to skip. You may useInteger.MAX_VALUEto 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:
0if thenext characeteris not a newline and the stream remains unchanged,1if thenext characeterwas '\n' and has beenskipped, or2if thenext characetershave been '\r' and '\n' and have beenskipped.
-
skipOver
This method reads allnext charactersuntil the givensubstringhas been detected.
After the call of this method, the current index will point to the next character after the first occurrence ofsubstringor to the end of data if the givensubstringwas NOT found.- Parameters:
substring- is the substring to search and skip over starting at the current index.- Returns:
trueif the givensubstringoccurred and has been passed andfalseif the end of the string has been reached without any occurrence of the givensubstring.
-
skipOver
This method reads allnext charactersuntil the givensubstringhas been detected.
After the call of this method, the current index will point to the next character after the first occurrence ofsubstringor to the end of data if the givensubstringwas NOT found.- Parameters:
substring- is the substring to search and skip over starting at the current index.ignoreCase- - iftruethe case of the characters is ignored when compared with characters fromsubstring.- Returns:
trueif the givensubstringoccurred and has been passed andfalseif the end of the string has been reached without any occurrence of the givensubstring.
-
skipOver
This method consumes allnext charactersuntil the givensubstringhas been detected, a character wasacceptedby the givenCharFilteror 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- - iftruethe case of the characters is ignored when compared with characters fromsubstring.stopFilter- is the filter used todetectstop characters. If such character was detected, the skip is stopped and the parser points to the character after the stop character. Thesubstringshould NOT contain astop character.- Returns:
trueif the givensubstringoccurred and has been passed andfalseif a stop character has been detected or the end of the string has been reached without any occurrence of the givensubstringor stop character.
-
skipWhile
int skipWhile(char c) This method reads allnext charactersthat 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 charactercor 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 charactersthat areacceptedby the givenfilter.
After the call of this method, the current index will point to the next character that was NOTacceptedby the givenfilteror to the end if NO such character exists. -
skipWhile
This 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.- Parameters:
filter- is used todecidewhich 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 todecidewhich characters should be accepted.- Returns:
- the first character that was not
acceptedby the givenCharFilter. Only theacceptedcharacters have been consumed, this scanner still points to the returned character.
-
skipWhileAndPeek
- Parameters:
filter- is used todecidewhich characters should be accepted.max- is the maximum number of characters that may be skipped.- Returns:
- the first character that was not
acceptedby the givenCharFilter. Only theacceptedcharacters have been consumed, this scanner still points to the returned character.
-
getBufferParsed
String getBufferParsed()- Returns:
- the
Stringwith 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
Stringwith 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:
closein interfaceAutoCloseable
-