Completely rework SNBT Parser

(NOTE: ChatGPT Was used for regex only)
This commit is contained in:
zontreck 2024-08-29 06:55:12 -07:00
parent 7c87ef444f
commit 84192c69db
11 changed files with 150 additions and 107 deletions

View file

@ -417,8 +417,12 @@ class StringReader {
StringReader(this._buffer);
// Check if there's more to read
bool get canRead => _position < _buffer.length;
/// Check if there's more to read
bool get canRead => _canRead();
bool _canRead() {
skipWhitespace();
return _position < _buffer.length;
}
// Get the number of chars seeked
int get getSeeked => _lastPostion - _position;
@ -433,6 +437,18 @@ class StringReader {
}
}
/// Generates a snapshot of the text location if applicable
String getSnapshot() {
if (canRead) {
if (_position + 64 < _buffer.length) {
return _buffer.substring(_position, _position + 64);
} else {
return _buffer.substring(_position);
}
} else
return "";
}
// Peek the next character without advancing the position
String peek() {
skipWhitespace();
@ -446,7 +462,7 @@ class StringReader {
// Skip any whitespace characters
void skipWhitespace() {
if (_quotedString) return; // We need whitespace for strings
while (canRead && isWhitespace(_buffer[_position])) {
while ((_position < _buffer.length) && isWhitespace(_buffer[_position])) {
_position++;
}
}
@ -457,10 +473,12 @@ class StringReader {
}
// Read until a specific character is found
String readUntil(String stopChar) {
String readUntil(String stopChar, int maxChars) {
StringBuffer result = StringBuffer();
while (canRead && peek() != stopChar) {
int index = 0;
while (canRead && peek() != stopChar && index < maxChars) {
result.write(next());
index++;
}
return result.toString();
}