2015-07-11 10:01:06 -04:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "UTF8Util.h"
|
2016-12-11 10:56:23 -05:00
|
|
|
#include <codecvt>
|
|
|
|
#include <locale>
|
2016-12-21 19:31:07 -05:00
|
|
|
|
|
|
|
namespace utf8
|
|
|
|
{
|
2016-12-11 10:56:23 -05:00
|
|
|
std::wstring utf8::decode(const std::string &str)
|
|
|
|
{
|
2016-12-21 19:31:07 -05:00
|
|
|
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
|
|
|
|
return conv.from_bytes(str);
|
2016-12-11 10:56:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string utf8::encode(const std::wstring &wstr)
|
|
|
|
{
|
2016-12-21 19:31:07 -05:00
|
|
|
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
|
|
|
|
return conv.to_bytes(wstr);
|
2016-12-11 10:56:23 -05:00
|
|
|
}
|
2016-12-19 22:16:29 -05:00
|
|
|
|
|
|
|
std::string utf8::encode(const std::u16string &wstr)
|
|
|
|
{
|
2016-12-21 19:31:07 -05:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<int16_t>, int16_t> conv;
|
|
|
|
auto p = reinterpret_cast<const int16_t *>(wstr.data());
|
|
|
|
return conv.to_bytes(p, p + wstr.size());
|
|
|
|
#else
|
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> conv;
|
|
|
|
return conv.to_bytes(wstr);
|
|
|
|
#endif
|
2016-12-19 22:16:29 -05:00
|
|
|
}
|
2016-12-21 19:31:07 -05:00
|
|
|
}
|