2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2021-02-09 16:08:43 +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
|
|
|
*/
|
|
|
|
|
2019-08-06 20:04:12 +02:00
|
|
|
#pragma once
|
|
|
|
|
2021-09-07 15:54:23 +02:00
|
|
|
#include <AK/StringView.h>
|
2019-08-06 20:04:12 +02:00
|
|
|
#include <Kernel/KBuffer.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-08-06 20:04:12 +02:00
|
|
|
class KBufferBuilder {
|
2021-09-07 15:54:23 +02:00
|
|
|
AK_MAKE_NONCOPYABLE(KBufferBuilder);
|
|
|
|
|
2019-08-06 20:04:12 +02:00
|
|
|
public:
|
|
|
|
using OutputType = KBuffer;
|
|
|
|
|
2021-09-07 15:54:23 +02:00
|
|
|
static KResultOr<KBufferBuilder> try_create();
|
|
|
|
|
2020-12-18 14:10:10 +01:00
|
|
|
KBufferBuilder(KBufferBuilder&&) = default;
|
2021-09-07 15:54:23 +02:00
|
|
|
KBufferBuilder& operator=(KBufferBuilder&&) = default;
|
2021-02-28 14:42:08 +01:00
|
|
|
~KBufferBuilder() = default;
|
2019-08-06 20:04:12 +02:00
|
|
|
|
2021-09-06 18:24:13 +02:00
|
|
|
KResult append(const StringView&);
|
|
|
|
KResult append(char);
|
|
|
|
KResult append(const char*, int);
|
2019-08-06 20:04:12 +02:00
|
|
|
|
2021-09-06 18:24:13 +02:00
|
|
|
KResult append_escaped_for_json(const StringView&);
|
|
|
|
KResult append_bytes(ReadonlyBytes);
|
2020-11-02 12:56:36 +01:00
|
|
|
|
2020-10-07 13:59:26 +02:00
|
|
|
template<typename... Parameters>
|
2021-09-06 18:24:13 +02:00
|
|
|
KResult appendff(CheckedFormatString<Parameters...>&& fmtstr, const Parameters&... parameters)
|
2020-10-07 13:59:26 +02:00
|
|
|
{
|
2021-07-20 13:58:38 +02:00
|
|
|
// FIXME: This really not ideal, but vformat expects StringBuilder.
|
2021-05-12 21:40:29 +02:00
|
|
|
StringBuilder builder;
|
2021-08-30 02:58:22 -07:00
|
|
|
AK::VariadicFormatParams variadic_format_params { parameters... };
|
|
|
|
vformat(builder, fmtstr.view(), variadic_format_params);
|
2021-09-06 18:24:13 +02:00
|
|
|
return append_bytes(builder.string_view().bytes());
|
2020-10-07 13:59:26 +02:00
|
|
|
}
|
|
|
|
|
2020-09-17 13:51:09 -06:00
|
|
|
bool flush();
|
2020-12-18 14:10:10 +01:00
|
|
|
OwnPtr<KBuffer> build();
|
2019-08-06 20:04:12 +02:00
|
|
|
|
2021-09-06 18:24:13 +02:00
|
|
|
ReadonlyBytes bytes() const
|
|
|
|
{
|
|
|
|
if (!m_buffer)
|
|
|
|
return {};
|
|
|
|
return ReadonlyBytes { m_buffer->data(), m_buffer->size() };
|
|
|
|
}
|
|
|
|
|
2019-08-06 20:04:12 +02:00
|
|
|
private:
|
2021-09-07 15:54:23 +02:00
|
|
|
explicit KBufferBuilder(NonnullOwnPtr<KBuffer>);
|
|
|
|
|
2020-09-17 13:51:09 -06:00
|
|
|
bool check_expand(size_t);
|
2020-12-18 14:10:10 +01:00
|
|
|
u8* insertion_ptr()
|
|
|
|
{
|
|
|
|
if (!m_buffer)
|
|
|
|
return nullptr;
|
|
|
|
return m_buffer->data() + m_size;
|
|
|
|
}
|
2019-08-06 20:04:12 +02:00
|
|
|
|
2021-09-07 15:44:45 +02:00
|
|
|
OwnPtr<KBuffer> m_buffer;
|
2019-08-06 20:04:12 +02:00
|
|
|
size_t m_size { 0 };
|
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
}
|