Browse Source

LibRegex: Consider named capture groups as normal capture groups too

AnotherTest 4 năm trước cách đây
mục cha
commit
1bdc1cf77e

+ 2 - 1
Userland/Libraries/LibJS/Tests/builtins/RegExp/RegExp.prototype.exec.js

@@ -44,9 +44,10 @@ test("basic named captures", () => {
     let re = /f(?<os>o.*)/;
     let res = re.exec("fooooo");
 
-    expect(res.length).toBe(1);
+    expect(res.length).toBe(2);
     expect(res.index).toBe(0);
     expect(res[0]).toBe("fooooo");
+    expect(res[1]).toBe("ooooo");
     expect(res.groups).not.toBe(undefined);
     expect(res.groups.os).toBe("ooooo");
 });

+ 4 - 0
Userland/Libraries/LibRegex/RegexParser.cpp

@@ -1692,6 +1692,7 @@ bool ECMA262Parser::parse_capture_group(ByteCode& stack, size_t& match_length_mi
 
         if (consume("<")) {
             ++m_parser_state.named_capture_groups_count;
+            auto group_index = ++m_parser_state.capture_groups_count; // Named capture groups count as normal capture groups too.
             auto name = read_capture_group_specifier();
 
             if (name.is_empty()) {
@@ -1707,12 +1708,15 @@ bool ECMA262Parser::parse_capture_group(ByteCode& stack, size_t& match_length_mi
             consume(TokenType::RightParen, Error::MismatchingParen);
 
             stack.insert_bytecode_group_capture_left(name);
+            stack.insert_bytecode_group_capture_left(group_index);
             stack.append(move(capture_group_bytecode));
             stack.insert_bytecode_group_capture_right(name);
+            stack.insert_bytecode_group_capture_right(group_index);
 
             match_length_minimum += length;
 
             m_parser_state.named_capture_group_minimum_lengths.set(name, length);
+            m_parser_state.capture_group_minimum_lengths.set(group_index, length);
             return true;
         }