From ff3806231880e5ba51d288c1139b6a96b7027110 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Sat, 13 Nov 2021 03:18:40 +0100 Subject: [PATCH] LibRegex: Correctly translate BRE pattern end anchors Previously we were always choosing the "nothing special" code path, even if the dollar symbol was at the end of the pattern (and therefore should have been considered special). Fix that by actually checking if the pattern end follows, and emitting the correct instruction if necessary. --- Userland/Libraries/LibRegex/RegexParser.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Userland/Libraries/LibRegex/RegexParser.cpp b/Userland/Libraries/LibRegex/RegexParser.cpp index ef35382831..91c42e5173 100644 --- a/Userland/Libraries/LibRegex/RegexParser.cpp +++ b/Userland/Libraries/LibRegex/RegexParser.cpp @@ -516,6 +516,20 @@ bool PosixBasicParser::parse_one_char_or_collation_element(ByteCode& bytecode, s return true; } + // Dollars are special if at the end of a pattern. + if (match(TokenType::Dollar)) { + consume(); + + // If we are at the end of a pattern, emit an end check instruction. + if (match(TokenType::Eof)) { + bytecode.empend((ByteCodeValueType)OpCodeId::CheckEnd); + return true; + } + + // We are not at the end of the string, so we should roll back and continue as normal. + back(2); + } + // None of these are special in BRE. if (match(TokenType::Char) || match(TokenType::Questionmark) || match(TokenType::RightParen) || match(TokenType::HyphenMinus) || match(TokenType::Circumflex) || match(TokenType::RightCurly) || match(TokenType::Comma) || match(TokenType::Colon)