update cppcommon

This commit is contained in:
gwdwyy 2013-07-18 11:23:29 +08:00
parent 46ecb1382a
commit 7aadcb2990
2 changed files with 16 additions and 5 deletions

View File

@ -293,11 +293,6 @@ namespace CPPCOMMON
{
for(uint i = 0; i < uniLen; i++)
{
//char c = 0;
//c = ((pUni[i]>>8) & 0x00ff);
//res += c;
//c = (pUni[i] & 0x00ff);
//res += c;
pair<char, char> char2= uint16ToChar2(pUni[i]);
res += char2.first;
res += char2.second;
@ -368,6 +363,19 @@ namespace CPPCOMMON
return res;
}
size_t getUtf8WordLen(const string& utf)
{
string uni = utf8ToUnicode(utf);
if(uni.empty()||uni.size()%2)
{
return 0;
}
else
{
return uni.size()/2;
}
}
}
#ifdef TEST_STR_FUNCTS
@ -434,6 +442,7 @@ int main()
{
cout<<line<<endl;
string s = gbkToUtf8(line);
cout<<getUtf8WordLen(s)<<endl;
s = utf8ToGbk(s);
cout<<s<<endl;
}

View File

@ -40,6 +40,8 @@ namespace CPPCOMMON
int code_convert(const char *from_charset,const char *to_charset,char *inbuf,size_t inlen,char *outbuf,size_t outlen);
string gbkToUtf8(const string& gbk);
string utf8ToGbk(const string& utf);
size_t getUtf8WordLen(const string& utf);
inline uint16_t twocharToUint16(char high, char low)
{