AK: Add a method to check if a UTF-16 string contains any code point

This commit is contained in:
Timothy Flynn 2025-07-28 09:44:06 -04:00 committed by Jelle Raaijmakers
parent 8ada4b7fdc
commit 21d7d236e6
3 changed files with 33 additions and 0 deletions

View File

@ -219,6 +219,7 @@ 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 bool contains_any_of(ReadonlySpan<u32> needles) const { return utf16_view().contains_any_of(needles); }
[[nodiscard]] ALWAYS_INLINE size_t count(Utf16View const& needle) const { return utf16_view().count(needle); }

View File

@ -480,6 +480,16 @@ 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 bool contains_any_of(ReadonlySpan<u32> needles) const
{
for (auto code_point : *this) {
if (needles.contains_slow(code_point))
return true;
}
return false;
}
[[nodiscard]] constexpr size_t count(Utf16View const& needle) const
{
if (needle.is_empty())

View File

@ -564,6 +564,28 @@ TEST_CASE(contains)
EXPECT(u"ab😀"sv.contains(u"😀"sv));
}
TEST_CASE(contains_any_of)
{
EXPECT(!u""sv.contains_any_of({}));
EXPECT(!u"a"sv.contains_any_of({}));
EXPECT(u"a"sv.contains_any_of({ { 'a' } }));
EXPECT(u"a"sv.contains_any_of({ { 'a', 'b' } }));
EXPECT(u"b"sv.contains_any_of({ { 'a', 'b' } }));
EXPECT(!u"a"sv.contains_any_of({ { 'b' } }));
EXPECT(!u"b"sv.contains_any_of({ { 'a' } }));
EXPECT(u"ab"sv.contains_any_of({ { 'a' } }));
EXPECT(u"ab"sv.contains_any_of({ { 'b' } }));
EXPECT(u"ab"sv.contains_any_of({ { 'a', 'b' } }));
EXPECT(!u"ab"sv.contains_any_of({ { 'c' } }));
EXPECT(!u"😀"sv.contains_any_of({ { 0xd83d } }));
EXPECT(!u"😀"sv.contains_any_of({ { 0xde00 } }));
EXPECT(u"😀"sv.contains_any_of({ { 0x1f600 } }));
EXPECT(u"ab😀"sv.contains_any_of({ { 0x1f600 } }));
}
TEST_CASE(count)
{
EXPECT_EQ(u""sv.count({}), 0uz);