Package com.yahoo.text
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.- Since:
- 5.1.15
- Author:
- bratseth
-
-
Constructor Summary
Constructors Constructor Description PositionedString(String string)
Creates this from a given string.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
at()
Returns a textual description of the current position, useful for appending to error messages.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 positionString
consumeTo(char c)
Sets the position of this to the next occurrence of c after the current position.String
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
string()
The complete string value of thisString
substring()
Returns the substring of this string from the current position to the endString
substring(int end)
Returns a substring of this from the current position to the end argumentString
toString()
Returns the string
-
-
-
Constructor Detail
-
PositionedString
public PositionedString(String string)
Creates this from a given string.
-
-
Method Detail
-
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 andposition
and advances the current position toposition
-
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.
-
-