diff --git a/AK/Utf8View.cpp b/AK/Utf8View.cpp index b3904a1eef..7e404522cd 100644 --- a/AK/Utf8View.cpp +++ b/AK/Utf8View.cpp @@ -2,6 +2,11 @@ namespace AK { +Utf8View::Utf8View(const String& string) + : m_string(string) +{ +} + Utf8View::Utf8View(const StringView& string) : m_string(string) { @@ -14,7 +19,7 @@ const unsigned char* Utf8View::begin_ptr() const const unsigned char* Utf8View::end_ptr() const { - return (const unsigned char*)m_string.characters_without_null_termination() + m_string.length(); + return begin_ptr() + m_string.length(); } Utf8CodepointIterator Utf8View::begin() const @@ -27,6 +32,20 @@ Utf8CodepointIterator Utf8View::end() const return { end_ptr(), 0 }; } +int Utf8View::byte_offset_of(const Utf8CodepointIterator& it) const +{ + ASSERT(it.m_ptr >= begin_ptr()); + ASSERT(it.m_ptr <= end_ptr()); + + return it.m_ptr - begin_ptr(); +} + +Utf8View Utf8View::substring_view(int byte_offset, int byte_length) const +{ + StringView string = m_string.substring_view(byte_offset, byte_length); + return Utf8View { string }; +} + static inline bool decode_first_byte( unsigned char byte, int& out_codepoint_length_in_bytes, diff --git a/AK/Utf8View.h b/AK/Utf8View.h index 016b3f81f2..73727af3f2 100644 --- a/AK/Utf8View.h +++ b/AK/Utf8View.h @@ -26,6 +26,7 @@ private: class Utf8View { public: + explicit Utf8View(const String&); explicit Utf8View(const StringView&); ~Utf8View() {} @@ -34,6 +35,12 @@ public: Utf8CodepointIterator begin() const; Utf8CodepointIterator end() const; + const unsigned char* bytes() const { return begin_ptr(); } + int byte_length() const { return m_string.length(); } + int byte_offset_of(const Utf8CodepointIterator&) const; + Utf8View substring_view(int byte_offset, int byte_length) const; + bool is_empty() const { return m_string.is_empty(); } + bool validate() const; private: