mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:27:35 +00:00
AK: Add Utf8View::byte_offset_of overload for code point index lookups
This commit is contained in:
parent
a9716ad44e
commit
c4ee576531
2 changed files with 16 additions and 0 deletions
|
@ -65,6 +65,21 @@ size_t Utf8View::byte_offset_of(const Utf8CodePointIterator& it) const
|
||||||
return it.m_ptr - begin_ptr();
|
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
|
Utf8View Utf8View::substring_view(size_t byte_offset, size_t byte_length) const
|
||||||
{
|
{
|
||||||
StringView string = m_string.substring_view(byte_offset, byte_length);
|
StringView string = m_string.substring_view(byte_offset, byte_length);
|
||||||
|
|
|
@ -67,6 +67,7 @@ public:
|
||||||
const unsigned char* bytes() const { return begin_ptr(); }
|
const unsigned char* bytes() const { return begin_ptr(); }
|
||||||
size_t byte_length() const { return m_string.length(); }
|
size_t byte_length() const { return m_string.length(); }
|
||||||
size_t byte_offset_of(const Utf8CodePointIterator&) const;
|
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, size_t byte_length) const;
|
||||||
Utf8View substring_view(size_t byte_offset) const { return substring_view(byte_offset, byte_length() - byte_offset); }
|
Utf8View substring_view(size_t byte_offset) const { return substring_view(byte_offset, byte_length() - byte_offset); }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue