diff options
author | Max Kellermann <mk@cm4all.com> | 2021-01-14 13:16:52 +0100 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2021-03-04 18:04:11 +0100 |
commit | 6e1c8edf095bb89aa50db4bb54e4507320f14e76 (patch) | |
tree | 1fc08d1088baf00d2a110d09fba01c03f43eb437 /src/util | |
parent | 32b7b2e2fa1d2acbadea0390b63872451591ff5b (diff) |
util/AllocatedString: add string_view constructor
Replaces the static Duplicate() method.
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/AllocatedString.hxx | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/src/util/AllocatedString.hxx b/src/util/AllocatedString.hxx index 2a5aaa02d..6d02aa25e 100644 --- a/src/util/AllocatedString.hxx +++ b/src/util/AllocatedString.hxx @@ -65,6 +65,9 @@ public: BasicAllocatedString(std::nullptr_t n) noexcept :value(n) {} + explicit BasicAllocatedString(string_view src) noexcept + :value(Duplicate(src)) {} + BasicAllocatedString(BasicAllocatedString &&src) noexcept :value(src.Steal()) {} @@ -86,12 +89,6 @@ public: return Donate(p); } - static BasicAllocatedString Duplicate(string_view src) { - auto p = new value_type[src.size() + 1]; - *std::copy_n(src.data(), src.size(), p) = SENTINEL; - return Donate(p); - } - BasicAllocatedString &operator=(BasicAllocatedString &&src) noexcept { std::swap(value, src.value); return *this; @@ -138,7 +135,14 @@ public: } BasicAllocatedString Clone() const { - return Duplicate(*this); + return BasicAllocatedString(Duplicate(*this)); + } + +private: + static pointer Duplicate(string_view src) { + auto p = new value_type[src.size() + 1]; + *std::copy_n(src.data(), src.size(), p) = SENTINEL; + return p; } }; |