Class StringReader
java.lang.Object
dev.polv.taleapi.command.argument.StringReader
A simple string reader for parsing command arguments.
Provides methods for reading different types of values from a string, tracking the current position, and handling whitespace.
-
Constructor Summary
ConstructorsConstructorDescriptionStringReader(String string) Creates a new StringReader for the given input.StringReader(String string, int cursor) Creates a new StringReader starting at a specific position. -
Method Summary
Modifier and TypeMethodDescriptionbooleancanRead()Checks if there are more characters to read.booleancanRead(int length) Checks if the specified number of characters can be read.voidexpect(char c) Expects and consumes a specific character.intReturns the current cursor position.getRead()Returns the portion of the string that has been read.Returns the remaining unread portion of the string.intReturns the number of characters remaining.Returns the full input string.intReturns the total length of the input string.static booleanisAllowedInUnquotedString(char c) Checks if the given character terminates an unquoted string.charpeek()Returns the character at the current position without advancing.charpeek(int offset) Returns the character at the specified offset from current position.charread()Reads and returns the character at the current position, advancing the cursor.booleanReads a boolean from the current position.doubleReads a double from the current position.floatReads a float from the current position.intreadInt()Reads an integer from the current position.longreadLong()Reads a long from the current position.Reads a quoted string (handles escape sequences).Reads all remaining characters as a string.Reads a string, handling both quoted and unquoted forms.Reads an unquoted string (stops at whitespace).voidsetCursor(int cursor) Sets the cursor position.voidskip()Skips the current character.voidSkips all whitespace characters at the current position.toString()
-
Constructor Details
-
StringReader
Creates a new StringReader for the given input.- Parameters:
string- the input string to read
-
StringReader
Creates a new StringReader starting at a specific position.- Parameters:
string- the input stringcursor- the starting position
-
-
Method Details
-
getString
Returns the full input string.- Returns:
- the input string
-
getCursor
public int getCursor()Returns the current cursor position.- Returns:
- the cursor position
-
setCursor
public void setCursor(int cursor) Sets the cursor position.- Parameters:
cursor- the new position
-
getRemainingLength
public int getRemainingLength()Returns the number of characters remaining.- Returns:
- remaining character count
-
getTotalLength
public int getTotalLength()Returns the total length of the input string.- Returns:
- the string length
-
getRead
Returns the portion of the string that has been read.- Returns:
- the read portion
-
getRemaining
Returns the remaining unread portion of the string.- Returns:
- the remaining portion
-
canRead
public boolean canRead()Checks if there are more characters to read.- Returns:
trueif more characters are available
-
canRead
public boolean canRead(int length) Checks if the specified number of characters can be read.- Parameters:
length- the number of characters- Returns:
trueif that many characters are available
-
peek
public char peek()Returns the character at the current position without advancing.- Returns:
- the current character
-
peek
public char peek(int offset) Returns the character at the specified offset from current position.- Parameters:
offset- the offset from cursor- Returns:
- the character at that position
-
read
public char read()Reads and returns the character at the current position, advancing the cursor.- Returns:
- the read character
-
skip
public void skip()Skips the current character. -
skipWhitespace
public void skipWhitespace()Skips all whitespace characters at the current position. -
readInt
Reads an integer from the current position.- Returns:
- the parsed integer
- Throws:
CommandException- if no valid integer is found
-
readLong
Reads a long from the current position.- Returns:
- the parsed long
- Throws:
CommandException- if no valid long is found
-
readDouble
Reads a double from the current position.- Returns:
- the parsed double
- Throws:
CommandException- if no valid double is found
-
readFloat
Reads a float from the current position.- Returns:
- the parsed float
- Throws:
CommandException- if no valid float is found
-
readBoolean
Reads a boolean from the current position.- Returns:
- the parsed boolean
- Throws:
CommandException- if no valid boolean is found
-
readUnquotedString
Reads an unquoted string (stops at whitespace).- Returns:
- the read string
-
readQuotedString
Reads a quoted string (handles escape sequences).- Returns:
- the read string (without quotes)
- Throws:
CommandException- if the string is malformed
-
readString
Reads a string, handling both quoted and unquoted forms.- Returns:
- the read string
- Throws:
CommandException- if a quoted string is malformed
-
readRemaining
Reads all remaining characters as a string.- Returns:
- the remaining string
-
isAllowedInUnquotedString
public static boolean isAllowedInUnquotedString(char c) Checks if the given character terminates an unquoted string.- Parameters:
c- the character to check- Returns:
trueif the character is allowed in an unquoted string
-
expect
Expects and consumes a specific character.- Parameters:
c- the expected character- Throws:
CommandException- if the character is not found
-
toString
-