47 lines
954 B
C
47 lines
954 B
C
|
|
#pragma once
|
||
|
|
|
||
|
|
#include <string>
|
||
|
|
#include <string_view>
|
||
|
|
|
||
|
|
#include <windows.h>
|
||
|
|
|
||
|
|
namespace XCEngine::UI::Editor::Support {
|
||
|
|
|
||
|
|
inline std::string WideToUtf8(std::wstring_view text) {
|
||
|
|
if (text.empty()) {
|
||
|
|
return {};
|
||
|
|
}
|
||
|
|
|
||
|
|
const int requiredBytes = WideCharToMultiByte(
|
||
|
|
CP_UTF8,
|
||
|
|
0,
|
||
|
|
text.data(),
|
||
|
|
static_cast<int>(text.size()),
|
||
|
|
nullptr,
|
||
|
|
0,
|
||
|
|
nullptr,
|
||
|
|
nullptr);
|
||
|
|
if (requiredBytes <= 0) {
|
||
|
|
return {};
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string utf8(static_cast<std::size_t>(requiredBytes), '\0');
|
||
|
|
const int convertedBytes = WideCharToMultiByte(
|
||
|
|
CP_UTF8,
|
||
|
|
0,
|
||
|
|
text.data(),
|
||
|
|
static_cast<int>(text.size()),
|
||
|
|
utf8.data(),
|
||
|
|
requiredBytes,
|
||
|
|
nullptr,
|
||
|
|
nullptr);
|
||
|
|
if (convertedBytes <= 0) {
|
||
|
|
return {};
|
||
|
|
}
|
||
|
|
|
||
|
|
utf8.resize(static_cast<std::size_t>(convertedBytes));
|
||
|
|
return utf8;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace XCEngine::UI::Editor::Support
|