mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +00:00
AK: Add StringBuilder::try_join()
This is a failable version of StringBuilder::join().
This commit is contained in:
parent
0c688fa117
commit
2366265c53
Notes:
sideshowbarker
2024-07-17 17:49:11 +09:00
Author: https://github.com/MacDue Commit: https://github.com/SerenityOS/serenity/commit/2366265c53 Pull-request: https://github.com/SerenityOS/serenity/pull/17001
1 changed files with 11 additions and 5 deletions
|
@ -78,15 +78,21 @@ public:
|
|||
|
||||
template<class SeparatorType, class CollectionType>
|
||||
void join(SeparatorType const& separator, CollectionType const& collection, StringView fmtstr = "{}"sv)
|
||||
{
|
||||
MUST(try_join(separator, collection, fmtstr));
|
||||
}
|
||||
|
||||
template<class SeparatorType, class CollectionType>
|
||||
ErrorOr<void> try_join(SeparatorType const& separator, CollectionType const& collection, StringView fmtstr = "{}"sv)
|
||||
{
|
||||
bool first = true;
|
||||
for (auto& item : collection) {
|
||||
if (first)
|
||||
first = false;
|
||||
else
|
||||
append(separator);
|
||||
appendff(fmtstr, item);
|
||||
if (!first)
|
||||
TRY(try_append(separator));
|
||||
TRY(try_appendff(fmtstr, item));
|
||||
first = false;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
Loading…
Add table
Reference in a new issue