AK: Add StringBuilder::join() for joining collections with a separator

This patch adds a generic StringBuilder::join(separator, collection):

    Vector<String> strings = { "well", "hello", "friends" };
    StringBuilder builder;
    builder.join("+ ", strings);
    builder.to_string(); // "well + hello + friends"
This commit is contained in:
Andreas Kling 2020-03-20 14:33:46 +01:00
parent 218f082226
commit 4eef3e5a09

View file

@ -56,6 +56,19 @@ public:
bool is_empty() const { return m_length == 0; }
void trim(size_t count) { m_length -= count; }
template<class SeparatorType, class CollectionType>
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);