mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 14:07:45 +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
1 changed files with 13 additions and 0 deletions
|
@ -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…
Add table
Add a link
Reference in a new issue