AK: Add Utf8View::code_point_offset_of()

This commit is contained in:
Jelle Raaijmakers 2025-06-11 16:56:42 +02:00 committed by Jelle Raaijmakers
commit 6f926e6977
Notes: github-actions[bot] 2025-06-13 13:10:13 +00:00
3 changed files with 27 additions and 0 deletions

View file

@ -30,6 +30,18 @@ Utf8CodePointIterator Utf8View::iterator_at_byte_offset_without_validation(size_
return Utf8CodePointIterator { reinterpret_cast<u8 const*>(m_string.characters_without_null_termination()) + byte_offset, m_string.length() - byte_offset };
}
size_t Utf8View::code_point_offset_of(size_t byte_offset) const
{
VERIFY(byte_offset < byte_length());
size_t code_point_offset = 0;
for (auto it = begin(); !it.done(); ++it) {
if (it.m_ptr > begin_ptr() + byte_offset)
break;
++code_point_offset;
}
return code_point_offset - 1;
}
size_t Utf8View::byte_offset_of(size_t code_point_offset) const
{
size_t byte_offset = 0;