2022-05-03 10:25:52 +00:00
|
|
|
|
#include "HGString.h"
|
2022-05-18 03:30:13 +00:00
|
|
|
|
#include "../modules/base/HGDef.h"
|
|
|
|
|
#include "../modules/base/HGInc.h"
|
2022-05-03 10:25:52 +00:00
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
#if defined(HG_CMP_MSC)
|
|
|
|
|
static std::string AnsiToUtf8(const char* text)
|
|
|
|
|
{
|
|
|
|
|
int wlen = ::MultiByteToWideChar(CP_ACP, 0, text, -1, NULL, 0);
|
|
|
|
|
WCHAR* pUnicode = new WCHAR[wlen];
|
|
|
|
|
::MultiByteToWideChar(CP_ACP, 0, text, -1, pUnicode, wlen);
|
|
|
|
|
int len = ::WideCharToMultiByte(CP_UTF8, 0, pUnicode, -1, NULL, 0, NULL, NULL);
|
|
|
|
|
CHAR* pUTF8 = new CHAR[len];
|
|
|
|
|
::WideCharToMultiByte(CP_UTF8, 0, pUnicode, -1, pUTF8, len, NULL, NULL);
|
|
|
|
|
delete[] pUnicode;
|
|
|
|
|
std::string ret = pUTF8;
|
|
|
|
|
delete[] pUTF8;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
static std::string Utf8ToAnsi(const char* text)
|
|
|
|
|
{
|
|
|
|
|
int wlen = ::MultiByteToWideChar(CP_UTF8, 0, text, -1, NULL, 0);
|
|
|
|
|
WCHAR* pUnicode = new WCHAR[wlen];
|
|
|
|
|
::MultiByteToWideChar(CP_UTF8, 0, text, -1, pUnicode, wlen);
|
|
|
|
|
int len = ::WideCharToMultiByte(CP_ACP, 0, pUnicode, -1, NULL, 0, NULL, NULL);
|
|
|
|
|
CHAR* pAnsi = new CHAR[len];
|
|
|
|
|
::WideCharToMultiByte(CP_ACP, 0, pUnicode, -1, pAnsi, len, NULL, NULL);
|
|
|
|
|
delete[] pUnicode;
|
|
|
|
|
std::string ret = pAnsi;
|
|
|
|
|
delete[] pAnsi;
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-05-23 10:29:23 +00:00
|
|
|
|
std::string Utf8ToStdString(const std::string& utf8Str)
|
2022-05-03 10:25:52 +00:00
|
|
|
|
{
|
|
|
|
|
#if defined(HG_CMP_MSC)
|
2022-05-23 10:29:23 +00:00
|
|
|
|
return Utf8ToAnsi(utf8Str.c_str());
|
2022-05-03 10:25:52 +00:00
|
|
|
|
#else
|
2022-05-23 10:29:23 +00:00
|
|
|
|
return utf8Str;
|
2022-05-03 10:25:52 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-23 10:29:23 +00:00
|
|
|
|
std::string StdStringToUtf8(const std::string& stdStr)
|
2022-05-03 10:25:52 +00:00
|
|
|
|
{
|
|
|
|
|
#if defined(HG_CMP_MSC)
|
2022-05-23 10:29:23 +00:00
|
|
|
|
return AnsiToUtf8(stdStr.c_str());
|
2022-05-03 10:25:52 +00:00
|
|
|
|
#else
|
2022-05-23 10:29:23 +00:00
|
|
|
|
return stdStr;
|
2022-05-03 10:25:52 +00:00
|
|
|
|
#endif
|
|
|
|
|
}
|