mirror of
https://github.com/yanyiwu/cppjieba.git
synced 2025-07-18 00:00:12 +08:00
update cppcommon
This commit is contained in:
parent
65280738e4
commit
aa0f95bcc6
111
src/cppcommon/encoding.cpp
Normal file
111
src/cppcommon/encoding.cpp
Normal file
@ -0,0 +1,111 @@
|
||||
#include "encoding.h"
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
const string& UnicodeEncoding::UTF8ENC = "utf-8";
|
||||
const string& UnicodeEncoding::GBKENC = "gbk";
|
||||
|
||||
UnicodeEncoding::UnicodeEncoding()
|
||||
{
|
||||
_encVec.push_back(UTF8ENC);
|
||||
_encVec.push_back(GBKENC);
|
||||
_encoding = UTF8ENC;
|
||||
}
|
||||
|
||||
UnicodeEncoding::~UnicodeEncoding()
|
||||
{
|
||||
}
|
||||
|
||||
bool UnicodeEncoding::setEncoding(const string& enc)
|
||||
{
|
||||
if(!isInVec<string>(_encVec, enc))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
_encoding = enc;
|
||||
return true;
|
||||
}
|
||||
string UnicodeEncoding::encode(const string& str)
|
||||
{
|
||||
if(!isUniStrValid(str))
|
||||
{
|
||||
cout<<__FILE__<<__LINE__<<endl;
|
||||
return "";
|
||||
}
|
||||
if(UTF8ENC == _encoding)
|
||||
{
|
||||
return unicodeToUtf8(str);
|
||||
}
|
||||
else if(GBKENC == _encoding)
|
||||
{
|
||||
return utf8ToGbk(unicodeToUtf8(str));
|
||||
}
|
||||
cout<<__FILE__<<__LINE__<<endl;
|
||||
return "";
|
||||
}
|
||||
string UnicodeEncoding::decode(const string& str)
|
||||
{
|
||||
if(str.empty())
|
||||
{
|
||||
cout<<__FILE__<<__LINE__<<endl;
|
||||
return "";
|
||||
}
|
||||
string res;
|
||||
if(UTF8ENC == _encoding)
|
||||
{
|
||||
|
||||
res = utf8ToUnicode(str);
|
||||
if(isUniStrValid(res))
|
||||
{
|
||||
return res;
|
||||
}
|
||||
}
|
||||
else if(GBKENC == _encoding)
|
||||
{
|
||||
res = utf8ToUnicode(gbkToUtf8(str));
|
||||
if(isUniStrValid(res))
|
||||
{
|
||||
return res;
|
||||
}
|
||||
}
|
||||
cout<<__FILE__<<__LINE__<<endl;
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef ENCODING_UT
|
||||
using namespace CPPCOMMON;
|
||||
int main()
|
||||
{
|
||||
UnicodeEncoding enc;
|
||||
ifstream ifile("testdata/dict.utf8");
|
||||
string line;
|
||||
//enc.setEncoding(UnicodeEncoding::UTF8ENC);
|
||||
//enc.setEncoding(UnicodeEncoding::GBKENC);
|
||||
//while(getline(ifile, line))
|
||||
//{
|
||||
// cout<<line<<endl;
|
||||
// cout<<enc.encode(enc.decode(line))<<endl;
|
||||
// cout<<enc.decode(enc.encode(line))<<endl;
|
||||
// cout<<enc.decode(line)<<endl;
|
||||
// cout<<enc.encode(line)<<endl;
|
||||
//}
|
||||
ifile.close();
|
||||
ifile.open("testdata/dict.gbk");
|
||||
enc.setEncoding(UnicodeEncoding::GBKENC);
|
||||
while(getline(ifile, line))
|
||||
{
|
||||
|
||||
cout<<line<<endl;
|
||||
cout<<line.size()<<endl;
|
||||
cout<<enc.encode(enc.decode(line))<<endl;
|
||||
cout<<enc.decode(enc.encode(line))<<endl;
|
||||
cout<<enc.decode(line)<<endl;
|
||||
cout<<enc.encode(line)<<endl;
|
||||
}
|
||||
ifile.close();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
39
src/cppcommon/encoding.h
Normal file
39
src/cppcommon/encoding.h
Normal file
@ -0,0 +1,39 @@
|
||||
/************************************
|
||||
* file enc : utf8
|
||||
* author : wuyanyi09@gmail.com
|
||||
************************************/
|
||||
|
||||
#ifndef CPPCOMMON_ENCODING_H
|
||||
#define CPPCOMMON_ENCODING_H
|
||||
|
||||
#include "str_functs.h"
|
||||
#include "vec_functs.h"
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
using namespace std;
|
||||
class UnicodeEncoding
|
||||
{
|
||||
public:
|
||||
static const string& UTF8ENC;
|
||||
static const string& GBKENC;
|
||||
private:
|
||||
string _encoding;
|
||||
vector<string> _encVec;
|
||||
public:
|
||||
UnicodeEncoding();
|
||||
~UnicodeEncoding();
|
||||
public:
|
||||
bool setEncoding(const string& enc);
|
||||
string encode(const string& str);
|
||||
string decode(const string& str);
|
||||
public:
|
||||
bool isUniStrValid(const string& unistr)
|
||||
{
|
||||
return !(unistr.empty() || unistr.size() % 2);
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user