LibRegex: Ensure nullable quantifiers backtrack when input remains

Makes patterns like `/(a?b??)*/` correctly match the string
This commit is contained in:
aplefull 2025-03-01 18:23:15 +01:00 committed by Ali Mohammad Pur
parent 40c71ff3c0
commit 61744322ad
Notes: github-actions[bot] 2025-03-02 14:20:04 +00:00
2 changed files with 13 additions and 0 deletions

View file

@ -1118,6 +1118,10 @@ ALWAYS_INLINE ExecutionResult OpCode_JumpNonEmpty::execute(MatchInput const& inp
}
}
if (state.string_position < input.view.length()) {
return ExecutionResult::Failed_ExecuteLowPrioForks;
}
return ExecutionResult::Continue;
}

View file

@ -986,6 +986,15 @@ TEST_CASE(extremely_long_fork_chain)
EXPECT_EQ(result.success, true);
}
TEST_CASE(nullable_quantifiers)
{
// clang-format off
Regex<ECMA262> re("(a?b?""?)*"); // Pattern (a?b??)* has to be concatenated to avoid "??)", which is a trigraph.
// clang-format on
auto result = re.match("ab"sv);
EXPECT_EQ(result.matches.at(0).view, "ab"sv);
}
TEST_CASE(theoretically_infinite_loop)
{
Array patterns {