Skip to content

Commit 0394891

Browse files
committed
caseInsensitie() returns Parser<?> to avoid copy
1 parent 1bafe46 commit 0394891

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,29 +269,35 @@ public static Parser<String> string(String value) {
269269
/**
270270
* Matches a literal {@code string} case insensitively.
271271
*
272+
* <p>If you need to access the input substring that matched case insensitively,
273+
* you can use {@code .source()}.
274+
*
272275
* @since 9.9.3
273276
*/
274-
public static Parser<String> caseInsensitive(String value) {
275-
checkArgument(value.length() > 0, "value cannot be empty");
276-
return new Parser<>() {
277+
public static Parser<?> caseInsensitive(String string) {
278+
checkArgument(string.length() > 0, "value cannot be empty");
279+
return new Parser<String>() {
277280
@Override MatchResult<String> skipAndMatch(
278281
Parser<?> skip, CharInput input, int start, ErrorContext context) {
279282
start = skipIfAny(skip, input, start);
280-
if (input.startsWithCaseInsensitive(value, start)) {
283+
if (input.startsWithCaseInsensitive(string, start)) {
281284
return new MatchResult.Success<>(
282-
start, start + value.length(), input.snippet(start, value.length()));
285+
start, start + string.length(), input.snippet(start, string.length()));
283286
}
284-
return context.expecting(value, start);
287+
return context.expecting(string, start);
285288
}
286289
};
287290
}
288291

289292
/**
290293
* {@code caseInsensitiveWord("or")} matches "Or" and "OR", but not "orange", case insensitively.
291294
*
295+
* <p>If you need to access the input substring that matched case insensitively,
296+
* you can use {@code .source()}.
297+
*
292298
* @since 9.9.3
293299
*/
294-
public static Parser<String> caseInsensitiveWord(String word) {
300+
public static Parser<?> caseInsensitiveWord(String word) {
295301
return caseInsensitive(word).notImmediatelyFollowedBy(CharPredicate.WORD, "[a-zA-Z0-9_]");
296302
}
297303

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import static com.google.common.labs.parse.Parser.digits;
1010
import static com.google.common.labs.parse.Parser.first;
1111
import static com.google.common.labs.parse.Parser.literally;
12+
import static com.google.common.labs.parse.Parser.one;
1213
import static com.google.common.labs.parse.Parser.quotedBy;
1314
import static com.google.common.labs.parse.Parser.sequence;
14-
import static com.google.common.labs.parse.Parser.one;
1515
import static com.google.common.labs.parse.Parser.string;
1616
import static com.google.common.labs.parse.Parser.word;
1717
import static com.google.common.labs.parse.Parser.zeroOrMore;
@@ -165,7 +165,7 @@ public void string_cannotBeEmpty() {
165165

166166
@Test
167167
public void caseInsensitive_success() {
168-
Parser<String> parser = caseInsensitive("foo");
168+
Parser<String> parser = caseInsensitive("foo").source();
169169
assertThat(parser.parse("FoO")).isEqualTo("FoO");
170170
assertThat(parser.matches("FoO")).isTrue();
171171
assertThat(parser.parseToStream("fOo")).containsExactly("fOo");
@@ -174,7 +174,7 @@ public void caseInsensitive_success() {
174174

175175
@Test
176176
public void caseInsensitive_success_source() {
177-
Parser<String> parser = caseInsensitive("foo");
177+
Parser<String> parser = caseInsensitive("foo").source();
178178
assertThat(parser.source().parse("FoO")).isEqualTo("FoO");
179179
assertThat(parser.source().matches("FoO")).isTrue();
180180
assertThat(parser.source().parseToStream("fOo")).containsExactly("fOo");
@@ -250,8 +250,8 @@ public void word_skipping_failIfFollowedByWordChar() {
250250

251251
@Test
252252
public void caseInsensitiveWord_success() {
253-
assertThat(caseInsensitiveWord("foo").parse("FoO")).isEqualTo("FoO");
254-
assertThat(caseInsensitiveWord("foo").matches("FoO")).isTrue();
253+
assertThat(caseInsensitiveWord("foo").source().parse("FoO")).isEqualTo("FoO");
254+
assertThat(caseInsensitiveWord("foo").source().matches("FoO")).isTrue();
255255
}
256256

257257
@Test
@@ -266,17 +266,17 @@ public void caseInsensitiveWord_failIfFollowedByWordChar() {
266266

267267
@Test
268268
public void caseInsensitiveWord_successIfNotFollowedByWordChar() {
269-
assertThat(caseInsensitiveWord("foo").probe("FoO?")).containsExactly("FoO");
270-
assertThat(caseInsensitiveWord("foo").probe("FoO-")).containsExactly("FoO");
269+
assertThat(caseInsensitiveWord("foo").source().probe("FoO?")).containsExactly("FoO");
270+
assertThat(caseInsensitiveWord("foo").source().probe("FoO-")).containsExactly("FoO");
271271
}
272272

273273
@Test
274274
public void caseInsensitiveWord_skipping_success() {
275-
assertThat(caseInsensitiveWord("foo").skipping(Character::isWhitespace).parseToStream("fOo"))
275+
assertThat(caseInsensitiveWord("foo").source().skipping(Character::isWhitespace).parseToStream("fOo"))
276276
.containsExactly("fOo");
277-
assertThat(caseInsensitiveWord("foo").skipping(Character::isWhitespace).parseToStream("FoO fOO"))
277+
assertThat(caseInsensitiveWord("foo").source().skipping(Character::isWhitespace).parseToStream("FoO fOO"))
278278
.containsExactly("FoO", "fOO");
279-
assertThat(caseInsensitiveWord("foo").skipping(Character::isWhitespace).probe(" FoO-fOo"))
279+
assertThat(caseInsensitiveWord("foo").source().skipping(Character::isWhitespace).probe(" FoO-fOo"))
280280
.containsExactly("FoO");
281281
}
282282

0 commit comments

Comments
 (0)