Class StringReader

java.lang.Object
dev.polv.taleapi.command.argument.StringReader

public class StringReader extends Object
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

    Constructors
    Constructor
    Description
    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 Type
    Method
    Description
    boolean
    Checks if there are more characters to read.
    boolean
    canRead(int length)
    Checks if the specified number of characters can be read.
    void
    expect(char c)
    Expects and consumes a specific character.
    int
    Returns the current cursor position.
    Returns the portion of the string that has been read.
    Returns the remaining unread portion of the string.
    int
    Returns the number of characters remaining.
    Returns the full input string.
    int
    Returns the total length of the input string.
    static boolean
    Checks if the given character terminates an unquoted string.
    char
    Returns the character at the current position without advancing.
    char
    peek(int offset)
    Returns the character at the specified offset from current position.
    char
    Reads and returns the character at the current position, advancing the cursor.
    boolean
    Reads a boolean from the current position.
    double
    Reads a double from the current position.
    float
    Reads a float from the current position.
    int
    Reads an integer from the current position.
    long
    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).
    void
    setCursor(int cursor)
    Sets the cursor position.
    void
    Skips the current character.
    void
    Skips all whitespace characters at the current position.
     

    Methods inherited from class java.lang.Object

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

    • StringReader

      public StringReader(String string)
      Creates a new StringReader for the given input.
      Parameters:
      string - the input string to read
    • StringReader

      public StringReader(String string, int cursor)
      Creates a new StringReader starting at a specific position.
      Parameters:
      string - the input string
      cursor - the starting position
  • Method Details

    • getString

      public String 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

      public String getRead()
      Returns the portion of the string that has been read.
      Returns:
      the read portion
    • getRemaining

      public String 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:
      true if 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:
      true if 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

      public int readInt() throws CommandException
      Reads an integer from the current position.
      Returns:
      the parsed integer
      Throws:
      CommandException - if no valid integer is found
    • readLong

      public long readLong() throws CommandException
      Reads a long from the current position.
      Returns:
      the parsed long
      Throws:
      CommandException - if no valid long is found
    • readDouble

      public double readDouble() throws CommandException
      Reads a double from the current position.
      Returns:
      the parsed double
      Throws:
      CommandException - if no valid double is found
    • readFloat

      public float readFloat() throws CommandException
      Reads a float from the current position.
      Returns:
      the parsed float
      Throws:
      CommandException - if no valid float is found
    • readBoolean

      public boolean readBoolean() throws CommandException
      Reads a boolean from the current position.
      Returns:
      the parsed boolean
      Throws:
      CommandException - if no valid boolean is found
    • readUnquotedString

      public String readUnquotedString()
      Reads an unquoted string (stops at whitespace).
      Returns:
      the read string
    • readQuotedString

      public String readQuotedString() throws CommandException
      Reads a quoted string (handles escape sequences).
      Returns:
      the read string (without quotes)
      Throws:
      CommandException - if the string is malformed
    • readString

      public String readString() throws CommandException
      Reads a string, handling both quoted and unquoted forms.
      Returns:
      the read string
      Throws:
      CommandException - if a quoted string is malformed
    • readRemaining

      public String 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:
      true if the character is allowed in an unquoted string
    • expect

      public void expect(char c) throws CommandException
      Expects and consumes a specific character.
      Parameters:
      c - the expected character
      Throws:
      CommandException - if the character is not found
    • toString

      public String toString()
      Overrides:
      toString in class Object