2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-11-16 00:47:54 +01:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
#pragma once
|
|
|
|
|
2020-02-14 21:41:10 +01:00
|
|
|
#include <AK/ByteBuffer.h>
|
2020-09-23 13:21:18 +02:00
|
|
|
#include <AK/Format.h>
|
2020-02-14 21:41:10 +01:00
|
|
|
#include <AK/Forward.h>
|
2020-09-22 13:11:05 +02:00
|
|
|
#include <AK/StringView.h>
|
2019-06-14 06:43:56 +02:00
|
|
|
#include <stdarg.h>
|
2018-10-10 11:53:07 +02:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
class StringBuilder {
|
|
|
|
public:
|
2023-08-12 00:38:01 +03:00
|
|
|
static constexpr size_t inline_capacity = 256;
|
|
|
|
|
2024-07-19 15:38:41 -04:00
|
|
|
using Buffer = Detail::ByteBuffer<inline_capacity>;
|
2023-12-16 17:49:34 +03:30
|
|
|
using OutputType = ByteString;
|
2019-08-07 21:28:07 +02:00
|
|
|
|
2022-12-09 20:15:35 +03:30
|
|
|
static ErrorOr<StringBuilder> create(size_t initial_capacity = inline_capacity);
|
|
|
|
|
2020-11-24 22:04:22 +01:00
|
|
|
explicit StringBuilder(size_t initial_capacity = inline_capacity);
|
2021-01-10 16:29:28 -07:00
|
|
|
~StringBuilder() = default;
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2021-11-16 00:47:54 +01:00
|
|
|
ErrorOr<void> try_append(StringView);
|
|
|
|
ErrorOr<void> try_append(Utf16View const&);
|
|
|
|
ErrorOr<void> try_append(Utf32View const&);
|
|
|
|
ErrorOr<void> try_append_code_point(u32);
|
|
|
|
ErrorOr<void> try_append(char);
|
|
|
|
template<typename... Parameters>
|
|
|
|
ErrorOr<void> try_appendff(CheckedFormatString<Parameters...>&& fmtstr, Parameters const&... parameters)
|
|
|
|
{
|
2023-01-13 02:19:40 +00:00
|
|
|
VariadicFormatParams<AllowDebugOnlyFormatters::No, Parameters...> variadic_format_params { parameters... };
|
2021-11-16 00:47:54 +01:00
|
|
|
return vformat(*this, fmtstr.view(), variadic_format_params);
|
|
|
|
}
|
|
|
|
ErrorOr<void> try_append(char const*, size_t);
|
2022-01-04 23:37:15 +01:00
|
|
|
ErrorOr<void> try_append_repeated(char, size_t);
|
2022-02-24 19:15:31 +02:00
|
|
|
ErrorOr<void> try_append_escaped_for_json(StringView);
|
2021-11-16 00:47:54 +01:00
|
|
|
|
2021-11-11 00:55:02 +01:00
|
|
|
void append(StringView);
|
2021-08-09 11:53:28 -04:00
|
|
|
void append(Utf16View const&);
|
2021-08-09 11:48:50 -04:00
|
|
|
void append(Utf32View const&);
|
2018-10-10 11:53:07 +02:00
|
|
|
void append(char);
|
2020-08-05 16:31:20 -04:00
|
|
|
void append_code_point(u32);
|
2021-08-09 11:48:50 -04:00
|
|
|
void append(char const*, size_t);
|
|
|
|
void appendvf(char const*, va_list);
|
2022-01-04 23:37:15 +01:00
|
|
|
void append_repeated(char, size_t);
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2021-05-11 15:48:37 +02:00
|
|
|
void append_as_lowercase(char);
|
2021-11-11 00:55:02 +01:00
|
|
|
void append_escaped_for_json(StringView);
|
2020-11-02 12:56:36 +01:00
|
|
|
|
2020-09-22 13:11:05 +02:00
|
|
|
template<typename... Parameters>
|
2021-08-09 11:48:50 -04:00
|
|
|
void appendff(CheckedFormatString<Parameters...>&& fmtstr, Parameters const&... parameters)
|
2020-09-23 13:21:18 +02:00
|
|
|
{
|
2023-01-13 02:19:40 +00:00
|
|
|
VariadicFormatParams<AllowDebugOnlyFormatters::No, Parameters...> variadic_format_params { parameters... };
|
2021-11-16 01:15:21 +01:00
|
|
|
MUST(vformat(*this, fmtstr.view(), variadic_format_params));
|
2020-09-23 13:21:18 +02:00
|
|
|
}
|
2020-09-22 13:11:05 +02:00
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
[[nodiscard]] ByteString to_byte_string() const;
|
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 13:27:43 +01:00
|
|
|
|
2024-07-19 15:38:41 -04:00
|
|
|
[[nodiscard]] String to_string_without_validation();
|
|
|
|
ErrorOr<String> to_string();
|
2024-03-23 11:33:26 +01:00
|
|
|
|
|
|
|
[[nodiscard]] FlyString to_fly_string_without_validation() const;
|
2023-02-14 14:37:39 +00:00
|
|
|
ErrorOr<FlyString> to_fly_string() const;
|
2023-01-27 14:53:04 -05:00
|
|
|
|
2023-03-09 15:00:14 +00:00
|
|
|
[[nodiscard]] ErrorOr<ByteBuffer> to_byte_buffer() const;
|
2018-10-10 11:53:07 +02:00
|
|
|
|
2021-04-11 02:31:30 -07:00
|
|
|
[[nodiscard]] StringView string_view() const;
|
2019-09-25 11:49:41 +03:00
|
|
|
void clear();
|
|
|
|
|
2023-08-12 00:38:01 +03:00
|
|
|
[[nodiscard]] size_t length() const;
|
|
|
|
[[nodiscard]] bool is_empty() const;
|
|
|
|
void trim(size_t count);
|
2019-09-29 16:20:09 +02:00
|
|
|
|
2020-03-20 14:33:46 +01:00
|
|
|
template<class SeparatorType, class CollectionType>
|
2022-02-22 21:31:05 +00:00
|
|
|
void join(SeparatorType const& separator, CollectionType const& collection, StringView fmtstr = "{}"sv)
|
2023-01-14 00:08:18 +00:00
|
|
|
{
|
|
|
|
MUST(try_join(separator, collection, fmtstr));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class SeparatorType, class CollectionType>
|
|
|
|
ErrorOr<void> try_join(SeparatorType const& separator, CollectionType const& collection, StringView fmtstr = "{}"sv)
|
2020-03-20 14:33:46 +01:00
|
|
|
{
|
|
|
|
bool first = true;
|
|
|
|
for (auto& item : collection) {
|
2023-01-14 00:08:18 +00:00
|
|
|
if (!first)
|
|
|
|
TRY(try_append(separator));
|
|
|
|
TRY(try_appendff(fmtstr, item));
|
|
|
|
first = false;
|
2020-03-20 14:33:46 +01:00
|
|
|
}
|
2023-01-14 00:08:18 +00:00
|
|
|
return {};
|
2020-03-20 14:33:46 +01:00
|
|
|
}
|
|
|
|
|
2024-07-19 15:38:41 -04:00
|
|
|
Optional<Buffer::OutlineBuffer> leak_buffer_for_string_construction(Badge<Detail::StringData>);
|
|
|
|
|
2018-10-10 11:53:07 +02:00
|
|
|
private:
|
2024-07-19 15:38:41 -04:00
|
|
|
explicit StringBuilder(Buffer);
|
|
|
|
|
2021-11-10 14:33:44 +01:00
|
|
|
ErrorOr<void> will_append(size_t);
|
2023-08-12 00:38:01 +03:00
|
|
|
u8* data();
|
|
|
|
u8 const* data() const;
|
2019-01-18 03:27:51 +01:00
|
|
|
|
2024-07-19 15:38:41 -04:00
|
|
|
Buffer m_buffer;
|
2018-10-10 11:53:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 12:18:30 +01:00
|
|
|
#if USING_AK_GLOBALLY
|
2018-10-10 11:53:07 +02:00
|
|
|
using AK::StringBuilder;
|
2022-11-26 12:18:30 +01:00
|
|
|
#endif
|