From fa2fde143247ee25cedfac02a1b3f4a12598705e Mon Sep 17 00:00:00 2001 From: Corentin Wallez Date: Thu, 6 Jan 2022 09:01:48 +0000 Subject: [PATCH] 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 Reviewed-by: Brandon Jones Commit-Queue: Corentin Wallez --- src/common/WindowsUtils.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/common/WindowsUtils.cpp b/src/common/WindowsUtils.cpp index f4aef7cb3d..a0f041aa62 100644 --- a/src/common/WindowsUtils.cpp +++ b/src/common/WindowsUtils.cpp @@ -23,12 +23,11 @@ std::string WCharToUTF8(const wchar_t* input) { // input. It will return a size that includes the null terminator. 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::unique_ptr result = std::make_unique(requiredSize); - WideCharToMultiByte(CP_UTF8, 0, input, -1, result.get(), requiredSize, nullptr, nullptr); + std::string result; + result.resize(requiredSize - 1); + WideCharToMultiByte(CP_UTF8, 0, input, -1, result.data(), requiredSize, nullptr, nullptr); - // This will allocate the returned std::string and then destroy result. - return std::string(result.get(), result.get() + (requiredSize - 1)); + return result; } 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. 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::unique_ptr result = std::make_unique(requiredSize); - MultiByteToWideChar(CP_UTF8, 0, input, -1, result.get(), requiredSize); + std::wstring result; + result.resize(requiredSize - 1); + MultiByteToWideChar(CP_UTF8, 0, input, -1, result.data(), requiredSize); - // This will allocate the returned std::string and then destroy result. - return std::wstring(result.get(), result.get() + (requiredSize - 1)); + return result; }