AK: Add Utf8View::byte_offset_of overload for code point index lookups

This commit is contained in:
Timothy Flynn 2021-08-16 10:27:26 -04:00 committed by Ali Mohammad Pur
parent a9716ad44e
commit c4ee576531
Notes: sideshowbarker 2024-07-18 05:34:04 +09:00
2 changed files with 16 additions and 0 deletions

View file

@ -65,6 +65,21 @@ size_t Utf8View::byte_offset_of(const Utf8CodePointIterator& it) const
return it.m_ptr - begin_ptr();
}
size_t Utf8View::byte_offset_of(size_t code_point_offset) const
{
size_t byte_offset = 0;
for (auto it = begin(); !it.done(); ++it) {
if (code_point_offset == 0)
return byte_offset;
byte_offset += it.underlying_code_point_length_in_bytes();
--code_point_offset;
}
return byte_offset;
}
Utf8View Utf8View::substring_view(size_t byte_offset, size_t byte_length) const
{
StringView string = m_string.substring_view(byte_offset, byte_length);

View file

@ -67,6 +67,7 @@ public:
const unsigned char* bytes() const { return begin_ptr(); }
size_t byte_length() const { return m_string.length(); }
size_t byte_offset_of(const Utf8CodePointIterator&) const;
size_t byte_offset_of(size_t code_point_offset) const;
Utf8View substring_view(size_t byte_offset, size_t byte_length) const;
Utf8View substring_view(size_t byte_offset) const { return substring_view(byte_offset, byte_length() - byte_offset); }