From 9ebed7d8d5654cf0ef7def5c603afc117344a943 Mon Sep 17 00:00:00 2001 From: stasoid Date: Tue, 22 Oct 2024 11:26:12 +0500 Subject: [PATCH] AK: Add StringBuilder::append_repeated(StringView, size_t) By analogy with append_repeated(char, size_t) --- AK/StringBuilder.cpp | 15 +++++++++++++++ AK/StringBuilder.h | 2 ++ 2 files changed, 17 insertions(+) diff --git a/AK/StringBuilder.cpp b/AK/StringBuilder.cpp index 28d1c3126d0..a2c1b6d3b02 100644 --- a/AK/StringBuilder.cpp +++ b/AK/StringBuilder.cpp @@ -105,6 +105,16 @@ ErrorOr StringBuilder::try_append_repeated(char ch, size_t n) return {}; } +ErrorOr StringBuilder::try_append_repeated(StringView string, size_t n) +{ + if (string.is_empty()) + return {}; + TRY(will_append(string.length() * n)); + for (size_t i = 0; i < n; ++i) + TRY(try_append(string)); + return {}; +} + void StringBuilder::append(StringView string) { MUST(try_append(string)); @@ -130,6 +140,11 @@ void StringBuilder::append_repeated(char ch, size_t n) MUST(try_append_repeated(ch, n)); } +void StringBuilder::append_repeated(StringView string, size_t n) +{ + MUST(try_append_repeated(string, n)); +} + ErrorOr StringBuilder::to_byte_buffer() const { return ByteBuffer::copy(data(), length()); diff --git a/AK/StringBuilder.h b/AK/StringBuilder.h index 0d19552040d..b1d71949d7d 100644 --- a/AK/StringBuilder.h +++ b/AK/StringBuilder.h @@ -39,6 +39,7 @@ public: } ErrorOr try_append(char const*, size_t); ErrorOr try_append_repeated(char, size_t); + ErrorOr try_append_repeated(StringView, size_t); ErrorOr try_append_escaped_for_json(StringView); void append(StringView); @@ -49,6 +50,7 @@ public: void append(char const*, size_t); void appendvf(char const*, va_list); void append_repeated(char, size_t); + void append_repeated(StringView, size_t); void append_as_lowercase(char); void append_escaped_for_json(StringView);