mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-14 21:42:19 +00:00
AK: Implement a UTF-16 method to check if a string is ASCII whitespace
This commit is contained in:
parent
f53389bab1
commit
6c73dff120
Notes:
github-actions[bot]
2025-07-24 17:01:46 +00:00
Author: https://github.com/trflynn89
Commit: 6c73dff120
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5592
Reviewed-by: https://github.com/gmta ✅
3 changed files with 25 additions and 0 deletions
|
@ -156,6 +156,7 @@ public:
|
|||
|
||||
[[nodiscard]] ALWAYS_INLINE bool is_empty() const { return length_in_code_units() == 0uz; }
|
||||
[[nodiscard]] ALWAYS_INLINE bool is_ascii() const { return utf16_view().is_ascii(); }
|
||||
[[nodiscard]] ALWAYS_INLINE bool is_ascii_whitespace() const { return utf16_view().is_ascii_whitespace(); }
|
||||
|
||||
[[nodiscard]] ALWAYS_INLINE size_t length_in_code_units() const
|
||||
{
|
||||
|
|
|
@ -268,8 +268,16 @@ public:
|
|||
}
|
||||
|
||||
[[nodiscard]] constexpr bool is_empty() const { return length_in_code_units() == 0; }
|
||||
|
||||
[[nodiscard]] bool is_ascii() const;
|
||||
|
||||
[[nodiscard]] constexpr bool is_ascii_whitespace() const
|
||||
{
|
||||
if (has_ascii_storage())
|
||||
return all_of(ascii_span(), AK::is_ascii_space);
|
||||
return all_of(utf16_span(), AK::is_ascii_space);
|
||||
}
|
||||
|
||||
[[nodiscard]] ALWAYS_INLINE bool validate(AllowLonelySurrogates allow_lonely_surrogates = AllowLonelySurrogates::Yes) const
|
||||
{
|
||||
size_t valid_code_units = 0;
|
||||
|
|
|
@ -341,6 +341,22 @@ TEST_CASE(is_ascii)
|
|||
EXPECT(!u"The quick (“brown”) fox can’t jump 32.3 feet, right?"sv.is_ascii());
|
||||
}
|
||||
|
||||
TEST_CASE(is_ascii_whitespace)
|
||||
{
|
||||
EXPECT(Utf16View {}.is_ascii_whitespace());
|
||||
EXPECT(u" "sv.is_ascii_whitespace());
|
||||
EXPECT(u"\t"sv.is_ascii_whitespace());
|
||||
EXPECT(u"\r"sv.is_ascii_whitespace());
|
||||
EXPECT(u"\n"sv.is_ascii_whitespace());
|
||||
EXPECT(u" \t\r\n\v "sv.is_ascii_whitespace());
|
||||
|
||||
EXPECT(!u"a"sv.is_ascii_whitespace());
|
||||
EXPECT(!u"😀"sv.is_ascii_whitespace());
|
||||
EXPECT(!u"\u00a0"sv.is_ascii_whitespace());
|
||||
EXPECT(!u"\ufeff"sv.is_ascii_whitespace());
|
||||
EXPECT(!u" \t \u00a0 \ufeff "sv.is_ascii_whitespace());
|
||||
}
|
||||
|
||||
TEST_CASE(to_ascii_lowercase)
|
||||
{
|
||||
EXPECT_EQ(u""sv.to_ascii_lowercase(), u""sv);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue