AK: Return NonnullRefPtr from StringImpl::create methods

None of these return a nullptr.
This commit is contained in:
Timothy Flynn 2025-04-06 09:53:04 -04:00
commit ecfba5d7fb
4 changed files with 24 additions and 24 deletions

View file

@ -59,7 +59,7 @@ ByteString ByteString::isolated_copy() const
char* buffer; char* buffer;
auto impl = StringImpl::create_uninitialized(length(), buffer); auto impl = StringImpl::create_uninitialized(length(), buffer);
memcpy(buffer, m_impl->characters(), m_impl->length()); memcpy(buffer, m_impl->characters(), m_impl->length());
return ByteString(move(*impl)); return impl;
} }
ByteString ByteString::substring(size_t start, size_t length) const ByteString ByteString::substring(size_t start, size_t length) const
@ -185,7 +185,7 @@ ByteString ByteString::repeated(char ch, size_t count)
char* buffer; char* buffer;
auto impl = StringImpl::create_uninitialized(count, buffer); auto impl = StringImpl::create_uninitialized(count, buffer);
memset(buffer, ch, count); memset(buffer, ch, count);
return *impl; return impl;
} }
ByteString ByteString::repeated(StringView string, size_t count) ByteString ByteString::repeated(StringView string, size_t count)
@ -196,7 +196,7 @@ ByteString ByteString::repeated(StringView string, size_t count)
auto impl = StringImpl::create_uninitialized(count * string.length(), buffer); auto impl = StringImpl::create_uninitialized(count * string.length(), buffer);
for (size_t i = 0; i < count; i++) for (size_t i = 0; i < count; i++)
__builtin_memcpy(buffer + i * string.length(), string.characters_without_null_termination(), string.length()); __builtin_memcpy(buffer + i * string.length(), string.characters_without_null_termination(), string.length());
return *impl; return impl;
} }
ByteString ByteString::bijective_base_from(size_t value, unsigned base, StringView map) ByteString ByteString::bijective_base_from(size_t value, unsigned base, StringView map)
@ -334,7 +334,7 @@ ByteString escape_html_entities(StringView html)
} }
ByteString::ByteString(FlyString const& string) ByteString::ByteString(FlyString const& string)
: m_impl(*StringImpl::create(string.bytes())) : m_impl(StringImpl::create(string.bytes()))
{ {
} }
@ -349,7 +349,7 @@ ByteString ByteString::to_lowercase() const
for (auto [i, character] : enumerate(view())) for (auto [i, character] : enumerate(view()))
buffer[i] = static_cast<char>(to_ascii_lowercase(character)); buffer[i] = static_cast<char>(to_ascii_lowercase(character));
return *impl; return impl;
} }
ByteString ByteString::to_uppercase() const ByteString ByteString::to_uppercase() const
@ -363,7 +363,7 @@ ByteString ByteString::to_uppercase() const
for (auto [i, character] : enumerate(view())) for (auto [i, character] : enumerate(view()))
buffer[i] = static_cast<char>(to_ascii_uppercase(character)); buffer[i] = static_cast<char>(to_ascii_uppercase(character));
return *impl; return impl;
} }
ByteString ByteString::to_snakecase() const ByteString ByteString::to_snakecase() const
@ -410,7 +410,7 @@ ErrorOr<ByteString> ByteString::from_utf8(ReadonlyBytes bytes)
{ {
if (!Utf8View(bytes).validate()) if (!Utf8View(bytes).validate())
return Error::from_string_literal("ByteString::from_utf8: Input was not valid UTF-8"); return Error::from_string_literal("ByteString::from_utf8: Input was not valid UTF-8");
return ByteString { *StringImpl::create(bytes) }; return StringImpl::create(bytes);
} }
} }

View file

@ -46,7 +46,7 @@ public:
} }
ByteString(StringView view) ByteString(StringView view)
: m_impl(*StringImpl::create(view.characters_without_null_termination(), view.length())) : m_impl(StringImpl::create(view.characters_without_null_termination(), view.length()))
{ {
} }
@ -62,17 +62,17 @@ public:
} }
ByteString(char const* cstring, ShouldChomp shouldChomp = NoChomp) ByteString(char const* cstring, ShouldChomp shouldChomp = NoChomp)
: m_impl(*StringImpl::create(cstring, shouldChomp)) : m_impl(StringImpl::create(cstring, shouldChomp))
{ {
} }
ByteString(char const* cstring, size_t length, ShouldChomp shouldChomp = NoChomp) ByteString(char const* cstring, size_t length, ShouldChomp shouldChomp = NoChomp)
: m_impl(*StringImpl::create(cstring, length, shouldChomp)) : m_impl(StringImpl::create(cstring, length, shouldChomp))
{ {
} }
explicit ByteString(ReadonlyBytes bytes, ShouldChomp shouldChomp = NoChomp) explicit ByteString(ReadonlyBytes bytes, ShouldChomp shouldChomp = NoChomp)
: m_impl(*StringImpl::create(bytes, shouldChomp)) : m_impl(StringImpl::create(bytes, shouldChomp))
{ {
} }
@ -82,7 +82,7 @@ public:
} }
ByteString(NonnullRefPtr<StringImpl const>&& impl) ByteString(NonnullRefPtr<StringImpl const>&& impl)
: m_impl(*move(impl)) : m_impl(move(impl))
{ {
} }
@ -264,7 +264,7 @@ public:
template<OneOf<ReadonlyBytes, Bytes> T> template<OneOf<ReadonlyBytes, Bytes> T>
ByteString& operator=(T bytes) ByteString& operator=(T bytes)
{ {
m_impl = *StringImpl::create(bytes); m_impl = StringImpl::create(bytes);
return *this; return *this;
} }

