AK: Rename StringImpl to ByteStringImpl

StringImpl is very specific to ByteString. Let's rename it to match, to
avoid confusion with the StringBase and StringData classes.
This commit is contained in:
Timothy Flynn 2025-04-06 10:26:37 -04:00 committed by Andreas Kling
parent 0a256b0a9a
commit 3f439efe21
Notes: github-actions[bot] 2025-04-07 15:45:41 +00:00
7 changed files with 58 additions and 59 deletions

View file

@ -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<char>(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<char>(to_ascii_uppercase(character));
@ -400,7 +400,7 @@ ErrorOr<ByteString> 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);
}
}

View file

@ -6,11 +6,11 @@
#pragma once
#include <AK/ByteStringImpl.h>
#include <AK/Format.h>
#include <AK/Forward.h>
#include <AK/RefPtr.h>
#include <AK/StringBuilder.h>
#include <AK/StringImpl.h>
#include <AK/StringUtils.h>
#include <AK/Traits.h>
@ -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<StringImpl const>&& impl)
ByteString(NonnullRefPtr<ByteStringImpl const>&& 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<StringImpl const> impl() const { return m_impl; }
[[nodiscard]] NonnullRefPtr<ByteStringImpl const> impl() const { return m_impl; }
ByteString& operator=(ByteString&& other)
{
@ -262,7 +262,7 @@ public:
template<OneOf<ReadonlyBytes, Bytes> T>
ByteString& operator=(T bytes)
{
m_impl = StringImpl::create(bytes);
m_impl = ByteStringImpl::create(bytes);
return *this;
}
@ -322,7 +322,7 @@ public:
}
private:
NonnullRefPtr<StringImpl const> m_impl;
NonnullRefPtr<ByteStringImpl const> m_impl;
};
template<>

View file

@ -4,43 +4,43 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ByteStringImpl.h>
#include <AK/CharacterTypes.h>
#include <AK/StringHash.h>
#include <AK/StringImpl.h>
#include <AK/kmalloc.h>
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 const> StringImpl::create_uninitialized(size_t length, char*& buffer)
NonnullRefPtr<ByteStringImpl const> 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<char*>(new_stringimpl->characters());
buffer[length] = '\0';
return new_stringimpl;
}
NonnullRefPtr<StringImpl const> StringImpl::create(char const* cstring, size_t length, ShouldChomp should_chomp)
NonnullRefPtr<ByteStringImpl const> ByteStringImpl::create(char const* cstring, size_t length, ShouldChomp should_chomp)
{
if (should_chomp) {
while (length) {
@ -62,7 +62,7 @@ NonnullRefPtr<StringImpl const> StringImpl::create(char const* cstring, size_t l
return new_stringimpl;
}
NonnullRefPtr<StringImpl const> StringImpl::create(char const* cstring, ShouldChomp shouldChomp)
NonnullRefPtr<ByteStringImpl const> ByteStringImpl::create(char const* cstring, ShouldChomp shouldChomp)
{
if (!cstring || !*cstring)
return the_empty_stringimpl();
@ -70,17 +70,17 @@ NonnullRefPtr<StringImpl const> StringImpl::create(char const* cstring, ShouldCh
return create(cstring, strlen(cstring), shouldChomp);
}
NonnullRefPtr<StringImpl const> StringImpl::create(ReadonlyBytes bytes, ShouldChomp shouldChomp)
NonnullRefPtr<ByteStringImpl const> ByteStringImpl::create(ReadonlyBytes bytes, ShouldChomp shouldChomp)
{
return StringImpl::create(reinterpret_cast<char const*>(bytes.data()), bytes.size(), shouldChomp);
return ByteStringImpl::create(reinterpret_cast<char const*>(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;

View file

@ -22,21 +22,21 @@ enum ShouldChomp {
size_t allocation_size_for_stringimpl(size_t length);
class StringImpl : public RefCounted<StringImpl> {
class ByteStringImpl : public RefCounted<ByteStringImpl> {
public:
static NonnullRefPtr<StringImpl const> create_uninitialized(size_t length, char*& buffer);
static NonnullRefPtr<StringImpl const> create(char const* cstring, ShouldChomp = NoChomp);
static NonnullRefPtr<StringImpl const> create(char const* cstring, size_t length, ShouldChomp = NoChomp);
static NonnullRefPtr<StringImpl const> create(ReadonlyBytes, ShouldChomp = NoChomp);
static NonnullRefPtr<ByteStringImpl const> create_uninitialized(size_t length, char*& buffer);
static NonnullRefPtr<ByteStringImpl const> create(char const* cstring, ShouldChomp = NoChomp);
static NonnullRefPtr<ByteStringImpl const> create(char const* cstring, size_t length, ShouldChomp = NoChomp);
static NonnullRefPtr<ByteStringImpl const> create(ReadonlyBytes, ShouldChomp = NoChomp);
void operator delete(void* ptr)
{
kfree_sized(ptr, allocation_size_for_stringimpl(static_cast<StringImpl*>(ptr)->m_length));
kfree_sized(ptr, allocation_size_for_stringimpl(static_cast<ByteStringImpl*>(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<StringImpl> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, StringImpl const& value)
struct Formatter<ByteStringImpl> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, ByteStringImpl const& value)
{
return Formatter<StringView>::format(builder, { value.characters(), value.length() });
}
@ -112,5 +112,4 @@ struct Formatter<StringImpl> : Formatter<StringView> {
#if USING_AK_GLOBALLY
using AK::Chomp;
using AK::NoChomp;
using AK::StringImpl;
#endif

View file

@ -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

View file

@ -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<typename T>
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;

View file

@ -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",