mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +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
Notes:
sideshowbarker
2024-07-19 08:13:05 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/4eef3e5a09f
1 changed files with 13 additions and 0 deletions
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in a new issue