Fix SNBT Parsing and writing of single quoted strings, and strings with quotes or single quotes within.

This commit is contained in:
zontreck 2025-01-22 01:48:17 -07:00
parent bdb087fabc
commit 18e98ca918
9 changed files with 93 additions and 14 deletions

View file

@ -503,18 +503,31 @@ class StringReader {
return result.toString();
}
// Read a string enclosed in double quotes
/// Read a string enclosed in double quotes
String readQuotedString() {
_quotedString = true;
if (next() != '"') {
throw Exception('Expected double quotes at the start of a string');
var nxtChar = next();
if (nxtChar != '"' && nxtChar != "'") {
throw Exception(
'Expected double quotes or single quotes at the start of a string');
}
StringBuffer result = StringBuffer();
bool escaping = false;
String quoteDigit = nxtChar;
while (canRead) {
String char = next();
if (char == '"') {
if (char == '"' && quoteDigit == "\"") {
break;
} else if (char == '\\' && peek() == '\'' && quoteDigit == '\'') {
escaping = true;
continue;
} else if (char == '\'' && quoteDigit == '\'') {
if (!escaping) break;
}
escaping = false;
result.write(char);
}
_quotedString = false;
@ -552,13 +565,15 @@ class StringReader {
}
String readString() {
if (peek() == "\"") {
if (peek() == "\"" || peek() == "'") {
return readQuotedString();
} else
return readUnquotedString();
}
// Read a specific character and throw an exception if it's not found
/// Read a specific character and throw an exception if it's not found
///
/// Parameter is case-insensitive
void expect(String expectedChar) {
if (next().toLowerCase() != expectedChar.toLowerCase()) {
throw Exception('Expected $expectedChar');
@ -573,4 +588,10 @@ class StringReader {
_position = _lastPostion;
_lastPostion = 0;
}
@override
String toString() {
// Returns the entire value starting from position
return _buffer.substring(_position);
}
}