From 777c232e162b1ea1d563821fb8ded3c0450db1a3 Mon Sep 17 00:00:00 2001 From: Matthew Olsson Date: Sun, 23 May 2021 16:14:48 -0700 Subject: [PATCH] AK: Add String::repeated(StringView, size_t count) --- AK/String.cpp | 12 ++++++++++++ AK/String.h | 1 + 2 files changed, 13 insertions(+) diff --git a/AK/String.cpp b/AK/String.cpp index ed5b51a4f3a..f8301e5bded 100644 --- a/AK/String.cpp +++ b/AK/String.cpp @@ -234,6 +234,7 @@ bool String::ends_with(char ch) const return false; return characters()[length() - 1] == ch; } + String String::repeated(char ch, size_t count) { if (!count) @@ -244,6 +245,17 @@ String String::repeated(char ch, size_t count) return *impl; } +String String::repeated(const StringView& string, size_t count) +{ + if (!count || string.is_empty()) + return empty(); + char* buffer; + auto impl = StringImpl::create_uninitialized(count * string.length(), buffer); + for (size_t i = 0; i < count; i++) + __builtin_memcpy(buffer + i * string.length(), string.characters_without_null_termination(), string.length()); + return *impl; +} + String String::bijective_base_from(size_t value, unsigned base, StringView map) { if (map.is_null()) diff --git a/AK/String.h b/AK/String.h index 9a9ed106782..56ef4162001 100644 --- a/AK/String.h +++ b/AK/String.h @@ -92,6 +92,7 @@ public: String(const FlyString&); [[nodiscard]] static String repeated(char, size_t count); + [[nodiscard]] static String repeated(const StringView&, size_t count); [[nodiscard]] static String bijective_base_from(size_t value, unsigned base = 26, StringView map = {});