1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:08:11 +00:00

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