mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40: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,21 +221,25 @@ String GenericLexer::consume_and_unescape_string(char escape_char)
|
||||||
if (view.is_null())
|
if (view.is_null())
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
// Transform common escape sequences
|
StringBuilder builder;
|
||||||
auto unescape_character = [](char c) {
|
for (size_t i = 0; i < view.length(); ++i)
|
||||||
static const char* escape_map = "n\nr\rt\tb\bf\f";
|
builder.append(consume_escaped_character(escape_char));
|
||||||
for (size_t i = 0; escape_map[i] != '\0'; i += 2)
|
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])
|
if (c == escape_map[i])
|
||||||
return escape_map[i + 1];
|
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);
|
|
||||||
}
|
}
|
||||||
return builder.to_string();
|
|
||||||
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ignore a number of characters (1 by default)
|
// Ignore a number of characters (1 by default)
|
||||||
|
|
|
@ -52,6 +52,7 @@ public:
|
||||||
bool consume_specific(char);
|
bool consume_specific(char);
|
||||||
bool consume_specific(StringView);
|
bool consume_specific(StringView);
|
||||||
bool consume_specific(const char*);
|
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(size_t count);
|
||||||
StringView consume_all();
|
StringView consume_all();
|
||||||
StringView consume_line();
|
StringView consume_line();
|
||||||
|
|
Loading…
Reference in a new issue