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