2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-11-16 02:47:54 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2023-08-12 00:38:01 +03:00
|
|
|
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2020-11-15 15:11:21 +03:00
|
|
|
#include <AK/ByteBuffer.h>
|
2020-11-25 00:04:22 +03:00
|
|
|
#include <AK/Checked.h>
|
2019-06-07 12:41:11 +03:00
|
|
|
#include <AK/PrintfImplementation.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 15:27:43 +03:00
|
|
|
#include <AK/String.h>
|
2020-03-08 14:34:33 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-03-23 15:45:10 +03:00
|
|
|
#include <AK/StringView.h>
|
2021-10-11 08:08:08 +03:00
|
|
|
#include <AK/UnicodeUtils.h>
|
2020-05-17 21:03:03 +03:00
|
|
|
#include <AK/Utf32View.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2022-02-16 01:23:34 +03:00
|
|
|
#ifndef KERNEL
|
2023-12-16 17:19:34 +03:00
|
|
|
# include <AK/ByteString.h>
|
2023-02-14 17:37:39 +03:00
|
|
|
# include <AK/FlyString.h>
|
2022-02-16 01:23:34 +03:00
|
|
|
# include <AK/Utf16View.h>
|
|
|
|
#endif
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
namespace AK {
|
|
|
|
|
2021-11-10 16:33:44 +03:00
|
|
|
inline ErrorOr<void> StringBuilder::will_append(size_t size)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2023-08-12 00:38:01 +03:00
|
|
|
if (m_use_inline_capacity_only == UseInlineCapacityOnly::Yes) {
|
|
|
|
VERIFY(m_buffer.capacity() == StringBuilder::inline_capacity);
|
|
|
|
Checked<size_t> current_pointer = m_buffer.size();
|
|
|
|
current_pointer += size;
|
|
|
|
VERIFY(!current_pointer.has_overflow());
|
|
|
|
if (current_pointer <= StringBuilder::inline_capacity) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return Error::from_errno(ENOMEM);
|
|
|
|
}
|
|
|
|
|
2021-05-31 01:58:07 +03:00
|
|
|
Checked<size_t> needed_capacity = m_buffer.size();
|
2020-11-25 00:04:22 +03:00
|
|
|
needed_capacity += size;
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!needed_capacity.has_overflow());
|
2021-05-31 01:55:51 +03:00
|
|
|
// Prefer to completely use the existing capacity first
|
|
|
|
if (needed_capacity <= m_buffer.capacity())
|
2021-11-10 16:33:44 +03:00
|
|
|
return {};
|
2021-05-18 00:18:34 +03:00
|
|
|
Checked<size_t> expanded_capacity = needed_capacity;
|
2021-05-31 01:55:51 +03:00
|
|
|
expanded_capacity *= 2;
|
2021-05-18 00:18:34 +03:00
|
|
|
VERIFY(!expanded_capacity.has_overflow());
|
2021-11-10 16:33:44 +03:00
|
|
|
TRY(m_buffer.try_ensure_capacity(expanded_capacity.value()));
|
|
|
|
return {};
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2022-12-09 19:45:35 +03:00
|
|
|
ErrorOr<StringBuilder> StringBuilder::create(size_t initial_capacity)
|
|
|
|
{
|
|
|
|
StringBuilder builder;
|
|
|
|
TRY(builder.m_buffer.try_ensure_capacity(initial_capacity));
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
2019-12-09 19:45:40 +03:00
|
|
|
StringBuilder::StringBuilder(size_t initial_capacity)
|
2019-01-29 00:55:55 +03:00
|
|
|
{
|
2021-05-31 01:54:43 +03:00
|
|
|
m_buffer.ensure_capacity(initial_capacity);
|
2019-01-29 00:55:55 +03:00
|
|
|
}
|
|
|
|
|
2023-08-12 00:38:01 +03:00
|
|
|
StringBuilder::StringBuilder(UseInlineCapacityOnly use_inline_capacity_only)
|
|
|
|
: m_use_inline_capacity_only(use_inline_capacity_only)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t StringBuilder::length() const
|
|
|
|
{
|
|
|
|
return m_buffer.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StringBuilder::is_empty() const
|
|
|
|
{
|
|
|
|
return m_buffer.is_empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringBuilder::trim(size_t count)
|
|
|
|
{
|
|
|
|
auto decrease_count = min(m_buffer.size(), count);
|
|
|
|
m_buffer.resize(m_buffer.size() - decrease_count);
|
|
|
|
}
|
|
|
|
|
2021-11-16 02:47:54 +03:00
|
|
|
ErrorOr<void> StringBuilder::try_append(StringView string)
|
2018-11-18 16:57:41 +03:00
|
|
|
{
|
2021-11-16 02:47:54 +03:00
|
|
|
if (string.is_empty())
|
|
|
|
return {};
|
|
|
|
TRY(will_append(string.length()));
|
|
|
|
TRY(m_buffer.try_append(string.characters_without_null_termination(), string.length()));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<void> StringBuilder::try_append(char ch)
|
|
|
|
{
|
|
|
|
TRY(will_append(1));
|
2022-02-13 14:32:30 +03:00
|
|
|
TRY(m_buffer.try_append(ch));
|
2021-11-16 02:47:54 +03:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2022-01-05 01:37:15 +03:00
|
|
|
ErrorOr<void> StringBuilder::try_append_repeated(char ch, size_t n)
|
|
|
|
{
|
|
|
|
TRY(will_append(n));
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
|
|
TRY(try_append(ch));
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-11-16 02: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 16:57:41 +03:00
|
|
|
}
|
|
|
|
|
2021-08-09 18:48:50 +03:00
|
|
|
void StringBuilder::append(char const* characters, size_t length)
|
2019-01-29 00:55:55 +03:00
|
|
|
{
|
2021-11-16 02:47:54 +03:00
|
|
|
MUST(try_append(characters, length));
|
2019-01-29 00:55:55 +03:00
|
|
|
}
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
void StringBuilder::append(char ch)
|
|
|
|
{
|
2021-11-16 02:47:54 +03:00
|
|
|
MUST(try_append(ch));
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2021-08-09 18:48:50 +03:00
|
|
|
void StringBuilder::appendvf(char const* fmt, va_list ap)
|
2019-01-18 04:41:27 +03:00
|
|
|
{
|
2019-05-28 12:53:16 +03:00
|
|
|
printf_internal([this](char*&, char ch) {
|
2019-01-18 04:41:27 +03:00
|
|
|
append(ch);
|
2019-05-28 12:53:16 +03:00
|
|
|
},
|
|
|
|
nullptr, fmt, ap);
|
2019-01-30 18:28:51 +03:00
|
|
|
}
|
|
|
|
|
2022-01-05 01:37:15 +03:00
|
|
|
void StringBuilder::append_repeated(char ch, size_t n)
|
|
|
|
{
|
|
|
|
MUST(try_append_repeated(ch, n));
|
|
|
|
}
|
|
|
|
|
2023-03-09 18:00:14 +03:00
|
|
|
ErrorOr<ByteBuffer> StringBuilder::to_byte_buffer() const
|
2019-01-18 05:27:51 +03:00
|
|
|
{
|
2023-03-09 18:00:14 +03:00
|
|
|
return ByteBuffer::copy(data(), length());
|
2023-03-06 22:06:54 +03:00
|
|
|
}
|
|
|
|
|
2022-02-16 01:23:34 +03:00
|
|
|
#ifndef KERNEL
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString StringBuilder::to_byte_string() const
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2020-05-15 14:36:50 +03:00
|
|
|
if (is_empty())
|
2023-12-16 17:19:34 +03:00
|
|
|
return ByteString::empty();
|
|
|
|
return ByteString((char const*)data(), length());
|
2019-09-25 11:49:41 +03:00
|
|
|
}
|
|
|
|
|
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 15:27:43 +03:00
|
|
|
ErrorOr<String> StringBuilder::to_string() const
|
|
|
|
{
|
|
|
|
return String::from_utf8(string_view());
|
|
|
|
}
|
2023-02-14 17:37:39 +03:00
|
|
|
|
2023-12-29 17:30:15 +03:00
|
|
|
String StringBuilder::to_string_without_validation() const
|
|
|
|
{
|
|
|
|
return String::from_utf8_without_validation(string_view().bytes());
|
|
|
|
}
|
|
|
|
|
2023-02-14 17:37:39 +03:00
|
|
|
ErrorOr<FlyString> StringBuilder::to_fly_string() const
|
|
|
|
{
|
|
|
|
return FlyString::from_utf8(string_view());
|
|
|
|
}
|
2022-02-16 01:23:34 +03:00
|
|
|
#endif
|
2020-02-14 23:41:10 +03:00
|
|
|
|
2023-08-12 00:38:01 +03:00
|
|
|
u8* StringBuilder::data()
|
|
|
|
{
|
|
|
|
return m_buffer.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
u8 const* StringBuilder::data() const
|
|
|
|
{
|
|
|
|
return m_buffer.data();
|
|
|
|
}
|
|
|
|
|
2019-09-25 11:49:41 +03:00
|
|
|
StringView StringBuilder::string_view() const
|
|
|
|
{
|
2021-05-31 01:58:07 +03:00
|
|
|
return StringView { data(), m_buffer.size() };
|
2019-09-25 11:49:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void StringBuilder::clear()
|
|
|
|
{
|
2019-01-18 17:35:38 +03:00
|
|
|
m_buffer.clear();
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2021-11-16 02:47:54 +03:00
|
|
|
ErrorOr<void> StringBuilder::try_append_code_point(u32 code_point)
|
2020-06-04 22:04:31 +03:00
|
|
|
{
|
2023-01-07 22:48:20 +03:00
|
|
|
auto nwritten = TRY(AK::UnicodeUtils::try_code_point_to_utf8(code_point, [this](char c) { return try_append(c); }));
|
2021-10-11 08:08:08 +03:00
|
|
|
if (nwritten < 0) {
|
2021-11-16 02:47:54 +03:00
|
|
|
TRY(try_append(0xef));
|
|
|
|
TRY(try_append(0xbf));
|
|
|
|
TRY(try_append(0xbd));
|
2020-06-04 22:04:31 +03:00
|
|
|
}
|
2021-11-16 02:47:54 +03:00
|
|
|
return {};
|
2020-06-04 22:04:31 +03:00
|
|
|
}
|
|
|
|
|
2021-11-16 02:47:54 +03:00
|
|
|
void StringBuilder::append_code_point(u32 code_point)
|
|
|
|
{
|
2023-12-29 19: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)));
|
|
|
|
} else if (code_point <= 0x10ffff) {
|
|
|
|
(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)));
|
|
|
|
} else {
|
|
|
|
(void)will_append(3);
|
|
|
|
m_buffer.append(0xef);
|
|
|
|
m_buffer.append(0xbf);
|
|
|
|
m_buffer.append(0xbd);
|
|
|
|
}
|
2021-11-16 02:47:54 +03:00
|
|
|
}
|
|
|
|
|
2022-02-16 01:23:34 +03:00
|
|
|
#ifndef KERNEL
|
2021-11-16 02:47:54 +03:00
|
|
|
ErrorOr<void> StringBuilder::try_append(Utf16View const& utf16_view)
|
2021-08-09 18:53:28 +03:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < utf16_view.length_in_code_units();) {
|
|
|
|
auto code_point = utf16_view.code_point_at(i);
|
2021-11-16 02:47:54 +03:00
|
|
|
TRY(try_append_code_point(code_point));
|
2021-08-09 18:53:28 +03:00
|
|
|
|
|
|
|
i += (code_point > 0xffff ? 2 : 1);
|
|
|
|
}
|
2021-11-16 02:47:54 +03:00
|
|
|
return {};
|
2021-08-09 18:53:28 +03:00
|
|
|
}
|
|
|
|
|
2021-11-16 02:47:54 +03:00
|
|
|
void StringBuilder::append(Utf16View const& utf16_view)
|
|
|
|
{
|
|
|
|
MUST(try_append(utf16_view));
|
|
|
|
}
|
2022-02-16 01:23:34 +03:00
|
|
|
#endif
|
2021-11-16 02:47:54 +03:00
|
|
|
|
|
|
|
ErrorOr<void> StringBuilder::try_append(Utf32View const& utf32_view)
|
2020-05-17 21:03:03 +03:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < utf32_view.length(); ++i) {
|
2020-08-05 23:31:20 +03:00
|
|
|
auto code_point = utf32_view.code_points()[i];
|
2021-11-16 02:47:54 +03:00
|
|
|
TRY(try_append_code_point(code_point));
|
2020-05-17 21:03:03 +03:00
|
|
|
}
|
2021-11-16 02:47:54 +03:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringBuilder::append(Utf32View const& utf32_view)
|
|
|
|
{
|
|
|
|
MUST(try_append(utf32_view));
|
2020-05-17 21:03:03 +03:00
|
|
|
}
|
|
|
|
|
2021-05-11 16:48:37 +03:00
|
|
|
void StringBuilder::append_as_lowercase(char ch)
|
|
|
|
{
|
|
|
|
if (ch >= 'A' && ch <= 'Z')
|
|
|
|
append(ch + 0x20);
|
|
|
|
else
|
|
|
|
append(ch);
|
|
|
|
}
|
|
|
|
|
2021-11-11 02:55:02 +03:00
|
|
|
void StringBuilder::append_escaped_for_json(StringView string)
|
2022-02-24 20:15:31 +03:00
|
|
|
{
|
|
|
|
MUST(try_append_escaped_for_json(string));
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<void> StringBuilder::try_append_escaped_for_json(StringView string)
|
2020-11-02 14:56:36 +03:00
|
|
|
{
|
|
|
|
for (auto ch : string) {
|
|
|
|
switch (ch) {
|
|
|
|
case '\b':
|
2022-07-11 20:32:29 +03:00
|
|
|
TRY(try_append("\\b"sv));
|
2020-11-02 14:56:36 +03:00
|
|
|
break;
|
|
|
|
case '\n':
|
2022-07-11 20:32:29 +03:00
|
|
|
TRY(try_append("\\n"sv));
|
2020-11-02 14:56:36 +03:00
|
|
|
break;
|
|
|
|
case '\t':
|
2022-07-11 20:32:29 +03:00
|
|
|
TRY(try_append("\\t"sv));
|
2020-11-02 14:56:36 +03:00
|
|
|
break;
|
|
|
|
case '\"':
|
2022-07-11 20:32:29 +03:00
|
|
|
TRY(try_append("\\\""sv));
|
2020-11-02 14:56:36 +03:00
|
|
|
break;
|
|
|
|
case '\\':
|
2022-07-11 20:32:29 +03:00
|
|
|
TRY(try_append("\\\\"sv));
|
2020-11-02 14:56:36 +03:00
|
|
|
break;
|
|
|
|
default:
|
2021-09-11 15:20:31 +03:00
|
|
|
if (ch >= 0 && ch <= 0x1f)
|
2022-02-24 20:15:31 +03:00
|
|
|
TRY(try_appendff("\\u{:04x}", ch));
|
2021-09-11 15:20:31 +03:00
|
|
|
else
|
2022-02-24 20:15:31 +03:00
|
|
|
TRY(try_append(ch));
|
2020-11-02 14:56:36 +03:00
|
|
|
}
|
|
|
|
}
|
2022-02-24 20:15:31 +03:00
|
|
|
return {};
|
2020-11-02 14:56:36 +03:00
|
|
|
}
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|