mirror of
https://github.com/AxioDL/metaforce.git
synced 2025-07-03 12:35:51 +00:00
utf8 fix
This commit is contained in:
parent
56c21d108c
commit
b6c1563272
1
hecl/.gitignore
vendored
1
hecl/.gitignore
vendored
@ -1,2 +1,3 @@
|
|||||||
DataSpecRegistry.hpp
|
DataSpecRegistry.hpp
|
||||||
|
blender/hecl.zip
|
||||||
|
|
||||||
|
2
hecl/extern/Athena
vendored
2
hecl/extern/Athena
vendored
@ -1 +1 @@
|
|||||||
Subproject commit fd55c9298330d0809b5d2c7dc4277f87c6d29c19
|
Subproject commit bca146dbfce090d2c12773e26f4f097a3843d9c1
|
@ -1,3 +1,4 @@
|
|||||||
|
#include <utf8proc.h>
|
||||||
#include "HECL/HECL.hpp"
|
#include "HECL/HECL.hpp"
|
||||||
|
|
||||||
namespace HECL
|
namespace HECL
|
||||||
@ -5,45 +6,40 @@ namespace HECL
|
|||||||
|
|
||||||
std::string WideToUTF8(const std::wstring& src)
|
std::string WideToUTF8(const std::wstring& src)
|
||||||
{
|
{
|
||||||
#if _WIN32
|
|
||||||
int len = WideCharToMultiByte(CP_UTF8, 0, src.c_str(), src.size(), nullptr, 0, nullptr, nullptr);
|
|
||||||
std::string retval(len, '\0');
|
|
||||||
WideCharToMultiByte(CP_UTF8, 0, src.c_str(), src.size(), &retval[0], len, nullptr, nullptr);
|
|
||||||
return retval;
|
|
||||||
#else
|
|
||||||
std::string retval;
|
std::string retval;
|
||||||
retval.reserve(src.length());
|
retval.reserve(src.length());
|
||||||
std::mbstate_t state = {};
|
|
||||||
for (wchar_t ch : src)
|
for (wchar_t ch : src)
|
||||||
{
|
{
|
||||||
char mb[MB_LEN_MAX];
|
utf8proc_uint8_t mb[4];
|
||||||
int c = std::wcrtomb(mb, ch, &state);
|
utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb);
|
||||||
retval.append(mb, c);
|
if (c < 0)
|
||||||
|
{
|
||||||
|
LogModule.report(LogVisor::Warning, "invalid UTF-8 character while encoding");
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
retval.append(reinterpret_cast<char*>(mb), c);
|
||||||
}
|
}
|
||||||
return retval;
|
return retval;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::wstring UTF8ToWide(const std::string& src)
|
std::wstring UTF8ToWide(const std::string& src)
|
||||||
{
|
{
|
||||||
#if _WIN32
|
|
||||||
int len = MultiByteToWideChar(CP_UTF8, 0, src.c_str(), src.size(), nullptr, 0);
|
|
||||||
std::wstring retval(len, L'\0');
|
|
||||||
MultiByteToWideChar(CP_UTF8, 0, src.c_str(), src.size(), &retval[0], len);
|
|
||||||
return retval;
|
|
||||||
#else
|
|
||||||
std::wstring retval;
|
std::wstring retval;
|
||||||
retval.reserve(src.length());
|
retval.reserve(src.length());
|
||||||
const char* buf = src.c_str();
|
const utf8proc_uint8_t* buf = reinterpret_cast<const utf8proc_uint8_t*>(src.c_str());
|
||||||
std::mbstate_t state = {};
|
|
||||||
while (*buf)
|
while (*buf)
|
||||||
{
|
{
|
||||||
wchar_t wc;
|
utf8proc_int32_t wc;
|
||||||
buf += std::mbrtowc(&wc, buf, MB_LEN_MAX, &state);
|
utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc);
|
||||||
retval += wc;
|
if (len < 0)
|
||||||
|
{
|
||||||
|
LogModule.report(LogVisor::Warning, "invalid UTF-8 character while decoding");
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
buf += len;
|
||||||
|
retval += wchar_t(wc);
|
||||||
}
|
}
|
||||||
return retval;
|
return retval;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user