mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +00:00
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:
parent
f882581e91
commit
515fca4f7a
Notes:
sideshowbarker
2024-07-16 23:59:28 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/515fca4f7a Pull-request: https://github.com/SerenityOS/serenity/pull/17766 Reviewed-by: https://github.com/linusg ✅
3 changed files with 50 additions and 3 deletions
|
@ -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
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
|
|
@ -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({}));
|
||||||
|
|
Loading…
Reference in a new issue