mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-29 04:09:13 +00:00
AK: Simplify ASCII case conversion implementations a bit
* Use `any_of` instead of manual loops * Don't check if a code point is upper/lowercase twice. The check we are using is already present inside the case converter. * Move StringImpl's implementation into ByteString. ByteString is its only user, so let's avoid some jumping around. The other ASCII case methods on StringImpl will soon also be removed.
This commit is contained in:
parent
a371f849e3
commit
13d7d3a60d
Notes:
github-actions[bot]
2025-04-07 15:46:17 +00:00
Author: https://github.com/trflynn89
Commit: 13d7d3a60d
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4253
4 changed files with 31 additions and 53 deletions
|
@ -374,49 +374,29 @@ ErrorOr<String> String::from_byte_string(ByteString const& byte_string)
|
|||
|
||||
String String::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)
|
||||
if (!any_of(bytes(), is_ascii_upper_alpha))
|
||||
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);
|
||||
}
|
||||
|
||||
for (auto character : bytes_as_string_view())
|
||||
lowercase_bytes.unchecked_append(AK::to_ascii_lowercase(character));
|
||||
|
||||
return String::from_utf8_without_validation(lowercase_bytes);
|
||||
}
|
||||
|
||||
String String::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)
|
||||
if (!any_of(bytes(), is_ascii_lower_alpha))
|
||||
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);
|
||||
}
|
||||
|
||||
for (auto character : bytes_as_string_view())
|
||||
uppercase_bytes.unchecked_append(AK::to_ascii_uppercase(character));
|
||||
|
||||
return String::from_utf8_without_validation(uppercase_bytes);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue