mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 07:34:57 +00:00
AK: Implement StringView::to_{lower,upper}case_string
This patch refactors StringImpl::to_{lower,upper}case to use the new static methods StringImpl::create_{lower,upper}cased if they have to use to create a new StringImpl. This allows implementing StringView's to_{lower,upper}case_string using the same methods. It also replaces the usage of hand-written to_ascii_lowercase() and similar methods with those from CharacterTypes.h.
This commit is contained in:
parent
5ce9305c5f
commit
3ea65200d8
4 changed files with 41 additions and 36 deletions
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/CharacterTypes.h>
|
||||
#include <AK/FlyString.h>
|
||||
#include <AK/HashTable.h>
|
||||
#include <AK/Memory.h>
|
||||
|
@ -90,60 +91,48 @@ RefPtr<StringImpl> StringImpl::create(ReadonlyBytes bytes, ShouldChomp shouldCho
|
|||
return StringImpl::create(reinterpret_cast<const char*>(bytes.data()), bytes.size(), shouldChomp);
|
||||
}
|
||||
|
||||
static inline bool is_ascii_lowercase(char c)
|
||||
RefPtr<StringImpl> StringImpl::create_lowercased(char const* cstring, size_t length)
|
||||
{
|
||||
return c >= 'a' && c <= 'z';
|
||||
if (!cstring)
|
||||
return nullptr;
|
||||
if (!length)
|
||||
return the_empty_stringimpl();
|
||||
char* buffer;
|
||||
auto impl = create_uninitialized(length, buffer);
|
||||
for (size_t i = 0; i < length; ++i)
|
||||
buffer[i] = (char)to_ascii_lowercase(cstring[i]);
|
||||
return impl;
|
||||
}
|
||||
|
||||
static inline bool is_ascii_uppercase(char c)
|
||||
RefPtr<StringImpl> StringImpl::create_uppercased(char const* cstring, size_t length)
|
||||
{
|
||||
return c >= 'A' && c <= 'Z';
|
||||
}
|
||||
|
||||
static inline char to_ascii_lowercase(char c)
|
||||
{
|
||||
if (is_ascii_uppercase(c))
|
||||
return c | 0x20;
|
||||
return c;
|
||||
}
|
||||
|
||||
static inline char to_ascii_uppercase(char c)
|
||||
{
|
||||
if (is_ascii_lowercase(c))
|
||||
return c & ~0x20;
|
||||
return c;
|
||||
if (!cstring)
|
||||
return nullptr;
|
||||
if (!length)
|
||||
return the_empty_stringimpl();
|
||||
char* buffer;
|
||||
auto impl = create_uninitialized(length, buffer);
|
||||
for (size_t i = 0; i < length; ++i)
|
||||
buffer[i] = (char)to_ascii_uppercase(cstring[i]);
|
||||
return impl;
|
||||
}
|
||||
|
||||
NonnullRefPtr<StringImpl> StringImpl::to_lowercase() const
|
||||
{
|
||||
for (size_t i = 0; i < m_length; ++i) {
|
||||
if (!is_ascii_lowercase(characters()[i]))
|
||||
goto slow_path;
|
||||
if (is_ascii_upper_alpha(characters()[i]))
|
||||
return create_lowercased(characters(), m_length).release_nonnull();
|
||||
}
|
||||
return const_cast<StringImpl&>(*this);
|
||||
|
||||
slow_path:
|
||||
char* buffer;
|
||||
auto lowercased = create_uninitialized(m_length, buffer);
|
||||
for (size_t i = 0; i < m_length; ++i)
|
||||
buffer[i] = to_ascii_lowercase(characters()[i]);
|
||||
return lowercased;
|
||||
}
|
||||
|
||||
NonnullRefPtr<StringImpl> StringImpl::to_uppercase() const
|
||||
{
|
||||
for (size_t i = 0; i < m_length; ++i) {
|
||||
if (!is_ascii_uppercase(characters()[i]))
|
||||
goto slow_path;
|
||||
if (is_ascii_lower_alpha(characters()[i]))
|
||||
return create_uppercased(characters(), m_length).release_nonnull();
|
||||
}
|
||||
return const_cast<StringImpl&>(*this);
|
||||
|
||||
slow_path:
|
||||
char* buffer;
|
||||
auto uppercased = create_uninitialized(m_length, buffer);
|
||||
for (size_t i = 0; i < m_length; ++i)
|
||||
buffer[i] = to_ascii_uppercase(characters()[i]);
|
||||
return uppercased;
|
||||
}
|
||||
|
||||
void StringImpl::compute_hash() const
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue