Skip to content

Commit 21d7d23

Browse files
trflynn89gmta
authored andcommitted
AK: Add a method to check if a UTF-16 string contains any code point
1 parent 8ada4b7 commit 21d7d23

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

AK/Utf16StringBase.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ class Utf16StringBase {
219219

220220
[[nodiscard]] ALWAYS_INLINE bool contains(char16_t needle) const { return find_code_unit_offset(needle).has_value(); }
221221
[[nodiscard]] ALWAYS_INLINE bool contains(Utf16View const& needle) const { return find_code_unit_offset(needle).has_value(); }
222+
[[nodiscard]] ALWAYS_INLINE bool contains_any_of(ReadonlySpan<u32> needles) const { return utf16_view().contains_any_of(needles); }
222223

223224
[[nodiscard]] ALWAYS_INLINE size_t count(Utf16View const& needle) const { return utf16_view().count(needle); }
224225

AK/Utf16View.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,16 @@ class Utf16View {
480480
[[nodiscard]] constexpr bool contains(char16_t needle) const { return find_code_unit_offset(needle).has_value(); }
481481
[[nodiscard]] constexpr bool contains(Utf16View const& needle) const { return find_code_unit_offset(needle).has_value(); }
482482

483+
[[nodiscard]] constexpr bool contains_any_of(ReadonlySpan<u32> needles) const
484+
{
485+
for (auto code_point : *this) {
486+
if (needles.contains_slow(code_point))
487+
return true;
488+
}
489+
490+
return false;
491+
}
492+
483493
[[nodiscard]] constexpr size_t count(Utf16View const& needle) const
484494
{
485495
if (needle.is_empty())

Tests/AK/TestUtf16View.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,28 @@ TEST_CASE(contains)
564564
EXPECT(u"ab😀"sv.contains(u"😀"sv));
565565
}
566566

567+
TEST_CASE(contains_any_of)
568+
{
569+
EXPECT(!u""sv.contains_any_of({}));
570+
EXPECT(!u"a"sv.contains_any_of({}));
571+
572+
EXPECT(u"a"sv.contains_any_of({ { 'a' } }));
573+
EXPECT(u"a"sv.contains_any_of({ { 'a', 'b' } }));
574+
EXPECT(u"b"sv.contains_any_of({ { 'a', 'b' } }));
575+
EXPECT(!u"a"sv.contains_any_of({ { 'b' } }));
576+
EXPECT(!u"b"sv.contains_any_of({ { 'a' } }));
577+
578+
EXPECT(u"ab"sv.contains_any_of({ { 'a' } }));
579+
EXPECT(u"ab"sv.contains_any_of({ { 'b' } }));
580+
EXPECT(u"ab"sv.contains_any_of({ { 'a', 'b' } }));
581+
EXPECT(!u"ab"sv.contains_any_of({ { 'c' } }));
582+
583+
EXPECT(!u"😀"sv.contains_any_of({ { 0xd83d } }));
584+
EXPECT(!u"😀"sv.contains_any_of({ { 0xde00 } }));
585+
EXPECT(u"😀"sv.contains_any_of({ { 0x1f600 } }));
586+
EXPECT(u"ab😀"sv.contains_any_of({ { 0x1f600 } }));
587+
}
588+
567589
TEST_CASE(count)
568590
{
569591
EXPECT_EQ(u""sv.count({}), 0uz);

0 commit comments

Comments
 (0)