1
0
mirror of https://github.com/google/benchmark.git synced 2025-02-17 23:00:15 +08:00

Eliminate usage of deprecated API in sysinfo.cc ()

* 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
str = std::string(hostname, DWCOUNT);
#else
// Using wstring_convert, Is deprecated in C++17
using convert_type = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_type, wchar_t> converter;
std::wstring wStr(hostname, DWCOUNT);
str = converter.to_bytes(wStr);
std::vector<wchar_t> converted;
// Find the length first.
int len = ::MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, hostname,
DWCOUNT, converted.begin(), 0);
// 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
return str;
#else // defined(BENCHMARK_OS_WINDOWS)