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 1caf57644f
commit a81c9bfb81
5 changed files with 18 additions and 14 deletions

View File

@ -469,7 +469,7 @@ Windows1252ToUTF8(const char *s) noexcept
* Fallback to not transcoding windows-1252 to utf-8, that may result
* in invalid utf-8 unless nonprintable characters are replaced.
*/
auto t = AllocatedString::Duplicate(s);
AllocatedString t(s);
for (size_t i = 0; t[i] != AllocatedString::SENTINEL; i++)
if (!IsPrintableASCII(t[i]))

View File

@ -44,7 +44,7 @@ try {
#ifdef HAVE_ICU
const auto u = UCharFromUTF8(src);
if (u.IsNull())
return AllocatedString::Duplicate(src);
return AllocatedString(src);
AllocatedArray<UChar> folded(u.size() * 2U);
@ -54,7 +54,7 @@ try {
U_FOLD_CASE_DEFAULT,
&error_code);
if (folded_length == 0 || error_code != U_ZERO_ERROR)
return AllocatedString::Duplicate(src);
return AllocatedString(src);
folded.SetSize(folded_length);
return UCharToUTF8({folded.begin(), folded.size()});
@ -63,7 +63,7 @@ try {
#error not implemented
#endif
} catch (...) {
return AllocatedString::Duplicate(src);
return AllocatedString(src);
}
#endif /* HAVE_ICU_CASE_FOLD */

View File

@ -46,7 +46,7 @@ IcuCompare::IcuCompare(std::string_view _needle) noexcept
#else
IcuCompare::IcuCompare(std::string_view _needle) noexcept
:needle(AllocatedString::Duplicate(_needle)) {}
:needle(_needle) {}
#endif

View File

@ -95,7 +95,7 @@ DoConvert(iconv_t conv, std::string_view src)
if (in_left > 0)
throw std::runtime_error("Charset conversion failed");
return AllocatedString::Duplicate({buffer, sizeof(buffer) - out_left});
return AllocatedString({buffer, sizeof(buffer) - out_left});
}
#endif
@ -151,7 +151,7 @@ IcuConverter::FromUTF8(std::string_view s) const
throw std::runtime_error(FormatString("Failed to convert from Unicode: %s",
u_errorName(code)).c_str());
return AllocatedString::Duplicate({buffer, size_t(target - buffer)});
return AllocatedString({buffer, size_t(target - buffer)});
#elif defined(HAVE_ICONV)
return DoConvert(from_utf8, s);

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