From 4eef3e5a09f6dcdddb654e05a77831ef27a3a537 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 20 Mar 2020 14:33:46 +0100 Subject: [PATCH] AK: Add StringBuilder::join() for joining collections with a separator This patch adds a generic StringBuilder::join(separator, collection): Vector strings = { "well", "hello", "friends" }; StringBuilder builder; builder.join("+ ", strings); builder.to_string(); // "well + hello + friends" --- AK/StringBuilder.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/AK/StringBuilder.h b/AK/StringBuilder.h index 139fe3017a2..fc627b230bb 100644 --- a/AK/StringBuilder.h +++ b/AK/StringBuilder.h @@ -56,6 +56,19 @@ public: bool is_empty() const { return m_length == 0; } void trim(size_t count) { m_length -= count; } + template + void join(const SeparatorType& separator, const CollectionType& collection) + { + bool first = true; + for (auto& item : collection) { + if (first) + first = false; + else + append(separator); + append(item); + } + } + private: void will_append(size_t);