Add functions for converting between UTF-8/16.

This commit is contained in:
Jordan Woyak 2013-02-27 18:00:42 -06:00
parent 9ff704f202
commit 0ea458b4dc
2 changed files with 41 additions and 0 deletions

View File

@ -22,6 +22,10 @@
#include "CommonPaths.h"
#include "StringUtil.h"
#ifdef _WIN32
#include <Windows.h>
#endif
// faster than sscanf
bool AsciiToHex(const char* _szValue, u32& result)
{
@ -375,3 +379,33 @@ std::string UriEncode(const std::string & sSrc)
delete [] pStart;
return sResult;
}
#ifdef _WIN32
std::string UTF16ToUTF8(const std::wstring& input)
{
auto const size = WideCharToMultiByte(CP_UTF8, 0, input.data(), input.size(), nullptr, 0, nullptr, nullptr);
std::string output;
output.resize(size);
if (size != WideCharToMultiByte(CP_UTF8, 0, input.data(), input.size(), &output[0], output.size(), nullptr, nullptr))
output.clear();
return output;
}
std::wstring UTF8ToUTF16(const std::string& input)
{
auto const size = MultiByteToWideChar(CP_UTF8, 0, input.data(), input.size(), nullptr, 0);
std::wstring output;
output.resize(size);
if (size != MultiByteToWideChar(CP_UTF8, 0, input.data(), input.size(), &output[0], output.size()))
output.clear();
return output;
}
#endif

View File

@ -97,4 +97,11 @@ std::string ReplaceAll(std::string result, const std::string& src, const std::st
std::string UriDecode(const std::string & sSrc);
std::string UriEncode(const std::string & sSrc);
#ifdef _WIN32
std::string UTF16ToUTF8(const std::wstring& str);
std::wstring UTF8ToUTF16(const std::string& str);
#endif
#endif // _STRINGUTIL_H_