util/AllocatedString: add string_view constructor
Replaces the static Duplicate() method.
This commit is contained in:
parent
32b7b2e2fa
commit
6e1c8edf09
@ -469,7 +469,7 @@ Windows1252ToUTF8(const char *s) noexcept
|
|||||||
* Fallback to not transcoding windows-1252 to utf-8, that may result
|
* Fallback to not transcoding windows-1252 to utf-8, that may result
|
||||||
* in invalid utf-8 unless nonprintable characters are replaced.
|
* 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++)
|
for (size_t i = 0; t[i] != AllocatedString::SENTINEL; i++)
|
||||||
if (!IsPrintableASCII(t[i]))
|
if (!IsPrintableASCII(t[i]))
|
||||||
|
@ -44,7 +44,7 @@ try {
|
|||||||
#ifdef HAVE_ICU
|
#ifdef HAVE_ICU
|
||||||
const auto u = UCharFromUTF8(src);
|
const auto u = UCharFromUTF8(src);
|
||||||
if (u.IsNull())
|
if (u.IsNull())
|
||||||
return AllocatedString::Duplicate(src);
|
return AllocatedString(src);
|
||||||
|
|
||||||
AllocatedArray<UChar> folded(u.size() * 2U);
|
AllocatedArray<UChar> folded(u.size() * 2U);
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ try {
|
|||||||
U_FOLD_CASE_DEFAULT,
|
U_FOLD_CASE_DEFAULT,
|
||||||
&error_code);
|
&error_code);
|
||||||
if (folded_length == 0 || error_code != U_ZERO_ERROR)
|
if (folded_length == 0 || error_code != U_ZERO_ERROR)
|
||||||
return AllocatedString::Duplicate(src);
|
return AllocatedString(src);
|
||||||
|
|
||||||
folded.SetSize(folded_length);
|
folded.SetSize(folded_length);
|
||||||
return UCharToUTF8({folded.begin(), folded.size()});
|
return UCharToUTF8({folded.begin(), folded.size()});
|
||||||
@ -63,7 +63,7 @@ try {
|
|||||||
#error not implemented
|
#error not implemented
|
||||||
#endif
|
#endif
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
return AllocatedString::Duplicate(src);
|
return AllocatedString(src);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* HAVE_ICU_CASE_FOLD */
|
#endif /* HAVE_ICU_CASE_FOLD */
|
||||||
|
@ -46,7 +46,7 @@ IcuCompare::IcuCompare(std::string_view _needle) noexcept
|
|||||||
#else
|
#else
|
||||||
|
|
||||||
IcuCompare::IcuCompare(std::string_view _needle) noexcept
|
IcuCompare::IcuCompare(std::string_view _needle) noexcept
|
||||||
:needle(AllocatedString::Duplicate(_needle)) {}
|
:needle(_needle) {}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -95,7 +95,7 @@ DoConvert(iconv_t conv, std::string_view src)
|
|||||||
if (in_left > 0)
|
if (in_left > 0)
|
||||||
throw std::runtime_error("Charset conversion failed");
|
throw std::runtime_error("Charset conversion failed");
|
||||||
|
|
||||||
return AllocatedString::Duplicate({buffer, sizeof(buffer) - out_left});
|
return AllocatedString({buffer, sizeof(buffer) - out_left});
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@ -151,7 +151,7 @@ IcuConverter::FromUTF8(std::string_view s) const
|
|||||||
throw std::runtime_error(FormatString("Failed to convert from Unicode: %s",
|
throw std::runtime_error(FormatString("Failed to convert from Unicode: %s",
|
||||||
u_errorName(code)).c_str());
|
u_errorName(code)).c_str());
|
||||||
|
|
||||||
return AllocatedString::Duplicate({buffer, size_t(target - buffer)});
|
return AllocatedString({buffer, size_t(target - buffer)});
|
||||||
|
|
||||||
#elif defined(HAVE_ICONV)
|
#elif defined(HAVE_ICONV)
|
||||||
return DoConvert(from_utf8, s);
|
return DoConvert(from_utf8, s);
|
||||||
|
@ -65,6 +65,9 @@ public:
|
|||||||
BasicAllocatedString(std::nullptr_t n) noexcept
|
BasicAllocatedString(std::nullptr_t n) noexcept
|
||||||
:value(n) {}
|
:value(n) {}
|
||||||
|
|
||||||
|
explicit BasicAllocatedString(string_view src) noexcept
|
||||||
|
:value(Duplicate(src)) {}
|
||||||
|
|
||||||
BasicAllocatedString(BasicAllocatedString &&src) noexcept
|
BasicAllocatedString(BasicAllocatedString &&src) noexcept
|
||||||
:value(src.Steal()) {}
|
:value(src.Steal()) {}
|
||||||
|
|
||||||
@ -86,12 +89,6 @@ public:
|
|||||||
return Donate(p);
|
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 {
|
BasicAllocatedString &operator=(BasicAllocatedString &&src) noexcept {
|
||||||
std::swap(value, src.value);
|
std::swap(value, src.value);
|
||||||
return *this;
|
return *this;
|
||||||
@ -138,7 +135,14 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
BasicAllocatedString Clone() const {
|
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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user