Serenity Operating System
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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"

+13
+13
AK/StringBuilder.h
··· 56 56 bool is_empty() const { return m_length == 0; } 57 57 void trim(size_t count) { m_length -= count; } 58 58 59 + template<class SeparatorType, class CollectionType> 60 + void join(const SeparatorType& separator, const CollectionType& collection) 61 + { 62 + bool first = true; 63 + for (auto& item : collection) { 64 + if (first) 65 + first = false; 66 + else 67 + append(separator); 68 + append(item); 69 + } 70 + } 71 + 59 72 private: 60 73 void will_append(size_t); 61 74