Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StringReader: allow more escapes like JSON #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/main/java/com/mojang/brigadier/StringReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions src/test/java/com/mojang/brigadier/StringReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(""));
}

Expand Down Expand Up @@ -585,4 +586,4 @@ public void readBoolean_none() throws Exception {
assertThat(ex.getCursor(), is(0));
}
}
}
}