Eliminate usage of deprecated API in sysinfo.cc (#1474)

* Eliminate usage of deprecated  API in sysinfo.cc

The `std::wstring_convert` is deprecated in C++17.
Since this code is in the windows branch, we could use the win32 API (MultiByteToWideChar)

* ran clang-format
This commit is contained in:
Vy Nguyen 2022-08-30 10:32:46 -04:00 committed by GitHub
parent ff629d847c
commit db55c89f31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -417,11 +417,19 @@ std::string GetSystemName() {
#ifndef UNICODE #ifndef UNICODE
str = std::string(hostname, DWCOUNT); str = std::string(hostname, DWCOUNT);
#else #else
// Using wstring_convert, Is deprecated in C++17 std::vector<wchar_t> converted;
using convert_type = std::codecvt_utf8<wchar_t>; // Find the length first.
std::wstring_convert<convert_type, wchar_t> converter; int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, hostname,
std::wstring wStr(hostname, DWCOUNT); DWCOUNT, converted.begin(), 0);
str = converter.to_bytes(wStr); // TODO: Report error from GetLastError()?
if (len == 0) return std::string("");
converted.reserve(len + 1);
len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, hostname, DWCOUNT,
converted.begin(), converted.size());
// TODO: Report error from GetLastError()?
if (len == 0) return std::string("");
str = std::string(converted.data());
#endif #endif
return str; return str;
#else // defined(BENCHMARK_OS_WINDOWS) #else // defined(BENCHMARK_OS_WINDOWS)