Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) lib: unmatched groups #27

Merged
merged 1 commit into from
Jun 22, 2024
Merged
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
10 changes: 9 additions & 1 deletion lib/src/main/java/org/pcre4j/Pcre4jUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,15 @@ public static int[] convertOvectorToStringIndices(String subject, byte[] subject
// Convert byte offsets to string indices
final var stringIndices = new int[ovector.length];
for (var valueIndex = 0; valueIndex < ovector.length; valueIndex++) {
stringIndices[valueIndex] = byteOffsetToStringIndex[(int) (ovector[valueIndex] - ovector[0])];
final var byteIndex = ovector[valueIndex];

// Handle case when group was not matched
if (byteIndex == -1) {
stringIndices[valueIndex] = -1;
continue;
}

stringIndices[valueIndex] = byteOffsetToStringIndex[(int) (byteIndex - ovector[0])];
}

return stringIndices;
Expand Down
28 changes: 28 additions & 0 deletions regex/src/test/java/org/pcre4j/regex/MatcherTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -618,4 +618,32 @@ void captureGroups() {
assertEquals(javaMatcher.end("two"), pcre4jMatcher.end("two"));
}

@Test
void unmatchedGroups() {
var regex = "42((?<exclamation>!)|(?<question>\\?))";
var input = "42!";
var javaMatcher = java.util.regex.Pattern.compile(regex).matcher(input);
var pcre4jMatcher = Pattern.compile(regex).matcher(input);

assertEquals(javaMatcher.find(), pcre4jMatcher.find());

assertEquals(javaMatcher.group(), pcre4jMatcher.group());
assertEquals(javaMatcher.group(0), pcre4jMatcher.group(0));
assertEquals(javaMatcher.group(1), pcre4jMatcher.group(1));
assertEquals(javaMatcher.group(2), pcre4jMatcher.group(2));
assertEquals(javaMatcher.groupCount(), pcre4jMatcher.groupCount());
assertEquals(javaMatcher.start(), pcre4jMatcher.start());
assertEquals(javaMatcher.start(0), pcre4jMatcher.start(0));
assertEquals(javaMatcher.start(1), pcre4jMatcher.start(1));
assertEquals(javaMatcher.start(2), pcre4jMatcher.start(2));
assertEquals(javaMatcher.start("exclamation"), pcre4jMatcher.start("exclamation"));
assertEquals(javaMatcher.start("question"), pcre4jMatcher.start("question"));
assertEquals(javaMatcher.end(), pcre4jMatcher.end());
assertEquals(javaMatcher.end(0), pcre4jMatcher.end(0));
assertEquals(javaMatcher.end(1), pcre4jMatcher.end(1));
assertEquals(javaMatcher.end(2), pcre4jMatcher.end(2));
assertEquals(javaMatcher.end("exclamation"), pcre4jMatcher.end("exclamation"));
assertEquals(javaMatcher.end("question"), pcre4jMatcher.end("question"));
}

}