mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-15 04:13:11 +00:00
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:
parent
218f082226
commit
4eef3e5a09
|
@ -56,6 +56,19 @@ public:
|
||||||
bool is_empty() const { return m_length == 0; }
|
bool is_empty() const { return m_length == 0; }
|
||||||
void trim(size_t count) { m_length -= count; }
|
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:
|
private:
|
||||||
void will_append(size_t);
|
void will_append(size_t);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue