108 lines
2.1 KiB
C++
108 lines
2.1 KiB
C++
|
#include "HGString.h"
|
|||
|
#include "../base/HGDef.h"
|
|||
|
#include "../base/HGInc.h"
|
|||
|
#include <algorithm>
|
|||
|
|
|||
|
void TrimString(std::string& str)
|
|||
|
{
|
|||
|
std::string str1;
|
|||
|
bool add1 = false;
|
|||
|
std::string::const_iterator iter1;
|
|||
|
for (iter1 = str.begin(); iter1 != str.end(); ++iter1)
|
|||
|
{
|
|||
|
int c = (HGByte)(*iter1);
|
|||
|
if (!add1)
|
|||
|
{
|
|||
|
if (!isspace(c))
|
|||
|
{
|
|||
|
str1.push_back(c);
|
|||
|
add1 = true;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
str1.push_back(c);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (str1.empty())
|
|||
|
{
|
|||
|
str.clear();
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
std::string str2;
|
|||
|
bool add2 = false;
|
|||
|
std::string::const_reverse_iterator iter2;
|
|||
|
for (iter2 = str1.rbegin(); iter2 != str1.rend(); ++iter2)
|
|||
|
{
|
|||
|
int c = (HGByte)(*iter2);
|
|||
|
if (!add2)
|
|||
|
{
|
|||
|
if (!isspace(c))
|
|||
|
{
|
|||
|
str2.push_back(c);
|
|||
|
add2 = true;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
str2.push_back(c);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (str2.empty())
|
|||
|
{
|
|||
|
str.clear();
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
str = std::string(str2.rbegin(), str2.rend());
|
|||
|
}
|
|||
|
|
|||
|
#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 char* utf8)
|
|||
|
{
|
|||
|
#if defined(HG_CMP_MSC)
|
|||
|
return Utf8ToAnsi(utf8);
|
|||
|
#else
|
|||
|
return utf8;
|
|||
|
#endif
|
|||
|
}
|
|||
|
|
|||
|
std::string StdStringToUtf8(const char* str)
|
|||
|
{
|
|||
|
#if defined(HG_CMP_MSC)
|
|||
|
return AnsiToUtf8(str);
|
|||
|
#else
|
|||
|
return str;
|
|||
|
#endif
|
|||
|
}
|