0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-18 07:50:57 +01:00

ircd::local: Add additional char/char16_t buffer conversion.

This commit is contained in:
Jason Volk 2016-10-26 01:28:59 -07:00
parent 97c0d3df7d
commit 6a1fb4e8fb
2 changed files with 37 additions and 0 deletions

View file

@ -32,6 +32,7 @@ namespace locale {
// On newer platforms (gcc-5 etc) these conversions are standard C++. // On newer platforms (gcc-5 etc) these conversions are standard C++.
// On older platforms the definition file may use boost::locale. // On older platforms the definition file may use boost::locale.
size_t convert(const char16_t *const &, char *const &buf, const size_t &max); size_t convert(const char16_t *const &, char *const &buf, const size_t &max);
size_t convert(const char *const &, char16_t *const &buf, const size_t &max); // uint8_t = max*2
std::string convert(const char16_t *const &); std::string convert(const char16_t *const &);
std::string convert(const std::u16string &); std::string convert(const std::u16string &);
std::u16string convert(const char *const &); std::u16string convert(const char *const &);

View file

@ -112,3 +112,39 @@ ircd::locale::convert(const char16_t *const &str,
return rb_strlcpy(buf, s.c_str(), max); return rb_strlcpy(buf, s.c_str(), max);
} }
#endif #endif
#ifdef HAVE_CODECVT
size_t
ircd::locale::convert(const char *const &str,
char16_t *const &buf,
const size_t &max)
{
static std::wstring_convert<std::codecvt_utf8<char16_t>, char16_t> converter;
if(unlikely(!max))
return 0;
//TODO: optimize
const auto s(converter.from_bytes(str));
const auto cpsz(std::min(s.size(), size_t(max - 1)));
memcpy(buf, s.data(), cpsz * 2);
buf[cpsz] = char16_t(0);
return cpsz;
}
#else
size_t
ircd::locale::convert(const char *const &str,
char16_t *const &buf,
const size_t &max)
{
if(unlikely(!max))
return 0;
//TODO: optimize
const auto s(boost::locale::conv::utf_to_utf<char16_t>(str));
const auto cpsz(std::min(s.size(), size_t(max - 1)));
memcpy(buf, s.data(), cpsz * 2);
buf[cpsz] = char16_t(0);
return cpsz;
}
#endif