AK: Add some more utility methods to Utf8View

This commit is contained in:
Sergey Bugaev 2019-09-04 23:40:36 +03:00 committed by Andreas Kling
parent 95fe775d81
commit c379f43d2a
Notes: sideshowbarker 2024-07-19 12:16:26 +09:00
2 changed files with 27 additions and 1 deletions

View file

@ -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,

View file

@ -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: