AK: Add a fallible StringBuilder::create() factory function

This is nice, and is also used by the Jakt runtime.
This commit is contained in:
Ali Mohammad Pur 2022-12-09 20:15:35 +03:30 committed by Ali Mohammad Pur
parent 21c2d8bd98
commit 543890c5c9
Notes: sideshowbarker 2024-07-17 03:26:24 +09:00
2 changed files with 9 additions and 0 deletions

View file

@ -36,6 +36,13 @@ inline ErrorOr<void> StringBuilder::will_append(size_t size)
return {};
}
ErrorOr<StringBuilder> StringBuilder::create(size_t initial_capacity)
{
StringBuilder builder;
TRY(builder.m_buffer.try_ensure_capacity(initial_capacity));
return builder;
}
StringBuilder::StringBuilder(size_t initial_capacity)
{
m_buffer.ensure_capacity(initial_capacity);

View file

@ -18,6 +18,8 @@ class StringBuilder {
public:
using OutputType = DeprecatedString;
static ErrorOr<StringBuilder> create(size_t initial_capacity = inline_capacity);
explicit StringBuilder(size_t initial_capacity = inline_capacity);
~StringBuilder() = default;