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