3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-11-16 21:10:30 +01:00

Fix saving of escaped strings

Dieser Commit ist enthalten in:
Andrew Steinborn 2020-06-24 11:21:34 -04:00
Ursprung 32c7f6481c
Commit 0c1d657b19

Datei anzeigen

@ -16,6 +16,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.regex.Pattern;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
@ -25,6 +26,10 @@ import org.apache.logging.log4j.Logger;
public abstract class AnnotatedConfig { public abstract class AnnotatedConfig {
private static final Logger logger = LogManager.getLogger(AnnotatedConfig.class); private static final Logger logger = LogManager.getLogger(AnnotatedConfig.class);
@SuppressWarnings("checkstyle:IllegalTokenText") // Need to follow the TOML spec exactly
private static final Pattern STRING_NEEDS_ESCAPE = Pattern.compile(
"(\"|\\\\|[\\u0000-\\u0008]|[\\u000a-\\u001f]|\\u007f)"
);
public static Logger getLogger() { public static Logger getLogger() {
return logger; return logger;
@ -201,11 +206,7 @@ public abstract class AnnotatedConfig {
} }
if (value instanceof String) { if (value instanceof String) {
String stringValue = (String) value; return writeString((String) value);
if (stringValue.isEmpty()) {
return "\"\"";
}
return "\"" + stringValue.replace("\n", "\\n") + "\"";
} }
return value != null ? value.toString() : "null"; return value != null ? value.toString() : "null";
@ -219,6 +220,23 @@ public abstract class AnnotatedConfig {
return key; return key;
} }
private static String writeString(String str) {
if (str.isEmpty()) {
return "\"\"";
}
// According to the TOML specification (https://toml.io/en/v1.0.0-rc.1#section-7):
//
// Any Unicode character may be used except those that must be escaped: quotation mark,
// backslash, and the control characters other than tab (U+0000 to U+0008, U+000A to U+001F,
// U+007F).
if (STRING_NEEDS_ESCAPE.matcher(str).find()) {
return "'" + str + "'";
} else {
return "\"" + str.replace("\n", "\\n") + "\"";
}
}
protected static String unescapeKeyIfNeeded(String key) { protected static String unescapeKeyIfNeeded(String key) {
int lastIndex; int lastIndex;
if (key.indexOf('"') == 0 && (lastIndex = key.lastIndexOf('"')) == (key.length() - 1)) { if (key.indexOf('"') == 0 && (lastIndex = key.lastIndexOf('"')) == (key.length() - 1)) {