mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-23 21:15:14 +00:00
LibRegex: Correct the alternative matching order when one is empty
Previously we were compiling `/a|/` into what effectively would be `/|a`, which is clearly incorrect.
This commit is contained in:
parent
76e99fa6c8
commit
3b0943d24c
Notes:
sideshowbarker
2024-07-18 03:20:18 +09:00
Author: https://github.com/alimpfard Commit: https://github.com/SerenityOS/serenity/commit/3b0943d24c Pull-request: https://github.com/SerenityOS/serenity/pull/12511
2 changed files with 22 additions and 7 deletions
|
@ -958,6 +958,21 @@ TEST_CASE(optimizer_char_class_lut)
|
|||
EXPECT_EQ(re.match("1635488940000"sv).success, false);
|
||||
}
|
||||
|
||||
TEST_CASE(optimizer_alternation)
|
||||
{
|
||||
Array tests {
|
||||
// Pattern, Subject, Expected length
|
||||
Tuple { "a|"sv, "a"sv, 1u },
|
||||
};
|
||||
|
||||
for (auto& test : tests) {
|
||||
Regex<ECMA262> re(test.get<0>());
|
||||
auto result = re.match(test.get<1>());
|
||||
EXPECT(result.success);
|
||||
EXPECT_EQ(result.matches.first().view.length(), test.get<2>());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE(posix_basic_dollar_is_end_anchor)
|
||||
{
|
||||
// Ensure that a dollar sign at the end only matches the end of the line.
|
||||
|
|
|
@ -450,17 +450,17 @@ void Optimizer::append_alternation(ByteCode& target, ByteCode&& left, ByteCode&&
|
|||
if (left_is_empty && right_is_empty)
|
||||
return;
|
||||
|
||||
// ForkJump right (+ left.size() + 2 + right.size())
|
||||
// (left)
|
||||
// Jump end (+ right.size())
|
||||
// ForkJump left (+ 2 + right.size())
|
||||
// (right)
|
||||
// Jump end (+ left.size())
|
||||
// (left)
|
||||
// LABEL end
|
||||
target.append(static_cast<ByteCodeValueType>(OpCodeId::ForkJump));
|
||||
target.append(left.size() + 2 + right.size());
|
||||
target.extend(move(left));
|
||||
target.append(static_cast<ByteCodeValueType>(OpCodeId::Jump));
|
||||
target.append(right.size());
|
||||
target.append(2 + right.size());
|
||||
target.extend(move(right));
|
||||
target.append(static_cast<ByteCodeValueType>(OpCodeId::Jump));
|
||||
target.append(left.size());
|
||||
target.extend(move(left));
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue