From 134099a39d5e7af7e7212fa660f3360af0047180 Mon Sep 17 00:00:00 2001 From: Mary Strodl Date: Wed, 28 Apr 2021 10:31:30 -0400 Subject: [PATCH] StringReader: allow more escapes like JSON --- .../com/mojang/brigadier/StringReader.java | 33 +++++++++++++++++-- .../mojang/brigadier/StringReaderTest.java | 9 ++--- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/mojang/brigadier/StringReader.java b/src/main/java/com/mojang/brigadier/StringReader.java index c1465df6..f36f1d36 100644 --- a/src/main/java/com/mojang/brigadier/StringReader.java +++ b/src/main/java/com/mojang/brigadier/StringReader.java @@ -200,8 +200,37 @@ public String readStringUntil(char terminator) throws CommandSyntaxException { while (canRead()) { final char c = read(); if (escaped) { - if (c == terminator || c == SYNTAX_ESCAPE) { - result.append(c); + Character mogrified = null; + if (c == terminator) { + mogrified = terminator; + } else { + // Adapted from section 9 of ECMA-404: + switch (c) { + case SYNTAX_ESCAPE: + mogrified = SYNTAX_ESCAPE; + break; + case '/': + mogrified = '/'; + break; + case 'b': + mogrified = '\b'; + break; + case 'f': + mogrified = '\f'; + break; + case 'n': + mogrified = '\n'; + break; + case 'r': + mogrified = '\r'; + break; + case 't': + mogrified = '\t'; + break; + } + } + if(mogrified != null) { + result.append(mogrified); escaped = false; } else { setCursor(getCursor() - 1); diff --git a/src/test/java/com/mojang/brigadier/StringReaderTest.java b/src/test/java/com/mojang/brigadier/StringReaderTest.java index 23e2c14d..8b7057d7 100644 --- a/src/test/java/com/mojang/brigadier/StringReaderTest.java +++ b/src/test/java/com/mojang/brigadier/StringReaderTest.java @@ -214,9 +214,10 @@ public void readQuotedString_withEscapedQuote() throws Exception { @Test public void readQuotedString_withEscapedEscapes() throws Exception { - final StringReader reader = new StringReader("\"\\\\o/\""); - assertThat(reader.readQuotedString(), equalTo("\\o/")); - assertThat(reader.getRead(), equalTo("\"\\\\o/\"")); + final String original = "\"\\n\\\\o/\""; + final StringReader reader = new StringReader(original); + assertThat(reader.readQuotedString(), equalTo("\n\\o/")); + assertThat(reader.getRead(), equalTo(original)); assertThat(reader.getRemaining(), equalTo("")); } @@ -585,4 +586,4 @@ public void readBoolean_none() throws Exception { assertThat(ex.getCursor(), is(0)); } } -} \ No newline at end of file +}