Skip to content

Commit 03bb7ae

Browse files
committed
quotedStringWithEscapes(char, Function)
1 parent aa31fae commit 03bb7ae

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,21 @@ public static Parser<String> string(String value) {
145145
}
146146

147147
/**
148-
* String literal quoted by {@code quoteChar} and allows backslash escapes (no Unicode escapes or
149-
* C-style escapes like '\n', '\t' etc.).
148+
* String literal quoted by {@code quoteChar} and allows backslash escapes (no Unicode escapes).
150149
*
151-
* <p>For example, {@code "foo\\bar"} is parsed as {@code foo\bar}.
150+
* <p>Any escaped character will be passed to the {@code escapeFunction} to translate. If you need
151+
* ST-Query style escaping that doesn't treat '\t', '\n' etc. specially, just pass {@code
152+
* Object::toString}.
152153
*
153154
* @since 9.4
154155
*/
155-
public static Parser<String> quotedStringWithEscapes(char quoteChar) {
156+
public static Parser<String> quotedStringWithEscapes(
157+
char quoteChar, Function<? super Character, String> escapeFunction) {
156158
checkArgument(quoteChar != '\\', "quoteChar cannot be \\");
157159
String quoteString = Character.toString(quoteChar);
158160
return anyOf(
159161
consecutive(isNot(quoteChar).and(isNot('\\')), "quoted chars"),
160-
string("\\").then(single(CharPredicate.ANY, "escaped char").source()))
162+
string("\\").then(single(CharPredicate.ANY, "escaped char").map(escapeFunction)))
161163
.zeroOrMore(joining())
162164
.immediatelyBetween(quoteString, quoteString);
163165
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static SearchCriteria parse(String input) {
6767
// A search term is either quoted, or unquoted (but cannot be a keyword)
6868
Parser<Term> unquoted =
6969
WORD.suchThat(w -> !keywords.contains(w), "search term").map(Term::new);
70-
Parser<Term> quoted = Parser.quotedStringWithEscapes('"').map(Term::new);
70+
Parser<Term> quoted = Parser.quotedStringWithEscapes('"', Object::toString).map(Term::new);
7171

7272
// Leaf-level search term can be a quoted, unquoted term, or a sub-criteria inside parentheses.
7373
// They are then grouped by the boolean operators.

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2305,7 +2305,7 @@ public void single_failure() {
23052305

23062306
@Test
23072307
public void quotedStringWithEscapes_singleQuote_success() {
2308-
Parser<String> singleQuoted = Parser.quotedStringWithEscapes('\'');
2308+
Parser<String> singleQuoted = Parser.quotedStringWithEscapes('\'', Object::toString);
23092309
assertThat(singleQuoted.parse("''")).isEmpty();
23102310
assertThat(singleQuoted.parse("'foo'")).isEqualTo("foo");
23112311
assertThat(singleQuoted.parse("'foo\\'s'")).isEqualTo("foo's");
@@ -2316,7 +2316,7 @@ public void quotedStringWithEscapes_singleQuote_success() {
23162316

23172317
@Test
23182318
public void quotedStringWithEscapes_doubleQuote_success() {
2319-
Parser<String> doubleQuoted = Parser.quotedStringWithEscapes('"');
2319+
Parser<String> doubleQuoted = Parser.quotedStringWithEscapes('"', Object::toString);
23202320
assertThat(doubleQuoted.parse("\"\"")).isEmpty();
23212321
assertThat(doubleQuoted.parse("\"bar\"")).isEqualTo("bar");
23222322
assertThat(doubleQuoted.parse("\"bar\\\"baz\"")).isEqualTo("bar\"baz");
@@ -2325,15 +2325,17 @@ public void quotedStringWithEscapes_doubleQuote_success() {
23252325

23262326
@Test
23272327
public void quotedStringWithEscapes_failures() {
2328-
Parser<String> singleQuoted = Parser.quotedStringWithEscapes('\'');
2328+
Parser<String> singleQuoted = Parser.quotedStringWithEscapes('\'', Object::toString);
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
23322332
}
23332333

23342334
@Test
23352335
public void quotedStringWithEscapes_backslashQuoteChar_throws() {
2336-
assertThrows(IllegalArgumentException.class, () -> Parser.quotedStringWithEscapes('\\'));
2336+
assertThrows(
2337+
IllegalArgumentException.class,
2338+
() -> Parser.quotedStringWithEscapes('\\', Object::toString));
23372339
}
23382340

23392341
@Test

0 commit comments

Comments
 (0)