string: add trivial wchar-to-utf8 conversions

This commit is contained in:
Danny Robson 2016-11-22 21:48:57 +11:00
parent d1ec872bd4
commit a1b35dfbbe
2 changed files with 27 additions and 0 deletions

View File

@ -17,9 +17,30 @@
#include "./string.hpp"
#include <cstring>
#include <codecvt>
#include <locale>
using util::tokeniser;
///////////////////////////////////////////////////////////////////////////////
std::string
util::to_utf8 (const wchar_t *src)
{
using convert_t = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_t,wchar_t> converter;
return converter.to_bytes (src);
}
//-----------------------------------------------------------------------------
std::string
util::to_utf8 (const std::wstring &src)
{
return to_utf8 (src.c_str ());
}
///////////////////////////////////////////////////////////////////////////////
// TODO: Horribly inefficient, but God help you if you're relying on this
// being efficient in the first place.

View File

@ -20,6 +20,12 @@
#include "./view.hpp"
namespace util {
std::string to_utf8 (const wchar_t*);
std::string to_utf8 (const std::wstring&);
}
bool
strbegins(const char *restrict str,
const char *restrict prefix);