From 2366265c535a0fc4509e76233e3fa72aaacae6eb Mon Sep 17 00:00:00 2001 From: MacDue Date: Sat, 14 Jan 2023 00:08:18 +0000 Subject: [PATCH] AK: Add StringBuilder::try_join() This is a failable version of StringBuilder::join(). --- AK/StringBuilder.h | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/AK/StringBuilder.h b/AK/StringBuilder.h index 98299ea86b..f6a5a5e320 100644 --- a/AK/StringBuilder.h +++ b/AK/StringBuilder.h @@ -78,15 +78,21 @@ public: template void join(SeparatorType const& separator, CollectionType const& collection, StringView fmtstr = "{}"sv) + { + MUST(try_join(separator, collection, fmtstr)); + } + + template + ErrorOr 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: