diff options
author | crupest <crupest@outlook.com> | 2020-04-20 01:17:28 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-04-20 01:17:28 +0800 |
commit | 857adcb7ee2d45b2e29d4250fa4246bb8861a8f9 (patch) | |
tree | 2ebe62c58768ded27c5c6b15d1ab04400c9ecf9b /src/win/string.cpp | |
parent | 494d2db1c1203acc9d9f2b127b75c6df6160057a (diff) | |
download | cru-857adcb7ee2d45b2e29d4250fa4246bb8861a8f9.tar.gz cru-857adcb7ee2d45b2e29d4250fa4246bb8861a8f9.tar.bz2 cru-857adcb7ee2d45b2e29d4250fa4246bb8861a8f9.zip |
...
Diffstat (limited to 'src/win/string.cpp')
-rw-r--r-- | src/win/string.cpp | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/src/win/string.cpp b/src/win/string.cpp index fb9811eb..5518e6af 100644 --- a/src/win/string.cpp +++ b/src/win/string.cpp @@ -8,9 +8,9 @@ namespace cru::platform::win { std::string ToUtf8String(const std::wstring_view& string) { if (string.empty()) return std::string{}; - const auto length = - ::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, string.data(), -1, - nullptr, 0, nullptr, nullptr); + const auto length = ::WideCharToMultiByte( + CP_UTF8, WC_ERR_INVALID_CHARS, string.data(), + static_cast<int>(string.size()), nullptr, 0, nullptr, nullptr); if (length == 0) { throw Win32Error(::GetLastError(), "Failed to convert wide string to UTF-8."); @@ -18,9 +18,10 @@ std::string ToUtf8String(const std::wstring_view& string) { std::string result; result.resize(length); - if (::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, string.data(), -1, - result.data(), static_cast<int>(result.size()), - nullptr, nullptr) == 0) + if (::WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, string.data(), + static_cast<int>(string.size()), result.data(), + static_cast<int>(result.size()), nullptr, + nullptr) == 0) throw Win32Error(::GetLastError(), "Failed to convert wide string to UTF-8."); return result; @@ -29,8 +30,9 @@ std::string ToUtf8String(const std::wstring_view& string) { std::wstring ToUtf16String(const std::string_view& string) { if (string.empty()) return std::wstring{}; - const auto length = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, - string.data(), -1, nullptr, 0); + const auto length = + ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, string.data(), + static_cast<int>(string.size()), nullptr, 0); if (length == 0) { throw Win32Error(::GetLastError(), "Failed to convert wide string to UTF-16."); @@ -38,8 +40,8 @@ std::wstring ToUtf16String(const std::string_view& string) { std::wstring result; result.resize(length); - if (::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, string.data(), -1, - result.data(), + if (::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, string.data(), + static_cast<int>(string.size()), result.data(), static_cast<int>(result.size())) == 0) throw win::Win32Error(::GetLastError(), "Failed to convert wide string to UTF-16."); |