1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 04:27:43 +00:00

LibRegex: Add the literal string search optimisation

This switches to using a simple string equality check if the regex
pattern is strictly a string literal.
Technically this optimisation can also be made on bounded literal
patterns like /[abc]def/ or /abc|def/ as well, but those are
significantly more complex to implement due to our bytecode-only
approach.
This commit is contained in:
Ali Mohammad Pur 2023-07-28 20:59:33 +03:30 committed by Andreas Kling
parent 221c52c696
commit 18f4b6c670
6 changed files with 82 additions and 4 deletions

View file

@ -419,6 +419,26 @@ private:
template<class Parser>
bool Matcher<Parser>::execute(MatchInput const& input, MatchState& state, size_t& operations) const
{
if (m_pattern->parser_result.optimization_data.pure_substring_search.has_value() && input.view.is_string_view()) {
// Yay, we can do a simple substring search!
auto& needle = m_pattern->parser_result.optimization_data.pure_substring_search.value();
if (needle.length() + state.string_position > input.view.length())
return false;
auto haystack = input.view.string_view().substring_view(state.string_position);
if (input.regex_options.has_flag_set(AllFlags::Insensitive)) {
if (!haystack.substring_view(0, needle.length()).equals_ignoring_ascii_case(needle))
return false;
} else {
if (!haystack.starts_with(needle))
return false;
}
state.string_position += needle.length();
state.string_position_in_code_units += needle.length();
return true;
}
BumpAllocatedLinkedList<MatchState> states_to_try_next;
#if REGEX_DEBUG
size_t recursion_level = 0;