Skip to content

Commit f2258e4

Browse files
committed
quotedStringWithEscapes() more forgiving to allow control chars
1 parent 309912c commit f2258e4

File tree

2 files changed

+4
-11
lines changed

2 files changed

+4
-11
lines changed

dot-parse/src/main/java/com/google/common/labs/parse/Parser.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,14 @@ public static Parser<String> string(String value) {
160160
* @since 9.4
161161
*/
162162
public static Parser<String> quotedStringWithEscapes(
163-
char quoteChar, Function<? super Character, String> unescapeFunction) {
163+
char quoteChar, Function<? super Character, ? extends CharSequence> unescapeFunction) {
164164
requireNonNull(unescapeFunction);
165-
CharPredicate allowedChars = c -> !Character.isISOControl(c);
166165
checkArgument(quoteChar != '\\', "quoteChar cannot be '\\'");
167-
checkArgument(allowedChars.test(quoteChar), "quoteChar cannot be %s", quoteChar);
166+
checkArgument(!Character.isISOControl(quoteChar), "quoteChar cannot be a control character");
168167
String quoteString = Character.toString(quoteChar);
169168
return anyOf(
170-
consecutive(isNot(quoteChar).and(isNot('\\')).and(allowedChars), "quoted chars"),
171-
string("\\").then(single(allowedChars, "escaped char").map(unescapeFunction)))
169+
consecutive(isNot(quoteChar).and(isNot('\\')), "quoted chars"),
170+
string("\\").then(single(CharPredicate.ANY, "escaped char").map(unescapeFunction)))
172171
.zeroOrMore(joining())
173172
.immediatelyBetween(quoteString, quoteString);
174173
}

dot-parse/src/test/java/com/google/common/labs/parse/ParserTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2329,12 +2329,6 @@ public void quotedStringWithEscapes_failures() {
23292329
assertThrows(ParseException.class, () -> singleQuoted.parse("'foo")); // unclosed
23302330
assertThrows(ParseException.class, () -> singleQuoted.parse("'foo'bar")); // leftover
23312331
assertThrows(ParseException.class, () -> singleQuoted.parse("'foo\\")); // dangling escape
2332-
assertThrows(ParseException.class, () -> singleQuoted.parse("foo\n")); // literal newline
2333-
assertThrows(ParseException.class, () -> singleQuoted.parse("foo\r")); // CR
2334-
assertThrows(ParseException.class, () -> singleQuoted.parse("foo\t"));
2335-
assertThrows(ParseException.class, () -> singleQuoted.parse("\\\n")); // literal newline
2336-
assertThrows(ParseException.class, () -> singleQuoted.parse("\\\r")); // CR
2337-
assertThrows(ParseException.class, () -> singleQuoted.parse("\\\t"));
23382332
}
23392333

23402334
@Test

0 commit comments

Comments
 (0)