StringParser (open)
String parser class, has some useful functions for string parsing
eof and current can both be calculated from data and position, but they are used very often, so it would be inefficient to write wrapper functions for them.
The position is leading, but eof MAY be used for shortcuts (but MAY be ignored)
Syntax
LOADLIB "wh::util/stringparser.whlib";
OBJECTTYPE StringParser
Constructor
- MACRO NEW(STRING data)
Initializes a string parser
Properties
- PROPERTY current
Current character, or empty string at eof.
- PROPERTY eof
Whether position is at end of data
- PROPERTY position
Integer with current position within data
- PROPERTY remaining_data
Returns current character and the rest of the unparsed data
- PROPERTY set_all
A string with all byte characters (0-255)
- PROPERTY set_alpha
A string with ASCII letters (a-z, A-Z)
- PROPERTY set_ascii
A string with all ASCII characters (0-127)
- PROPERTY set_control
A string with all control characters (0-31, 127)
- PROPERTY set_digit
A string with all ASCII digits (0-9)
Functions
- INTEGER FUNCTION GetDistanceFromState(RECORD state)
Returns the difference in characters from a saved state to the current position.
- STRING FUNCTION GetLastParsed(INTEGER n)
Get the last N parsed characters, without moving the current position
- STRING FUNCTION GetRemainingData()
Returns all reamining (unparsed) data (inclusing current character)
- BOOLEAN FUNCTION Next()
Set position to the next character, returns whether eof has been reached
- STRING FUNCTION ParseN(INTEGER n)
Parses a number characters
- STRING FUNCTION ParseUntilCharacter(STRING c)
Parses until a specific character, or the end of the data. Returns the data from the current position up to (but not including) the end character
- STRING FUNCTION ParseWhileInSet(STRING myset)
Parses all subsequent characters that are in the set
- STRING FUNCTION ParseWhileNotInSet(STRING myset)
Parses all subsequent characters that are not in the set
- STRING FUNCTION PeekN(INTEGER n, INTEGER pluspos)
Returns the next N characters, without moving the current position
- STRING FUNCTION RemoveFromSet(STRING myset, STRING removethese)
Removes a number of characters from a character set
- MACRO Reset(STRING data)
Resets the parser for a new string
- MACRO RestoreState(RECORD state)
Restores the state of the stringparser
- RECORD FUNCTION SaveState()
Saves the state of the stringparser
- BOOLEAN FUNCTION SkipN(INTEGER n)
Skips N characters
- BOOLEAN FUNCTION TryParse(STRING c)
Tries to parse a string, returns whether successful.
- BOOLEAN FUNCTION TryParseCase(STRING c)
Tries to parse a string (case insensitive), returns whether successful.