Use C++17 non-const string.data() in WindowsUtils

Bug: dawn:824
Change-Id: I9a7880bc38b15864364505f9f9399e440c048e7a
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/75064
Reviewed-by: Austin Eng <enga@chromium.org>
Reviewed-by: Brandon Jones <bajones@chromium.org>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Corentin Wallez 2022-01-06 09:01:48 +00:00 committed by Dawn LUCI CQ
parent 3e65d4f67e
commit fa2fde1432
1 changed files with 8 additions and 10 deletions

View File

@ -23,12 +23,11 @@ std::string WCharToUTF8(const wchar_t* input) {
// input. It will return a size that includes the null terminator. // input. It will return a size that includes the null terminator.
int requiredSize = WideCharToMultiByte(CP_UTF8, 0, input, -1, nullptr, 0, nullptr, nullptr); int requiredSize = WideCharToMultiByte(CP_UTF8, 0, input, -1, nullptr, 0, nullptr, nullptr);
// When we can use C++17 this can be changed to use string.data() instead. std::string result;
std::unique_ptr<char[]> result = std::make_unique<char[]>(requiredSize); result.resize(requiredSize - 1);
WideCharToMultiByte(CP_UTF8, 0, input, -1, result.get(), requiredSize, nullptr, nullptr); WideCharToMultiByte(CP_UTF8, 0, input, -1, result.data(), requiredSize, nullptr, nullptr);
// This will allocate the returned std::string and then destroy result. return result;
return std::string(result.get(), result.get() + (requiredSize - 1));
} }
std::wstring UTF8ToWStr(const char* input) { std::wstring UTF8ToWStr(const char* input) {
@ -36,10 +35,9 @@ std::wstring UTF8ToWStr(const char* input) {
// input. It will return a size that includes the null terminator. // input. It will return a size that includes the null terminator.
int requiredSize = MultiByteToWideChar(CP_UTF8, 0, input, -1, nullptr, 0); int requiredSize = MultiByteToWideChar(CP_UTF8, 0, input, -1, nullptr, 0);
// When we can use C++17 this can be changed to use string.data() instead. std::wstring result;
std::unique_ptr<wchar_t[]> result = std::make_unique<wchar_t[]>(requiredSize); result.resize(requiredSize - 1);
MultiByteToWideChar(CP_UTF8, 0, input, -1, result.get(), requiredSize); MultiByteToWideChar(CP_UTF8, 0, input, -1, result.data(), requiredSize);
// This will allocate the returned std::string and then destroy result. return result;
return std::wstring(result.get(), result.get() + (requiredSize - 1));
} }