util/AllocatedString: add concatenating constructor

This commit is contained in:
Max Kellermann 2021-03-17 18:39:18 +01:00 committed by Max Kellermann
parent e07e0bc9c1
commit 52f46b94e9

View File

@ -71,6 +71,18 @@ public:
explicit BasicAllocatedString(const_pointer src)
:value(Duplicate(src)) {}
/**
* Concatenate several strings.
*/
BasicAllocatedString(std::initializer_list<string_view> 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<string_view> src) noexcept {
std::size_t size = 0;
for (std::string_view i : src)
size += i.size();
return size;
}
};
class AllocatedString : public BasicAllocatedString<char> {