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
2 changes: 1 addition & 1 deletion java/com/google/re2j/Matcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ private void appendReplacementInternal(StringBuilder sb, String replacement) {
}
for (i += 2; i < m; i++) {
c = replacement.charAt(i);
if (c < '0' || c > '9' || n * 10 + c - '0' > groupCount) {
if (c < '0' || c > '9') {
break;
}
n = n * 10 + c - '0';
Expand Down
20 changes: 18 additions & 2 deletions javatests/com/google/re2j/MatcherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void testReplaceAll() {
ApiTestUtils.testReplaceAll(
"abcdefghijklmnopqrstuvwxyz123",
"(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)",
"$10$20",
"$10$2\\0",
"jb0wo0123");
ApiTestUtils.testReplaceAll(
"\u00e1\u0062\u00e7\u2655", "(.)", "<$1>", "<\u00e1><\u0062><\u00e7><\u2655>");
Expand Down Expand Up @@ -83,7 +83,7 @@ public void testReplaceFirst() {
ApiTestUtils.testReplaceFirst(
"abcdefghijklmnopqrstuvwxyz123",
"(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)(.)",
"$10$20",
"$10$2\\0",
"jb0nopqrstuvwxyz123");
ApiTestUtils.testReplaceFirst(
"\u00e1\u0062\u00e7\u2655", "(.)", "<$1>", "<\u00e1>\u0062\u00e7\u2655");
Expand Down Expand Up @@ -166,6 +166,22 @@ public void testInvalidReplacement() {
}
}

@Test
public void testInvalidReplacementDoubleDigit() {
try {
ApiTestUtils.testReplaceFirst("abc", "(abc)", "$10", "xxx");
fail();
} catch (IndexOutOfBoundsException e) {
/* ok */
assertTrue(true);
}
}

@Test
public void testValidReplacementSingleDigitSeparated() {
ApiTestUtils.testReplaceFirst("abc", "(abc)", "$1\\0", "abc0");
}

@Test
public void testInvalidGroupNoMatch() {
try {
Expand Down