char16_t formatting support

This commit is contained in:
Jack Andersen 2017-01-23 21:40:09 -10:00
parent 80b2223e88
commit b8abd466dd
4 changed files with 56 additions and 1 deletions

2
hecl/extern/athena vendored

@ -1 +1 @@
Subproject commit 483870850c24956d9549bad5149a97e80de42f2a Subproject commit dd8b60f779a97281ebf9f7778c4c0365fdc54457

View File

@ -52,7 +52,9 @@ extern logvisor::Module LogModule;
#endif #endif
std::string WideToUTF8(const std::wstring& src); std::string WideToUTF8(const std::wstring& src);
std::string Char16ToUTF8(const std::u16string& src);
std::wstring UTF8ToWide(const std::string& src); std::wstring UTF8ToWide(const std::string& src);
std::u16string UTF8ToChar16(const std::string& src);
/* humanize_number port from FreeBSD's libutil */ /* humanize_number port from FreeBSD's libutil */
enum class HNFlags enum class HNFlags
@ -424,6 +426,7 @@ std::string Format(const char* format, ...);
std::wstring WideFormat(const wchar_t* format, ...); std::wstring WideFormat(const wchar_t* format, ...);
std::u16string Char16Format(const wchar_t* format, ...);
static inline bool CheckFreeSpace(const SystemChar* path, size_t reqSz) static inline bool CheckFreeSpace(const SystemChar* path, size_t reqSz)
{ {

View File

@ -22,6 +22,24 @@ std::string WideToUTF8(const std::wstring& src)
return retval; 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 UTF8ToWide(const std::string& src)
{ {
std::wstring retval; std::wstring retval;
@ -42,4 +60,24 @@ std::wstring UTF8ToWide(const std::string& src)
return retval; 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;
}
} }

View File

@ -65,6 +65,20 @@ std::wstring WideFormat(const wchar_t* format, ...)
return std::wstring(resultBuf, printSz); return std::wstring(resultBuf, printSz);
} }
std::u16string Char16Format(const wchar_t* format, ...)
{
wchar_t resultBuf[FORMAT_BUF_SZ];
va_list va;
va_start(va, format);
int printSz = vswprintf(resultBuf, FORMAT_BUF_SZ, format, va);
va_end(va);
std::u16string res;
res.reserve(printSz);
for (size_t i=0 ; i<printSz ; ++i)
res.push_back(resultBuf[i]);
return res;
}
void SanitizePath(std::string& path) void SanitizePath(std::string& path)
{ {
if (path.empty()) if (path.empty())