Use Windows API for utf8-utf16 conversions

This commit is contained in:
Ted John 2018-05-14 22:45:38 +01:00 committed by Aaron van Geffen
parent 8e919d2f84
commit 085d85585a
2 changed files with 22 additions and 6 deletions

View File

@ -67,28 +67,44 @@ namespace String
return returnValue;
}
std::string ToUtf8(const std::wstring &s)
std::string ToUtf8(const std::wstring_view& src)
{
#ifdef _WIN32
int srcLen = (int)src.size();
int sizeReq = WideCharToMultiByte(CODE_PAGE::CP_UTF8, 0, src.data(), srcLen, nullptr, 0, nullptr, nullptr);
auto result = std::string(sizeReq, 0);
WideCharToMultiByte(CODE_PAGE::CP_UTF8, 0, src.data(), srcLen, result.data(), sizeReq, nullptr, nullptr);
return result;
#else
std::string result;
utf8 * cstr = widechar_to_utf8(s.c_str());
utf8 * cstr = widechar_to_utf8(std::wstring(src).c_str());
if (cstr != nullptr)
{
result = std::string(cstr);
}
free(cstr);
return result;
#endif
}
std::wstring ToUtf16(const std::string_view& s)
std::wstring ToUtf16(const std::string_view& src)
{
#ifdef _WIN32
int srcLen = (int)src.size();
int sizeReq = MultiByteToWideChar(CP_ACP, 0, src.data(), srcLen, nullptr, 0);
auto result = std::wstring(sizeReq, 0);
MultiByteToWideChar(CP_ACP, 0, src.data(), srcLen, result.data(), sizeReq);
return result;
#else
std::wstring result;
wchar_t * wcstr = utf8_to_widechar(s.data());
wchar_t * wcstr = utf8_to_widechar(std::string(src).c_str());
if (wcstr != nullptr)
{
result = std::wstring(wcstr);
}
free(wcstr);
return result;
#endif
}
bool IsNullOrEmpty(const utf8 * str)

View File

@ -42,8 +42,8 @@ namespace String
std::string ToStd(const utf8 * str);
std::string StdFormat_VA(const utf8 * format, va_list args);
std::string StdFormat(const utf8 * format, ...);
std::string ToUtf8(const std::wstring &s);
std::wstring ToUtf16(const std::string_view& s);
std::string ToUtf8(const std::wstring_view& src);
std::wstring ToUtf16(const std::string_view& src);
bool IsNullOrEmpty(const utf8 * str);
sint32 Compare(const std::string &a, const std::string &b, bool ignoreCase = false);