update cppcommon

This commit is contained in:
gwdwyy 2013-07-09 14:01:26 +08:00
parent 19afccec57
commit 5fcee293d1
2 changed files with 32 additions and 12 deletions

View File

@ -198,12 +198,13 @@ namespace CPPCOMMON
char * utfStr = new char[len<<1]; char * utfStr = new char[len<<1];
for(int i = 0; i < len; i+=2) for(int i = 0; i < len; i+=2)
{ {
uint16_t tmp1 = uniStr[i]; //uint16_t tmp1 = uniStr[i];
tmp1 <<= 8; //tmp1 <<= 8;
tmp1&= 0xff00; //tmp1 &= 0xff00;
uint16_t tmp2 = uniStr[i+1]; //uint16_t tmp2 = uniStr[i+1];
tmp2 &= 0x00ff; //tmp2 &= 0x00ff;
uniArr[i>>1] = tmp1 | tmp2; //uniArr[i>>1] = tmp1 | tmp2;
uniArr[i>>1] = twocharToUint16(uniStr[i], uniStr[i+1]);
} }
string res; string res;
@ -269,11 +270,14 @@ namespace CPPCOMMON
{ {
for(uint i = 0; i < uniLen; i++) for(uint i = 0; i < uniLen; i++)
{ {
char c = 0; //char c = 0;
c = ((pUni[i]>>8) & 0x00ff); //c = ((pUni[i]>>8) & 0x00ff);
res += c; //res += c;
c = (pUni[i] & 0x00ff); //c = (pUni[i] & 0x00ff);
res += c; //res += c;
pair<char, char> char2= uint16ToChar2(pUni[i]);
res += char2.first;
res += char2.second;
} }
} }
delete [] pUni; delete [] pUni;
@ -324,7 +328,7 @@ int main()
// //cout<<strlen(utf8str); // //cout<<strlen(utf8str);
// cout<<utf8str<<endl; // cout<<utf8str<<endl;
//} //}
ifstream ifile("jieba.dict.utf8"); ifstream ifile("testdata/dict.txt");
string line; string line;
while(getline(ifile, line)) while(getline(ifile, line))
{ {

View File

@ -6,6 +6,7 @@
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
#include <map>
#include <stdint.h> #include <stdint.h>
#include "typedefs.h" #include "typedefs.h"
namespace CPPCOMMON namespace CPPCOMMON
@ -31,5 +32,20 @@ namespace CPPCOMMON
string unicodeToUtf8(const string& uniStr); string unicodeToUtf8(const string& uniStr);
int utf8ToUnicode(const char* inutf8, int len, uint16_t* unicode); int utf8ToUnicode(const char* inutf8, int len, uint16_t* unicode);
string utf8ToUnicode(const string& utfStr); string utf8ToUnicode(const string& utfStr);
inline uint16_t twocharToUint16(char high, char low)
{
return (((uint16_t(high) & 0x00ff ) << 8) | (uint16_t(low) & 0x00ff));
}
inline pair<char, char> uint16ToChar2(uint16_t in)
{
pair<char, char> res;
res.first = (in>>8) & 0x00ff; //high
res.second = (in) & 0x00ff; //low
return res;
}
} }
#endif #endif