View file

@ -40,7 +40,7 @@ NonnullRefPtr<StringImpl const> StringImpl::create_uninitialized(size_t length,
return new_stringimpl; return new_stringimpl;
} }
RefPtr<StringImpl const> StringImpl::create(char const* cstring, size_t length, ShouldChomp should_chomp) NonnullRefPtr<StringImpl const> StringImpl::create(char const* cstring, size_t length, ShouldChomp should_chomp)
{ {
if (should_chomp) { if (should_chomp) {
while (length) { while (length) {
@ -62,7 +62,7 @@ RefPtr<StringImpl const> StringImpl::create(char const* cstring, size_t length,
return new_stringimpl; return new_stringimpl;
} }
RefPtr<StringImpl const> StringImpl::create(char const* cstring, ShouldChomp shouldChomp) NonnullRefPtr<StringImpl const> StringImpl::create(char const* cstring, ShouldChomp shouldChomp)
{ {
if (!cstring || !*cstring) if (!cstring || !*cstring)
return the_empty_stringimpl(); return the_empty_stringimpl();
@ -70,12 +70,12 @@ RefPtr<StringImpl const> StringImpl::create(char const* cstring, ShouldChomp sho
return create(cstring, strlen(cstring), shouldChomp); return create(cstring, strlen(cstring), shouldChomp);
} }
RefPtr<StringImpl const> StringImpl::create(ReadonlyBytes bytes, ShouldChomp shouldChomp) NonnullRefPtr<StringImpl const> StringImpl::create(ReadonlyBytes bytes, ShouldChomp shouldChomp)
{ {
return StringImpl::create(reinterpret_cast<char const*>(bytes.data()), bytes.size(), shouldChomp); return StringImpl::create(reinterpret_cast<char const*>(bytes.data()), bytes.size(), shouldChomp);
} }
RefPtr<StringImpl const> StringImpl::create_lowercased(char const* cstring, size_t length) NonnullRefPtr<StringImpl const> StringImpl::create_lowercased(char const* cstring, size_t length)
{ {
if (!length) if (!length)
return the_empty_stringimpl(); return the_empty_stringimpl();
@ -86,7 +86,7 @@ RefPtr<StringImpl const> StringImpl::create_lowercased(char const* cstring, size
return impl; return impl;
} }
RefPtr<StringImpl const> StringImpl::create_uppercased(char const* cstring, size_t length) NonnullRefPtr<StringImpl const> StringImpl::create_uppercased(char const* cstring, size_t length)
{ {
if (!length) if (!length)
return the_empty_stringimpl(); return the_empty_stringimpl();

View file

@ -7,8 +7,8 @@
#pragma once #pragma once
#include <AK/Badge.h> #include <AK/Badge.h>
#include <AK/NonnullRefPtr.h>
#include <AK/RefCounted.h> #include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/Span.h> #include <AK/Span.h>
#include <AK/Types.h> #include <AK/Types.h>
#include <AK/kmalloc.h> #include <AK/kmalloc.h>
@ -25,11 +25,11 @@ size_t allocation_size_for_stringimpl(size_t length);
class StringImpl : public RefCounted<StringImpl> { class StringImpl : public RefCounted<StringImpl> {
public: public:
static NonnullRefPtr<StringImpl const> create_uninitialized(size_t length, char*& buffer); static NonnullRefPtr<StringImpl const> create_uninitialized(size_t length, char*& buffer);
static RefPtr<StringImpl const> create(char const* cstring, ShouldChomp = NoChomp); static NonnullRefPtr<StringImpl const> create(char const* cstring, ShouldChomp = NoChomp);
static RefPtr<StringImpl const> create(char const* cstring, size_t length, ShouldChomp = NoChomp); static NonnullRefPtr<StringImpl const> create(char const* cstring, size_t length, ShouldChomp = NoChomp);
static RefPtr<StringImpl const> create(ReadonlyBytes, ShouldChomp = NoChomp); static NonnullRefPtr<StringImpl const> create(ReadonlyBytes, ShouldChomp = NoChomp);
static RefPtr<StringImpl const> create_lowercased(char const* cstring, size_t length); static NonnullRefPtr<StringImpl const> create_lowercased(char const* cstring, size_t length);
static RefPtr<StringImpl const> create_uppercased(char const* cstring, size_t length); static NonnullRefPtr<StringImpl const> create_uppercased(char const* cstring, size_t length);
void operator delete(void* ptr) void operator delete(void* ptr)
{ {