代码片段,来源于Arcemu开源项目。
(1)切分
using namespace std;
vector<string> StrSplit(const string & src, const string & sep)
{
vector<string> r;
string s;
for(string::const_iterator i = src.begin(); i != src.end(); i++)
{
if(sep.find(*i) != string::npos)
{
if(s.length()) r.push_back(s);
s = "";
}
else
{
s += *i;
}
}
if(s.length()) r.push_back(s);
return r;
}
(2)编码转换
// win32 only
// cebernic added it
// for multilanguage supports
// --------------------------------------------------------------------------------------------------
SERVER_DECL const char* _StringToUTF8(const char* pASCIIBuf)
{
#ifdef WIN32
DWORD UniCodeLen = MultiByteToWideChar(CP_ACP, 0, pASCIIBuf, -1, 0, 0);
std::vector <wchar_t> vWCH(UniCodeLen);
MultiByteToWideChar(CP_ACP, 0, pASCIIBuf, -1, &vWCH[0], UniCodeLen);
DWORD dwUtf8Len = WideCharToMultiByte(CP_UTF8, 0, &vWCH[0], UniCodeLen , NULL, NULL, NULL, NULL);
ASSERT(dwUtf8Len + 1 < 2048);
memset(_StringConversionStorage, 0, (sizeof(char)*dwUtf8Len) + 1);
WideCharToMultiByte(CP_UTF8, 0, &vWCH[0], UniCodeLen , _StringConversionStorage, dwUtf8Len, NULL, NULL);
return &_StringConversionStorage[0];
#else
return &pASCIIBuf[0];
#endif
}
SERVER_DECL const char* _StringToANSI(const char* pUtf8Buf)
{
#ifdef WIN32
DWORD UniCodeLen = MultiByteToWideChar(CP_UTF8, 0, pUtf8Buf, -1, NULL, 0);
std::vector <wchar_t> vWCH(UniCodeLen);
MultiByteToWideChar(CP_UTF8, 0, pUtf8Buf, -1, &vWCH[0] , UniCodeLen);
DWORD dwASCIILen = WideCharToMultiByte(CP_ACP, 0, &vWCH[0], UniCodeLen , NULL , NULL , NULL, NULL);
ASSERT(dwASCIILen + 1 < 2048);
memset(_StringConversionStorage, 0, (sizeof(char)*dwASCIILen) + 1);
WideCharToMultiByte(CP_ACP, 0, &vWCH[0], UniCodeLen , _StringConversionStorage, dwASCIILen, NULL, NULL);
return &_StringConversionStorage[0];
#else
return &pUtf8Buf[0];
#endif
}