diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2020-10-19 10:05:20 +0330 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-10-22 23:49:51 +0200 |
commit | 27040e65eb9485eeccaa0235753032c935a3e890 (patch) | |
tree | a7e78f43603303a3ea5c44b1431ba02025956a83 /AK | |
parent | c057225a366a19eccab249068edcae5159088fce (diff) | |
download | serenity-27040e65eb9485eeccaa0235753032c935a3e890.zip |
AK: Add `GenericLexer::consume_escaped_character()'
...and use it in `consume_and_unescape_string()'.
Diffstat (limited to 'AK')
-rw-r--r-- | AK/GenericLexer.cpp | 30 | ||||
-rw-r--r-- | AK/GenericLexer.h | 1 |
2 files changed, 18 insertions, 13 deletions
diff --git a/AK/GenericLexer.cpp b/AK/GenericLexer.cpp index 582ccf9a45..565ecf3917 100644 --- a/AK/GenericLexer.cpp +++ b/AK/GenericLexer.cpp @@ -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) { diff --git a/AK/GenericLexer.h b/AK/GenericLexer.h index 0e0ffa9bc4..ce5d123f16 100644 --- a/AK/GenericLexer.h +++ b/AK/GenericLexer.h @@ -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(); |