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
15a06fad9b
commit
f6142c6ff0
@ -15,6 +15,11 @@ void testKeyWordExt(const char * dictPath, const char * filePath)
|
||||
cerr<<__FILE__<<__LINE__<<endl;
|
||||
return ;
|
||||
}
|
||||
if(!ext.loadStopWords("../dicts/stopwords.gbk.v1.0"))
|
||||
{
|
||||
cerr<<__FILE__<<__LINE__<<endl;
|
||||
return ;
|
||||
}
|
||||
ifstream ifile(filePath);
|
||||
vector<string> res;
|
||||
string line;
|
||||
|
@ -29,6 +29,8 @@ 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
|
||||
argv_functs.ut: argv_functs.cpp str_functs.cpp argv_functs.h
|
||||
g++ -o $@ argv_functs.cpp str_functs.cpp -DARGV_FUNCTS_UT
|
||||
encoding.ut: encoding.cpp str_functs.cpp str_functs.h encoding.h
|
||||
g++ -o $@ encoding.cpp str_functs.cpp -DENCODING_UT
|
||||
vec_functs.test: vec_functs.cpp vec_functs.h vec_functs.tcc
|
||||
|
42
src/cppcommon/argv_functs.cpp
Normal file
42
src/cppcommon/argv_functs.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
#include "argv_functs.h"
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
bool getArgvMap(int argc, char** argv, map<string,string>& mpss)
|
||||
{
|
||||
mpss.clear();
|
||||
for(int i = 0; i < argc; i++)
|
||||
{
|
||||
if(strStartsWith(argv[i], "--"))
|
||||
{
|
||||
if(i + 1 < argc && !strStartsWith(argv[i+1], "--"))
|
||||
{
|
||||
mpss[argv[i]] = argv[i+1];
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#ifdef ARGV_FUNCTS_UT
|
||||
|
||||
using namespace CPPCOMMON;
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
map<string,string> argvMap;
|
||||
getArgvMap(argc, argv, argvMap);
|
||||
PRINT_MAPSS(argvMap);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
18
src/cppcommon/argv_functs.h
Normal file
18
src/cppcommon/argv_functs.h
Normal file
@ -0,0 +1,18 @@
|
||||
/************************************
|
||||
* file enc : ascii
|
||||
* author : wuyanyi09@gmail.com
|
||||
************************************/
|
||||
|
||||
#ifndef CPPCOMMON_ARGV_FUNCTS_H
|
||||
#define CPPCOMMON_ARGV_FUNCTS_H
|
||||
|
||||
#include "str_functs.h"
|
||||
#include "map_functs.h"
|
||||
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
using namespace std;
|
||||
bool getArgvMap(int argc, char** argv, map<string, string>& mpss);
|
||||
}
|
||||
|
||||
#endif
|
@ -9,5 +9,7 @@
|
||||
#include "config.h"
|
||||
#include "typedefs.h"
|
||||
#include "str_functs.h"
|
||||
#include "map_functs.h"
|
||||
#include "arfv_functs.h"
|
||||
|
||||
#endif
|
||||
|
18
src/cppcommon/map_functs.h
Normal file
18
src/cppcommon/map_functs.h
Normal file
@ -0,0 +1,18 @@
|
||||
/************************************
|
||||
* file enc : ascii
|
||||
* author : wuyanyi09@gmail.com
|
||||
************************************/
|
||||
|
||||
|
||||
#ifndef CPPCOMMON_MAP_FUNCTS_H
|
||||
#define CPPCOMMON_MAP_FUNCTS_H
|
||||
|
||||
#define PRINT_MAPSS(mpss) \
|
||||
{\
|
||||
for(map<string, string>::const_iterator it = mpss.begin(); it != mpss.end(); it++)\
|
||||
{\
|
||||
cout<<it->first<<' '<<it->second<<endl;\
|
||||
}\
|
||||
}
|
||||
|
||||
#endif
|
@ -7,7 +7,7 @@
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
//http://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf
|
||||
std::string string_format(const std::string fmt, ...)
|
||||
string string_format(const string fmt, ...)
|
||||
{
|
||||
int size = 100;
|
||||
std::string str;
|
||||
@ -407,6 +407,11 @@ namespace CPPCOMMON
|
||||
return res;
|
||||
}
|
||||
|
||||
bool strStartsWith(const string& str, const string& prefix)
|
||||
{
|
||||
return str.substr(0, prefix.size()) == prefix;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#ifdef TEST_STR_FUNCTS
|
||||
@ -452,16 +457,16 @@ int main()
|
||||
// cout<<utf8str<<endl;
|
||||
//}
|
||||
//cout<<string_format("hehe%s11asd%dasf","[here]",2);
|
||||
ifstream ifile("testdata/dict.gbk");
|
||||
string line;
|
||||
Unicode unicode;
|
||||
while(getline(ifile, line))
|
||||
{
|
||||
cout<<line<<endl;
|
||||
utf8ToUnicode(line, unicode);
|
||||
printUnicode(unicode);
|
||||
cout<<unicodeToUtf8(unicode)<<endl;;
|
||||
}
|
||||
//ifstream ifile("testdata/dict.gbk");
|
||||
//string line;
|
||||
//Unicode unicode;
|
||||
//while(getline(ifile, line))
|
||||
//{
|
||||
// cout<<line<<endl;
|
||||
// utf8ToUnicode(line, unicode);
|
||||
// printUnicode(unicode);
|
||||
// cout<<unicodeToUtf8(unicode)<<endl;;
|
||||
//}
|
||||
//vector<string> tmp;
|
||||
//tmp.push_back("1");
|
||||
////tmp.push_back("2");
|
||||
@ -476,6 +481,12 @@ int main()
|
||||
// s = utf8ToGbk(s);
|
||||
// cout<<s<<endl;
|
||||
//}
|
||||
cout<<strStartsWith("--help","--")<<endl;
|
||||
cout<<strStartsWith("--help","-")<<endl;
|
||||
cout<<strStartsWith("--help","he")<<endl;
|
||||
cout<<strStartsWith("help","help")<<endl;
|
||||
cout<<strStartsWith("","help")<<endl;
|
||||
cout<<strStartsWith("hel","")<<endl;
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,5 +1,5 @@
|
||||
/************************************
|
||||
* file enc : utf8
|
||||
* file enc : ascii
|
||||
* author : wuyanyi09@gmail.com
|
||||
************************************/
|
||||
#ifndef CPPCOMMON_STR_FUNCTS_H
|
||||
@ -20,7 +20,7 @@
|
||||
namespace CPPCOMMON
|
||||
{
|
||||
using namespace std;
|
||||
std::string string_format(const std::string fmt, ...) ;
|
||||
string string_format(const string fmt, ...) ;
|
||||
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");
|
||||
@ -67,6 +67,7 @@ namespace CPPCOMMON
|
||||
cout<<uniVecToStr(unicode)<<endl;
|
||||
}
|
||||
|
||||
bool strStartsWith(const string& str, const string& prefix);
|
||||
|
||||
}
|
||||
#endif
|
||||
|
@ -1,5 +1,5 @@
|
||||
/************************************
|
||||
* file enc : utf8
|
||||
* file enc : ascii
|
||||
* author : wuyanyi09@gmail.com
|
||||
************************************/
|
||||
#ifndef CPPCOMMON_VEC_FUNCTS_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user