1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:37:45 +00:00

AK: Enhance GenericLexer's string consumption

The `consume_quoted_string()` can now take an escape character. This allows it
(for example) to capture a string's enclosing quotes. The escape character is
optional by default.

You can also consume and unescape a quoted string with the eponymous method
`consume_and_unescape_string()`. It takes an escape character as parameter
(backslash by default). It builds a String in which common escape sequences
get... unescaped :^) (e.g. \n, \r, \t...).
This commit is contained in:
Benoit Lormeau 2020-09-26 12:21:14 +02:00 committed by Andreas Kling
parent 1ab6dd67e9
commit 8f34b493e4
2 changed files with 44 additions and 8 deletions

View file

@ -27,6 +27,7 @@
#pragma once
#include <AK/Function.h>
#include <AK/String.h>
#include <AK/StringView.h>
namespace AK {
@ -36,7 +37,7 @@ public:
explicit GenericLexer(const StringView& input);
virtual ~GenericLexer();
// A lambda/function can be used to match characters as the user pleases
// A lambda/function can be used to match characters as the user pleases
using Condition = Function<bool(char)>;
size_t tell() const { return m_index; }
@ -64,8 +65,8 @@ public:
StringView consume_until(char);
StringView consume_until(const char*);
StringView consume_until(Condition);
// FIXME: provide an escape character
StringView consume_quoted_string();
StringView consume_quoted_string(char escape_char = 0);
String consume_and_unescape_string(char escape_char = '\\');
void ignore(size_t count = 1);
void ignore_while(Condition);