mirror of
https://github.com/AxioDL/metaforce.git
synced 2025-12-08 18:24:55 +00:00
char16_t formatting support
This commit is contained in:
@@ -22,6 +22,24 @@ std::string WideToUTF8(const std::wstring& src)
|
||||
return retval;
|
||||
}
|
||||
|
||||
std::string Char16ToUTF8(const std::u16string& src)
|
||||
{
|
||||
std::string retval;
|
||||
retval.reserve(src.length());
|
||||
for (char16_t ch : src)
|
||||
{
|
||||
utf8proc_uint8_t mb[4];
|
||||
utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb);
|
||||
if (c < 0)
|
||||
{
|
||||
LogModule.report(logvisor::Warning, "invalid UTF-8 character while encoding");
|
||||
return retval;
|
||||
}
|
||||
retval.append(reinterpret_cast<char*>(mb), c);
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
std::wstring UTF8ToWide(const std::string& src)
|
||||
{
|
||||
std::wstring retval;
|
||||
@@ -42,4 +60,24 @@ std::wstring UTF8ToWide(const std::string& src)
|
||||
return retval;
|
||||
}
|
||||
|
||||
std::u16string UTF8ToChar16(const std::string& src)
|
||||
{
|
||||
std::u16string retval;
|
||||
retval.reserve(src.length());
|
||||
const utf8proc_uint8_t* buf = reinterpret_cast<const utf8proc_uint8_t*>(src.c_str());
|
||||
while (*buf)
|
||||
{
|
||||
utf8proc_int32_t wc;
|
||||
utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc);
|
||||
if (len < 0)
|
||||
{
|
||||
LogModule.report(logvisor::Warning, "invalid UTF-8 character while decoding");
|
||||
return retval;
|
||||
}
|
||||
buf += len;
|
||||
retval += char16_t(wc);
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user