From d49d2c7ec4cbe2c0792cf112055a6f5eca79dd63 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Mon, 24 Jan 2022 23:25:53 +0200 Subject: [PATCH] AK: Add a consume_until(StringView) overload to GenericLexer This allows us to skip a strlen call. --- AK/GenericLexer.cpp | 16 ++++++++++++++++ AK/GenericLexer.h | 1 + Userland/Libraries/LibCpp/Preprocessor.cpp | 2 +- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/AK/GenericLexer.cpp b/AK/GenericLexer.cpp index 7a0372d6a0..05bd6ebb95 100644 --- a/AK/GenericLexer.cpp +++ b/AK/GenericLexer.cpp @@ -84,6 +84,22 @@ StringView GenericLexer::consume_until(const char* stop) return m_input.substring_view(start, length); } +// Consume and return characters until the string `stop` is found +// The `stop` string is ignored, as it is user-defined +StringView GenericLexer::consume_until(StringView stop) +{ + size_t start = m_index; + while (!is_eof() && !next_is(stop)) + m_index++; + size_t length = m_index - start; + + ignore(stop.length()); + + if (length == 0) + return {}; + return m_input.substring_view(start, length); +} + /* * Consume a string surrounded by single or double quotes. The returned * StringView does not include the quotes. An escape character can be provided diff --git a/AK/GenericLexer.h b/AK/GenericLexer.h index 0a44bffc65..37604d2bd5 100644 --- a/AK/GenericLexer.h +++ b/AK/GenericLexer.h @@ -113,6 +113,7 @@ public: StringView consume_line(); StringView consume_until(char); StringView consume_until(const char*); + StringView consume_until(StringView); StringView consume_quoted_string(char escape_char = 0); String consume_and_unescape_string(char escape_char = '\\'); diff --git a/Userland/Libraries/LibCpp/Preprocessor.cpp b/Userland/Libraries/LibCpp/Preprocessor.cpp index 9cf3cd82fb..f1ade8f27b 100644 --- a/Userland/Libraries/LibCpp/Preprocessor.cpp +++ b/Userland/Libraries/LibCpp/Preprocessor.cpp @@ -356,7 +356,7 @@ String Preprocessor::remove_escaped_newlines(StringView value) AK::StringBuilder processed_value; GenericLexer lexer { value }; while (!lexer.is_eof()) { - processed_value.append(lexer.consume_until("\\\n")); + processed_value.append(lexer.consume_until("\\\n"sv)); } return processed_value.to_string(); }