Class PositionedString

java.lang.Object
com.yahoo.text.PositionedString

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

    Constructors
    Constructor
    Description
    Creates this from a given string.
  • Method Summary

    Modifier and Type
    Method
    Description
    at()
    Returns a textual description of the current position, useful for appending to error messages.
    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
    Advances the position by 1 if the character at the current position is c.
    void
    Consumes zero or more whitespace characters starting at the current position
    consumeTo(char c)
    Sets the position of this to the next occurrence of c after the current position.
    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
    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
    The complete string value of this
    Returns the substring of this string from the current position to the end
    substring(int end)
    Returns a substring of this from the current position to the end argument
    Returns the string

    Methods inherited from class java.lang.Object

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

    • PositionedString

      public PositionedString(String string)
      Creates this from a given string.
  • Method Details

    • string

      public 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:
      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 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:
      IllegalArgumentException - if there was no occurrence of c after the current position
    • consumeToPosition

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

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

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

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

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

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