mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:17:34 +00:00
LibRegex: Make sure there are as many group matches as actual matches
Fixes #6131.
This commit is contained in:
parent
1bdc1cf77e
commit
ade97d4094
2 changed files with 24 additions and 0 deletions
|
@ -101,3 +101,21 @@ test("optionally seen capture group", () => {
|
||||||
expect(res[1]).toBe("mozilla");
|
expect(res[1]).toBe("mozilla");
|
||||||
expect(res[2]).toBeUndefined();
|
expect(res[2]).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// #6131
|
||||||
|
test("capture group with two '?' qualifiers", () => {
|
||||||
|
let res = /()??/.exec("");
|
||||||
|
|
||||||
|
expect(res.length).toBe(2);
|
||||||
|
expect(res[0]).toBe("");
|
||||||
|
expect(res[1]).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("named capture group with two '?' qualifiers", () => {
|
||||||
|
let res = /(?<foo>)??/.exec("");
|
||||||
|
|
||||||
|
expect(res.length).toBe(2);
|
||||||
|
expect(res[0]).toBe("");
|
||||||
|
expect(res[1]).toBeUndefined();
|
||||||
|
expect(res.groups.foo).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
|
@ -254,6 +254,9 @@ RegexResult Matcher<Parser>::match(const Vector<RegexStringView> views, Optional
|
||||||
MatchOutput output_copy;
|
MatchOutput output_copy;
|
||||||
if (match_count) {
|
if (match_count) {
|
||||||
output_copy.capture_group_matches = output.capture_group_matches;
|
output_copy.capture_group_matches = output.capture_group_matches;
|
||||||
|
// Make sure there are as many capture matches as there are actual matches.
|
||||||
|
if (output_copy.capture_group_matches.size() < match_count)
|
||||||
|
output_copy.capture_group_matches.resize(match_count);
|
||||||
for (auto& matches : output_copy.capture_group_matches)
|
for (auto& matches : output_copy.capture_group_matches)
|
||||||
matches.resize(m_pattern.parser_result.capture_groups_count + 1);
|
matches.resize(m_pattern.parser_result.capture_groups_count + 1);
|
||||||
if (!input.regex_options.has_flag_set(AllFlags::SkipTrimEmptyMatches)) {
|
if (!input.regex_options.has_flag_set(AllFlags::SkipTrimEmptyMatches)) {
|
||||||
|
@ -262,6 +265,9 @@ RegexResult Matcher<Parser>::match(const Vector<RegexStringView> views, Optional
|
||||||
}
|
}
|
||||||
|
|
||||||
output_copy.named_capture_group_matches = output.named_capture_group_matches;
|
output_copy.named_capture_group_matches = output.named_capture_group_matches;
|
||||||
|
// Make sure there are as many capture matches as there are actual matches.
|
||||||
|
if (output_copy.named_capture_group_matches.size() < match_count)
|
||||||
|
output_copy.named_capture_group_matches.resize(match_count);
|
||||||
|
|
||||||
output_copy.matches = output.matches;
|
output_copy.matches = output.matches;
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue