mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
AK: Add `GenericLexer::consume_escaped_character()'
...and use it in `consume_and_unescape_string()'.
This commit is contained in:
parent
c057225a36
commit
27040e65eb
Notes:
sideshowbarker
2024-07-19 01:47:49 +09:00
Author: https://github.com/alimpfard Commit: https://github.com/SerenityOS/serenity/commit/27040e65eb9 Pull-request: https://github.com/SerenityOS/serenity/pull/3797 Reviewed-by: https://github.com/benit8
2 changed files with 18 additions and 13 deletions
|
@ -221,23 +221,27 @@ String GenericLexer::consume_and_unescape_string(char escape_char)
|
|||
if (view.is_null())
|
||||
return {};
|
||||
|
||||
// Transform common escape sequences
|
||||
auto unescape_character = [](char c) {
|
||||
static const char* escape_map = "n\nr\rt\tb\bf\f";
|
||||
for (size_t i = 0; escape_map[i] != '\0'; i += 2)
|
||||
if (c == escape_map[i])
|
||||
return escape_map[i + 1];
|
||||
return c;
|
||||
};
|
||||
|
||||
StringBuilder builder;
|
||||
for (size_t i = 0; i < view.length(); ++i) {
|
||||
char c = (view[i] == escape_char) ? unescape_character(view[++i]) : view[i];
|
||||
builder.append(c);
|
||||
}
|
||||
for (size_t i = 0; i < view.length(); ++i)
|
||||
builder.append(consume_escaped_character(escape_char));
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
char GenericLexer::consume_escaped_character(char escape_char, const StringView& escape_map)
|
||||
{
|
||||
if (!consume_specific(escape_char))
|
||||
return consume();
|
||||
|
||||
auto c = consume();
|
||||
|
||||
for (size_t i = 0; i < escape_map.length(); i += 2) {
|
||||
if (c == escape_map[i])
|
||||
return escape_map[i + 1];
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
// Ignore a number of characters (1 by default)
|
||||
void GenericLexer::ignore(size_t count)
|
||||
{
|
||||
|
|
|
@ -52,6 +52,7 @@ public:
|
|||
bool consume_specific(char);
|
||||
bool consume_specific(StringView);
|
||||
bool consume_specific(const char*);
|
||||
char consume_escaped_character(char escape_char = '\\', const StringView& escape_map = "n\nr\rt\tb\bf\f");
|
||||
StringView consume(size_t count);
|
||||
StringView consume_all();
|
||||
StringView consume_line();
|
||||
|
|
Loading…
Reference in a new issue