mirror of
https://github.com/yanyiwu/cppjieba.git
synced 2025-07-18 00:00:12 +08:00
init
This commit is contained in:
commit
8f35e77bc8
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
*tmp*
|
||||
tags
|
||||
*swp
|
||||
*.out
|
||||
*.o
|
||||
*.d
|
||||
*.ut
|
||||
log
|
42
cppcommon/Makefile
Normal file
42
cppcommon/Makefile
Normal file
@ -0,0 +1,42 @@
|
||||
CC = g++
|
||||
CCOPT = -Wall -c
|
||||
LINK = g++
|
||||
LINKOPT =
|
||||
PACK = ar
|
||||
PACKOPT = rc
|
||||
SOURCES := $(wildcard *.cpp)
|
||||
OBJS := $(patsubst %.cpp,%.o,$(SOURCES))
|
||||
|
||||
DOPACK = $(PACK) $(PACKOPT) $@ $?
|
||||
DOLINK = $(LINK) $(LINKOPT) -o $@ $?
|
||||
|
||||
CMLIB = cmlib.a
|
||||
|
||||
%.o: %.cpp
|
||||
$(CC) $(CCOPT) $<
|
||||
|
||||
|
||||
all: $(CMLIB)
|
||||
|
||||
|
||||
$(CMLIB): $(OBJS)
|
||||
$(DOPACK)
|
||||
|
||||
|
||||
file_functs.test: file_functs.cpp file_functs.h
|
||||
g++ -o $@ $< -DTEST_FILE_FUNCTS
|
||||
io_functs.test: io_functs.cpp io_functs.h
|
||||
g++ -o $@ $< -DTEST_IO_FUNCTS
|
||||
str_functs.ut: str_functs.cpp str_functs.h
|
||||
g++ -o $@ $< -DTEST_STR_FUNCTS
|
||||
vec_functs.test: vec_functs.cpp vec_functs.h vec_functs.tcc
|
||||
g++ -o $@ $< -DTEST_VEC_FUNCTS
|
||||
|
||||
logger.ut: logger.cpp logger.h file_functs.cpp file_functs.h
|
||||
g++ -o $@ $< file_functs.cpp -DUNIT_TEST
|
||||
config.ut: config.cpp config.h
|
||||
g++ -o $@ $< -DCONFIG_UT $(CMLIB)
|
||||
|
||||
clean:
|
||||
rm -f *.test *.ut *.o $(CMLIB)
|
||||
|
112
cppcommon/config.cpp
Normal file
112
cppcommon/config.cpp
Normal file
@ -0,0 +1,112 @@
|
||||
#include "config.h"
|
||||
#include "str_functs.h"
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
Config::Config()
|
||||
{
|
||||
_isInit = false;
|
||||
}
|
||||
|
||||
Config::~Config()
|
||||
{
|
||||
}
|
||||
|
||||
bool Config::init(const string& configFile)
|
||||
{
|
||||
char msgBuf[1024];
|
||||
if(_isInit)
|
||||
{
|
||||
LogFatal("already have been initialized. ");
|
||||
return false;
|
||||
}
|
||||
ifstream ifile(configFile.c_str());
|
||||
if(!ifile)
|
||||
{
|
||||
sprintf(msgBuf, "open configFile[%s] failed.", configFile.c_str());
|
||||
LogFatal(msgBuf);
|
||||
return false;
|
||||
}
|
||||
string line, key, value;
|
||||
vector<string> vecBuf;
|
||||
while(getline(ifile, line))
|
||||
{
|
||||
line = _stripComment(line);
|
||||
if(line.empty())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
vecBuf = splitStr(line, "=");
|
||||
if(2 != vecBuf.size())
|
||||
{
|
||||
sprintf(msgBuf, "line[%s] is illegal.", line.c_str());
|
||||
LogFatal(msgBuf);
|
||||
return false;
|
||||
}
|
||||
key = vecBuf[0];
|
||||
value = vecBuf[1];
|
||||
if(_map.end() != _map.find(key))
|
||||
{
|
||||
sprintf(msgBuf, "key[%s] already exists.", key.c_str());
|
||||
LogFatal(msgBuf);
|
||||
return false;
|
||||
}
|
||||
_map[key] = value;
|
||||
}
|
||||
ifile.close();
|
||||
_isInit = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void Config::display()
|
||||
{
|
||||
for(map<string, string>::iterator it = _map.begin(); it != _map.end(); it++)
|
||||
{
|
||||
cout<<"("<<it->first<<","<<it->second<<")"<<endl;
|
||||
}
|
||||
}
|
||||
|
||||
string Config::getByKey(const string& key)
|
||||
{
|
||||
if(_map.end() != _map.find(key))
|
||||
{
|
||||
return _map[key];
|
||||
}
|
||||
else
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
string Config::_stripComment(const string& line)
|
||||
{
|
||||
string res = line;
|
||||
string::size_type pos = res.find('#');
|
||||
if(string::npos != pos)
|
||||
{
|
||||
res = res.substr(0, pos);
|
||||
}
|
||||
return stripStr(res);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
Config gConfig;
|
||||
}
|
||||
|
||||
|
||||
#ifdef CONFIG_UT
|
||||
using namespace CPPCOMMON;
|
||||
int main()
|
||||
{
|
||||
gConfig.init("1.conf");
|
||||
gConfig.init("1.conf");
|
||||
gConfig.display();
|
||||
cout<<gConfig.getByKey("a")<<endl;
|
||||
cout<<gConfig.getByKey("cm")<<endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
38
cppcommon/config.h
Normal file
38
cppcommon/config.h
Normal file
@ -0,0 +1,38 @@
|
||||
#ifndef CPPCOMMON_CONFIG_H
|
||||
#define CPPCOMMON_CONFIG_H
|
||||
|
||||
|
||||
#include <map>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include "logger.h"
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
using std::map;
|
||||
using std::string;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
using std::ifstream;
|
||||
class Config
|
||||
{
|
||||
public:
|
||||
Config();
|
||||
~Config();
|
||||
bool init(const string& configFile);
|
||||
void display();
|
||||
string getByKey(const string& key);
|
||||
private:
|
||||
string _stripComment(const string& line);
|
||||
map<string, string> _map;
|
||||
bool _isInit;
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
extern Config gConfig;
|
||||
}
|
||||
|
||||
#endif
|
45
cppcommon/file_functs.cpp
Normal file
45
cppcommon/file_functs.cpp
Normal file
@ -0,0 +1,45 @@
|
||||
#include "file_functs.h"
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
using namespace std;
|
||||
bool checkFileExist(const char * filepath)
|
||||
{
|
||||
fstream _file;
|
||||
_file.open(filepath, ios::in);
|
||||
if(_file)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
bool createDir(const char * dir_path, bool p)
|
||||
{
|
||||
string dir_str(dir_path);
|
||||
string cmd = "mkdir";
|
||||
if(p)
|
||||
{
|
||||
cmd += " -p";
|
||||
}
|
||||
cmd += " " + dir_str;
|
||||
int res = system(cmd.c_str());
|
||||
return res;
|
||||
}
|
||||
bool checkDirExist(const char * dir_path)
|
||||
{
|
||||
return checkFileExist(dir_path);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef TEST_FILE_FUNCTS
|
||||
#include <iostream>
|
||||
using namespace CPPCOMMON;
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
char filename[] = "1/2/3";
|
||||
if(!checkFileExist(filename))
|
||||
{
|
||||
createDir(filename);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
16
cppcommon/file_functs.h
Normal file
16
cppcommon/file_functs.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef CPPCOMMON_FILE_FUNCTS_H
|
||||
#define CPPCOMMON_FILE_FUNCTS_H
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stdlib.h>
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
bool checkFileExist(const char * filepath);
|
||||
bool createDir(const char * dir_path, bool p = true);
|
||||
bool checkDirExist(const char * dir_path);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
25
cppcommon/io_functs.cpp
Normal file
25
cppcommon/io_functs.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
#include "io_functs.h"
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
string loadFile2Str(const char * const filepath)
|
||||
{
|
||||
ifstream in(filepath, ios::in);
|
||||
istreambuf_iterator<char> beg(in), end;
|
||||
string str(beg, end);
|
||||
in.close();
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef TEST_IO_FUNCTS
|
||||
#include <iostream>
|
||||
using namespace CPPCOMMON;
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
char filename[] = "1/2/3";
|
||||
cout<<loadFile2Str("1")<<endl;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
10
cppcommon/io_functs.h
Normal file
10
cppcommon/io_functs.h
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef CPPCOMMON_IO_FUNCTS_H
|
||||
#define CPPCOMMON_IO_FUNCTS_H
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
using namespace std;
|
||||
string loadFile2Str(const char * const filepath);
|
||||
}
|
||||
#endif
|
77
cppcommon/logger.cpp
Normal file
77
cppcommon/logger.cpp
Normal file
@ -0,0 +1,77 @@
|
||||
#include "logger.h"
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
const char * Logger::_logFormat = "%s [File:%s] [Line:%d] [%s] Msg:%s";
|
||||
const char * Logger::_timeFormat = "%Y-%m-%d %H:%M:%S";
|
||||
const char * Logger::_logDir = "./log/";
|
||||
const char * Logger::_logName = "run.log";
|
||||
Logger::Logger()
|
||||
{
|
||||
|
||||
_isCoutOpen = true;
|
||||
_logLevel[LL_DEBUG] = "DEBUG";
|
||||
_logLevel[LL_INFO] = "INFO";
|
||||
_logLevel[LL_WARN] = "WARN";
|
||||
_logLevel[LL_ERROR] = "ERROR";
|
||||
_logLevel[LL_FATAL] = "FATAL";
|
||||
InitDefault();
|
||||
}
|
||||
Logger::~Logger()
|
||||
{
|
||||
if(_logFile)
|
||||
{
|
||||
_logFile.close();
|
||||
}
|
||||
}
|
||||
void Logger::InitDefault()
|
||||
{
|
||||
_logCoutLevel = LL_INFO;
|
||||
_logFileLevel = LL_DEBUG;
|
||||
if(!checkDirExist(_logDir))
|
||||
{
|
||||
createDir(_logDir);
|
||||
}
|
||||
_logFile.open((string(_logDir) + string(_logName)).c_str(), ios::app);
|
||||
|
||||
}
|
||||
bool Logger::Logging(unsigned int level, const string& msg, const string& fileName, const int& lineNo)
|
||||
{
|
||||
if(level < LL_DEBUG || level > LL_FATAL)
|
||||
{
|
||||
cerr<<"level's value is out of range"<<endl;
|
||||
return false;
|
||||
}
|
||||
time(&_timeNow);
|
||||
strftime(_cStrBuf, sizeof(_cStrBuf), _timeFormat, localtime(&_timeNow));
|
||||
string timeStr = _cStrBuf;
|
||||
sprintf(_cStrBuf, _logFormat, timeStr.c_str(), fileName.c_str(), lineNo, _logLevel[level], msg.c_str());
|
||||
if(_isCoutOpen && level >= _logCoutLevel)
|
||||
{
|
||||
cout<<_cStrBuf<<endl;
|
||||
}
|
||||
if(_logFile && level >= _logFileLevel)
|
||||
{
|
||||
_logFile<<_cStrBuf<<endl;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
Logger loggerSingleTon;
|
||||
}
|
||||
|
||||
|
||||
#ifdef UNIT_TEST
|
||||
using namespace CPPCOMMON;
|
||||
int main()
|
||||
{
|
||||
LogDebug("debug log!");
|
||||
LogInfo("test info log");
|
||||
LogWarn("warning log");
|
||||
LogError("error log");
|
||||
LogFatal("fatal !!!!");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
59
cppcommon/logger.h
Normal file
59
cppcommon/logger.h
Normal file
@ -0,0 +1,59 @@
|
||||
#ifndef CPPCOMMON_LOGGER_H
|
||||
#define CPPCOMMON_LOGGER_H
|
||||
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <stdio.h>
|
||||
#include "file_functs.h"
|
||||
|
||||
#define LL_DEBUG 1
|
||||
#define LL_INFO 2
|
||||
#define LL_WARN 3
|
||||
#define LL_ERROR 4
|
||||
#define LL_FATAL 5
|
||||
#define LEVEL_ARRAY_SIZE 6
|
||||
#define CSTR_BUFFER_SIZE 1024
|
||||
|
||||
|
||||
#define LogDebug(msg) loggerSingleTon.Logging(LL_DEBUG, msg, __FILE__, __LINE__)
|
||||
#define LogInfo(msg) loggerSingleTon.Logging(LL_INFO, msg, __FILE__, __LINE__)
|
||||
#define LogWarn(msg) loggerSingleTon.Logging(LL_WARN, msg, __FILE__, __LINE__)
|
||||
#define LogError(msg) loggerSingleTon.Logging(LL_ERROR, msg, __FILE__, __LINE__)
|
||||
#define LogFatal(msg) loggerSingleTon.Logging(LL_FATAL, msg, __FILE__, __LINE__)
|
||||
|
||||
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
using namespace std;
|
||||
class Logger
|
||||
{
|
||||
public:
|
||||
Logger();
|
||||
~Logger();
|
||||
public:
|
||||
void InitDefault();
|
||||
bool Logging(unsigned int level, const string& msg, const string& fileName, const int& lineNo);
|
||||
private:
|
||||
bool _isCoutOpen;
|
||||
char _cStrBuf[CSTR_BUFFER_SIZE];
|
||||
const char * _logLevel[LEVEL_ARRAY_SIZE];
|
||||
ofstream _logFile;
|
||||
static const char * _logFormat;
|
||||
static const char * _timeFormat;
|
||||
static const char * _logDir;
|
||||
static const char * _logName;
|
||||
unsigned int _logCoutLevel;
|
||||
unsigned int _logFileLevel;
|
||||
time_t _timeNow;
|
||||
};
|
||||
}
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
extern Logger loggerSingleTon;
|
||||
}
|
||||
|
||||
#endif
|
270
cppcommon/str_functs.cpp
Normal file
270
cppcommon/str_functs.cpp
Normal file
@ -0,0 +1,270 @@
|
||||
#include "str_functs.h"
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
string joinStr(const vector<string>& src, const string& connectorStr)
|
||||
{
|
||||
string res;
|
||||
string tmpStr;
|
||||
size_t len = src.size();
|
||||
for(size_t i = 0; i < len - 1; i++)
|
||||
{
|
||||
res += stripStr(src[i]);
|
||||
res += connectorStr;
|
||||
}
|
||||
if(0 < len)
|
||||
{
|
||||
res += stripStr(src[len-1]);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
vector<string> splitStr(const string& source, const string& pattern)
|
||||
{
|
||||
vector<string> res;
|
||||
splitStr(source, res, pattern);
|
||||
return res;
|
||||
}
|
||||
void splitStr(const string& source, vector<string>& out_vec, const string& pattern)
|
||||
{
|
||||
if(0 == pattern.size())
|
||||
{
|
||||
return;
|
||||
}
|
||||
string s = source + pattern;
|
||||
string::size_type pos;
|
||||
int length = s.size();
|
||||
|
||||
for(int i = 0; i < length; i++)
|
||||
{
|
||||
pos = s.find(pattern, i);
|
||||
if(pos < length)
|
||||
{
|
||||
string tmp = stripStr(s.substr(i, pos - i));
|
||||
if("" != tmp)
|
||||
{
|
||||
out_vec.push_back(tmp);
|
||||
}
|
||||
i = pos + pattern.size() - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
string stripStr(const string& str, const string& patternStr)
|
||||
{
|
||||
if(str.empty())
|
||||
{
|
||||
return str;
|
||||
}
|
||||
string::size_type posL = str.find_first_not_of(patternStr);
|
||||
if(string::npos == posL)
|
||||
{
|
||||
return str;
|
||||
}
|
||||
string::size_type posR = str.find_last_not_of(patternStr);
|
||||
return str.substr(posL, posR - posL + 1);
|
||||
|
||||
}
|
||||
|
||||
bool splitStrMultiPatterns(
|
||||
const string& strSrc,
|
||||
vector<string>& outVec,
|
||||
const vector<string>& patterns
|
||||
)
|
||||
{
|
||||
char transChar = '#';
|
||||
int transLenThreshold = 10;
|
||||
string transStr;
|
||||
transStr += transChar;
|
||||
while(strSrc.find(transStr) != string::npos)
|
||||
{
|
||||
transStr += transChar;
|
||||
if(transStr.size() > transLenThreshold)
|
||||
return false;
|
||||
}
|
||||
string strSrcMutable = strSrc;
|
||||
for(int i = 0; i < patterns.size(); i++)
|
||||
{
|
||||
strSrcMutable = replaceStr(strSrcMutable, patterns[i], transStr);
|
||||
}
|
||||
splitStr(strSrcMutable, outVec, transStr);
|
||||
return true;
|
||||
}
|
||||
|
||||
string upperStr(const string& strIn)
|
||||
{
|
||||
string str = strIn;
|
||||
transform(str.begin(), str.end(), str.begin(), (int (*)(int))toupper);
|
||||
return str;
|
||||
}
|
||||
|
||||
string lowerStr(const string& strIn)
|
||||
{
|
||||
string str = strIn;
|
||||
transform(str.begin(), str.end(), str.begin(), (int (*)(int))tolower);
|
||||
return str;
|
||||
}
|
||||
|
||||
string replaceStr(const string& strSrc, const string& oldStr, const string& newStr, int count)
|
||||
{
|
||||
string strRet = strSrc;
|
||||
size_t pos = 0;
|
||||
int l_count = 0;
|
||||
if(-1 == count)
|
||||
count = strRet.size();
|
||||
while((pos = strRet.find(oldStr, pos)) != string::npos)
|
||||
{
|
||||
strRet.replace(pos, oldStr.size(), newStr);
|
||||
if(++l_count >= count)
|
||||
break;
|
||||
pos += newStr.size();
|
||||
}
|
||||
return strRet;
|
||||
}
|
||||
|
||||
unsigned int countStrDistance(const string& A, const string& B)
|
||||
{
|
||||
unsigned int lenA = A.size();
|
||||
unsigned int lenB = B.size();
|
||||
unsigned int len = (lenA < lenB ? lenA : lenB);
|
||||
unsigned int res = lenA + lenB - 2 * len;
|
||||
for(size_t i = 0; i < len; i++)
|
||||
{
|
||||
if(A[i] != B[i])
|
||||
res++;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
unsigned int countStrSimilarity(const string& A, const string& B)
|
||||
{
|
||||
unsigned int lenA = A.size();
|
||||
unsigned int lenB = B.size();
|
||||
unsigned int len = (lenA < lenB ? lenA : lenB);
|
||||
unsigned int res = 0;
|
||||
for(size_t i = 0; i < len; i++)
|
||||
{
|
||||
if(A[i] == B[i])
|
||||
res++;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
//unicode utf8 transform
|
||||
size_t unicodeToUtf8(uint16_t *in, size_t len, char * out)
|
||||
{
|
||||
size_t res = 0;
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
uint16_t unicode = in[i];
|
||||
if (unicode >= 0x0000 && unicode <= 0x007f)
|
||||
{
|
||||
*out = (uint8_t)unicode;
|
||||
out += 1;
|
||||
res += 1;
|
||||
}
|
||||
else if (unicode >= 0x0080 && unicode <= 0x07ff)
|
||||
{
|
||||
*out = 0xc0 | (unicode >> 6);
|
||||
out += 1;
|
||||
*out = 0x80 | (unicode & (0xff >> 2));
|
||||
out += 1;
|
||||
res += 2;
|
||||
}
|
||||
else if (unicode >= 0x0800 && unicode <= 0xffff)
|
||||
{
|
||||
*out = 0xe0 | (unicode >> 12);
|
||||
out += 1;
|
||||
*out = 0x80 | ((unicode >> 6) & 0x3f);
|
||||
out += 1;
|
||||
*out = 0x80 | (unicode & 0x3f);
|
||||
out += 1;
|
||||
res += 3;
|
||||
}
|
||||
|
||||
}
|
||||
*out = '\0';
|
||||
return res;
|
||||
}
|
||||
|
||||
/*from: http://www.cppblog.com/lf426/archive/2008/03/31/45796.html */
|
||||
int utf8ToUnicode(const char* inutf8, int len, uint16_t* unicode)
|
||||
{
|
||||
int length;
|
||||
const unsigned char* utf8 = (const unsigned char*) inutf8;
|
||||
const unsigned char* t = (const unsigned char*) inutf8;
|
||||
|
||||
length = 0;
|
||||
while (utf8 - t < len)
|
||||
{
|
||||
if ( *(unsigned char *) utf8 <= 0x7f )
|
||||
{
|
||||
//expand with 0s.
|
||||
*unicode++ = *utf8++;
|
||||
}
|
||||
//2 byte.
|
||||
else if ( *(unsigned char *) utf8 <= 0xdf )
|
||||
{
|
||||
*unicode++ = ((*(unsigned char *) utf8 & 0x1f) << 6) + ((*(unsigned char *) (utf8 + 1)) & 0x3f);
|
||||
utf8 += 2;
|
||||
}
|
||||
//3 byte.Chinese may use 3 byte.
|
||||
else {
|
||||
*unicode++ = ((int) (*(unsigned char *) utf8 & 0x0f) << 12) +
|
||||
((*(unsigned char *) (utf8 + 1) & 0x3f) << 6) +
|
||||
(*(unsigned char *) (utf8 + 2) & 0x3f);
|
||||
utf8 += 3;
|
||||
}
|
||||
length++;
|
||||
}
|
||||
|
||||
*unicode = 0;
|
||||
return length;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#ifdef TEST_STR_FUNCTS
|
||||
#include <iostream>
|
||||
using namespace CPPCOMMON;
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
//string s = " \t\n1 a h \n";
|
||||
//cout<<"["<<stripStr(s)<<"]"<<endl;
|
||||
//cout<<countStrDistance("Aheheh","heheh1212")<<endl;
|
||||
//cout<<joinStr(splitStr(s), ",")<<endl;
|
||||
//vector<string> vec;
|
||||
//splitStr("1 3 4", vec);
|
||||
//char * a[] = {"3","jaj","ads"};
|
||||
//vector<string> pats(a,a+3);
|
||||
//vec.clear();
|
||||
//splitStrMultiPattern("1 #3 jajs5 asdf3ads 4", vec, pats);
|
||||
//for(int i=0;i<vec.size();i++)
|
||||
//{
|
||||
// cout<<vec[i]<<endl;
|
||||
//}
|
||||
//string s = "1111aaafasfa,asdj.sadhashfhaha";
|
||||
//upperStr(s);
|
||||
//cout<<s<<endl;
|
||||
//
|
||||
//s = "ab1ba2ab3";
|
||||
//cout<<replaceStr(s,"ab","###")<<endl;
|
||||
ifstream ifile("testdata/dict.txt");
|
||||
string line;
|
||||
while(getline(ifile, line))
|
||||
{
|
||||
uint16_t strbuf[1024];
|
||||
|
||||
size_t unilen = utf8ToUnicode(line.c_str(), line.size(), strbuf);
|
||||
for(int i = 0; i < unilen; i++)
|
||||
{
|
||||
// printf("%x\n", strbuf[i]);
|
||||
}
|
||||
char utf8str[512]={0};
|
||||
unicodeToUtf8(strbuf, unilen, utf8str);
|
||||
//cout<<strlen(utf8str);
|
||||
cout<<utf8str<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
32
cppcommon/str_functs.h
Normal file
32
cppcommon/str_functs.h
Normal file
@ -0,0 +1,32 @@
|
||||
#ifndef CPPCOMMON_STR_FUNCTS_H
|
||||
#define CPPCOMMON_STR_FUNCTS_H
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <stdint.h>
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
using namespace std;
|
||||
string joinStr(const vector<string>& source, const string& connector);
|
||||
vector<string> splitStr(const string& source, const string& pattern = " \t\n");
|
||||
void splitStr(const string& source, vector<string>& out_vec, const string& pattern = " \t\n");
|
||||
bool splitStrMultiPatterns(
|
||||
const string& strSrc,
|
||||
vector<string>& outVec,
|
||||
const vector<string>& patterns
|
||||
);
|
||||
string upperStr(const string& str);
|
||||
string lowerStr(const string& str);
|
||||
string replaceStr(const string& strSrc, const string& oldStr, const string& newStr, int count = -1);
|
||||
string stripStr(const string& str, const string& patternstr = " \n\t");
|
||||
unsigned int countStrDistance(const string& A, const string& B);
|
||||
unsigned int countStrSimilarity(const string& A, const string& B);
|
||||
|
||||
|
||||
size_t unicodeToUtf8(uint16_t *in, size_t len, char * out);
|
||||
int utf8ToUnicode(const char* inutf8, int len, uint16_t* unicode);
|
||||
}
|
||||
#endif
|
100
cppcommon/testdata/dict.txt
vendored
Normal file
100
cppcommon/testdata/dict.txt
vendored
Normal file
@ -0,0 +1,100 @@
|
||||
AT&T 3 nz
|
||||
B超 3 n
|
||||
c# 3 nz
|
||||
C# 3 nz
|
||||
c++ 3 nz
|
||||
C++ 3 nz
|
||||
T恤 4 n
|
||||
一 217830 m
|
||||
一一 1670 m
|
||||
一一二 11 m
|
||||
一一例 3 m
|
||||
一一分 8 m
|
||||
一一列举 34 i
|
||||
一一对 9 m
|
||||
一一对应 43 l
|
||||
一一记 2 m
|
||||
一一道来 4 l
|
||||
一丁 18 d
|
||||
一丁不识 3 i
|
||||
一丁点 3 m
|
||||
一丁点儿 24 m
|
||||
一七 22 m
|
||||
一七八不 3 l
|
||||
一万 442 m
|
||||
一万一千 4 m
|
||||
一万一千五百二十颗 2 m
|
||||
一万一千八百八十斤 2 m
|
||||
一万一千多间 2 m
|
||||
一万一千零九十五册 4 m
|
||||
一万七千 5 m
|
||||
一万七千余 2 m
|
||||
一万七千多 2 m
|
||||
一万七千多户 2 m
|
||||
一万万 4 m
|
||||
一万万两 4 m
|
||||
一万三千 8 m
|
||||
一万三千五百一十七 2 m
|
||||
一万三千五百斤 4 m
|
||||
一万三千余种 2 m
|
||||
一万三千块 2 m
|
||||
一万两 124 m
|
||||
一万两万 4 m
|
||||
一万两千 3 m
|
||||
一万个 62 m
|
||||
一万九千 2 m
|
||||
一万九千余 2 m
|
||||
一万二 10 m
|
||||
一万二千 7 m
|
||||
一万二千两 2 m
|
||||
一万二千五百 4 m
|
||||
龛 223 ng
|
||||
龜 2 zg
|
||||
龟 903 ns
|
||||
龟儿子 123 n
|
||||
龟兆 3 nz
|
||||
龟兹 215 ns
|
||||
龟兹王 3 nrt
|
||||
龟冷搘床 3 v
|
||||
龟冷支床 3 n
|
||||
龟卜 3 n
|
||||
龟厌不告 3 l
|
||||
龟壳 33 n
|
||||
龟壳花 3 n
|
||||
龟头 34 n
|
||||
龟头炎 3 n
|
||||
龟山 23 ns
|
||||
龟山乡 3 ns
|
||||
龟山岛 3 ns
|
||||
龟年鹤寿 3 ns
|
||||
龟年鹤算 3 l
|
||||
龟文 3 nz
|
||||
龟文写迹 3 n
|
||||
龟文鸟迹 3 n
|
||||
龟板 10 n
|
||||
龟毛免角 3 n
|
||||
龟毛兔角 3 n
|
||||
龟溪 3 ns
|
||||
龟玉 3 nz
|
||||
龟王 3 nz
|
||||
龟甲 92 ns
|
||||
龟甲胶 3 nz
|
||||
龟筮 3 n
|
||||
龟纹 3 n
|
||||
龟缩 29 v
|
||||
龟肉 3 n
|
||||
龟背 21 n
|
||||
龟背竹 3 n
|
||||
龟苓膏 3 n
|
||||
龟苗 3 n
|
||||
龟裂 34 v
|
||||
龟足 5 v
|
||||
龟鉴 2 n
|
||||
龟镜 3 nz
|
||||
龟鳖 3 n
|
||||
龟鹤遐寿 3 l
|
||||
龟龄鹤算 3 n
|
||||
龟龙片甲 3 nz
|
||||
龟龙麟凤 3 ns
|
||||
龠 5 g
|
||||
龢 732 zg
|
30
cppcommon/vec_functs.cpp
Normal file
30
cppcommon/vec_functs.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
#include "vec_functs.h"
|
||||
|
||||
#ifdef TEST_VEC_FUNCTS
|
||||
using namespace CPPCOMMON;
|
||||
int main()
|
||||
{
|
||||
vector<int> vec;
|
||||
for(int i=0;i<5;i++)
|
||||
vec.push_back(i);
|
||||
vector<int> pats;
|
||||
pats.push_back(0);
|
||||
pats.push_back(3);
|
||||
//pats.push_back(4);
|
||||
vector<pair<int, vector<int> > > res;
|
||||
splitVec<int>(vec,res,pats);
|
||||
cout<<isInVec<int>(vec, 0)<<endl;
|
||||
cout<<isInVec<int>(vec, -1)<<endl;
|
||||
for(int i =0;i<res.size();i++)
|
||||
{
|
||||
cout<<"first:"<<res[i].first<<endl;
|
||||
cout<<"seconde:"<<endl;
|
||||
for(int j = 0;j<res[i].second.size();j++)
|
||||
{
|
||||
cout<<res[i].second[j]<<endl;
|
||||
}
|
||||
cout<<endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
31
cppcommon/vec_functs.h
Normal file
31
cppcommon/vec_functs.h
Normal file
@ -0,0 +1,31 @@
|
||||
#ifndef CPPCOMMON_VEC_FUNCTS_H
|
||||
#define CPPCOMMON_VEC_FUNCTS_H
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <queue>
|
||||
#include <string>
|
||||
|
||||
#define FOR_VECTOR(vec, i) for(size_t i = 0; i < vec.size(); i++)
|
||||
|
||||
#define PRINT_VECTOR(vec) FOR_VECTOR(vec, i)\
|
||||
{\
|
||||
cout<<vec[i]<<endl;\
|
||||
}
|
||||
|
||||
#define PRINT_MATRIX(mat) FOR_VECTOR(mat, i) \
|
||||
{\
|
||||
FOR_VECTOR(mat[i], j)\
|
||||
{\
|
||||
cout<<"["<<i<<","<<j<<"]:"<<mat[i][j]<<endl;\
|
||||
}\
|
||||
}
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
using namespace std;
|
||||
}
|
||||
|
||||
#include "vec_functs.tcc"
|
||||
|
||||
#endif
|
92
cppcommon/vec_functs.tcc
Normal file
92
cppcommon/vec_functs.tcc
Normal file
@ -0,0 +1,92 @@
|
||||
|
||||
#ifndef CPPCOMMON_VEC_FUNCTS_TCC
|
||||
#define CPPCOMMON_VEC_FUNCTS_TCC
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <algorithm>
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
using namespace std;
|
||||
template<typename T>
|
||||
bool isInVec(const vector<T>& vec, const T& item)
|
||||
{
|
||||
typename vector<T>::const_iterator it = find(vec.begin(), vec.end(), item);
|
||||
return it != vec.end();
|
||||
}
|
||||
template<typename T>
|
||||
void splitVec(const vector<T>& vecSrc, vector< pair<T, vector<T> > >& outVec, const vector<T>& patterns)
|
||||
{
|
||||
vector<T> tmp;
|
||||
T pattern;
|
||||
size_t patternSize = patterns.size();
|
||||
for(size_t i = 0; i < vecSrc.size(); i++)
|
||||
{
|
||||
size_t patternPos = patternSize;
|
||||
for(size_t j = 0; j < patternSize; j++)
|
||||
{
|
||||
if(patterns[j] == vecSrc[i])
|
||||
{
|
||||
patternPos = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(patternPos != patternSize)
|
||||
{
|
||||
if(!tmp.empty())
|
||||
{
|
||||
outVec.push_back(make_pair<T, vector<T> >(pattern, tmp));
|
||||
tmp.clear();
|
||||
}
|
||||
pattern = patterns[patternPos];
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp.push_back(vecSrc[i]);
|
||||
}
|
||||
}
|
||||
if(!tmp.empty())
|
||||
{
|
||||
outVec.push_back(make_pair<T, vector<T> >(pattern, tmp));
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void splitVec(const vector<T>& vecSrc, vector< vector<T> >& outVec, const vector<T>& patternVec)
|
||||
{
|
||||
vector<T> tmp;
|
||||
for(size_t i = 0; i < vecSrc.size(); i++)
|
||||
{
|
||||
bool flag = false;
|
||||
for(size_t j = 0; j < patternVec.size(); j++)
|
||||
{
|
||||
if(patternVec[j] == vecSrc[i])
|
||||
{
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(flag)
|
||||
{
|
||||
if(!tmp.empty())
|
||||
{
|
||||
outVec.push_back(tmp);
|
||||
tmp.clear();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
tmp.push_back(vecSrc[i]);
|
||||
}
|
||||
}
|
||||
if(!tmp.empty())
|
||||
{
|
||||
outVec.push_back(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user