diff --git a/AK/String.cpp b/AK/String.cpp index 034e8c7362d..7ec9c041888 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -377,13 +378,15 @@ String String::to_ascii_lowercase() const if (!any_of(bytes(), is_ascii_upper_alpha)) return *this; - Vector lowercase_bytes; - lowercase_bytes.ensure_capacity(bytes().size()); + String result; - for (auto character : bytes_as_string_view()) - lowercase_bytes.unchecked_append(AK::to_ascii_lowercase(character)); + MUST(result.replace_with_new_string(byte_count(), [&](Bytes buffer) -> ErrorOr { + for (auto [i, byte] : enumerate(bytes())) + buffer[i] = static_cast(AK::to_ascii_lowercase(byte)); + return {}; + })); - return String::from_utf8_without_validation(lowercase_bytes); + return result; } String String::to_ascii_uppercase() const @@ -391,13 +394,15 @@ String String::to_ascii_uppercase() const if (!any_of(bytes(), is_ascii_lower_alpha)) return *this; - Vector uppercase_bytes; - uppercase_bytes.ensure_capacity(bytes().size()); + String result; - for (auto character : bytes_as_string_view()) - uppercase_bytes.unchecked_append(AK::to_ascii_uppercase(character)); + MUST(result.replace_with_new_string(byte_count(), [&](Bytes buffer) -> ErrorOr { + for (auto [i, byte] : enumerate(bytes())) + buffer[i] = static_cast(AK::to_ascii_uppercase(byte)); + return {}; + })); - return String::from_utf8_without_validation(uppercase_bytes); + return result; } bool String::equals_ignoring_ascii_case(String const& other) const