util/AllocatedString: add string_view constructor

Replaces the static Duplicate() method.
This commit is contained in:
Max Kellermann
2021-01-14 13:16:52 +01:00
committed by Max Kellermann
parent 32b7b2e2fa
commit 6e1c8edf09
5 changed files with 18 additions and 14 deletions

View File

@@ -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;
}
};