diff --git a/AK/ByteString.cpp b/AK/ByteString.cpp index 4705e2d16ed..0ff35add19c 100644 --- a/AK/ByteString.cpp +++ b/AK/ByteString.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -339,12 +340,30 @@ ByteString::ByteString(FlyString const& string) ByteString ByteString::to_lowercase() const { - return m_impl->to_lowercase(); + if (!any_of(bytes(), is_ascii_upper_alpha)) + return *this; + + char* buffer = nullptr; + auto impl = StringImpl::create_uninitialized(length(), buffer); + + for (auto [i, character] : enumerate(view())) + buffer[i] = static_cast(to_ascii_lowercase(character)); + + return *impl; } ByteString ByteString::to_uppercase() const { - return m_impl->to_uppercase(); + if (!any_of(bytes(), is_ascii_lower_alpha)) + return *this; + + char* buffer = nullptr; + auto impl = StringImpl::create_uninitialized(length(), buffer); + + for (auto [i, character] : enumerate(view())) + buffer[i] = static_cast(to_ascii_uppercase(character)); + + return *impl; } ByteString ByteString::to_snakecase() const diff --git a/AK/String.cpp b/AK/String.cpp index 49f907600c8..034e8c7362d 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -374,49 +374,29 @@ ErrorOr 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 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 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); } diff --git a/AK/StringImpl.cpp b/AK/StringImpl.cpp index e245e5e9b7f..b0809690ca5 100644 --- a/AK/StringImpl.cpp +++ b/AK/StringImpl.cpp @@ -97,24 +97,6 @@ RefPtr StringImpl::create_uppercased(char const* cstring, size return impl; } -NonnullRefPtr StringImpl::to_lowercase() const -{ - for (size_t i = 0; i < m_length; ++i) { - if (is_ascii_upper_alpha(characters()[i])) - return create_lowercased(characters(), m_length).release_nonnull(); - } - return const_cast(*this); -} - -NonnullRefPtr StringImpl::to_uppercase() const -{ - for (size_t i = 0; i < m_length; ++i) { - if (is_ascii_lower_alpha(characters()[i])) - return create_uppercased(characters(), m_length).release_nonnull(); - } - return const_cast(*this); -} - unsigned StringImpl::case_insensitive_hash() const { return case_insensitive_string_hash(characters(), length()); diff --git a/AK/StringImpl.h b/AK/StringImpl.h index ed796ccd7c2..c5f4e18c62c 100644 --- a/AK/StringImpl.h +++ b/AK/StringImpl.h @@ -31,9 +31,6 @@ public: static RefPtr create_lowercased(char const* cstring, size_t length); static RefPtr create_uppercased(char const* cstring, size_t length); - NonnullRefPtr to_lowercase() const; - NonnullRefPtr to_uppercase() const; - void operator delete(void* ptr) { kfree_sized(ptr, allocation_size_for_stringimpl(static_cast(ptr)->m_length));