mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-15 20:33:10 +00:00
AK: Add StringBuilder::try_join()
This is a failable version of StringBuilder::join().
This commit is contained in:
parent
0c688fa117
commit
2366265c53
|
@ -78,15 +78,21 @@ public:
|
||||||
|
|
||||||
template<class SeparatorType, class CollectionType>
|
template<class SeparatorType, class CollectionType>
|
||||||
void join(SeparatorType const& separator, CollectionType const& collection, StringView fmtstr = "{}"sv)
|
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;
|
bool first = true;
|
||||||
for (auto& item : collection) {
|
for (auto& item : collection) {
|
||||||
if (first)
|
if (!first)
|
||||||
|
TRY(try_append(separator));
|
||||||
|
TRY(try_appendff(fmtstr, item));
|
||||||
first = false;
|
first = false;
|
||||||
else
|
|
||||||
append(separator);
|
|
||||||
appendff(fmtstr, item);
|
|
||||||
}
|
}
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
Loading…
Reference in a new issue