StringReader: allow more escapes like JSON
This commit is contained in:
parent
cf754c4ef6
commit
134099a39d
2 changed files with 36 additions and 6 deletions
|
@ -200,8 +200,37 @@ public class StringReader implements ImmutableStringReader {
|
||||||
while (canRead()) {
|
while (canRead()) {
|
||||||
final char c = read();
|
final char c = read();
|
||||||
if (escaped) {
|
if (escaped) {
|
||||||
if (c == terminator || c == SYNTAX_ESCAPE) {
|
Character mogrified = null;
|
||||||
result.append(c);
|
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;
|
escaped = false;
|
||||||
} else {
|
} else {
|
||||||
setCursor(getCursor() - 1);
|
setCursor(getCursor() - 1);
|
||||||
|
|
|
@ -214,9 +214,10 @@ public class StringReaderTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void readQuotedString_withEscapedEscapes() throws Exception {
|
public void readQuotedString_withEscapedEscapes() throws Exception {
|
||||||
final StringReader reader = new StringReader("\"\\\\o/\"");
|
final String original = "\"\\n\\\\o/\"";
|
||||||
assertThat(reader.readQuotedString(), equalTo("\\o/"));
|
final StringReader reader = new StringReader(original);
|
||||||
assertThat(reader.getRead(), equalTo("\"\\\\o/\""));
|
assertThat(reader.readQuotedString(), equalTo("\n\\o/"));
|
||||||
|
assertThat(reader.getRead(), equalTo(original));
|
||||||
assertThat(reader.getRemaining(), equalTo(""));
|
assertThat(reader.getRemaining(), equalTo(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue