diff --git a/src/decoder/plugins/SidplayDecoderPlugin.cxx b/src/decoder/plugins/SidplayDecoderPlugin.cxx index edc0ab028..ba13e6ff4 100644 --- a/src/decoder/plugins/SidplayDecoderPlugin.cxx +++ b/src/decoder/plugins/SidplayDecoderPlugin.cxx @@ -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])) diff --git a/src/lib/icu/CaseFold.cxx b/src/lib/icu/CaseFold.cxx index ebb75f324..ec7ca80fb 100644 --- a/src/lib/icu/CaseFold.cxx +++ b/src/lib/icu/CaseFold.cxx @@ -44,7 +44,7 @@ try { #ifdef HAVE_ICU const auto u = UCharFromUTF8(src); if (u.IsNull()) - return AllocatedString::Duplicate(src); + return AllocatedString(src); AllocatedArray 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 */ diff --git a/src/lib/icu/Compare.cxx b/src/lib/icu/Compare.cxx index 6bc82bdd5..dfc5d2210 100644 --- a/src/lib/icu/Compare.cxx +++ b/src/lib/icu/Compare.cxx @@ -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 diff --git a/src/lib/icu/Converter.cxx b/src/lib/icu/Converter.cxx index a231668a8..6123c2490 100644 --- a/src/lib/icu/Converter.cxx +++ b/src/lib/icu/Converter.cxx @@ -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); 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; } };