1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 19:07:35 +00:00

LibJS+LibRegex: Don't repeat regex match in regexp_exec()

LibRegex already implements this loop in a more performant way, so all
LibJS has to do here is to return things in the right shape, and not
loop over the input string.
Previously this was a quadratic operation on string length, which lead
to crazy execution times on failing regexps - now it's nice and fast :^)

Note that a Regex test has to be updated to remove the stateful flag as
it repeats matching on multiple strings.
This commit is contained in:
Ali Mohammad Pur 2022-02-04 23:56:44 +03:30 committed by Andreas Kling
parent 2b028f6faa
commit a962ee020a
3 changed files with 29 additions and 45 deletions

View file

@ -180,8 +180,6 @@ RegexResult Matcher<Parser>::match(Vector<RegexStringView> const& views, Optiona
#endif
bool continue_search = input.regex_options.has_flag_set(AllFlags::Global) || input.regex_options.has_flag_set(AllFlags::Multiline);
if (input.regex_options.has_flag_set(AllFlags::Internal_Stateful))
continue_search = false;
auto single_match_only = input.regex_options.has_flag_set(AllFlags::SingleMatch);
@ -282,11 +280,7 @@ RegexResult Matcher<Parser>::match(Vector<RegexStringView> const& views, Optiona
break;
continue;
}
if (input.regex_options.has_flag_set(AllFlags::Internal_Stateful)) {
append_match(input, state, view_index);
break;
}
if (state.string_position < view_length) {
if (state.string_position < view_length && !input.regex_options.has_flag_set(AllFlags::Internal_Stateful)) {
return { false, 0, {}, {}, {}, operations };
}
@ -503,8 +497,6 @@ Optional<bool> Matcher<Parser>::execute(MatchInput const& input, MatchState& sta
return false;
case ExecutionResult::Failed_ExecuteLowPrioForks: {
if (states_to_try_next.is_empty()) {
if (input.regex_options.has_flag_set(AllFlags::Internal_Stateful))
return {};
return false;
}
state = states_to_try_next.take_last();