From 794dc368f19b1c9bf7161def8a26e1282e78a925 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Sun, 13 Jun 2021 22:46:21 +0200 Subject: LibRegex: Avoid prepending items to vectors --- Userland/Libraries/LibRegex/RegexMatcher.cpp | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibRegex/RegexMatcher.cpp b/Userland/Libraries/LibRegex/RegexMatcher.cpp index b402442220..a7ade1f533 100644 --- a/Userland/Libraries/LibRegex/RegexMatcher.cpp +++ b/Userland/Libraries/LibRegex/RegexMatcher.cpp @@ -293,7 +293,7 @@ Optional Matcher::execute(const MatchInput& input, MatchState& sta if (recursion_level > c_max_recursion) return false; - Vector fork_low_prio_states; + Vector reversed_fork_low_prio_states; MatchState fork_high_prio_state; Optional success; @@ -323,7 +323,7 @@ Optional Matcher::execute(const MatchInput& input, MatchState& sta switch (result) { case ExecutionResult::Fork_PrioLow: - fork_low_prio_states.prepend(state); + reversed_fork_low_prio_states.append(state); continue; case ExecutionResult::Fork_PrioHigh: fork_high_prio_state = state; @@ -344,8 +344,13 @@ Optional Matcher::execute(const MatchInput& input, MatchState& sta return true; case ExecutionResult::Failed: return false; - case ExecutionResult::Failed_ExecuteLowPrioForks: - return execute_low_prio_forks(input, state, output, fork_low_prio_states, recursion_level + 1); + case ExecutionResult::Failed_ExecuteLowPrioForks: { + Vector fork_low_prio_states; + fork_low_prio_states.ensure_capacity(reversed_fork_low_prio_states.size()); + for (ssize_t i = reversed_fork_low_prio_states.size() - 1; i >= 0; i--) + fork_low_prio_states.unchecked_append(move(reversed_fork_low_prio_states[i])); + return execute_low_prio_forks(input, state, output, move(fork_low_prio_states), recursion_level + 1); + } } } -- cgit v1.2.3