2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2024-10-04 08:19:50 -03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <andreas@ladybird.org>
|
2023-08-11 18:38:01 -03:00
|
|
|
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2020-11-15 09:11:21 -03:00
|
|
|
#include <AK/ByteBuffer.h>
|
2024-06-17 19:12:53 -03:00
|
|
|
#include <AK/ByteString.h>
|
2020-11-24 18:04:22 -03:00
|
|
|
#include <AK/Checked.h>
|
2024-06-17 19:12:53 -03:00
|
|
|
#include <AK/FlyString.h>
|
AK: Introduce the new String, replacement for DeprecatedString
DeprecatedString (formerly String) has been with us since the start,
and it has served us well. However, it has a number of shortcomings
that I'd like to address.
Some of these issues are hard if not impossible to solve incrementally
inside of DeprecatedString, so instead of doing that, let's build a new
String class and then incrementally move over to it instead.
Problems in DeprecatedString:
- It assumes string allocation never fails. This makes it impossible
to use in allocation-sensitive contexts, and is the reason we had to
ban DeprecatedString from the kernel entirely.
- The awkward null state. DeprecatedString can be null. It's different
from the empty state, although null strings are considered empty.
All code is immediately nicer when using Optional<DeprecatedString>
but DeprecatedString came before Optional, which is how we ended up
like this.
- The encoding of the underlying data is ambiguous. For the most part,
we use it as if it's always UTF-8, but there have been cases where
we pass around strings in other encodings (e.g ISO8859-1)
- operator[] and length() are used to iterate over DeprecatedString one
byte at a time. This is done all over the codebase, and will *not*
give the right results unless the string is all ASCII.
How we solve these issues in the new String:
- Functions that may allocate now return ErrorOr<String> so that ENOMEM
errors can be passed to the caller.
- String has no null state. Use Optional<String> when needed.
- String is always UTF-8. This is validated when constructing a String.
We may need to add a bypass for this in the future, for cases where
you have a known-good string, but for now: validate all the things!
- There is no operator[] or length(). You can get the underlying data
with bytes(), but for iterating over code points, you should be using
an UTF-8 iterator.
Furthermore, it has two nifty new features:
- String implements a small string optimization (SSO) for strings that
can fit entirely within a pointer. This means up to 3 bytes on 32-bit
platforms, and 7 bytes on 64-bit platforms. Such small strings will
not be heap-allocated.
- String can create substrings without making a deep copy of the
substring. Instead, the superstring gets +1 refcount from the
substring, and it acts like a view into the superstring. To make
substrings like this, use the substring_with_shared_superstring() API.
One caveat:
- String does not guarantee that the underlying data is null-terminated
like DeprecatedString does today. While this was nifty in a handful of
places where we were calling C functions, it did stand in the way of
shared-superstring substrings.
2022-12-01 09:27:43 -03:00
|
|
|
#include <AK/String.h>
|
2020-03-08 08:34:33 -03:00
|
|
|
#include <AK/StringBuilder.h>
|
2024-07-19 16:38:41 -03:00
|
|
|
#include <AK/StringData.h>
|
2020-03-23 09:45:10 -03:00
|
|
|
#include <AK/StringView.h>
|
2021-10-11 02:08:08 -03:00
|
|
|
#include <AK/UnicodeUtils.h>
|
2025-06-17 17:08:30 -03:00
|
|
|
#include <AK/Utf16String.h>
|
|
|
|
|
#include <AK/Utf16StringData.h>
|
2024-06-17 19:12:53 -03:00
|
|
|
#include <AK/Utf16View.h>
|
2026-06-10 14:14:13 -03:00
|
|
|
#include <AK/kmalloc.h>
|
2018-10-10 06:53:07 -03:00
|
|
|
|
2024-10-26 06:05:31 -03:00
|
|
|
#include <simdutf.h>
|
|
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
namespace AK {
|
|
|
|
|
|
2026-06-10 14:14:13 -03:00
|
|
|
StringBuilder::Buffer::Buffer(Buffer const& other)
|
|
|
|
|
{
|
|
|
|
|
MUST(try_resize(other.size()));
|
|
|
|
|
if (other.size() != 0)
|
|
|
|
|
__builtin_memcpy(data(), other.data(), other.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringBuilder::Buffer::Buffer(Buffer&& other)
|
|
|
|
|
{
|
|
|
|
|
move_from(move(other));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringBuilder::Buffer::~Buffer()
|
|
|
|
|
{
|
|
|
|
|
clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto StringBuilder::Buffer::operator=(Buffer const& other) -> Buffer&
|
|
|
|
|
{
|
|
|
|
|
if (this != &other) {
|
|
|
|
|
if (m_size > other.size())
|
|
|
|
|
trim(other.size(), true);
|
|
|
|
|
else
|
|
|
|
|
MUST(try_resize(other.size()));
|
|
|
|
|
|
|
|
|
|
if (other.size() != 0)
|
|
|
|
|
__builtin_memcpy(data(), other.data(), other.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto StringBuilder::Buffer::operator=(Buffer&& other) -> Buffer&
|
|
|
|
|
{
|
|
|
|
|
if (this != &other) {
|
|
|
|
|
clear();
|
|
|
|
|
move_from(move(other));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u8* StringBuilder::Buffer::data()
|
|
|
|
|
{
|
|
|
|
|
return m_inline ? m_inline_buffer : m_outline_buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u8 const* StringBuilder::Buffer::data() const
|
|
|
|
|
{
|
|
|
|
|
return m_inline ? m_inline_buffer : m_outline_buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Bytes StringBuilder::Buffer::span()
|
|
|
|
|
{
|
|
|
|
|
return { data(), size() };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ReadonlyBytes StringBuilder::Buffer::span() const
|
|
|
|
|
{
|
|
|
|
|
return { data(), size() };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void* StringBuilder::Buffer::end_pointer()
|
|
|
|
|
{
|
|
|
|
|
return data() + m_size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StringBuilder::Buffer::clear()
|
|
|
|
|
{
|
|
|
|
|
if (!m_inline) {
|
|
|
|
|
kfree(m_outline_buffer);
|
|
|
|
|
m_inline = true;
|
|
|
|
|
}
|
|
|
|
|
m_size = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StringBuilder::Buffer::resize(size_t new_size)
|
|
|
|
|
{
|
|
|
|
|
MUST(try_resize(new_size));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StringBuilder::Buffer::set_size(size_t new_size)
|
|
|
|
|
{
|
|
|
|
|
ASSERT(new_size <= capacity());
|
|
|
|
|
m_size = new_size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StringBuilder::Buffer::ensure_capacity(size_t new_capacity)
|
|
|
|
|
{
|
|
|
|
|
MUST(try_ensure_capacity(new_capacity));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorOr<void> StringBuilder::Buffer::try_resize(size_t new_size)
|
|
|
|
|
{
|
|
|
|
|
if (new_size <= m_size) {
|
|
|
|
|
trim(new_size, false);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TRY(try_ensure_capacity(new_size));
|
|
|
|
|
set_size(new_size);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorOr<void> StringBuilder::Buffer::try_ensure_capacity(size_t new_capacity)
|
|
|
|
|
{
|
|
|
|
|
if (new_capacity <= capacity())
|
|
|
|
|
return {};
|
|
|
|
|
return try_ensure_capacity_slowpath(new_capacity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorOr<void> StringBuilder::Buffer::try_append(char byte)
|
|
|
|
|
{
|
|
|
|
|
auto old_size = size();
|
|
|
|
|
Checked<size_t> new_size = old_size;
|
|
|
|
|
new_size += 1;
|
|
|
|
|
VERIFY(!new_size.has_overflow());
|
|
|
|
|
|
|
|
|
|
TRY(try_resize(new_size.value()));
|
|
|
|
|
data()[old_size] = static_cast<u8>(byte);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorOr<void> StringBuilder::Buffer::try_append(ReadonlyBytes bytes)
|
|
|
|
|
{
|
|
|
|
|
return try_append(bytes.data(), bytes.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorOr<void> StringBuilder::Buffer::try_append(void const* data, size_t data_size)
|
|
|
|
|
{
|
|
|
|
|
if (data_size == 0)
|
|
|
|
|
return {};
|
|
|
|
|
VERIFY(data != nullptr);
|
|
|
|
|
|
|
|
|
|
auto old_size = size();
|
|
|
|
|
Checked<size_t> new_size = old_size;
|
|
|
|
|
new_size += data_size;
|
|
|
|
|
VERIFY(!new_size.has_overflow());
|
|
|
|
|
|
|
|
|
|
TRY(try_resize(new_size.value()));
|
|
|
|
|
__builtin_memcpy(this->data() + old_size, data, data_size);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StringBuilder::Buffer::append(char byte)
|
|
|
|
|
{
|
|
|
|
|
MUST(try_append(byte));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StringBuilder::Buffer::append(void const* data, size_t data_size)
|
|
|
|
|
{
|
|
|
|
|
MUST(try_append(data, data_size));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto StringBuilder::Buffer::leak_outline_buffer() -> Optional<OutlineBuffer>
|
|
|
|
|
{
|
|
|
|
|
if (m_inline)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
auto* outline_buffer = m_outline_buffer;
|
|
|
|
|
auto size = m_size;
|
|
|
|
|
auto outline_capacity = m_outline_capacity;
|
|
|
|
|
|
|
|
|
|
m_inline = true;
|
|
|
|
|
m_size = 0;
|
|
|
|
|
|
|
|
|
|
return OutlineBuffer { Bytes { outline_buffer, size }, outline_capacity };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StringBuilder::Buffer::move_from(Buffer&& other)
|
|
|
|
|
{
|
|
|
|
|
m_size = other.m_size;
|
|
|
|
|
m_inline = other.m_inline;
|
|
|
|
|
|
|
|
|
|
if (other.m_inline) {
|
|
|
|
|
VERIFY(other.m_size <= inline_capacity);
|
|
|
|
|
if (other.m_size != 0)
|
|
|
|
|
__builtin_memcpy(m_inline_buffer, other.m_inline_buffer, other.m_size);
|
|
|
|
|
} else {
|
|
|
|
|
m_outline_buffer = other.m_outline_buffer;
|
|
|
|
|
m_outline_capacity = other.m_outline_capacity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
other.m_size = 0;
|
|
|
|
|
other.m_inline = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StringBuilder::Buffer::trim(size_t size, bool may_discard_existing_data)
|
|
|
|
|
{
|
|
|
|
|
VERIFY(size <= m_size);
|
|
|
|
|
if (!m_inline && size <= inline_capacity)
|
|
|
|
|
shrink_into_inline_buffer(size, may_discard_existing_data);
|
|
|
|
|
m_size = size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StringBuilder::Buffer::shrink_into_inline_buffer(size_t size, bool may_discard_existing_data)
|
|
|
|
|
{
|
|
|
|
|
auto* outline_buffer = m_outline_buffer;
|
|
|
|
|
if (!may_discard_existing_data)
|
|
|
|
|
__builtin_memcpy(m_inline_buffer, outline_buffer, size);
|
|
|
|
|
kfree(outline_buffer);
|
|
|
|
|
m_inline = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorOr<void> StringBuilder::Buffer::try_ensure_capacity_slowpath(size_t new_capacity)
|
|
|
|
|
{
|
|
|
|
|
new_capacity = max(new_capacity, (capacity() * 3) / 2);
|
|
|
|
|
new_capacity = kmalloc_good_size(new_capacity);
|
|
|
|
|
|
|
|
|
|
if (m_inline) {
|
|
|
|
|
auto* new_buffer = static_cast<u8*>(kmalloc(HeapPartition::String, new_capacity));
|
|
|
|
|
if (!new_buffer)
|
|
|
|
|
return Error::from_errno(ENOMEM);
|
|
|
|
|
|
|
|
|
|
__builtin_memcpy(new_buffer, data(), m_size);
|
|
|
|
|
m_outline_buffer = new_buffer;
|
|
|
|
|
} else {
|
|
|
|
|
auto* new_buffer = static_cast<u8*>(krealloc(HeapPartition::String, m_outline_buffer, new_capacity));
|
|
|
|
|
if (!new_buffer)
|
|
|
|
|
return Error::from_errno(ENOMEM);
|
|
|
|
|
|
|
|
|
|
m_outline_buffer = new_buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_outline_capacity = new_capacity;
|
|
|
|
|
m_inline = false;
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-22 09:28:38 -03:00
|
|
|
static constexpr size_t string_builder_prefix_size()
|
2025-06-17 17:08:30 -03:00
|
|
|
{
|
2026-06-22 09:28:38 -03:00
|
|
|
return sizeof(Detail::StringData);
|
2025-06-17 17:08:30 -03:00
|
|
|
}
|
2024-07-19 16:38:41 -03:00
|
|
|
|
2026-06-22 09:28:38 -03:00
|
|
|
void StringBuilder::initialize_buffer(size_t capacity)
|
2024-07-19 16:38:41 -03:00
|
|
|
{
|
2026-06-22 09:28:38 -03:00
|
|
|
auto prefix_size = string_builder_prefix_size();
|
2024-07-19 16:38:41 -03:00
|
|
|
if (capacity > StringBuilder::inline_capacity)
|
2025-10-04 05:38:38 -03:00
|
|
|
m_buffer.ensure_capacity(prefix_size + capacity);
|
|
|
|
|
m_buffer.resize(prefix_size);
|
2024-07-19 16:38:41 -03:00
|
|
|
}
|
|
|
|
|
|
2025-01-18 12:39:34 -03:00
|
|
|
StringBuilder::StringBuilder()
|
|
|
|
|
{
|
2026-06-22 09:28:38 -03:00
|
|
|
static constexpr auto prefix_size = string_builder_prefix_size();
|
2025-06-17 17:08:30 -03:00
|
|
|
static_assert(inline_capacity > prefix_size);
|
|
|
|
|
|
2026-06-22 09:28:38 -03:00
|
|
|
initialize_buffer(inline_capacity);
|
2025-01-18 12:39:34 -03:00
|
|
|
}
|
|
|
|
|
|
2019-12-09 13:45:40 -03:00
|
|
|
StringBuilder::StringBuilder(size_t initial_capacity)
|
2024-07-19 16:38:41 -03:00
|
|
|
{
|
2026-06-22 09:28:38 -03:00
|
|
|
initialize_buffer(initial_capacity);
|
2025-06-17 17:08:30 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline ErrorOr<void> StringBuilder::will_append(size_t size_in_bytes)
|
2023-08-11 18:38:01 -03:00
|
|
|
{
|
2024-07-19 10:41:05 -03:00
|
|
|
Checked<size_t> needed_capacity = m_buffer.size();
|
2025-06-17 17:08:30 -03:00
|
|
|
needed_capacity += size_in_bytes;
|
2024-07-19 10:41:05 -03:00
|
|
|
VERIFY(!needed_capacity.has_overflow());
|
|
|
|
|
// Prefer to completely use the existing capacity first
|
|
|
|
|
if (needed_capacity <= m_buffer.capacity())
|
|
|
|
|
return {};
|
|
|
|
|
Checked<size_t> expanded_capacity = needed_capacity;
|
|
|
|
|
expanded_capacity *= 2;
|
|
|
|
|
VERIFY(!expanded_capacity.has_overflow());
|
|
|
|
|
TRY(m_buffer.try_ensure_capacity(expanded_capacity.value()));
|
|
|
|
|
return {};
|
2023-08-11 18:38:01 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t StringBuilder::length() const
|
|
|
|
|
{
|
2026-06-22 09:28:38 -03:00
|
|
|
return m_buffer.size() - string_builder_prefix_size();
|
2023-08-11 18:38:01 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool StringBuilder::is_empty() const
|
|
|
|
|
{
|
2024-07-19 16:38:41 -03:00
|
|
|
return length() == 0;
|
2023-08-11 18:38:01 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StringBuilder::trim(size_t count)
|
|
|
|
|
{
|
|
|
|
|
auto decrease_count = min(m_buffer.size(), count);
|
|
|
|
|
m_buffer.resize(m_buffer.size() - decrease_count);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-15 20:47:54 -03:00
|
|
|
ErrorOr<void> StringBuilder::try_append(StringView string)
|
2018-11-18 11:57:41 -02:00
|
|
|
{
|
2021-11-15 20:47:54 -03:00
|
|
|
if (string.is_empty())
|
|
|
|
|
return {};
|
2025-06-17 17:08:30 -03:00
|
|
|
|
2026-06-22 09:28:38 -03:00
|
|
|
TRY(will_append(string.length()));
|
|
|
|
|
TRY(m_buffer.try_append(string.characters_without_null_termination(), string.length()));
|
2021-11-15 20:47:54 -03:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-04 05:26:22 -03:00
|
|
|
void StringBuilder::append_ascii_without_validation(ReadonlyBytes string)
|
|
|
|
|
{
|
|
|
|
|
MUST(try_append_ascii_without_validation(string));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorOr<void> StringBuilder::try_append_ascii_without_validation(ReadonlyBytes string)
|
|
|
|
|
{
|
|
|
|
|
if (string.is_empty())
|
|
|
|
|
return {};
|
|
|
|
|
|
2026-06-22 09:28:38 -03:00
|
|
|
TRY(will_append(string.size()));
|
|
|
|
|
TRY(m_buffer.try_append(string));
|
2025-10-04 05:26:22 -03:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-15 20:47:54 -03:00
|
|
|
ErrorOr<void> StringBuilder::try_append(char ch)
|
|
|
|
|
{
|
2026-06-22 09:28:38 -03:00
|
|
|
TRY(will_append(1));
|
|
|
|
|
TRY(m_buffer.try_append(ch));
|
2025-06-17 17:08:30 -03:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorOr<void> StringBuilder::try_append_code_unit(char16_t ch)
|
|
|
|
|
{
|
2026-06-22 09:28:38 -03:00
|
|
|
return try_append_code_point(ch);
|
2021-11-15 20:47:54 -03:00
|
|
|
}
|
|
|
|
|
|
2022-01-04 19:37:15 -03:00
|
|
|
ErrorOr<void> StringBuilder::try_append_repeated(char ch, size_t n)
|
|
|
|
|
{
|
2026-06-22 09:28:38 -03:00
|
|
|
TRY(will_append(n));
|
2025-08-08 16:41:31 -03:00
|
|
|
|
2022-01-04 19:37:15 -03:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
|
TRY(try_append(ch));
|
2025-08-08 16:41:31 -03:00
|
|
|
|
2022-01-04 19:37:15 -03:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 03:26:12 -03:00
|
|
|
ErrorOr<void> StringBuilder::try_append_repeated(StringView string, size_t n)
|
|
|
|
|
{
|
|
|
|
|
if (string.is_empty())
|
|
|
|
|
return {};
|
2025-07-25 10:07:22 -03:00
|
|
|
|
2026-06-22 09:28:38 -03:00
|
|
|
TRY(will_append(string.length() * n));
|
2025-07-25 10:07:22 -03:00
|
|
|
|
2024-10-22 03:26:12 -03:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
|
TRY(try_append(string));
|
2025-07-25 10:07:22 -03:00
|
|
|
|
2024-10-22 03:26:12 -03:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 10:24:19 -03:00
|
|
|
ErrorOr<void> StringBuilder::try_append_repeated(Utf16View const& string, size_t n)
|
|
|
|
|
{
|
|
|
|
|
if (string.is_empty())
|
|
|
|
|
return {};
|
|
|
|
|
|
2026-06-22 09:28:38 -03:00
|
|
|
if (string.has_ascii_storage()) {
|
|
|
|
|
TRY(will_append(string.length_in_code_units() * n));
|
2025-07-25 10:24:19 -03:00
|
|
|
} else {
|
2026-06-22 09:28:38 -03:00
|
|
|
auto utf8_length = simdutf::utf8_length_from_utf16(string.utf16_span().data(), string.length_in_code_units());
|
|
|
|
|
TRY(will_append(utf8_length * n));
|
2025-07-25 10:24:19 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
|
TRY(try_append(string));
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-15 20:47:54 -03:00
|
|
|
void StringBuilder::append(StringView string)
|
|
|
|
|
{
|
|
|
|
|
MUST(try_append(string));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorOr<void> StringBuilder::try_append(char const* characters, size_t length)
|
|
|
|
|
{
|
|
|
|
|
return try_append(StringView { characters, length });
|
2018-11-18 11:57:41 -02:00
|
|
|
}
|
|
|
|
|
|
2021-08-09 12:48:50 -03:00
|
|
|
void StringBuilder::append(char const* characters, size_t length)
|
2019-01-28 19:55:55 -02:00
|
|
|
{
|
2021-11-15 20:47:54 -03:00
|
|
|
MUST(try_append(characters, length));
|
2019-01-28 19:55:55 -02:00
|
|
|
}
|
|
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
void StringBuilder::append(char ch)
|
|
|
|
|
{
|
2021-11-15 20:47:54 -03:00
|
|
|
MUST(try_append(ch));
|
2018-10-10 06:53:07 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-17 17:08:30 -03:00
|
|
|
void StringBuilder::append_code_unit(char16_t ch)
|
|
|
|
|
{
|
|
|
|
|
MUST(try_append_code_unit(ch));
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 19:37:15 -03:00
|
|
|
void StringBuilder::append_repeated(char ch, size_t n)
|
|
|
|
|
{
|
|
|
|
|
MUST(try_append_repeated(ch, n));
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-22 03:26:12 -03:00
|
|
|
void StringBuilder::append_repeated(StringView string, size_t n)
|
|
|
|
|
{
|
|
|
|
|
MUST(try_append_repeated(string, n));
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-25 10:24:19 -03:00
|
|
|
void StringBuilder::append_repeated(Utf16View const& string, size_t n)
|
|
|
|
|
{
|
|
|
|
|
MUST(try_append_repeated(string, n));
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-09 12:00:14 -03:00
|
|
|
ErrorOr<ByteBuffer> StringBuilder::to_byte_buffer() const
|
2019-01-18 00:27:51 -02:00
|
|
|
{
|
2023-03-09 12:00:14 -03:00
|
|
|
return ByteBuffer::copy(data(), length());
|
2023-03-06 16:06:54 -03:00
|
|
|
}
|
|
|
|
|
|
2023-12-16 11:19:34 -03:00
|
|
|
ByteString StringBuilder::to_byte_string() const
|
2018-10-10 06:53:07 -03:00
|
|
|
{
|
2020-05-15 08:36:50 -03:00
|
|
|
if (is_empty())
|
2023-12-16 11:19:34 -03:00
|
|
|
return ByteString::empty();
|
|
|
|
|
return ByteString((char const*)data(), length());
|
2019-09-25 05:49:41 -03:00
|
|
|
}
|
|
|
|
|
|
2024-07-19 16:38:41 -03:00
|
|
|
ErrorOr<String> StringBuilder::to_string()
|
AK: Introduce the new String, replacement for DeprecatedString
DeprecatedString (formerly String) has been with us since the start,
and it has served us well. However, it has a number of shortcomings
that I'd like to address.
Some of these issues are hard if not impossible to solve incrementally
inside of DeprecatedString, so instead of doing that, let's build a new
String class and then incrementally move over to it instead.
Problems in DeprecatedString:
- It assumes string allocation never fails. This makes it impossible
to use in allocation-sensitive contexts, and is the reason we had to
ban DeprecatedString from the kernel entirely.
- The awkward null state. DeprecatedString can be null. It's different
from the empty state, although null strings are considered empty.
All code is immediately nicer when using Optional<DeprecatedString>
but DeprecatedString came before Optional, which is how we ended up
like this.
- The encoding of the underlying data is ambiguous. For the most part,
we use it as if it's always UTF-8, but there have been cases where
we pass around strings in other encodings (e.g ISO8859-1)
- operator[] and length() are used to iterate over DeprecatedString one
byte at a time. This is done all over the codebase, and will *not*
give the right results unless the string is all ASCII.
How we solve these issues in the new String:
- Functions that may allocate now return ErrorOr<String> so that ENOMEM
errors can be passed to the caller.
- String has no null state. Use Optional<String> when needed.
- String is always UTF-8. This is validated when constructing a String.
We may need to add a bypass for this in the future, for cases where
you have a known-good string, but for now: validate all the things!
- There is no operator[] or length(). You can get the underlying data
with bytes(), but for iterating over code points, you should be using
an UTF-8 iterator.
Furthermore, it has two nifty new features:
- String implements a small string optimization (SSO) for strings that
can fit entirely within a pointer. This means up to 3 bytes on 32-bit
platforms, and 7 bytes on 64-bit platforms. Such small strings will
not be heap-allocated.
- String can create substrings without making a deep copy of the
substring. Instead, the superstring gets +1 refcount from the
substring, and it acts like a view into the superstring. To make
substrings like this, use the substring_with_shared_superstring() API.
One caveat:
- String does not guarantee that the underlying data is null-terminated
like DeprecatedString does today. While this was nifty in a handful of
places where we were calling C functions, it did stand in the way of
shared-superstring substrings.
2022-12-01 09:27:43 -03:00
|
|
|
{
|
2024-07-19 16:38:41 -03:00
|
|
|
if (m_buffer.is_inline())
|
|
|
|
|
return String::from_utf8(string_view());
|
|
|
|
|
return String::from_string_builder({}, *this);
|
AK: Introduce the new String, replacement for DeprecatedString
DeprecatedString (formerly String) has been with us since the start,
and it has served us well. However, it has a number of shortcomings
that I'd like to address.
Some of these issues are hard if not impossible to solve incrementally
inside of DeprecatedString, so instead of doing that, let's build a new
String class and then incrementally move over to it instead.
Problems in DeprecatedString:
- It assumes string allocation never fails. This makes it impossible
to use in allocation-sensitive contexts, and is the reason we had to
ban DeprecatedString from the kernel entirely.
- The awkward null state. DeprecatedString can be null. It's different
from the empty state, although null strings are considered empty.
All code is immediately nicer when using Optional<DeprecatedString>
but DeprecatedString came before Optional, which is how we ended up
like this.
- The encoding of the underlying data is ambiguous. For the most part,
we use it as if it's always UTF-8, but there have been cases where
we pass around strings in other encodings (e.g ISO8859-1)
- operator[] and length() are used to iterate over DeprecatedString one
byte at a time. This is done all over the codebase, and will *not*
give the right results unless the string is all ASCII.
How we solve these issues in the new String:
- Functions that may allocate now return ErrorOr<String> so that ENOMEM
errors can be passed to the caller.
- String has no null state. Use Optional<String> when needed.
- String is always UTF-8. This is validated when constructing a String.
We may need to add a bypass for this in the future, for cases where
you have a known-good string, but for now: validate all the things!
- There is no operator[] or length(). You can get the underlying data
with bytes(), but for iterating over code points, you should be using
an UTF-8 iterator.
Furthermore, it has two nifty new features:
- String implements a small string optimization (SSO) for strings that
can fit entirely within a pointer. This means up to 3 bytes on 32-bit
platforms, and 7 bytes on 64-bit platforms. Such small strings will
not be heap-allocated.
- String can create substrings without making a deep copy of the
substring. Instead, the superstring gets +1 refcount from the
substring, and it acts like a view into the superstring. To make
substrings like this, use the substring_with_shared_superstring() API.
One caveat:
- String does not guarantee that the underlying data is null-terminated
like DeprecatedString does today. While this was nifty in a handful of
places where we were calling C functions, it did stand in the way of
shared-superstring substrings.
2022-12-01 09:27:43 -03:00
|
|
|
}
|
2023-02-14 11:37:39 -03:00
|
|
|
|
2024-07-19 16:38:41 -03:00
|
|
|
String StringBuilder::to_string_without_validation()
|
2023-12-29 11:30:15 -03:00
|
|
|
{
|
2024-07-19 16:38:41 -03:00
|
|
|
if (m_buffer.is_inline())
|
|
|
|
|
return String::from_utf8_without_validation(string_view().bytes());
|
|
|
|
|
return String::from_string_builder_without_validation({}, *this);
|
2023-12-29 11:30:15 -03:00
|
|
|
}
|
|
|
|
|
|
2024-03-23 07:33:26 -03:00
|
|
|
FlyString StringBuilder::to_fly_string_without_validation() const
|
|
|
|
|
{
|
|
|
|
|
return FlyString::from_utf8_without_validation(string_view().bytes());
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-14 11:37:39 -03:00
|
|
|
ErrorOr<FlyString> StringBuilder::to_fly_string() const
|
|
|
|
|
{
|
|
|
|
|
return FlyString::from_utf8(string_view());
|
|
|
|
|
}
|
2020-02-14 17:41:10 -03:00
|
|
|
|
2023-08-11 18:38:01 -03:00
|
|
|
u8* StringBuilder::data()
|
|
|
|
|
{
|
2026-06-22 09:28:38 -03:00
|
|
|
return m_buffer.data() + string_builder_prefix_size();
|
2023-08-11 18:38:01 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
u8 const* StringBuilder::data() const
|
|
|
|
|
{
|
2026-06-22 09:28:38 -03:00
|
|
|
return m_buffer.data() + string_builder_prefix_size();
|
2023-08-11 18:38:01 -03:00
|
|
|
}
|
|
|
|
|
|
2019-09-25 05:49:41 -03:00
|
|
|
StringView StringBuilder::string_view() const
|
|
|
|
|
{
|
2026-06-22 09:28:38 -03:00
|
|
|
return m_buffer.span().slice(string_builder_prefix_size());
|
2019-09-25 05:49:41 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StringBuilder::clear()
|
|
|
|
|
{
|
2026-06-22 09:28:38 -03:00
|
|
|
m_buffer.resize(string_builder_prefix_size());
|
2018-10-10 06:53:07 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-15 20:47:54 -03:00
|
|
|
ErrorOr<void> StringBuilder::try_append_code_point(u32 code_point)
|
2020-06-04 16:04:31 -03:00
|
|
|
{
|
2025-06-17 17:08:30 -03:00
|
|
|
if (!is_unicode(code_point)) {
|
|
|
|
|
TRY(try_append_code_point(UnicodeUtils::REPLACEMENT_CODE_POINT));
|
|
|
|
|
return {};
|
2020-06-04 16:04:31 -03:00
|
|
|
}
|
2025-06-17 17:08:30 -03:00
|
|
|
|
2026-06-22 09:28:38 -03:00
|
|
|
TRY(AK::UnicodeUtils::try_code_point_to_utf8(code_point, [this](char c) { return try_append(c); }));
|
2021-11-15 20:47:54 -03:00
|
|
|
return {};
|
2020-06-04 16:04:31 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-15 20:47:54 -03:00
|
|
|
void StringBuilder::append_code_point(u32 code_point)
|
|
|
|
|
{
|
2025-06-17 17:08:30 -03:00
|
|
|
if (!is_unicode(code_point)) {
|
|
|
|
|
append_code_point(UnicodeUtils::REPLACEMENT_CODE_POINT);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-29 13:27:58 -03:00
|
|
|
if (code_point <= 0x7f) {
|
|
|
|
|
m_buffer.append(static_cast<char>(code_point));
|
|
|
|
|
} else if (code_point <= 0x07ff) {
|
|
|
|
|
(void)will_append(2);
|
|
|
|
|
m_buffer.append(static_cast<char>((((code_point >> 6) & 0x1f) | 0xc0)));
|
|
|
|
|
m_buffer.append(static_cast<char>((((code_point >> 0) & 0x3f) | 0x80)));
|
|
|
|
|
} else if (code_point <= 0xffff) {
|
|
|
|
|
(void)will_append(3);
|
|
|
|
|
m_buffer.append(static_cast<char>((((code_point >> 12) & 0x0f) | 0xe0)));
|
|
|
|
|
m_buffer.append(static_cast<char>((((code_point >> 6) & 0x3f) | 0x80)));
|
|
|
|
|
m_buffer.append(static_cast<char>((((code_point >> 0) & 0x3f) | 0x80)));
|
2025-06-17 17:08:30 -03:00
|
|
|
} else {
|
2023-12-29 13:27:58 -03:00
|
|
|
(void)will_append(4);
|
|
|
|
|
m_buffer.append(static_cast<char>((((code_point >> 18) & 0x07) | 0xf0)));
|
|
|
|
|
m_buffer.append(static_cast<char>((((code_point >> 12) & 0x3f) | 0x80)));
|
|
|
|
|
m_buffer.append(static_cast<char>((((code_point >> 6) & 0x3f) | 0x80)));
|
|
|
|
|
m_buffer.append(static_cast<char>((((code_point >> 0) & 0x3f) | 0x80)));
|
|
|
|
|
}
|
2021-11-15 20:47:54 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorOr<void> StringBuilder::try_append(Utf16View const& utf16_view)
|
2021-08-09 12:53:28 -03:00
|
|
|
{
|
2024-10-26 06:05:31 -03:00
|
|
|
if (utf16_view.is_empty())
|
|
|
|
|
return {};
|
2025-06-12 20:29:41 -03:00
|
|
|
if (utf16_view.has_ascii_storage())
|
2025-10-04 05:26:22 -03:00
|
|
|
return try_append_ascii_without_validation(utf16_view.bytes());
|
2024-10-25 08:38:55 -03:00
|
|
|
|
2025-06-12 20:29:41 -03:00
|
|
|
auto remaining_view = utf16_view.utf16_span();
|
2025-06-26 13:52:23 -03:00
|
|
|
auto maximum_utf8_length = UnicodeUtils::maximum_utf8_length_from_utf16(remaining_view);
|
2024-10-26 06:05:31 -03:00
|
|
|
|
|
|
|
|
// Possibly over-allocate a little to ensure we don't have to allocate later.
|
|
|
|
|
TRY(will_append(maximum_utf8_length));
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
2025-06-26 13:52:23 -03:00
|
|
|
auto* uninitialized_data_pointer = static_cast<char*>(m_buffer.end_pointer());
|
2024-10-26 06:05:31 -03:00
|
|
|
|
|
|
|
|
// Fast path.
|
2025-06-26 13:52:23 -03:00
|
|
|
auto result = simdutf::convert_utf16_to_utf8_with_errors(remaining_view.data(), remaining_view.size(), uninitialized_data_pointer);
|
2024-10-26 06:05:31 -03:00
|
|
|
if (result.error == simdutf::SUCCESS) {
|
|
|
|
|
auto bytes_just_written = result.count;
|
|
|
|
|
m_buffer.set_size(m_buffer.size() + bytes_just_written);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-08-09 12:53:28 -03:00
|
|
|
|
2024-10-26 06:05:31 -03:00
|
|
|
// Slow path. Found unmatched surrogate code unit.
|
|
|
|
|
auto first_invalid_code_unit = result.count;
|
2025-06-26 13:52:23 -03:00
|
|
|
ASSERT(first_invalid_code_unit < remaining_view.size());
|
2024-10-26 06:05:31 -03:00
|
|
|
|
|
|
|
|
// Unfortunately, `simdutf` does not tell us how many bytes it just wrote in case of an error, so we have to calculate it ourselves.
|
2025-06-26 13:52:23 -03:00
|
|
|
auto bytes_just_written = simdutf::utf8_length_from_utf16(remaining_view.data(), first_invalid_code_unit);
|
2024-10-26 06:05:31 -03:00
|
|
|
|
|
|
|
|
do {
|
2025-06-26 13:52:23 -03:00
|
|
|
auto code_unit = remaining_view[first_invalid_code_unit++];
|
2024-10-26 06:05:31 -03:00
|
|
|
|
|
|
|
|
// Invalid surrogate code units are U+D800 - U+DFFF, so they are always encoded using 3 bytes.
|
|
|
|
|
ASSERT(code_unit >= 0xD800 && code_unit <= 0xDFFF);
|
|
|
|
|
ASSERT(m_buffer.size() + bytes_just_written + 3 < m_buffer.capacity());
|
|
|
|
|
uninitialized_data_pointer[bytes_just_written++] = (((code_unit >> 12) & 0x0f) | 0xe0);
|
|
|
|
|
uninitialized_data_pointer[bytes_just_written++] = (((code_unit >> 6) & 0x3f) | 0x80);
|
|
|
|
|
uninitialized_data_pointer[bytes_just_written++] = (((code_unit >> 0) & 0x3f) | 0x80);
|
2025-06-26 13:52:23 -03:00
|
|
|
} while (first_invalid_code_unit < remaining_view.size() && UnicodeUtils::is_utf16_low_surrogate(remaining_view.data()[first_invalid_code_unit]));
|
2024-10-26 06:05:31 -03:00
|
|
|
|
|
|
|
|
// Code unit might no longer be invalid, retry on the remaining data.
|
|
|
|
|
m_buffer.set_size(m_buffer.size() + bytes_just_written);
|
2025-06-26 13:52:23 -03:00
|
|
|
remaining_view = remaining_view.slice(first_invalid_code_unit);
|
2021-08-09 12:53:28 -03:00
|
|
|
}
|
2024-10-26 06:05:31 -03:00
|
|
|
|
2021-11-15 20:47:54 -03:00
|
|
|
return {};
|
2021-08-09 12:53:28 -03:00
|
|
|
}
|
|
|
|
|
|
2021-11-15 20:47:54 -03:00
|
|
|
void StringBuilder::append(Utf16View const& utf16_view)
|
|
|
|
|
{
|
|
|
|
|
MUST(try_append(utf16_view));
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 10:48:37 -03:00
|
|
|
void StringBuilder::append_as_lowercase(char ch)
|
|
|
|
|
{
|
|
|
|
|
if (ch >= 'A' && ch <= 'Z')
|
|
|
|
|
append(ch + 0x20);
|
|
|
|
|
else
|
|
|
|
|
append(ch);
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 20:55:02 -03:00
|
|
|
void StringBuilder::append_escaped_for_json(StringView string)
|
2022-02-24 14:15:31 -03:00
|
|
|
{
|
|
|
|
|
MUST(try_append_escaped_for_json(string));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorOr<void> StringBuilder::try_append_escaped_for_json(StringView string)
|
2020-11-02 08:56:36 -03:00
|
|
|
{
|
|
|
|
|
for (auto ch : string) {
|
|
|
|
|
switch (ch) {
|
|
|
|
|
case '\b':
|
2022-07-11 14:32:29 -03:00
|
|
|
TRY(try_append("\\b"sv));
|
2020-11-02 08:56:36 -03:00
|
|
|
break;
|
|
|
|
|
case '\n':
|
2022-07-11 14:32:29 -03:00
|
|
|
TRY(try_append("\\n"sv));
|
2020-11-02 08:56:36 -03:00
|
|
|
break;
|
|
|
|
|
case '\t':
|
2022-07-11 14:32:29 -03:00
|
|
|
TRY(try_append("\\t"sv));
|
2020-11-02 08:56:36 -03:00
|
|
|
break;
|
|
|
|
|
case '\"':
|
2022-07-11 14:32:29 -03:00
|
|
|
TRY(try_append("\\\""sv));
|
2020-11-02 08:56:36 -03:00
|
|
|
break;
|
|
|
|
|
case '\\':
|
2022-07-11 14:32:29 -03:00
|
|
|
TRY(try_append("\\\\"sv));
|
2020-11-02 08:56:36 -03:00
|
|
|
break;
|
|
|
|
|
default:
|
2021-09-11 09:20:31 -03:00
|
|
|
if (ch >= 0 && ch <= 0x1f)
|
2022-02-24 14:15:31 -03:00
|
|
|
TRY(try_appendff("\\u{:04x}", ch));
|
2021-09-11 09:20:31 -03:00
|
|
|
else
|
2022-02-24 14:15:31 -03:00
|
|
|
TRY(try_append(ch));
|
2020-11-02 08:56:36 -03:00
|
|
|
}
|
|
|
|
|
}
|
2022-02-24 14:15:31 -03:00
|
|
|
return {};
|
2020-11-02 08:56:36 -03:00
|
|
|
}
|
|
|
|
|
|
2025-06-17 17:08:30 -03:00
|
|
|
auto StringBuilder::leak_buffer_for_string_construction() -> Optional<Buffer::OutlineBuffer>
|
2024-07-19 16:38:41 -03:00
|
|
|
{
|
2026-06-10 14:14:13 -03:00
|
|
|
if (auto buffer = m_buffer.leak_outline_buffer(); buffer.has_value()) {
|
2024-07-19 16:38:41 -03:00
|
|
|
clear();
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-10 06:53:07 -03:00
|
|
|
}
|