diff --git a/src/lib/icu/CaseFold.cxx b/src/lib/icu/CaseFold.cxx index d41240c30..b4fe9d8bc 100644 --- a/src/lib/icu/CaseFold.cxx +++ b/src/lib/icu/CaseFold.cxx @@ -81,13 +81,15 @@ try { return AllocatedString<>::Duplicate(src); std::unique_ptr buffer(new wchar_t[size]); - if (LCMapStringEx(LOCALE_NAME_INVARIANT, - LCMAP_SORTKEY|LINGUISTIC_IGNORECASE, - u.c_str(), -1, buffer.get(), size, - nullptr, nullptr, 0) <= 0) + int result = LCMapStringEx(LOCALE_NAME_INVARIANT, + LCMAP_SORTKEY|LINGUISTIC_IGNORECASE, + u.c_str(), -1, buffer.get(), size, + nullptr, nullptr, 0); + if (result <= 0) return AllocatedString<>::Duplicate(src); - return WideCharToMultiByte(CP_UTF8, buffer.get()); + return WideCharToMultiByte(CP_UTF8, + {buffer.get(), size_t(result - 1)}); #else #error not implemented diff --git a/src/lib/icu/Win32.cxx b/src/lib/icu/Win32.cxx index bef7d2009..c44d40a8f 100644 --- a/src/lib/icu/Win32.cxx +++ b/src/lib/icu/Win32.cxx @@ -26,15 +26,16 @@ #include AllocatedString -WideCharToMultiByte(unsigned code_page, const wchar_t *src) +WideCharToMultiByte(unsigned code_page, std::wstring_view src) { - int length = WideCharToMultiByte(code_page, 0, src, -1, nullptr, 0, + int length = WideCharToMultiByte(code_page, 0, src.data(), src.size(), + nullptr, 0, nullptr, nullptr); if (length <= 0) throw MakeLastError("Failed to convert from Unicode"); std::unique_ptr buffer(new char[length]); - length = WideCharToMultiByte(code_page, 0, src, -1, + length = WideCharToMultiByte(code_page, 0, src.data(), src.size(), buffer.get(), length, nullptr, nullptr); if (length <= 0) @@ -44,14 +45,15 @@ WideCharToMultiByte(unsigned code_page, const wchar_t *src) } AllocatedString -MultiByteToWideChar(unsigned code_page, const char *src) +MultiByteToWideChar(unsigned code_page, std::string_view src) { - int length = MultiByteToWideChar(code_page, 0, src, -1, nullptr, 0); + int length = MultiByteToWideChar(code_page, 0, src.data(), src.size(), + nullptr, 0); if (length <= 0) throw MakeLastError("Failed to convert to Unicode"); std::unique_ptr buffer(new wchar_t[length]); - length = MultiByteToWideChar(code_page, 0, src, -1, + length = MultiByteToWideChar(code_page, 0, src.data(), src.size(), buffer.get(), length); if (length <= 0) throw MakeLastError("Failed to convert to Unicode"); diff --git a/src/lib/icu/Win32.hxx b/src/lib/icu/Win32.hxx index 3332d5896..060f2bb92 100644 --- a/src/lib/icu/Win32.hxx +++ b/src/lib/icu/Win32.hxx @@ -22,7 +22,7 @@ #include "util/Compiler.h" -#include +#include template class AllocatedString; @@ -31,13 +31,13 @@ template class AllocatedString; */ gcc_pure gcc_nonnull_all AllocatedString -WideCharToMultiByte(unsigned code_page, const wchar_t *src); +WideCharToMultiByte(unsigned code_page, std::wstring_view src); /** * Throws std::system_error on error. */ gcc_pure gcc_nonnull_all AllocatedString -MultiByteToWideChar(unsigned code_page, const char *src); +MultiByteToWideChar(unsigned code_page, std::string_view src); #endif