Skip to content
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
1 change: 1 addition & 0 deletions java/com/google/re2j/Matcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ private boolean genMatch(int startByte, int anchor) {
// From the JDK docs, looks like no.
boolean ok = pattern.re2().match(matcherInput, startByte, inputLength, anchor, groups, 1);
if (!ok) {
hasMatch = false;
return false;
}
hasMatch = true;
Expand Down
44 changes: 44 additions & 0 deletions javatests/com/google/re2j/MatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -524,4 +524,48 @@ public void testPatternLongestMatch() {
assertEquals("aaa bbb", text.substring(matcher.start(), matcher.end()));
}
}

@Test
public void testStateResetOnFailure() {
{
Matcher m = Pattern.compile("abc").matcher("abc");
assertTrue(m.find());
assertEquals("abc", m.group());

// Subsequent find() fails
assertFalse(m.find());
try {
m.group();
fail("Should have thrown IllegalStateException");
} catch (IllegalStateException expected) {
// Expected
}
try {
m.start();
fail("Should have thrown IllegalStateException");
} catch (IllegalStateException expected) {
// Expected
}
try {
m.end();
fail("Should have thrown IllegalStateException");
} catch (IllegalStateException expected) {
// Expected
}
}

{
// Test matches() then find()
Matcher m = Pattern.compile("([1-5][0-9]{2})-([1-5][0-9]{2})").matcher("201-299");
assertTrue(m.matches());
assertEquals("201-299", m.group());
assertFalse(m.find()); // should fail
try {
m.group(1);
fail("Should have thrown IllegalStateException");
} catch (IllegalStateException expected) {
// Expected
}
}
}
}
Loading