Class PositionedString


  • public class PositionedString
    extends java.lang.Object
    A string which has a current position. Useful for writing simple single-pass parsers.
    Since:
    5.1.15
    Author:
    bratseth
    • Constructor Summary

      Constructors 
      Constructor Description
      PositionedString​(java.lang.String string)
      Creates this from a given string.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String at()
      Returns a textual description of the current position, useful for appending to error messages.
      java.lang.String at​(int position)
      Returns a textual description of a given position, useful for appending to error messages.
      void consume​(char c)
      Consumes the character at this position.
      boolean consumeOptional​(char c)
      Advances the position by 1 if the character at the current position is c.
      void consumeSpaces()
      Consumes zero or more whitespace characters starting at the current position
      java.lang.String consumeTo​(char c)
      Sets the position of this to the next occurrence of c after the current position.
      java.lang.String consumeToPosition​(int position)
      Returns the substring between the current position and position and advances the current position to position
      int indexOf​(char c)
      Returns the position of the next occurrence of c, or -1 if there are no occurrences of c in the string after the current position.
      boolean peek​(char c)
      Returns whether the character at the current position is c.
      int position()
      The current position into this string
      void setPosition​(int position)
      Assigns the current position in the string
      void skip​(int n)
      Adds n to the current position
      java.lang.String string()
      The complete string value of this
      java.lang.String substring()
      Returns the substring of this string from the current position to the end
      java.lang.String substring​(int end)
      Returns a substring of this from the current position to the end argument
      java.lang.String toString()
      Returns the string
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • PositionedString

        public PositionedString​(java.lang.String string)
        Creates this from a given string.
    • Method Detail

      • string

        public java.lang.String string()
        The complete string value of this
      • position

        public int position()
        The current position into this string
      • setPosition

        public void setPosition​(int position)
        Assigns the current position in the string
      • consume

        public void consume​(char c)
        Consumes the character at this position.
        Precondition: The character at this position is c.
        Postcondition: The position is increased by 1
        Parameters:
        c - the expected character at this
        Throws:
        java.lang.IllegalArgumentException - if the character at this position is not c
      • consumeSpaces

        public void consumeSpaces()
        Consumes zero or more whitespace characters starting at the current position
      • consumeOptional

        public boolean consumeOptional​(char c)
        Advances the position by 1 if the character at the current position is c. Does nothing otherwise.
        Returns:
        whether this consumed a c at the current position, or if it did nothing
      • peek

        public boolean peek​(char c)
        Returns whether the character at the current position is c.
      • indexOf

        public int indexOf​(char c)
        Returns the position of the next occurrence of c, or -1 if there are no occurrences of c in the string after the current position.
      • skip

        public void skip​(int n)
        Adds n to the current position
      • consumeTo

        public java.lang.String consumeTo​(char c)
        Sets the position of this to the next occurrence of c after the current position.
        Parameters:
        c - the char to move the position to
        Returns:
        the substring between the current position and the new position at c
        Throws:
        java.lang.IllegalArgumentException - if there was no occurrence of c after the current position
      • consumeToPosition

        public java.lang.String consumeToPosition​(int position)
        Returns the substring between the current position and position and advances the current position to position
      • substring

        public java.lang.String substring​(int end)
        Returns a substring of this from the current position to the end argument
      • substring

        public java.lang.String substring()
        Returns the substring of this string from the current position to the end
      • at

        public java.lang.String at()
        Returns a textual description of the current position, useful for appending to error messages.
      • at

        public java.lang.String at​(int position)
        Returns a textual description of a given position, useful for appending to error messages.
      • toString

        public java.lang.String toString()
        Returns the string
        Overrides:
        toString in class java.lang.Object