diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2021-04-04 15:59:35 +0430 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-04-04 16:04:06 +0200 |
commit | 76f63c298059fe0ffa7aca07ffd9116925cdb64e (patch) | |
tree | 999fe438bcce0e68ec3bfe8cfa00470ba95c33a4 /Userland/Libraries/LibRegex | |
parent | ef8f8f47accca59fbdd00d5f0a918bf9a622f841 (diff) | |
download | serenity-76f63c298059fe0ffa7aca07ffd9116925cdb64e.zip |
LibRegex: Allocate entries for all capture groups in RegexResult
Not just the seen ones.
Fixes #6108.
Diffstat (limited to 'Userland/Libraries/LibRegex')
-rw-r--r-- | Userland/Libraries/LibRegex/RegexMatcher.cpp | 30 |
1 files changed, 9 insertions, 21 deletions
diff --git a/Userland/Libraries/LibRegex/RegexMatcher.cpp b/Userland/Libraries/LibRegex/RegexMatcher.cpp index 970db6bf1c..8bfa00def8 100644 --- a/Userland/Libraries/LibRegex/RegexMatcher.cpp +++ b/Userland/Libraries/LibRegex/RegexMatcher.cpp @@ -253,36 +253,24 @@ RegexResult Matcher<Parser>::match(const Vector<RegexStringView> views, Optional MatchOutput output_copy; if (match_count) { - auto capture_groups_count = min(output.capture_group_matches.size(), output.matches.size()); - for (size_t i = 0; i < capture_groups_count; ++i) { - if (input.regex_options.has_flag_set(AllFlags::SkipTrimEmptyMatches)) { - output_copy.capture_group_matches.append(output.capture_group_matches.at(i)); - } else { - Vector<Match> capture_group_matches; - for (size_t j = 0; j < output.capture_group_matches.at(i).size(); ++j) { - if (!output.capture_group_matches.at(i).at(j).view.is_null()) - capture_group_matches.append(output.capture_group_matches.at(i).at(j)); - } - output_copy.capture_group_matches.append(capture_group_matches); - } - } - - auto named_capture_groups_count = min(output.named_capture_group_matches.size(), output.matches.size()); - for (size_t i = 0; i < named_capture_groups_count; ++i) { - if (output.matches.at(i).view.length()) - output_copy.named_capture_group_matches.append(output.named_capture_group_matches.at(i)); + output_copy.capture_group_matches = output.capture_group_matches; + for (auto& matches : output_copy.capture_group_matches) + matches.resize(m_pattern.parser_result.capture_groups_count + 1); + if (!input.regex_options.has_flag_set(AllFlags::SkipTrimEmptyMatches)) { + for (auto& matches : output_copy.capture_group_matches) + matches.template remove_all_matching([](auto& match) { return match.view.is_null(); }); } - for (size_t i = 0; i < match_count; ++i) - output_copy.matches.append(output.matches.at(i)); + output_copy.named_capture_group_matches = output.named_capture_group_matches; + output_copy.matches = output.matches; } else { output_copy.capture_group_matches.clear_with_capacity(); output_copy.named_capture_group_matches.clear_with_capacity(); } return { - match_count ? true : false, + match_count != 0, match_count, move(output_copy.matches), move(output_copy.capture_group_matches), |