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