mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-28 13:18:19 +00:00
AK: Add a Utf16View method to retrieve an iterator at a code unit offset
This commit is contained in:
parent
4aa355658f
commit
bbda6d13f7
Notes:
github-actions[bot]
2025-08-07 00:07:51 +00:00
Author: https://github.com/trflynn89
Commit: bbda6d13f7
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5746
Reviewed-by: https://github.com/gmta ✅
2 changed files with 21 additions and 0 deletions
|
@ -425,6 +425,15 @@ public:
|
|||
return it.m_iterator.utf16 - m_string.utf16;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr Utf16CodePointIterator iterator_at_code_unit_offset(size_t code_unit_offset) const
|
||||
{
|
||||
VERIFY(code_unit_offset <= length_in_code_units());
|
||||
|
||||
if (has_ascii_storage())
|
||||
return { m_string.ascii + code_unit_offset, length_in_code_units() - code_unit_offset };
|
||||
return { m_string.utf16 + code_unit_offset, length_in_code_units() - code_unit_offset };
|
||||
}
|
||||
|
||||
Utf16String replace(Utf16View const& needle, Utf16View const& replacement, ReplaceMode) const;
|
||||
Utf16String escape_html_entities() const;
|
||||
|
||||
|
|
|
@ -571,6 +571,18 @@ TEST_CASE(iterator_offset)
|
|||
EXPECT_EQ(view.iterator_offset(view.end()), view.length_in_code_units());
|
||||
}
|
||||
|
||||
TEST_CASE(iterator_at_code_unit_offset)
|
||||
{
|
||||
Utf16View view { u"😂 foo 😀 bar"sv };
|
||||
|
||||
for (size_t i = 0; i < view.length_in_code_units(); ++i) {
|
||||
auto it = view.iterator_at_code_unit_offset(i);
|
||||
EXPECT_EQ(*it, view.code_point_at(i));
|
||||
}
|
||||
|
||||
EXPECT_EQ(view.iterator_at_code_unit_offset(view.length_in_code_units()), view.end());
|
||||
}
|
||||
|
||||
TEST_CASE(replace)
|
||||
{
|
||||
auto result = u""sv.replace({}, {}, ReplaceMode::FirstOnly);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue