AK: Implement a method to count instances of a needle in a UTF-16 string

This commit is contained in:
Timothy Flynn 2025-07-26 09:59:47 -04:00 committed by Andreas Kling
commit 48a3b2c28e
Notes: github-actions[bot] 2025-07-28 10:27:19 +00:00
3 changed files with 40 additions and 0 deletions

View file

@ -220,6 +220,8 @@ public:
[[nodiscard]] ALWAYS_INLINE bool contains(char16_t needle) const { return find_code_unit_offset(needle).has_value(); }
[[nodiscard]] ALWAYS_INLINE bool contains(Utf16View const& needle) const { return find_code_unit_offset(needle).has_value(); }
[[nodiscard]] ALWAYS_INLINE size_t count(Utf16View const& needle) const { return utf16_view().count(needle); }
[[nodiscard]] ALWAYS_INLINE bool starts_with(Utf16View const& needle) const { return utf16_view().starts_with(needle); }
[[nodiscard]] ALWAYS_INLINE bool ends_with(Utf16View const& needle) const { return utf16_view().ends_with(needle); }

View file

@ -479,6 +479,20 @@ public:
[[nodiscard]] constexpr bool contains(char16_t needle) const { return find_code_unit_offset(needle).has_value(); }
[[nodiscard]] constexpr bool contains(Utf16View const& needle) const { return find_code_unit_offset(needle).has_value(); }
[[nodiscard]] constexpr size_t count(Utf16View const& needle) const
{
if (needle.is_empty())
return length_in_code_units();
size_t count = 0;
for (size_t i = 0; i < length_in_code_units() - needle.length_in_code_units() + 1; ++i) {
if (substring_view(i).starts_with(needle))
++count;
}
return count;
}
[[nodiscard]] constexpr bool starts_with(Utf16View const& needle) const
{
auto needle_length = needle.length_in_code_units();

View file

@ -558,6 +558,30 @@ TEST_CASE(contains)
EXPECT(u"ab😀"sv.contains(u"😀"sv));
}
TEST_CASE(count)
{
EXPECT_EQ(u""sv.count({}), 0uz);
EXPECT_EQ(u"abc"sv.count({}), 3uz);
EXPECT_EQ(u""sv.count(u"a"sv), 0uz);
EXPECT_EQ(u"abc"sv.count(u"a"sv), 1uz);
EXPECT_EQ(u"abc"sv.count(u"b"sv), 1uz);
EXPECT_EQ(u"abc"sv.count(u"c"sv), 1uz);
EXPECT_EQ(u"abc"sv.count(u"ab"sv), 1uz);
EXPECT_EQ(u"abc"sv.count(u"bc"sv), 1uz);
EXPECT_EQ(u"abc"sv.count(u"abc"sv), 1uz);
EXPECT_EQ(u"abc"sv.count(u"d"sv), 0uz);
EXPECT_EQ(u"aaaa"sv.count(u"aa"sv), 3uz);
EXPECT_EQ(u"😀"sv.count({}), 2uz);
EXPECT_EQ(u"😀"sv.count(u"\xd83d"sv), 1uz);
EXPECT_EQ(u"😀"sv.count(u"\xde00"sv), 1uz);
EXPECT_EQ(u"😀"sv.count(u"😀"sv), 1uz);
EXPECT_EQ(u"😀😀😀"sv.count(u"😀"sv), 3uz);
EXPECT_EQ(u"😀😀😀"sv.count(u"😀😀"sv), 2uz);
}
TEST_CASE(starts_with)
{
EXPECT(Utf16View {}.starts_with(u""sv));