Class Cursor
-
- All Implemented Interfaces:
public final class CursorClass representing an iteration position over a given string, with convenience functions. This is intended for use with the token parsing system, but you can use it for other things too.
-
-
Method Summary
Modifier and Type Method Description final IntegergetIndex()Current iteration index, starting at -1.final UnitsetIndex(Integer index)Current iteration index, starting at -1.final BooleangetHasNext()final BooleangetHasPrevious()final StringgetInput()final StringconsumeNumber(Integer amount)Consume amount characters from this cursor, returning them as a String. final StringconsumeRemaining()Iterate over the rest of the string, returning the result. final StringconsumeWhile(Function1<Character, Boolean> predicate)Iterate over the rest of the string as long as the predicate returns true, returning the result.final BooleanskipWhitespace()Skip any immediate whitespace, updating the index. final Characternext()Increment the index and return the character found there, throwing if we're at the end of the string. final CharacternextOrNull()Increment the index and return the character found there, or nullif we're at the end of the string.final Characterprevious()Decrement the index and return the character found there, throwing if we're at the start of the string. final CharacterpreviousOrNull()Decrement the index and return the character found there, or nullif we're at the start of the string.final Characterpeek()Return the character at the current index. final CharacterpeekNext()Return the character at the next index, or nullif we're at the end of the string.final CharacterpeekPrevious()Return the character at the previous index, or nullif we're at the start of the string.-
-
Constructor Detail
-
Cursor
Cursor(String input)
- Parameters:
input- Input string to iterate over.
-
-
Method Detail
-
getHasNext
final Boolean getHasNext()
-
getHasPrevious
final Boolean getHasPrevious()
-
consumeNumber
final String consumeNumber(Integer amount)
Consume amount characters from this cursor, returning them as a String. Will stop consuming and return when the cursor runs out of characters, instead of throwing.
Returns an empty string if no characters remain.
-
consumeRemaining
final String consumeRemaining()
Iterate over the rest of the string, returning the result.
-
consumeWhile
final String consumeWhile(Function1<Character, Boolean> predicate)
Iterate over the rest of the string as long as the predicate returns
true, returning the result.
-
skipWhitespace
final Boolean skipWhitespace()
Skip any immediate whitespace, updating the index.
-
next
final Character next()
Increment the index and return the character found there, throwing if we're at the end of the string.
-
nextOrNull
final Character nextOrNull()
Increment the index and return the character found there, or
nullif we're at the end of the string.
-
previous
final Character previous()
Decrement the index and return the character found there, throwing if we're at the start of the string.
-
previousOrNull
final Character previousOrNull()
Decrement the index and return the character found there, or
nullif we're at the start of the string.
-
peekNext
final Character peekNext()
Return the character at the next index, or
nullif we're at the end of the string.
-
peekPrevious
final Character peekPrevious()
Return the character at the previous index, or
nullif we're at the start of the string.
-
-
-
-