Package com.yahoo.text
Class PositionedString
java.lang.Object
com.yahoo.text.PositionedString
A string which has a current position.
Useful for writing simple single-pass parsers.
- Author:
- bratseth
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionat()
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
consumeOptional
(char c) 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 positionconsumeTo
(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 andposition
and advances the current position toposition
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 stringvoid
setPosition
(int position) Assigns the current position in the stringvoid
skip
(int n) Adds n to the current positionstring()
The complete string value of thisReturns the substring of this string from the current position to the endsubstring
(int end) Returns a substring of this from the current position to the end argumenttoString()
Returns the string
-
Constructor Details
-
PositionedString
Creates this from a given string.
-
-
Method Details
-
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
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
Returns the substring between the current position andposition
and advances the current position toposition
-
substring
Returns a substring of this from the current position to the end argument -
substring
Returns the substring of this string from the current position to the end -
at
Returns a textual description of the current position, useful for appending to error messages. -
at
Returns a textual description of a given position, useful for appending to error messages. -
toString
Returns the string
-