Class Cursor

  • All Implemented Interfaces:

    
    public final class Cursor
    
                        

    Class 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.

    • Constructor Detail

      • Cursor

        Cursor(String input)
        Parameters:
        input - Input string to iterate over.
    • Method Detail

      • getIndex

         final Integer getIndex()

        Current iteration index, starting at -1.

      • 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.

      • 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.

      • 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 null if 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 null if we're at the start of the string.

      • peek

         final Character peek()

        Return the character at the current index.

      • peekNext

         final Character peekNext()

        Return the character at the next index, or null if we're at the end of the string.

      • peekPrevious

         final Character peekPrevious()

        Return the character at the previous index, or null if we're at the start of the string.