mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-20 10:25:28 +00:00
Add string to wstring conversion helper and related unittest
Bug: dawn:766 Change-Id: I318c630df01fcdb302d36873a783fdf1a39c608c Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/48200 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Commit-Queue: 陈俊嘉 <cjj19970505@live.cn>
This commit is contained in:
committed by
Commit Bot service account
parent
9446154d74
commit
672105aa0a
@@ -30,3 +30,16 @@ std::string WCharToUTF8(const wchar_t* input) {
|
||||
// This will allocate the returned std::string and then destroy result.
|
||||
return std::string(result.get(), result.get() + (requiredSize - 1));
|
||||
}
|
||||
|
||||
std::wstring UTF8ToWStr(const char* input) {
|
||||
// The -1 argument asks MultiByteToWideChar to use the null terminator to know the size of
|
||||
// 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<wchar_t[]> result = std::make_unique<wchar_t[]>(requiredSize);
|
||||
MultiByteToWideChar(CP_UTF8, 0, input, -1, result.get(), requiredSize);
|
||||
|
||||
// This will allocate the returned std::string and then destroy result.
|
||||
return std::wstring(result.get(), result.get() + (requiredSize - 1));
|
||||
}
|
||||
|
||||
@@ -19,4 +19,6 @@
|
||||
|
||||
std::string WCharToUTF8(const wchar_t* input);
|
||||
|
||||
std::wstring UTF8ToWStr(const char* input);
|
||||
|
||||
#endif // COMMON_WINDOWSUTILS_H_
|
||||
|
||||
Reference in New Issue
Block a user