1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:57:35 +00:00

AK: Add GenericLexer API to consume an escaped Unicode code point

This parsing is already duplicated between LibJS and LibRegex, and will
shortly be needed in more places in those libraries. Move it to AK to
prevent further duplication.

This API will consume escaped Unicode code points of the form:
    \\u{code point}
    \\unnnn (where each n is a hexadecimal digit)
    \\unnnn\\unnnn (where the two escaped values are a surrogate pair)
This commit is contained in:
Timothy Flynn 2021-08-17 22:20:04 -04:00 committed by Andreas Kling
parent 02e3633b7f
commit fd8ccedf2b
3 changed files with 132 additions and 0 deletions

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/Result.h>
#include <AK/StringView.h>
namespace AK {
@ -115,6 +116,13 @@ public:
StringView consume_quoted_string(char escape_char = 0);
String consume_and_unescape_string(char escape_char = '\\');
enum class UnicodeEscapeError {
MalformedUnicodeEscape,
UnicodeEscapeOverflow,
};
Result<u32, UnicodeEscapeError> consume_escaped_code_point(bool combine_surrogate_pairs = true);
constexpr void ignore(size_t count = 1)
{
count = min(count, m_input.length() - m_index);
@ -201,6 +209,10 @@ public:
protected:
StringView m_input;
size_t m_index { 0 };
private:
Result<u32, UnicodeEscapeError> decode_code_point();
Result<u32, UnicodeEscapeError> decode_single_or_paired_surrogate(bool combine_surrogate_pairs);
};
constexpr auto is_any_of(const StringView& values)