1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-11 10:18:15 +09:00

AK+LibWeb: Add {Fly,}String::to_ascii_{upper,lower}_case()

These don't have to worry about the input not being valid UTF-8 and
so can be infallible (and can even return self if no changes needed.)

We use this instead of Infra::to_ascii_{upper,lower}_case in LibWeb.
This commit is contained in:
Andreas Kling 2024-10-14 10:51:15 +02:00 committed by Andreas Kling
parent dd419b5a8d
commit 073bcfd386
Notes: github-actions[bot] 2024-10-14 18:49:00 +00:00
16 changed files with 147 additions and 13 deletions

View file

@ -173,6 +173,54 @@ ErrorOr<void> Formatter<FlyString>::format(FormatBuilder& builder, FlyString con
return Formatter<StringView>::format(builder, fly_string.bytes_as_string_view());
}
FlyString FlyString::to_ascii_lowercase() const
{
bool const has_ascii_uppercase = [&] {
for (u8 const byte : bytes()) {
if (AK::is_ascii_upper_alpha(byte))
return true;
}
return false;
}();
if (!has_ascii_uppercase)
return *this;
Vector<u8> lowercase_bytes;
lowercase_bytes.ensure_capacity(bytes().size());
for (u8 const byte : bytes()) {
if (AK::is_ascii_upper_alpha(byte))
lowercase_bytes.unchecked_append(AK::to_ascii_lowercase(byte));
else
lowercase_bytes.unchecked_append(byte);
}
return String::from_utf8_without_validation(lowercase_bytes);
}
FlyString FlyString::to_ascii_uppercase() const
{
bool const has_ascii_lowercase = [&] {
for (u8 const byte : bytes()) {
if (AK::is_ascii_lower_alpha(byte))
return true;
}
return false;
}();
if (!has_ascii_lowercase)
return *this;
Vector<u8> uppercase_bytes;
uppercase_bytes.ensure_capacity(bytes().size());
for (u8 const byte : bytes()) {
if (AK::is_ascii_lower_alpha(byte))
uppercase_bytes.unchecked_append(AK::to_ascii_uppercase(byte));
else
uppercase_bytes.unchecked_append(byte);
}
return String::from_utf8_without_validation(uppercase_bytes);
}
bool FlyString::equals_ignoring_ascii_case(FlyString const& other) const
{
if (*this == other)