From 5308d776006d3b07a6b08fb589d309dac984d06a Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 14 Apr 2025 15:33:33 +0200 Subject: [PATCH] LibRegex: Don't use Optional inside regex::Match This prevented Match from being trivially copyable, which we want it to be for fast Vector copying. --- Libraries/LibJS/Runtime/RegExpPrototype.cpp | 4 ++-- Libraries/LibRegex/RegexMatch.h | 7 +++++-- Tests/LibRegex/Regex.cpp | 8 ++++---- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Libraries/LibJS/Runtime/RegExpPrototype.cpp b/Libraries/LibJS/Runtime/RegExpPrototype.cpp index 966dbb3495c..5ab059e1ce9 100644 --- a/Libraries/LibJS/Runtime/RegExpPrototype.cpp +++ b/Libraries/LibJS/Runtime/RegExpPrototype.cpp @@ -328,9 +328,9 @@ static ThrowCompletionOr regexp_builtin_exec(VM& vm, RegExpObject& regexp MUST(array->create_data_property_or_throw(i, captured_value)); // e. If the ith capture of R was defined with a GroupName, then - if (capture.capture_group_name.has_value()) { + if (capture.capture_group_name >= 0) { // i. Let s be the CapturingGroupName of the corresponding RegExpIdentifierName. - auto group_name = regex.parser_result.bytecode.get_string(capture.capture_group_name.release_value()); + auto group_name = regex.parser_result.bytecode.get_string(capture.capture_group_name); // ii. Perform ! CreateDataPropertyOrThrow(groups, s, capturedValue). MUST(groups_object->create_data_property_or_throw(group_name, captured_value)); diff --git a/Libraries/LibRegex/RegexMatch.h b/Libraries/LibRegex/RegexMatch.h index 84fa5e5ba69..d39c7e3915d 100644 --- a/Libraries/LibRegex/RegexMatch.h +++ b/Libraries/LibRegex/RegexMatch.h @@ -502,7 +502,7 @@ public: void reset() { view = view.typed_null_view(); - capture_group_name.clear(); + capture_group_name = -1; line = 0; column = 0; global_offset = 0; @@ -510,7 +510,10 @@ public: } RegexStringView view {}; - Optional capture_group_name {}; // This is a string table index. + + // This is a string table index. -1 if none. Not using Optional to keep the struct trivially copyable. + ssize_t capture_group_name { -1 }; + size_t line { 0 }; size_t column { 0 }; size_t global_offset { 0 }; diff --git a/Tests/LibRegex/Regex.cpp b/Tests/LibRegex/Regex.cpp index 6804406185a..01bad48c1f6 100644 --- a/Tests/LibRegex/Regex.cpp +++ b/Tests/LibRegex/Regex.cpp @@ -426,10 +426,10 @@ TEST_CASE(named_capture_group) EXPECT_EQ(result.count, 2u); EXPECT_EQ(result.matches.at(0).view, "Opacity=255"); EXPECT_EQ(result.capture_group_matches.at(0).at(0).view, "255"); - EXPECT_EQ(re.parser_result.bytecode.get_string(result.capture_group_matches.at(0).at(0).capture_group_name.value()), "Test"); + EXPECT_EQ(re.parser_result.bytecode.get_string(result.capture_group_matches.at(0).at(0).capture_group_name), "Test"); EXPECT_EQ(result.matches.at(1).view, "AudibleBeep=0"); EXPECT_EQ(result.capture_group_matches.at(1).at(0).view, "0"); - EXPECT_EQ(re.parser_result.bytecode.get_string(result.capture_group_matches.at(1).at(0).capture_group_name.value()), "Test"); + EXPECT_EQ(re.parser_result.bytecode.get_string(result.capture_group_matches.at(1).at(0).capture_group_name), "Test"); } TEST_CASE(ecma262_named_capture_group_with_dollar_sign) @@ -449,10 +449,10 @@ TEST_CASE(ecma262_named_capture_group_with_dollar_sign) EXPECT_EQ(result.count, 2u); EXPECT_EQ(result.matches.at(0).view, "Opacity=255"); EXPECT_EQ(result.capture_group_matches.at(0).at(0).view, "255"); - EXPECT_EQ(re.parser_result.bytecode.get_string(result.capture_group_matches.at(0).at(0).capture_group_name.value()), "$Test$"); + EXPECT_EQ(re.parser_result.bytecode.get_string(result.capture_group_matches.at(0).at(0).capture_group_name), "$Test$"); EXPECT_EQ(result.matches.at(1).view, "AudibleBeep=0"); EXPECT_EQ(result.capture_group_matches.at(1).at(0).view, "0"); - EXPECT_EQ(re.parser_result.bytecode.get_string(result.capture_group_matches.at(1).at(0).capture_group_name.value()), "$Test$"); + EXPECT_EQ(re.parser_result.bytecode.get_string(result.capture_group_matches.at(1).at(0).capture_group_name), "$Test$"); } TEST_CASE(a_star)