From 09d8e44d569aebb26628e3fa3be58b061b7a2c50 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 3 Apr 2020 15:04:57 +0200 Subject: [PATCH] lib/icu/Converter: pass std::string_view --- src/lib/icu/Converter.cxx | 14 +++++++------- src/lib/icu/Converter.hxx | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/lib/icu/Converter.cxx b/src/lib/icu/Converter.cxx index 2473e799e..384851750 100644 --- a/src/lib/icu/Converter.cxx +++ b/src/lib/icu/Converter.cxx @@ -78,13 +78,13 @@ IcuConverter::Create(const char *charset) #elif defined(HAVE_ICONV) static AllocatedString -DoConvert(iconv_t conv, const char *src) +DoConvert(iconv_t conv, std::string_view src) { // TODO: dynamic buffer? char buffer[4096]; - char *in = const_cast(src); + char *in = const_cast(src.data()); char *out = buffer; - size_t in_left = strlen(src); + size_t in_left = src.size(); size_t out_left = sizeof(buffer); size_t n = iconv(conv, &in, &in_left, &out, &out_left); @@ -101,7 +101,7 @@ DoConvert(iconv_t conv, const char *src) #endif AllocatedString -IcuConverter::ToUTF8(const char *s) const +IcuConverter::ToUTF8(std::string_view s) const { #ifdef HAVE_ICU const std::lock_guard protect(mutex); @@ -110,12 +110,12 @@ IcuConverter::ToUTF8(const char *s) const // TODO: dynamic buffer? UChar buffer[4096], *target = buffer; - const char *source = s; + const char *source = s.data(); UErrorCode code = U_ZERO_ERROR; ucnv_toUnicode(converter, &target, buffer + std::size(buffer), - &source, source + strlen(source), + &source, source + s.size(), nullptr, true, &code); if (code != U_ZERO_ERROR) throw std::runtime_error(FormatString("Failed to convert to Unicode: %s", @@ -129,7 +129,7 @@ IcuConverter::ToUTF8(const char *s) const } AllocatedString -IcuConverter::FromUTF8(const char *s) const +IcuConverter::FromUTF8(std::string_view s) const { #ifdef HAVE_ICU const std::lock_guard protect(mutex); diff --git a/src/lib/icu/Converter.hxx b/src/lib/icu/Converter.hxx index 00d51fde3..f7e3eabe3 100644 --- a/src/lib/icu/Converter.hxx +++ b/src/lib/icu/Converter.hxx @@ -84,7 +84,7 @@ public: * Throws std::runtime_error on error. */ gcc_nonnull_all - AllocatedString ToUTF8(const char *s) const; + AllocatedString ToUTF8(std::string_view s) const; /** * Convert the string from UTF-8. @@ -92,7 +92,7 @@ public: * Throws std::runtime_error on error. */ gcc_nonnull_all - AllocatedString FromUTF8(const char *s) const; + AllocatedString FromUTF8(std::string_view s) const; }; #endif