Skip to content
Closed
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
15 changes: 12 additions & 3 deletions java/com/google/re2j/MachineInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,18 @@ boolean canCheckPrefix() {

@Override
int index(RE2 re2, int pos) {
pos += start;
int i = indexOf(str, re2.prefix, pos);
return i < 0 ? i : i - pos;
int p = pos + start;
while (true) {
int i = indexOf(str, re2.prefix, p);
if (i < 0) {
return -1;
}
if (i > 0 && Character.isLowSurrogate(str.charAt(i)) && Character.isHighSurrogate(str.charAt(i - 1))) {
p = i + 1;
continue;
}
return i - (pos + start);
}
}

@Override
Expand Down
19 changes: 19 additions & 0 deletions javatests/com/google/re2j/MatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -524,4 +524,23 @@ public void testPatternLongestMatch() {
assertEquals("aaa bbb", text.substring(matcher.start(), matcher.end()));
}
}

@Test
public void testSurrogatePairInteriorNoMatch() {
// A pattern whose literal prefix is a lone low surrogate should not match
// inside a valid surrogate pair (issue #207).
assertFalse(Pattern.compile("\uDC21").matcher("a\uD801\uDC21b").find());
assertFalse(Pattern.compile("\uDC21|\uDC22").matcher("a\uD801\uDC21b").find());
assertFalse(Pattern.compile("[\uD800-\uDFFF]").matcher("a\uD801\uDC21b").find());

// Search must skip the interior hit and resume to find an isolated low surrogate:
Matcher m = Pattern.compile("\uDC21").matcher("\uD801\uDC21\uDC21");
assertTrue(m.find());
assertEquals(2, m.start());
assertEquals(3, m.end());

// StringBuilder CharSequence input should also behave identically:
Matcher mSb = Pattern.compile("\uDC21").matcher(new StringBuilder("a\uD801\uDC21b"));
assertFalse(mSb.find());
}
}