StringBuilder: Add try_append_repeated() and append_repeated()

This two methods add the character as many times as specified by the
second parameter.
This commit is contained in:
Lucas CHOLLET 2022-01-04 23:37:15 +01:00 committed by Sam Atkins
parent d589898f5b
commit 62b8ccaffc
Notes: sideshowbarker 2024-07-17 11:06:06 +09:00
2 changed files with 15 additions and 0 deletions

View file

@ -56,6 +56,14 @@ ErrorOr<void> StringBuilder::try_append(char ch)
return {};
}
ErrorOr<void> StringBuilder::try_append_repeated(char ch, size_t n)
{
TRY(will_append(n));
for (size_t i = 0; i < n; ++i)
TRY(try_append(ch));
return {};
}
void StringBuilder::append(StringView string)
{
MUST(try_append(string));
@ -84,6 +92,11 @@ void StringBuilder::appendvf(char const* fmt, va_list ap)
nullptr, fmt, ap);
}
void StringBuilder::append_repeated(char ch, size_t n)
{
MUST(try_append_repeated(ch, n));
}
ByteBuffer StringBuilder::to_byte_buffer() const
{
// FIXME: Handle OOM failure.

View file

@ -35,6 +35,7 @@ public:
return vformat(*this, fmtstr.view(), variadic_format_params);
}
ErrorOr<void> try_append(char const*, size_t);
ErrorOr<void> try_append_repeated(char, size_t);
ErrorOr<void> try_append_escaped_for_json(StringView);
void append(StringView);
@ -46,6 +47,7 @@ public:
void append_code_point(u32);
void append(char const*, size_t);
void appendvf(char const*, va_list);
void append_repeated(char, size_t);
void append_as_lowercase(char);
void append_escaped_for_json(StringView);