diff options
author | Chris Robinson <[email protected]> | 2023-08-06 18:49:42 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2023-08-06 18:49:42 -0700 |
commit | 9296af5566afea4ba4cb78b374ef3ee0bf9bc04b (patch) | |
tree | ac71d8896956166aed320ed38ffb32847bc2cba7 /common | |
parent | 05d80f9b3283d7686a27e5d3ed0bac6086669368 (diff) |
Use a string_view for the backend open method
Diffstat (limited to 'common')
-rw-r--r-- | common/strutils.cpp | 17 | ||||
-rw-r--r-- | common/strutils.h | 5 |
2 files changed, 12 insertions, 10 deletions
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 <windows.h> -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<int>(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<int>(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<int>(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<int>(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 <string> #ifdef _WIN32 +#include <string_view> #include <wchar.h> -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 { |