AK: Make String::contains(code_point) handle non-ASCII

We currently only accept a char, instead of a full code point.
This commit is contained in:
Timothy Flynn 2023-03-08 09:06:59 -05:00 committed by Linus Groh
parent f882581e91
commit 515fca4f7a
Notes: sideshowbarker 2024-07-16 23:59:28 +09:00
3 changed files with 50 additions and 3 deletions

View file

@ -489,9 +489,10 @@ bool String::contains(StringView needle, CaseSensitivity case_sensitivity) const
return StringUtils::contains(bytes_as_string_view(), needle, case_sensitivity); return StringUtils::contains(bytes_as_string_view(), needle, case_sensitivity);
} }
bool String::contains(char needle, CaseSensitivity case_sensitivity) const bool String::contains(u32 needle, CaseSensitivity case_sensitivity) const
{ {
return contains(StringView { &needle, 1 }, case_sensitivity); auto needle_as_string = String::from_code_point(needle);
return contains(needle_as_string.bytes_as_string_view(), case_sensitivity);
} }
bool String::starts_with(u32 code_point) const bool String::starts_with(u32 code_point) const

View file

@ -172,7 +172,7 @@ public:
} }
[[nodiscard]] bool contains(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const; [[nodiscard]] bool contains(StringView, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
[[nodiscard]] bool contains(char, CaseSensitivity = CaseSensitivity::CaseSensitive) const; [[nodiscard]] bool contains(u32, CaseSensitivity = CaseSensitivity::CaseSensitive) const;
[[nodiscard]] u32 hash() const; [[nodiscard]] u32 hash() const;

View file

@ -712,6 +712,52 @@ TEST_CASE(trim)
} }
} }
TEST_CASE(contains)
{
EXPECT(!String {}.contains({}));
EXPECT(!String {}.contains(" "sv));
EXPECT(!String {}.contains(0));
EXPECT("a"_short_string.contains("a"sv));
EXPECT(!"a"_short_string.contains({}));
EXPECT(!"a"_short_string.contains("b"sv));
EXPECT(!"a"_short_string.contains("ab"sv));
EXPECT("a"_short_string.contains(0x0061));
EXPECT(!"a"_short_string.contains(0x0062));
EXPECT("abc"_short_string.contains("a"sv));
EXPECT("abc"_short_string.contains("b"sv));
EXPECT("abc"_short_string.contains("c"sv));
EXPECT("abc"_short_string.contains("ab"sv));
EXPECT("abc"_short_string.contains("bc"sv));
EXPECT("abc"_short_string.contains("abc"sv));
EXPECT(!"abc"_short_string.contains({}));
EXPECT(!"abc"_short_string.contains("ac"sv));
EXPECT(!"abc"_short_string.contains("abcd"sv));
EXPECT("abc"_short_string.contains(0x0061));
EXPECT("abc"_short_string.contains(0x0062));
EXPECT("abc"_short_string.contains(0x0063));
EXPECT(!"abc"_short_string.contains(0x0064));
auto emoji = MUST("😀"_string);
EXPECT(emoji.contains("\xF0"sv));
EXPECT(emoji.contains("\x9F"sv));
EXPECT(emoji.contains("\x98"sv));
EXPECT(emoji.contains("\x80"sv));
EXPECT(emoji.contains("\xF0\x9F"sv));
EXPECT(emoji.contains("\xF0\x9F\x98"sv));
EXPECT(emoji.contains("\xF0\x9F\x98\x80"sv));
EXPECT(emoji.contains("\x9F\x98\x80"sv));
EXPECT(emoji.contains("\x98\x80"sv));
EXPECT(!emoji.contains("a"sv));
EXPECT(!emoji.contains("🙃"sv));
EXPECT(emoji.contains(0x1F600));
EXPECT(!emoji.contains(0x1F643));
}
TEST_CASE(starts_with) TEST_CASE(starts_with)
{ {
EXPECT(String {}.starts_with_bytes({})); EXPECT(String {}.starts_with_bytes({}));