AK: Implement a method to acquire a UTF-16 iterator's code unit offset

This is the same as Utf8View::iterator_offset().
This commit is contained in:
Timothy Flynn 2025-07-25 09:33:28 -04:00 committed by Jelle Raaijmakers
commit 745f288796
Notes: github-actions[bot] 2025-07-25 16:18:24 +00:00

View file

@ -341,6 +341,23 @@ public:
return { m_string.utf16 + length_in_code_units(), 0 };
}
[[nodiscard]] constexpr size_t iterator_offset(Utf16CodePointIterator const& it) const
{
if (has_ascii_storage()) {
VERIFY(it.has_ascii_storage());
VERIFY(it.m_iterator.ascii >= m_string.ascii);
VERIFY(it.m_iterator.ascii <= m_string.ascii);
return it.m_iterator.ascii - m_string.ascii;
}
VERIFY(!it.has_ascii_storage());
VERIFY(it.m_iterator.utf16 >= m_string.utf16);
VERIFY(it.m_iterator.utf16 <= m_string.utf16);
return it.m_iterator.utf16 - m_string.utf16;
}
Utf16String replace(Utf16View const& needle, Utf16View const& replacement, ReplaceMode) const;
Utf16String escape_html_entities() const;