1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:47:34 +00:00

LibRegex: Consider named capture groups as normal capture groups too

This commit is contained in:
AnotherTest 2021-04-05 06:25:57 +04:30 committed by Andreas Kling
parent be0182d049
commit 1bdc1cf77e
2 changed files with 6 additions and 1 deletions

View file

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

View file

@ -1692,6 +1692,7 @@ bool ECMA262Parser::parse_capture_group(ByteCode& stack, size_t& match_length_mi
if (consume("<")) { if (consume("<")) {
++m_parser_state.named_capture_groups_count; ++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(); auto name = read_capture_group_specifier();
if (name.is_empty()) { 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); consume(TokenType::RightParen, Error::MismatchingParen);
stack.insert_bytecode_group_capture_left(name); stack.insert_bytecode_group_capture_left(name);
stack.insert_bytecode_group_capture_left(group_index);
stack.append(move(capture_group_bytecode)); stack.append(move(capture_group_bytecode));
stack.insert_bytecode_group_capture_right(name); stack.insert_bytecode_group_capture_right(name);
stack.insert_bytecode_group_capture_right(group_index);
match_length_minimum += length; match_length_minimum += length;
m_parser_state.named_capture_group_minimum_lengths.set(name, 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; return true;
} }