From 9296af5566afea4ba4cb78b374ef3ee0bf9bc04b Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Sun, 6 Aug 2023 18:49:42 -0700 Subject: Use a string_view for the backend open method --- common/strutils.cpp | 17 +++++++++-------- common/strutils.h | 5 +++-- 2 files changed, 12 insertions(+), 10 deletions(-) (limited to 'common') diff --git a/common/strutils.cpp b/common/strutils.cpp index 355cd030..f7868e2e 100644 --- a/common/strutils.cpp +++ b/common/strutils.cpp @@ -10,31 +10,32 @@ #define WIN32_LEAN_AND_MEAN #include -std::string wstr_to_utf8(const WCHAR *wstr) +std::string wstr_to_utf8(std::wstring_view wstr) { std::string ret; - int len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, nullptr, 0, nullptr, nullptr); + int len{WideCharToMultiByte(CP_UTF8, 0, wstr.data(), static_cast(wstr.length()), nullptr, + 0, nullptr, nullptr)}; if(len > 0) { ret.resize(len); - WideCharToMultiByte(CP_UTF8, 0, wstr, -1, &ret[0], len, nullptr, nullptr); - ret.pop_back(); + WideCharToMultiByte(CP_UTF8, 0, wstr.data(), static_cast(wstr.length()), &ret[0], len, + nullptr, nullptr); } return ret; } -std::wstring utf8_to_wstr(const char *str) +std::wstring utf8_to_wstr(std::string_view str) { std::wstring ret; - int len = MultiByteToWideChar(CP_UTF8, 0, str, -1, nullptr, 0); + int len{MultiByteToWideChar(CP_UTF8, 0, str.data(), static_cast(str.length()), nullptr, + 0)}; if(len > 0) { ret.resize(len); - MultiByteToWideChar(CP_UTF8, 0, str, -1, &ret[0], len); - ret.pop_back(); + MultiByteToWideChar(CP_UTF8, 0, str.data(), static_cast(str.length()), &ret[0], len); } return ret; diff --git a/common/strutils.h b/common/strutils.h index 67f057a7..7eee0c1d 100644 --- a/common/strutils.h +++ b/common/strutils.h @@ -5,10 +5,11 @@ #include #ifdef _WIN32 +#include #include -std::string wstr_to_utf8(const wchar_t *wstr); -std::wstring utf8_to_wstr(const char *str); +std::string wstr_to_utf8(std::wstring_view wstr); +std::wstring utf8_to_wstr(std::string_view str); #endif namespace al { -- cgit v1.2.3