diff --git a/src/lib/icu/Util.cxx b/src/lib/icu/Util.cxx index 74904dd09..34076f398 100644 --- a/src/lib/icu/Util.cxx +++ b/src/lib/icu/Util.cxx @@ -31,18 +31,15 @@ #include AllocatedArray -UCharFromUTF8(const char *src) +UCharFromUTF8(std::string_view src) { - assert(src != nullptr); - - const size_t src_length = strlen(src); - const size_t dest_capacity = src_length; + const size_t dest_capacity = src.size(); AllocatedArray dest(dest_capacity); UErrorCode error_code = U_ZERO_ERROR; int32_t dest_length; u_strFromUTF8(dest.begin(), dest_capacity, &dest_length, - src, src_length, + src.data(), src.size(), &error_code); if (U_FAILURE(error_code)) throw std::runtime_error(u_errorName(error_code)); @@ -52,19 +49,17 @@ UCharFromUTF8(const char *src) } AllocatedString<> -UCharToUTF8(ConstBuffer src) +UCharToUTF8(std::basic_string_view src) { - assert(!src.IsNull()); - /* worst-case estimate */ - size_t dest_capacity = 4 * src.size; + size_t dest_capacity = 4 * src.size(); std::unique_ptr dest(new char[dest_capacity + 1]); UErrorCode error_code = U_ZERO_ERROR; int32_t dest_length; u_strToUTF8(dest.get(), dest_capacity, &dest_length, - src.data, src.size, + src.data(), src.size(), &error_code); if (U_FAILURE(error_code)) throw std::runtime_error(u_errorName(error_code)); diff --git a/src/lib/icu/Util.hxx b/src/lib/icu/Util.hxx index 89ce3e597..4d71cf066 100644 --- a/src/lib/icu/Util.hxx +++ b/src/lib/icu/Util.hxx @@ -22,7 +22,8 @@ #include -template struct ConstBuffer; +#include + template class AllocatedArray; template class AllocatedString; @@ -32,7 +33,7 @@ template class AllocatedString; * Throws std::runtime_error on error. */ AllocatedArray -UCharFromUTF8(const char *src); +UCharFromUTF8(std::string_view src); /** * Wrapper for u_strToUTF8(). @@ -40,6 +41,6 @@ UCharFromUTF8(const char *src); * Throws std::runtime_error on error. */ AllocatedString -UCharToUTF8(ConstBuffer src); +UCharToUTF8(std::basic_string_view src); #endif