diff --git a/AK/ByteString.cpp b/AK/ByteString.cpp index ef4cdb3487d..97464213f93 100644 --- a/AK/ByteString.cpp +++ b/AK/ByteString.cpp @@ -57,7 +57,7 @@ ByteString ByteString::isolated_copy() const if (m_impl->length() == 0) return empty(); char* buffer; - auto impl = StringImpl::create_uninitialized(length(), buffer); + auto impl = ByteStringImpl::create_uninitialized(length(), buffer); memcpy(buffer, m_impl->characters(), m_impl->length()); return impl; } @@ -183,7 +183,7 @@ ByteString ByteString::repeated(char ch, size_t count) if (!count) return empty(); char* buffer; - auto impl = StringImpl::create_uninitialized(count, buffer); + auto impl = ByteStringImpl::create_uninitialized(count, buffer); memset(buffer, ch, count); return impl; } @@ -193,7 +193,7 @@ ByteString ByteString::repeated(StringView string, size_t count) if (!count || string.is_empty()) return empty(); char* buffer; - auto impl = StringImpl::create_uninitialized(count * string.length(), buffer); + auto impl = ByteStringImpl::create_uninitialized(count * string.length(), buffer); for (size_t i = 0; i < count; i++) __builtin_memcpy(buffer + i * string.length(), string.characters_without_null_termination(), string.length()); return impl; @@ -334,7 +334,7 @@ ByteString escape_html_entities(StringView html) } ByteString::ByteString(FlyString const& string) - : m_impl(StringImpl::create(string.bytes())) + : m_impl(ByteStringImpl::create(string.bytes())) { } @@ -344,7 +344,7 @@ ByteString ByteString::to_lowercase() const return *this; char* buffer = nullptr; - auto impl = StringImpl::create_uninitialized(length(), buffer); + auto impl = ByteStringImpl::create_uninitialized(length(), buffer); for (auto [i, character] : enumerate(view())) buffer[i] = static_cast(to_ascii_lowercase(character)); @@ -358,7 +358,7 @@ ByteString ByteString::to_uppercase() const return *this; char* buffer = nullptr; - auto impl = StringImpl::create_uninitialized(length(), buffer); + auto impl = ByteStringImpl::create_uninitialized(length(), buffer); for (auto [i, character] : enumerate(view())) buffer[i] = static_cast(to_ascii_uppercase(character)); @@ -400,7 +400,7 @@ ErrorOr ByteString::from_utf8(ReadonlyBytes bytes) { if (!Utf8View(bytes).validate()) return Error::from_string_literal("ByteString::from_utf8: Input was not valid UTF-8"); - return StringImpl::create(bytes); + return ByteStringImpl::create(bytes); } } diff --git a/AK/ByteString.h b/AK/ByteString.h index d8850c1303f..80a2d9ca513 100644 --- a/AK/ByteString.h +++ b/AK/ByteString.h @@ -6,11 +6,11 @@ #pragma once +#include #include #include #include #include -#include #include #include @@ -41,12 +41,12 @@ public: ~ByteString() = default; ByteString() - : m_impl(StringImpl::the_empty_stringimpl()) + : m_impl(ByteStringImpl::the_empty_stringimpl()) { } ByteString(StringView view) - : m_impl(StringImpl::create(view.characters_without_null_termination(), view.length())) + : m_impl(ByteStringImpl::create(view.characters_without_null_termination(), view.length())) { } @@ -58,30 +58,30 @@ public: ByteString(ByteString&& other) : m_impl(move(other.m_impl)) { - other.m_impl = StringImpl::the_empty_stringimpl(); + other.m_impl = ByteStringImpl::the_empty_stringimpl(); } ByteString(char const* cstring, ShouldChomp shouldChomp = NoChomp) - : m_impl(StringImpl::create(cstring, shouldChomp)) + : m_impl(ByteStringImpl::create(cstring, shouldChomp)) { } ByteString(char const* cstring, size_t length, ShouldChomp shouldChomp = NoChomp) - : m_impl(StringImpl::create(cstring, length, shouldChomp)) + : m_impl(ByteStringImpl::create(cstring, length, shouldChomp)) { } explicit ByteString(ReadonlyBytes bytes, ShouldChomp shouldChomp = NoChomp) - : m_impl(StringImpl::create(bytes, shouldChomp)) + : m_impl(ByteStringImpl::create(bytes, shouldChomp)) { } - ByteString(StringImpl const& impl) + ByteString(ByteStringImpl const& impl) : m_impl(impl) { } - ByteString(NonnullRefPtr&& impl) + ByteString(NonnullRefPtr&& impl) : m_impl(move(impl)) { } @@ -101,7 +101,7 @@ public: static ReturnType create_and_overwrite(size_t length, F&& fill_function) { char* buffer; - auto impl = StringImpl::create_uninitialized(length, buffer); + auto impl = ByteStringImpl::create_uninitialized(length, buffer); if constexpr (is_error_or) TRY(fill_function(Bytes { buffer, length })); @@ -240,10 +240,10 @@ public: [[nodiscard]] static ByteString empty() { - return StringImpl::the_empty_stringimpl(); + return ByteStringImpl::the_empty_stringimpl(); } - [[nodiscard]] NonnullRefPtr impl() const { return m_impl; } + [[nodiscard]] NonnullRefPtr impl() const { return m_impl; } ByteString& operator=(ByteString&& other) { @@ -262,7 +262,7 @@ public: template T> ByteString& operator=(T bytes) { - m_impl = StringImpl::create(bytes); + m_impl = ByteStringImpl::create(bytes); return *this; } @@ -322,7 +322,7 @@ public: } private: - NonnullRefPtr m_impl; + NonnullRefPtr m_impl; }; template<> diff --git a/AK/StringImpl.cpp b/AK/ByteStringImpl.cpp similarity index 54% rename from AK/StringImpl.cpp rename to AK/ByteStringImpl.cpp index d7af5a11757..47fb3683a3c 100644 --- a/AK/StringImpl.cpp +++ b/AK/ByteStringImpl.cpp @@ -4,43 +4,43 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include -#include #include namespace AK { -static StringImpl* s_the_empty_stringimpl = nullptr; +static ByteStringImpl* s_the_empty_stringimpl = nullptr; -StringImpl& StringImpl::the_empty_stringimpl() +ByteStringImpl& ByteStringImpl::the_empty_stringimpl() { if (!s_the_empty_stringimpl) { - void* slot = kmalloc(sizeof(StringImpl) + sizeof(char)); - s_the_empty_stringimpl = new (slot) StringImpl(ConstructTheEmptyStringImpl); + void* slot = kmalloc(sizeof(ByteStringImpl) + sizeof(char)); + s_the_empty_stringimpl = new (slot) ByteStringImpl(ConstructTheEmptyStringImpl); } return *s_the_empty_stringimpl; } -StringImpl::StringImpl(ConstructWithInlineBufferTag, size_t length) +ByteStringImpl::ByteStringImpl(ConstructWithInlineBufferTag, size_t length) : m_length(length) { } -StringImpl::~StringImpl() = default; +ByteStringImpl::~ByteStringImpl() = default; -NonnullRefPtr StringImpl::create_uninitialized(size_t length, char*& buffer) +NonnullRefPtr ByteStringImpl::create_uninitialized(size_t length, char*& buffer) { VERIFY(length); void* slot = kmalloc(allocation_size_for_stringimpl(length)); VERIFY(slot); - auto new_stringimpl = adopt_ref(*new (slot) StringImpl(ConstructWithInlineBuffer, length)); + auto new_stringimpl = adopt_ref(*new (slot) ByteStringImpl(ConstructWithInlineBuffer, length)); buffer = const_cast(new_stringimpl->characters()); buffer[length] = '\0'; return new_stringimpl; } -NonnullRefPtr StringImpl::create(char const* cstring, size_t length, ShouldChomp should_chomp) +NonnullRefPtr ByteStringImpl::create(char const* cstring, size_t length, ShouldChomp should_chomp) { if (should_chomp) { while (length) { @@ -62,7 +62,7 @@ NonnullRefPtr StringImpl::create(char const* cstring, size_t l return new_stringimpl; } -NonnullRefPtr StringImpl::create(char const* cstring, ShouldChomp shouldChomp) +NonnullRefPtr ByteStringImpl::create(char const* cstring, ShouldChomp shouldChomp) { if (!cstring || !*cstring) return the_empty_stringimpl(); @@ -70,17 +70,17 @@ NonnullRefPtr StringImpl::create(char const* cstring, ShouldCh return create(cstring, strlen(cstring), shouldChomp); } -NonnullRefPtr StringImpl::create(ReadonlyBytes bytes, ShouldChomp shouldChomp) +NonnullRefPtr ByteStringImpl::create(ReadonlyBytes bytes, ShouldChomp shouldChomp) { - return StringImpl::create(reinterpret_cast(bytes.data()), bytes.size(), shouldChomp); + return ByteStringImpl::create(reinterpret_cast(bytes.data()), bytes.size(), shouldChomp); } -unsigned StringImpl::case_insensitive_hash() const +unsigned ByteStringImpl::case_insensitive_hash() const { return case_insensitive_string_hash(characters(), length()); } -void StringImpl::compute_hash() const +void ByteStringImpl::compute_hash() const { if (!length()) m_hash = 0; diff --git a/AK/StringImpl.h b/AK/ByteStringImpl.h similarity index 67% rename from AK/StringImpl.h rename to AK/ByteStringImpl.h index 95dcc5148cb..484de6e9187 100644 --- a/AK/StringImpl.h +++ b/AK/ByteStringImpl.h @@ -22,21 +22,21 @@ enum ShouldChomp { size_t allocation_size_for_stringimpl(size_t length); -class StringImpl : public RefCounted { +class ByteStringImpl : public RefCounted { public: - static NonnullRefPtr create_uninitialized(size_t length, char*& buffer); - static NonnullRefPtr create(char const* cstring, ShouldChomp = NoChomp); - static NonnullRefPtr create(char const* cstring, size_t length, ShouldChomp = NoChomp); - static NonnullRefPtr create(ReadonlyBytes, ShouldChomp = NoChomp); + static NonnullRefPtr create_uninitialized(size_t length, char*& buffer); + static NonnullRefPtr create(char const* cstring, ShouldChomp = NoChomp); + static NonnullRefPtr create(char const* cstring, size_t length, ShouldChomp = NoChomp); + static NonnullRefPtr create(ReadonlyBytes, ShouldChomp = NoChomp); void operator delete(void* ptr) { - kfree_sized(ptr, allocation_size_for_stringimpl(static_cast(ptr)->m_length)); + kfree_sized(ptr, allocation_size_for_stringimpl(static_cast(ptr)->m_length)); } - static StringImpl& the_empty_stringimpl(); + static ByteStringImpl& the_empty_stringimpl(); - ~StringImpl(); + ~ByteStringImpl(); size_t length() const { return m_length; } // Includes NUL-terminator. @@ -51,7 +51,7 @@ public: return characters()[i]; } - bool operator==(StringImpl const& other) const + bool operator==(ByteStringImpl const& other) const { if (length() != other.length()) return false; @@ -76,7 +76,7 @@ private: enum ConstructTheEmptyStringImplTag { ConstructTheEmptyStringImpl }; - explicit StringImpl(ConstructTheEmptyStringImplTag) + explicit ByteStringImpl(ConstructTheEmptyStringImplTag) { m_inline_buffer[0] = '\0'; } @@ -84,7 +84,7 @@ private: enum ConstructWithInlineBufferTag { ConstructWithInlineBuffer }; - StringImpl(ConstructWithInlineBufferTag, size_t length); + ByteStringImpl(ConstructWithInlineBufferTag, size_t length); void compute_hash() const; @@ -96,12 +96,12 @@ private: inline size_t allocation_size_for_stringimpl(size_t length) { - return sizeof(StringImpl) + (sizeof(char) * length) + sizeof(char); + return sizeof(ByteStringImpl) + (sizeof(char) * length) + sizeof(char); } template<> -struct Formatter : Formatter { - ErrorOr format(FormatBuilder& builder, StringImpl const& value) +struct Formatter : Formatter { + ErrorOr format(FormatBuilder& builder, ByteStringImpl const& value) { return Formatter::format(builder, { value.characters(), value.length() }); } @@ -112,5 +112,4 @@ struct Formatter : Formatter { #if USING_AK_GLOBALLY using AK::Chomp; using AK::NoChomp; -using AK::StringImpl; #endif diff --git a/AK/CMakeLists.txt b/AK/CMakeLists.txt index 874f6adeed5..df6a6e219cf 100644 --- a/AK/CMakeLists.txt +++ b/AK/CMakeLists.txt @@ -1,11 +1,12 @@ set(SOURCES Assertions.cpp Base64.cpp + ByteString.cpp + ByteStringImpl.cpp CircularBuffer.cpp ConstrainedStream.cpp CountingStream.cpp DOSPackedTime.cpp - ByteString.cpp Error.cpp FloatingPointStringConversions.cpp FlyString.cpp @@ -26,7 +27,6 @@ set(SOURCES StringBase.cpp StringBuilder.cpp StringFloatingPointConversions.cpp - StringImpl.cpp StringUtils.cpp StringView.cpp Time.cpp diff --git a/AK/Forward.h b/AK/Forward.h index d67db621370..5c4efd1c2f9 100644 --- a/AK/Forward.h +++ b/AK/Forward.h @@ -25,11 +25,11 @@ enum class TrailingCodePointTransformation : u8; class BigEndianInputBitStream; class BigEndianOutputBitStream; class Bitmap; -using ByteBuffer = Detail::ByteBuffer<32>; +class ByteString; +class ByteStringImpl; class CircularBuffer; class ConstrainedStream; class CountingStream; -class ByteString; class Duration; class Error; class FlyString; @@ -48,7 +48,6 @@ class StackInfo; class Stream; class String; class StringBuilder; -class StringImpl; class StringView; class UnixDateTime; class Utf16View; @@ -57,6 +56,8 @@ class Utf32View; class Utf8CodePointIterator; class Utf8View; +using ByteBuffer = Detail::ByteBuffer<32>; + template class Span; @@ -191,7 +192,6 @@ using AK::StackInfo; using AK::Stream; using AK::String; using AK::StringBuilder; -using AK::StringImpl; using AK::StringView; using AK::TrailingCodePointTransformation; using AK::Traits; diff --git a/Meta/gn/secondary/AK/BUILD.gn b/Meta/gn/secondary/AK/BUILD.gn index 6dc7eda568e..9cf2b15ed56 100644 --- a/Meta/gn/secondary/AK/BUILD.gn +++ b/Meta/gn/secondary/AK/BUILD.gn @@ -43,6 +43,8 @@ shared_library("AK") { "ByteReader.h", "ByteString.cpp", "ByteString.h", + "ByteStringImpl.cpp", + "ByteStringImpl.h", "COWVector.h", "CharacterTypes.h", "Checked.h", @@ -175,8 +177,6 @@ shared_library("AK") { "StringFloatingPointConversions.cpp", "StringFloatingPointConversions.h", "StringHash.h", - "StringImpl.cpp", - "StringImpl.h", "StringUtils.cpp", "StringUtils.h", "StringView.cpp",