From 9dc530ab5109188ca4cdceefdaeb21fca7663de0 Mon Sep 17 00:00:00 2001 From: Max Kellermann <max@musicpd.org> Date: Fri, 3 Apr 2020 15:06:21 +0200 Subject: [PATCH] lib/icu/Util: pass std::string_view --- src/lib/icu/Util.cxx | 17 ++++++----------- src/lib/icu/Util.hxx | 7 ++++--- 2 files changed, 10 insertions(+), 14 deletions(-) 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 <string.h> AllocatedArray<UChar> -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<UChar> 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<UChar> src) +UCharToUTF8(std::basic_string_view<UChar> src) { - assert(!src.IsNull()); - /* worst-case estimate */ - size_t dest_capacity = 4 * src.size; + size_t dest_capacity = 4 * src.size(); std::unique_ptr<char[]> 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 <unicode/utypes.h> -template<typename T> struct ConstBuffer; +#include <string_view> + template<typename T> class AllocatedArray; template<typename T> class AllocatedString; @@ -32,7 +33,7 @@ template<typename T> class AllocatedString; * Throws std::runtime_error on error. */ AllocatedArray<UChar> -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<char> -UCharToUTF8(ConstBuffer<UChar> src); +UCharToUTF8(std::basic_string_view<UChar> src); #endif