From 52f46b94e93fff5e6c981ce13cb914b4743383a4 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 17 Mar 2021 18:39:18 +0100 Subject: [PATCH] util/AllocatedString: add concatenating constructor --- src/util/AllocatedString.hxx | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/util/AllocatedString.hxx b/src/util/AllocatedString.hxx index 27c047514..330335c3e 100644 --- a/src/util/AllocatedString.hxx +++ b/src/util/AllocatedString.hxx @@ -71,6 +71,18 @@ public: explicit BasicAllocatedString(const_pointer src) :value(Duplicate(src)) {} + /** + * Concatenate several strings. + */ + BasicAllocatedString(std::initializer_list src) + :value(new value_type[TotalSize(src) + 1]) + { + auto *p = value; + for (const auto i : src) + p = std::copy(i.begin(), i.end(), p); + *p = SENTINEL; + } + BasicAllocatedString(const BasicAllocatedString &src) noexcept :BasicAllocatedString(Duplicate(src.value)) {} @@ -158,6 +170,13 @@ private: ? Duplicate(string_view(src)) : nullptr; } + + static constexpr std::size_t TotalSize(std::initializer_list src) noexcept { + std::size_t size = 0; + for (std::string_view i : src) + size += i.size(); + return size; + } }; class AllocatedString : public BasicAllocatedString {