Refactor editor host resource boundary
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <codecvt>
|
||||
#include <locale>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
namespace XCEngine::UI::Editor::App {
|
||||
|
||||
inline std::wstring Utf8ToWide(std::string_view text) {
|
||||
@@ -12,31 +12,12 @@ inline std::wstring Utf8ToWide(std::string_view text) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const int requiredChars = MultiByteToWideChar(
|
||||
CP_UTF8,
|
||||
0,
|
||||
text.data(),
|
||||
static_cast<int>(text.size()),
|
||||
nullptr,
|
||||
0);
|
||||
if (requiredChars <= 0) {
|
||||
try {
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> converter = {};
|
||||
return converter.from_bytes(text.data(), text.data() + text.size());
|
||||
} catch (const std::range_error&) {
|
||||
return {};
|
||||
}
|
||||
|
||||
std::wstring wide(static_cast<std::size_t>(requiredChars), L'\0');
|
||||
const int convertedChars = MultiByteToWideChar(
|
||||
CP_UTF8,
|
||||
0,
|
||||
text.data(),
|
||||
static_cast<int>(text.size()),
|
||||
wide.data(),
|
||||
requiredChars);
|
||||
if (convertedChars <= 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
wide.resize(static_cast<std::size_t>(convertedChars));
|
||||
return wide;
|
||||
}
|
||||
|
||||
inline std::string WideToUtf8(std::wstring_view text) {
|
||||
@@ -44,35 +25,12 @@ inline std::string WideToUtf8(std::wstring_view text) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const int requiredBytes = WideCharToMultiByte(
|
||||
CP_UTF8,
|
||||
0,
|
||||
text.data(),
|
||||
static_cast<int>(text.size()),
|
||||
nullptr,
|
||||
0,
|
||||
nullptr,
|
||||
nullptr);
|
||||
if (requiredBytes <= 0) {
|
||||
try {
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> converter = {};
|
||||
return converter.to_bytes(text.data(), text.data() + text.size());
|
||||
} catch (const std::range_error&) {
|
||||
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::App
|
||||
|
||||
Reference in New Issue
Block a user