AK: Hide non fallible ByteBuffer and StringBuilder APIs in the kernel

This follows what we already do in structure like the Vector.
This commit is contained in:
Undefine authored and Lucas Chollet committed 2026-11-06 20:34:06 +01:00
1 parent 9ae1edc095
commit 7d522782c2
3 files changed
+55 -41

No files matched your search

+38 -32
View File
@@ -54,6 +54,7 @@ public:
return *this;
}
#ifndef KERNEL
ByteBuffer& operator=(ByteBuffer const& other)
{
if (this != &other) {
@@ -66,6 +67,7 @@ public:
}
return *this;
}
#endif
[[nodiscard]] static ErrorOr<ByteBuffer> create_uninitialized(size_t size)
{
@@ -184,16 +186,6 @@ public:
m_size = 0;
}
enum class ZeroFillNewElements {
No,
Yes,
};
ALWAYS_INLINE void resize(size_t new_size, ZeroFillNewElements zero_fill_new_elements = ZeroFillNewElements::No)
{
MUST(try_resize(new_size, zero_fill_new_elements));
}
void trim(size_t size, bool may_discard_existing_data)
{
VERIFY(size <= m_size);
@@ -202,10 +194,10 @@ public:
m_size = size;
}
ALWAYS_INLINE void ensure_capacity(size_t new_capacity)
{
MUST(try_ensure_capacity(new_capacity));
}
enum class ZeroFillNewElements {
No,
Yes,
};
ErrorOr<void> try_resize(size_t new_size, ZeroFillNewElements zero_fill_new_elements = ZeroFillNewElements::No)
{
@@ -239,24 +231,6 @@ public:
return Bytes { data() + old_size, length };
}
/// Like get_bytes_for_writing, but crashes if allocation fails.
Bytes must_get_bytes_for_writing(size_t length)
{
return MUST(get_bytes_for_writing(length));
}
void append(u8 byte)
{
MUST(try_append(byte));
}
void append(ReadonlyBytes bytes)
{
MUST(try_append(bytes));
}
void append(void const* data, size_t data_size) { append({ data, data_size }); }
ErrorOr<void> try_append(u8 byte)
{
auto old_size = size();
@@ -283,10 +257,42 @@ public:
return {};
}
#ifndef KERNEL
ALWAYS_INLINE void resize(size_t new_size, ZeroFillNewElements zero_fill_new_elements = ZeroFillNewElements::No)
{
MUST(try_resize(new_size, zero_fill_new_elements));
}
ALWAYS_INLINE void ensure_capacity(size_t new_capacity)
{
MUST(try_ensure_capacity(new_capacity));
}
Bytes must_get_bytes_for_writing(size_t length)
{
return MUST(get_bytes_for_writing(length));
}
void append(u8 byte)
{
MUST(try_append(byte));
}
void append(ReadonlyBytes bytes)
{
MUST(try_append(bytes));
}
void append(void const* data, size_t data_size)
{
append({ data, data_size });
}
void operator+=(ByteBuffer const& other)
{
MUST(try_append(other.data(), other.size()));
}
#endif
void overwrite(size_t offset, void const* data, size_t data_size)
{
+12 -6
View File
@@ -55,10 +55,12 @@ ErrorOr<StringBuilder> StringBuilder::create(size_t initial_capacity)
return builder;
}
#ifndef KERNEL
StringBuilder::StringBuilder(size_t initial_capacity)
{
m_buffer.ensure_capacity(initial_capacity);
}
#endif
StringBuilder::StringBuilder(UseInlineCapacityOnly use_inline_capacity_only)
: m_use_inline_capacity_only(use_inline_capacity_only)
@@ -105,16 +107,17 @@ ErrorOr<void> StringBuilder::try_append_repeated(char ch, size_t n)
return {};
}
void StringBuilder::append(StringView string)
{
MUST(try_append(string));
}
ErrorOr<void> StringBuilder::try_append(char const* characters, size_t length)
{
return try_append(StringView { characters, length });
}
#ifndef KERNEL
void StringBuilder::append(StringView string)
{
MUST(try_append(string));
}
void StringBuilder::append(char const* characters, size_t length)
{
MUST(try_append(characters, length));
@@ -137,6 +140,7 @@ void StringBuilder::append_repeated(char ch, size_t n)
{
MUST(try_append_repeated(ch, n));
}
#endif
ErrorOr<ByteBuffer> StringBuilder::to_byte_buffer() const
{
@@ -203,6 +207,7 @@ ErrorOr<void> StringBuilder::try_append_code_point(u32 code_point)
return {};
}
#ifndef KERNEL
void StringBuilder::append_code_point(u32 code_point)
{
if (code_point <= 0x7f) {
@@ -230,7 +235,6 @@ void StringBuilder::append_code_point(u32 code_point)
}
}
#ifndef KERNEL
ErrorOr<void> StringBuilder::try_append(Utf16View const& utf16_view)
{
// NOTE: This may under-allocate in the presence of surrogate pairs.
@@ -269,6 +273,7 @@ ErrorOr<void> StringBuilder::try_append(Utf32View const& utf32_view)
return {};
}
#ifndef KERNEL
void StringBuilder::append(Utf32View const& utf32_view)
{
MUST(try_append(utf32_view));
@@ -286,6 +291,7 @@ void StringBuilder::append_escaped_for_json(StringView string)
{
MUST(try_append_escaped_for_json(string));
}
#endif
ErrorOr<void> StringBuilder::try_append_escaped_for_json(StringView string)
{
+5 -3
View File
@@ -22,7 +22,11 @@ public:
static ErrorOr<StringBuilder> create(size_t initial_capacity = inline_capacity);
#ifdef KERNEL
StringBuilder() = default;
#else
explicit StringBuilder(size_t initial_capacity = inline_capacity);
#endif
enum class UseInlineCapacityOnly {
Yes,
@@ -48,10 +52,9 @@ public:
ErrorOr<void> try_append_repeated(char, size_t);
ErrorOr<void> try_append_escaped_for_json(StringView);
void append(StringView);
#ifndef KERNEL
void append(StringView);
void append(Utf16View const&);
#endif
void append(Utf32View const&);
void append(char);
void append_code_point(u32);
@@ -69,7 +72,6 @@ public:
MUST(vformat(*this, fmtstr.view(), variadic_format_params));
}
#ifndef KERNEL
[[nodiscard]] ByteString to_byte_string() const;
#endif