code_app/utility/HGString.cpp

51 lines
1.4 KiB
C++
Raw Normal View History

2022-05-03 10:25:52 +00:00
#include "HGString.h"
#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
std::string Utf8ToStdString(const std::string& utf8Str)
2022-05-03 10:25:52 +00:00
{
#if defined(HG_CMP_MSC)
return Utf8ToAnsi(utf8Str.c_str());
2022-05-03 10:25:52 +00:00
#else
return utf8Str;
2022-05-03 10:25:52 +00:00
#endif
}
std::string StdStringToUtf8(const std::string& stdStr)
2022-05-03 10:25:52 +00:00
{
#if defined(HG_CMP_MSC)
return AnsiToUtf8(stdStr.c_str());
2022-05-03 10:25:52 +00:00
#else
return stdStr;
2022-05-03 10:25:52 +00:00
#endif
}