mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-29 06:02:07 +03:00
AK: Add Utf8View::byte_offset_of overload for code point index lookups
This commit is contained in:
parent
a9716ad44e
commit
c4ee576531
Notes:
sideshowbarker
2024-07-18 05:34:04 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/c4ee5765312 Pull-request: https://github.com/SerenityOS/serenity/pull/9453
@ -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);
|
||||
|
@ -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); }
|
||||
|
Loading…
Reference in New Issue
Block a user