mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
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:
parent
d589898f5b
commit
62b8ccaffc
Notes:
sideshowbarker
2024-07-17 11:06:06 +09:00
Author: https://github.com/LucasChollet Commit: https://github.com/SerenityOS/serenity/commit/62b8ccaffc Pull-request: https://github.com/SerenityOS/serenity/pull/14529 Reviewed-by: https://github.com/AtkinsSJ ✅ Reviewed-by: https://github.com/kleinesfilmroellchen
2 changed files with 15 additions and 0 deletions
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue