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

@ -66,6 +66,11 @@ class IntArrayTag extends Tag {
while (reader.peek() != "]") {
value.add(int.parse(reader.readNumber()));
// The SNBT standard does not require a integer to be suffixed by a 'I'.
// This implementation honors that by making it optional.
// FIX 1/21/25 @Aria: Int Array was lacking the skipping of the I digit when it might possibly be present
if (reader.peek().toLowerCase() == "i") reader.expect("I");
if (reader.peek() == ",") reader.next();
}
reader.expect("]");

View file

@ -46,10 +46,17 @@ class StringTag extends Tag {
@override
void writeStringifiedValue(StringBuilder builder, int indent, bool isList) {
if (shouldQuote(value))
builder.append("${isList ? "".padLeft(indent, '\t') : ""}\"${value}\"");
else
builder.append("${isList ? "".padLeft(indent, '\t') : ""}${value}");
final useSingleQuotes = shouldUseSingleQuotes(value);
final quote = useSingleQuotes ? '\'' : '"';
final escapeQuote = useSingleQuotes ? '\\\'' : '\\"';
String escapedValue = value;
if (shouldEscapeSingleQuotes(value) && useSingleQuotes) {
escapedValue = value.replaceAll('\'', escapeQuote);
}
builder.append(
"${isList ? "".padLeft(indent, '\t') : ""}${quote}${escapedValue}${quote}");
}
@override