jieba_rb 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.gitmodules +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +51 -0
- data/Rakefile +11 -0
- data/ext/cppjieba/.gitignore +17 -0
- data/ext/cppjieba/.travis.yml +22 -0
- data/ext/cppjieba/CMakeLists.txt +27 -0
- data/ext/cppjieba/ChangeLog.md +81 -0
- data/ext/cppjieba/Dockerfile +11 -0
- data/ext/cppjieba/LICENSE +20 -0
- data/ext/cppjieba/README.md +359 -0
- data/ext/cppjieba/conf/CMakeLists.txt +1 -0
- data/ext/cppjieba/conf/server.conf +16 -0
- data/ext/cppjieba/dict/CMakeLists.txt +1 -0
- data/ext/cppjieba/dict/README.md +31 -0
- data/ext/cppjieba/dict/extra_dict/jieba.dict.small.utf8 +109750 -0
- data/ext/cppjieba/dict/gbk_dict/hmm_model.gbk +34 -0
- data/ext/cppjieba/dict/gbk_dict/jieba.dict.gbk +348982 -0
- data/ext/cppjieba/dict/hmm_model.utf8 +34 -0
- data/ext/cppjieba/dict/idf.utf8 +258826 -0
- data/ext/cppjieba/dict/jieba.dict.utf8 +348982 -0
- data/ext/cppjieba/dict/pos_dict/char_state_tab.utf8 +6653 -0
- data/ext/cppjieba/dict/pos_dict/prob_emit.utf8 +166 -0
- data/ext/cppjieba/dict/pos_dict/prob_start.utf8 +259 -0
- data/ext/cppjieba/dict/pos_dict/prob_trans.utf8 +5222 -0
- data/ext/cppjieba/dict/stop_words.utf8 +1534 -0
- data/ext/cppjieba/dict/user.dict.utf8 +3 -0
- data/ext/cppjieba/script/CMakeLists.txt +1 -0
- data/ext/cppjieba/script/cjserver.start +12 -0
- data/ext/cppjieba/script/cjserver.stop +13 -0
- data/ext/cppjieba/server/CMakeLists.txt +9 -0
- data/ext/cppjieba/server/Husky/HttpReqInfo.hpp +294 -0
- data/ext/cppjieba/server/Husky/IRequestHandler.hpp +18 -0
- data/ext/cppjieba/server/Husky/ThreadPoolServer.hpp +108 -0
- data/ext/cppjieba/server/Husky/WorkerThread.hpp +133 -0
- data/ext/cppjieba/server/server.cpp +91 -0
- data/ext/cppjieba/src/DictTrie.hpp +211 -0
- data/ext/cppjieba/src/FullSegment.hpp +153 -0
- data/ext/cppjieba/src/HMMSegment.hpp +394 -0
- data/ext/cppjieba/src/ISegment.hpp +17 -0
- data/ext/cppjieba/src/KeywordExtractor.hpp +173 -0
- data/ext/cppjieba/src/Limonp/ArgvContext.hpp +84 -0
- data/ext/cppjieba/src/Limonp/BlockingQueue.hpp +128 -0
- data/ext/cppjieba/src/Limonp/BoundedQueue.hpp +73 -0
- data/ext/cppjieba/src/Limonp/CastFloat.hpp +90 -0
- data/ext/cppjieba/src/Limonp/Condition.hpp +48 -0
- data/ext/cppjieba/src/Limonp/Config.hpp +118 -0
- data/ext/cppjieba/src/Limonp/HandyMacro.hpp +31 -0
- data/ext/cppjieba/src/Limonp/InitOnOff.hpp +21 -0
- data/ext/cppjieba/src/Limonp/LocalVector.hpp +171 -0
- data/ext/cppjieba/src/Limonp/Logger.hpp +74 -0
- data/ext/cppjieba/src/Limonp/Md5.hpp +432 -0
- data/ext/cppjieba/src/Limonp/MutexLock.hpp +57 -0
- data/ext/cppjieba/src/Limonp/MysqlClient.hpp +125 -0
- data/ext/cppjieba/src/Limonp/NonCopyable.hpp +22 -0
- data/ext/cppjieba/src/Limonp/StdExtension.hpp +139 -0
- data/ext/cppjieba/src/Limonp/StringUtil.hpp +349 -0
- data/ext/cppjieba/src/Limonp/Thread.hpp +50 -0
- data/ext/cppjieba/src/Limonp/ThreadPool.hpp +105 -0
- data/ext/cppjieba/src/MPSegment.hpp +148 -0
- data/ext/cppjieba/src/MixSegment.hpp +121 -0
- data/ext/cppjieba/src/PosTagger.hpp +109 -0
- data/ext/cppjieba/src/QuerySegment.hpp +123 -0
- data/ext/cppjieba/src/SegmentBase.hpp +78 -0
- data/ext/cppjieba/src/TransCode.hpp +63 -0
- data/ext/cppjieba/src/Trie.hpp +298 -0
- data/ext/cppjieba/test/CMakeLists.txt +7 -0
- data/ext/cppjieba/test/keyword_demo.cpp +16 -0
- data/ext/cppjieba/test/load_test.cpp +56 -0
- data/ext/cppjieba/test/segment_demo.cpp +59 -0
- data/ext/cppjieba/test/servertest/go_load_test.sh +2 -0
- data/ext/cppjieba/test/servertest/load_test.py +91 -0
- data/ext/cppjieba/test/servertest/run_curl.sh +11 -0
- data/ext/cppjieba/test/tagging_demo.cpp +12 -0
- data/ext/cppjieba/test/testdata/curl.res +1 -0
- data/ext/cppjieba/test/testdata/jieba.dict.0.1.utf8 +93 -0
- data/ext/cppjieba/test/testdata/jieba.dict.0.utf8 +93 -0
- data/ext/cppjieba/test/testdata/jieba.dict.1.utf8 +67 -0
- data/ext/cppjieba/test/testdata/jieba.dict.2.utf8 +64 -0
- data/ext/cppjieba/test/testdata/load_test.urls +2 -0
- data/ext/cppjieba/test/testdata/review.100 +100 -0
- data/ext/cppjieba/test/testdata/review.100.res +200 -0
- data/ext/cppjieba/test/testdata/server.conf +13 -0
- data/ext/cppjieba/test/testdata/testlines.gbk +9 -0
- data/ext/cppjieba/test/testdata/testlines.utf8 +8 -0
- data/ext/cppjieba/test/testdata/userdict.utf8 +6 -0
- data/ext/cppjieba/test/testdata/weicheng.utf8 +247 -0
- data/ext/cppjieba/test/unittest/CMakeLists.txt +28 -0
- data/ext/cppjieba/test/unittest/TKeywordExtractor.cpp +18 -0
- data/ext/cppjieba/test/unittest/TPosTagger.cpp +43 -0
- data/ext/cppjieba/test/unittest/TSegments.cpp +187 -0
- data/ext/cppjieba/test/unittest/TTrie.cpp +80 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-death-test.h +283 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-message.h +230 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-param-test.h +1421 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-param-test.h.pump +487 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-printers.h +796 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-spi.h +232 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-test-part.h +176 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest-typed-test.h +259 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest.h +2155 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest_pred_impl.h +358 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/gtest_prod.h +58 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-death-test-internal.h +308 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-filepath.h +210 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-internal.h +1226 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-linked_ptr.h +233 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-param-util-generated.h +4822 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-param-util-generated.h.pump +301 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-param-util.h +619 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-port.h +1788 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-string.h +350 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-tuple.h +968 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-tuple.h.pump +336 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-type-util.h +3330 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/include/gtest/internal/gtest-type-util.h.pump +296 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/src/.deps/.dirstamp +0 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/src/.deps/gtest-all.Plo +681 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/src/.deps/gtest_main.Plo +509 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/src/.dirstamp +0 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-all.cc +48 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-death-test.cc +1234 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-filepath.cc +380 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-internal-inl.h +1038 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-port.cc +746 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-printers.cc +356 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-test-part.cc +110 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest-typed-test.cc +110 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest.cc +4898 -0
- data/ext/cppjieba/test/unittest/gtest-1.6.0/src/gtest_main.cc +39 -0
- data/ext/cppjieba/test/unittest/gtest_main.cpp +39 -0
- data/ext/jieba/extconf.rb +26 -0
- data/ext/jieba/jieba.c +9 -0
- data/ext/jieba/jieba.h +9 -0
- data/ext/jieba/segment.cc +88 -0
- data/ext/jieba/segment.h +17 -0
- data/jieba_rb.gemspec +51 -0
- data/lib/jieba_rb/version.rb +3 -0
- data/lib/jieba_rb.rb +28 -0
- data/test/test_segment.rb +32 -0
- metadata +246 -0
@@ -0,0 +1,139 @@
|
|
1
|
+
#ifndef LIMONP_STD_EXTEMSION_HPP
|
2
|
+
#define LIMONP_STD_EXTEMSION_HPP
|
3
|
+
|
4
|
+
#include <map>
|
5
|
+
|
6
|
+
#if(__cplusplus == 201103L)
|
7
|
+
#include <unordered_map>
|
8
|
+
#include <unordered_set>
|
9
|
+
#else
|
10
|
+
#include <tr1/unordered_map>
|
11
|
+
#include <tr1/unordered_set>
|
12
|
+
namespace std
|
13
|
+
{
|
14
|
+
using std::tr1::unordered_map;
|
15
|
+
using std::tr1::unordered_set;
|
16
|
+
}
|
17
|
+
|
18
|
+
#endif
|
19
|
+
|
20
|
+
#include <set>
|
21
|
+
#include <vector>
|
22
|
+
#include <fstream>
|
23
|
+
#include <sstream>
|
24
|
+
|
25
|
+
|
26
|
+
namespace std
|
27
|
+
{
|
28
|
+
template<typename T>
|
29
|
+
ostream& operator << (ostream& os, const vector<T>& vec)
|
30
|
+
{
|
31
|
+
if(vec.empty())
|
32
|
+
{
|
33
|
+
return os << "[]";
|
34
|
+
}
|
35
|
+
os<<"[\""<<vec[0];
|
36
|
+
for(size_t i = 1; i < vec.size(); i++)
|
37
|
+
{
|
38
|
+
os<<"\", \""<<vec[i];
|
39
|
+
}
|
40
|
+
os<<"\"]";
|
41
|
+
return os;
|
42
|
+
}
|
43
|
+
template<class T1, class T2>
|
44
|
+
ostream& operator << (ostream& os, const pair<T1, T2>& pr)
|
45
|
+
{
|
46
|
+
os << pr.first << ":" << pr.second ;
|
47
|
+
return os;
|
48
|
+
}
|
49
|
+
|
50
|
+
|
51
|
+
template<class T>
|
52
|
+
string& operator << (string& str, const T& obj)
|
53
|
+
{
|
54
|
+
stringstream ss;
|
55
|
+
ss << obj; // call ostream& operator << (ostream& os,
|
56
|
+
return str = ss.str();
|
57
|
+
}
|
58
|
+
|
59
|
+
template<class T1, class T2>
|
60
|
+
ostream& operator << (ostream& os, const map<T1, T2>& mp)
|
61
|
+
{
|
62
|
+
if(mp.empty())
|
63
|
+
{
|
64
|
+
os<<"{}";
|
65
|
+
return os;
|
66
|
+
}
|
67
|
+
os<<'{';
|
68
|
+
typename map<T1, T2>::const_iterator it = mp.begin();
|
69
|
+
os<<*it;
|
70
|
+
it++;
|
71
|
+
while(it != mp.end())
|
72
|
+
{
|
73
|
+
os<<", "<<*it;
|
74
|
+
it++;
|
75
|
+
}
|
76
|
+
os<<'}';
|
77
|
+
return os;
|
78
|
+
}
|
79
|
+
template<class T1, class T2>
|
80
|
+
ostream& operator << (ostream& os, const std::unordered_map<T1, T2>& mp)
|
81
|
+
{
|
82
|
+
if(mp.empty())
|
83
|
+
{
|
84
|
+
return os << "{}";
|
85
|
+
}
|
86
|
+
os<<'{';
|
87
|
+
typename std::unordered_map<T1, T2>::const_iterator it = mp.begin();
|
88
|
+
os<<*it;
|
89
|
+
it++;
|
90
|
+
while(it != mp.end())
|
91
|
+
{
|
92
|
+
os<<", "<<*it++;
|
93
|
+
}
|
94
|
+
return os<<'}';
|
95
|
+
}
|
96
|
+
|
97
|
+
template<class T>
|
98
|
+
ostream& operator << (ostream& os, const set<T>& st)
|
99
|
+
{
|
100
|
+
if(st.empty())
|
101
|
+
{
|
102
|
+
os << "{}";
|
103
|
+
return os;
|
104
|
+
}
|
105
|
+
os<<'{';
|
106
|
+
typename set<T>::const_iterator it = st.begin();
|
107
|
+
os<<*it;
|
108
|
+
it++;
|
109
|
+
while(it != st.end())
|
110
|
+
{
|
111
|
+
os<<", "<<*it;
|
112
|
+
it++;
|
113
|
+
}
|
114
|
+
os<<'}';
|
115
|
+
return os;
|
116
|
+
}
|
117
|
+
|
118
|
+
template<class KeyType, class ContainType>
|
119
|
+
bool isIn(const ContainType& contain, const KeyType& key)
|
120
|
+
{
|
121
|
+
return contain.end() != contain.find(key);
|
122
|
+
}
|
123
|
+
|
124
|
+
template<class T>
|
125
|
+
basic_string<T> & operator << (basic_string<T> & s, ifstream & ifs)
|
126
|
+
{
|
127
|
+
return s.assign((istreambuf_iterator<T>(ifs)), istreambuf_iterator<T>());
|
128
|
+
}
|
129
|
+
|
130
|
+
template<class T>
|
131
|
+
ofstream & operator << (ofstream & ofs, const basic_string<T>& s)
|
132
|
+
{
|
133
|
+
ostreambuf_iterator<T> itr (ofs);
|
134
|
+
copy(s.begin(), s.end(), itr);
|
135
|
+
return ofs;
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
#endif
|
@@ -0,0 +1,349 @@
|
|
1
|
+
/************************************
|
2
|
+
* file enc : ascii
|
3
|
+
* author : wuyanyi09@gmail.com
|
4
|
+
************************************/
|
5
|
+
#ifndef LIMONP_STR_FUNCTS_H
|
6
|
+
#define LIMONP_STR_FUNCTS_H
|
7
|
+
#include <fstream>
|
8
|
+
#include <iostream>
|
9
|
+
#include <string>
|
10
|
+
#include <vector>
|
11
|
+
#include <algorithm>
|
12
|
+
#include <cctype>
|
13
|
+
#include <map>
|
14
|
+
#include <stdint.h>
|
15
|
+
#include <stdio.h>
|
16
|
+
#include <stdarg.h>
|
17
|
+
#include <memory.h>
|
18
|
+
#include <functional>
|
19
|
+
#include <locale>
|
20
|
+
#include <sstream>
|
21
|
+
#include <sys/types.h>
|
22
|
+
#include <iterator>
|
23
|
+
#include <algorithm>
|
24
|
+
#include "StdExtension.hpp"
|
25
|
+
|
26
|
+
namespace Limonp
|
27
|
+
{
|
28
|
+
using namespace std;
|
29
|
+
inline string string_format(const char* fmt, ...)
|
30
|
+
{
|
31
|
+
int size = 256;
|
32
|
+
std::string str;
|
33
|
+
va_list ap;
|
34
|
+
while (1) {
|
35
|
+
str.resize(size);
|
36
|
+
va_start(ap, fmt);
|
37
|
+
int n = vsnprintf((char *)str.c_str(), size, fmt, ap);
|
38
|
+
va_end(ap);
|
39
|
+
if (n > -1 && n < size) {
|
40
|
+
str.resize(n);
|
41
|
+
return str;
|
42
|
+
}
|
43
|
+
if (n > -1)
|
44
|
+
size = n + 1;
|
45
|
+
else
|
46
|
+
size *= 2;
|
47
|
+
}
|
48
|
+
return str;
|
49
|
+
}
|
50
|
+
|
51
|
+
template<class T>
|
52
|
+
void join(T begin, T end, string& res, const string& connector)
|
53
|
+
{
|
54
|
+
if(begin == end)
|
55
|
+
{
|
56
|
+
return;
|
57
|
+
}
|
58
|
+
stringstream ss;
|
59
|
+
ss<<*begin;
|
60
|
+
begin++;
|
61
|
+
while(begin != end)
|
62
|
+
{
|
63
|
+
ss << connector << *begin;
|
64
|
+
begin ++;
|
65
|
+
}
|
66
|
+
res = ss.str();
|
67
|
+
}
|
68
|
+
|
69
|
+
template<class T>
|
70
|
+
string join(T begin, T end, const string& connector)
|
71
|
+
{
|
72
|
+
string res;
|
73
|
+
join(begin ,end, res, connector);
|
74
|
+
return res;
|
75
|
+
}
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
inline bool split(const string& src, vector<string>& res, const string& pattern, size_t offset = 0, size_t len = string::npos)
|
80
|
+
{
|
81
|
+
if(src.empty())
|
82
|
+
{
|
83
|
+
return false;
|
84
|
+
}
|
85
|
+
res.clear();
|
86
|
+
|
87
|
+
size_t start = 0;
|
88
|
+
size_t end = 0;
|
89
|
+
size_t cnt = 0;
|
90
|
+
while(start < src.size() && res.size() < len)
|
91
|
+
{
|
92
|
+
end = src.find_first_of(pattern, start);
|
93
|
+
if(string::npos == end)
|
94
|
+
{
|
95
|
+
if(cnt >= offset)
|
96
|
+
{
|
97
|
+
res.push_back(src.substr(start));
|
98
|
+
}
|
99
|
+
return true;
|
100
|
+
}
|
101
|
+
//if(end == src.size() - 1)
|
102
|
+
//{
|
103
|
+
// res.push_back("");
|
104
|
+
// return true;
|
105
|
+
//}
|
106
|
+
if(cnt >= offset)
|
107
|
+
{
|
108
|
+
res.push_back(src.substr(start, end - start));
|
109
|
+
}
|
110
|
+
cnt ++;
|
111
|
+
start = end + 1;
|
112
|
+
}
|
113
|
+
return true;
|
114
|
+
}
|
115
|
+
|
116
|
+
inline string& upper(string& str)
|
117
|
+
{
|
118
|
+
transform(str.begin(), str.end(), str.begin(), (int (*)(int))toupper);
|
119
|
+
return str;
|
120
|
+
}
|
121
|
+
|
122
|
+
inline string& lower(string& str)
|
123
|
+
{
|
124
|
+
transform(str.begin(), str.end(), str.begin(), (int (*)(int))tolower);
|
125
|
+
return str;
|
126
|
+
}
|
127
|
+
|
128
|
+
inline std::string <rim(std::string &s)
|
129
|
+
{
|
130
|
+
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
|
131
|
+
return s;
|
132
|
+
}
|
133
|
+
|
134
|
+
inline std::string &rtrim(std::string &s)
|
135
|
+
{
|
136
|
+
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
|
137
|
+
return s;
|
138
|
+
}
|
139
|
+
|
140
|
+
inline std::string &trim(std::string &s)
|
141
|
+
{
|
142
|
+
return ltrim(rtrim(s));
|
143
|
+
}
|
144
|
+
|
145
|
+
inline std::string & ltrim(std::string & s, char x)
|
146
|
+
{
|
147
|
+
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::bind2nd(std::equal_to<char>(), x))));
|
148
|
+
return s;
|
149
|
+
}
|
150
|
+
|
151
|
+
inline std::string & rtrim(std::string & s, char x)
|
152
|
+
{
|
153
|
+
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::bind2nd(std::equal_to<char>(), x))).base(), s.end());
|
154
|
+
return s;
|
155
|
+
}
|
156
|
+
|
157
|
+
inline std::string &trim(std::string &s, char x)
|
158
|
+
{
|
159
|
+
return ltrim(rtrim(s, x), x);
|
160
|
+
}
|
161
|
+
|
162
|
+
inline bool startsWith(const string& str, const string& prefix)
|
163
|
+
{
|
164
|
+
if(prefix.length() > str.length())
|
165
|
+
{
|
166
|
+
return false;
|
167
|
+
}
|
168
|
+
return 0 == str.compare(0, prefix.length(), prefix);
|
169
|
+
}
|
170
|
+
|
171
|
+
inline bool endsWith(const string& str, const string& suffix)
|
172
|
+
{
|
173
|
+
if(suffix.length() > str.length())
|
174
|
+
{
|
175
|
+
return false;
|
176
|
+
}
|
177
|
+
return 0 == str.compare(str.length() - suffix.length(), suffix.length(), suffix);
|
178
|
+
}
|
179
|
+
|
180
|
+
inline bool isInStr(const string& str, char ch)
|
181
|
+
{
|
182
|
+
return str.find(ch) != string::npos;
|
183
|
+
}
|
184
|
+
|
185
|
+
inline uint16_t twocharToUint16(char high, char low)
|
186
|
+
{
|
187
|
+
return (((uint16_t(high) & 0x00ff ) << 8) | (uint16_t(low) & 0x00ff));
|
188
|
+
}
|
189
|
+
|
190
|
+
template <class Uint16Container>
|
191
|
+
bool utf8ToUnicode(const char * const str, size_t len, Uint16Container& vec)
|
192
|
+
{
|
193
|
+
if(!str)
|
194
|
+
{
|
195
|
+
return false;
|
196
|
+
}
|
197
|
+
char ch1, ch2;
|
198
|
+
uint16_t tmp;
|
199
|
+
vec.clear();
|
200
|
+
for(size_t i = 0;i < len;)
|
201
|
+
{
|
202
|
+
if(!(str[i] & 0x80)) // 0xxxxxxx
|
203
|
+
{
|
204
|
+
vec.push_back(str[i]);
|
205
|
+
i++;
|
206
|
+
}
|
207
|
+
else if ((uint8_t)str[i] <= 0xdf && i + 1 < len) // 110xxxxxx
|
208
|
+
{
|
209
|
+
ch1 = (str[i] >> 2) & 0x07;
|
210
|
+
ch2 = (str[i+1] & 0x3f) | ((str[i] & 0x03) << 6 );
|
211
|
+
tmp = (((uint16_t(ch1) & 0x00ff ) << 8) | (uint16_t(ch2) & 0x00ff));
|
212
|
+
vec.push_back(tmp);
|
213
|
+
i += 2;
|
214
|
+
}
|
215
|
+
else if((uint8_t)str[i] <= 0xef && i + 2 < len)
|
216
|
+
{
|
217
|
+
ch1 = ((uint8_t)str[i] << 4) | ((str[i+1] >> 2) & 0x0f );
|
218
|
+
ch2 = (((uint8_t)str[i+1]<<6) & 0xc0) | (str[i+2] & 0x3f);
|
219
|
+
tmp = (((uint16_t(ch1) & 0x00ff ) << 8) | (uint16_t(ch2) & 0x00ff));
|
220
|
+
vec.push_back(tmp);
|
221
|
+
i += 3;
|
222
|
+
}
|
223
|
+
else
|
224
|
+
{
|
225
|
+
return false;
|
226
|
+
}
|
227
|
+
}
|
228
|
+
return true;
|
229
|
+
}
|
230
|
+
template <class Uint16Container>
|
231
|
+
bool utf8ToUnicode(const string& str, Uint16Container& vec)
|
232
|
+
{
|
233
|
+
return utf8ToUnicode(str.c_str(), str.size(), vec);
|
234
|
+
}
|
235
|
+
|
236
|
+
template <class Uint16ContainerConIter>
|
237
|
+
bool unicodeToUtf8(Uint16ContainerConIter begin, Uint16ContainerConIter end, string& res)
|
238
|
+
{
|
239
|
+
if(begin >= end)
|
240
|
+
{
|
241
|
+
return false;
|
242
|
+
}
|
243
|
+
res.clear();
|
244
|
+
uint16_t ui;
|
245
|
+
while(begin != end)
|
246
|
+
{
|
247
|
+
ui = *begin;
|
248
|
+
if(ui <= 0x7f)
|
249
|
+
{
|
250
|
+
res += char(ui);
|
251
|
+
}
|
252
|
+
else if(ui <= 0x7ff)
|
253
|
+
{
|
254
|
+
res += char(((ui>>6) & 0x1f) | 0xc0);
|
255
|
+
res += char((ui & 0x3f) | 0x80);
|
256
|
+
}
|
257
|
+
else
|
258
|
+
{
|
259
|
+
res += char(((ui >> 12) & 0x0f )| 0xe0);
|
260
|
+
res += char(((ui>>6) & 0x3f )| 0x80 );
|
261
|
+
res += char((ui & 0x3f) | 0x80);
|
262
|
+
}
|
263
|
+
begin ++;
|
264
|
+
}
|
265
|
+
return true;
|
266
|
+
}
|
267
|
+
|
268
|
+
|
269
|
+
template <class Uint16Container>
|
270
|
+
bool gbkTrans(const char* const str, size_t len, Uint16Container& vec)
|
271
|
+
{
|
272
|
+
vec.clear();
|
273
|
+
if(!str)
|
274
|
+
{
|
275
|
+
return false;
|
276
|
+
}
|
277
|
+
size_t i = 0;
|
278
|
+
while(i < len)
|
279
|
+
{
|
280
|
+
if(0 == (str[i] & 0x80))
|
281
|
+
{
|
282
|
+
vec.push_back(uint16_t(str[i]));
|
283
|
+
i++;
|
284
|
+
}
|
285
|
+
else
|
286
|
+
{
|
287
|
+
if(i + 1 < len) //&& (str[i+1] & 0x80))
|
288
|
+
{
|
289
|
+
uint16_t tmp = (((uint16_t(str[i]) & 0x00ff ) << 8) | (uint16_t(str[i+1]) & 0x00ff));
|
290
|
+
vec.push_back(tmp);
|
291
|
+
i += 2;
|
292
|
+
}
|
293
|
+
else
|
294
|
+
{
|
295
|
+
return false;
|
296
|
+
}
|
297
|
+
}
|
298
|
+
}
|
299
|
+
return true;
|
300
|
+
}
|
301
|
+
|
302
|
+
template <class Uint16Container>
|
303
|
+
bool gbkTrans(const string& str, Uint16Container& vec)
|
304
|
+
{
|
305
|
+
return gbkTrans(str.c_str(), str.size(), vec);
|
306
|
+
}
|
307
|
+
|
308
|
+
template <class Uint16ContainerConIter>
|
309
|
+
bool gbkTrans(Uint16ContainerConIter begin, Uint16ContainerConIter end, string& res)
|
310
|
+
{
|
311
|
+
if(begin >= end)
|
312
|
+
{
|
313
|
+
return false;
|
314
|
+
}
|
315
|
+
res.clear();
|
316
|
+
//pair<char, char> pa;
|
317
|
+
char first, second;
|
318
|
+
while(begin != end)
|
319
|
+
{
|
320
|
+
//pa = uint16ToChar2(*begin);
|
321
|
+
first = ((*begin)>>8) & 0x00ff;
|
322
|
+
second = (*begin) & 0x00ff;
|
323
|
+
if(first & 0x80)
|
324
|
+
{
|
325
|
+
res += first;
|
326
|
+
res += second;
|
327
|
+
}
|
328
|
+
else
|
329
|
+
{
|
330
|
+
res += second;
|
331
|
+
}
|
332
|
+
begin++;
|
333
|
+
}
|
334
|
+
return true;
|
335
|
+
}
|
336
|
+
|
337
|
+
/*
|
338
|
+
* format example: "%Y-%m-%d %H:%M:%S"
|
339
|
+
*/
|
340
|
+
inline void getTime(const string& format, string& timeStr)
|
341
|
+
{
|
342
|
+
time_t timeNow;
|
343
|
+
time(&timeNow);
|
344
|
+
timeStr.resize(64);
|
345
|
+
size_t len = strftime((char*)timeStr.c_str(), timeStr.size(), format.c_str(), localtime(&timeNow));
|
346
|
+
timeStr.resize(len);
|
347
|
+
}
|
348
|
+
}
|
349
|
+
#endif
|
@@ -0,0 +1,50 @@
|
|
1
|
+
#ifndef LIMONP_THREAD_HPP
|
2
|
+
#define LIMONP_THREAD_HPP
|
3
|
+
|
4
|
+
#include "HandyMacro.hpp"
|
5
|
+
#include "NonCopyable.hpp"
|
6
|
+
|
7
|
+
namespace Limonp
|
8
|
+
{
|
9
|
+
class IThread: NonCopyable
|
10
|
+
{
|
11
|
+
private:
|
12
|
+
pthread_t thread_;
|
13
|
+
bool isStarted;
|
14
|
+
bool isJoined;
|
15
|
+
public:
|
16
|
+
IThread(): isStarted(false), isJoined(false)
|
17
|
+
{
|
18
|
+
}
|
19
|
+
virtual ~IThread()
|
20
|
+
{
|
21
|
+
if(isStarted && !isJoined)
|
22
|
+
{
|
23
|
+
LIMONP_CHECK(!pthread_detach(thread_));
|
24
|
+
}
|
25
|
+
};
|
26
|
+
public:
|
27
|
+
virtual void run() = 0;
|
28
|
+
void start()
|
29
|
+
{
|
30
|
+
assert(!isStarted);
|
31
|
+
LIMONP_CHECK(!pthread_create(&thread_, NULL, worker_, this));
|
32
|
+
isStarted = true;
|
33
|
+
}
|
34
|
+
void join()
|
35
|
+
{
|
36
|
+
assert(!isJoined);
|
37
|
+
LIMONP_CHECK(!pthread_join(thread_, NULL));
|
38
|
+
isJoined = true;
|
39
|
+
}
|
40
|
+
private:
|
41
|
+
static void * worker_(void * data)
|
42
|
+
{
|
43
|
+
IThread * ptr = (IThread* ) data;
|
44
|
+
ptr->run();
|
45
|
+
return NULL;
|
46
|
+
}
|
47
|
+
};
|
48
|
+
}
|
49
|
+
|
50
|
+
#endif
|
@@ -0,0 +1,105 @@
|
|
1
|
+
#ifndef LIMONP_THREAD_POOL_HPP
|
2
|
+
#define LIMONP_THREAD_POOL_HPP
|
3
|
+
|
4
|
+
#include "Thread.hpp"
|
5
|
+
#include "BlockingQueue.hpp"
|
6
|
+
|
7
|
+
namespace Limonp
|
8
|
+
{
|
9
|
+
class ITask
|
10
|
+
{
|
11
|
+
public:
|
12
|
+
virtual void run() = 0;
|
13
|
+
virtual ~ITask() {}
|
14
|
+
};
|
15
|
+
|
16
|
+
template <class TaskType, class ArgType>
|
17
|
+
ITask* CreateTask(ArgType arg)
|
18
|
+
{
|
19
|
+
return new TaskType(arg);
|
20
|
+
}
|
21
|
+
template <class TaskType, class ArgType0, class ArgType1>
|
22
|
+
ITask* CreateTask(ArgType0 arg0, ArgType1 arg1)
|
23
|
+
{
|
24
|
+
return new TaskType(arg0, arg1);
|
25
|
+
}
|
26
|
+
|
27
|
+
//class ThreadPool;
|
28
|
+
class ThreadPool: NonCopyable
|
29
|
+
{
|
30
|
+
private:
|
31
|
+
class Worker: public IThread
|
32
|
+
{
|
33
|
+
private:
|
34
|
+
ThreadPool * ptThreadPool_;
|
35
|
+
public:
|
36
|
+
Worker(ThreadPool* pool): ptThreadPool_(pool)
|
37
|
+
{
|
38
|
+
assert(ptThreadPool_);
|
39
|
+
}
|
40
|
+
virtual ~Worker()
|
41
|
+
{
|
42
|
+
}
|
43
|
+
public:
|
44
|
+
virtual void run()
|
45
|
+
{
|
46
|
+
while(true)
|
47
|
+
{
|
48
|
+
ITask * task = ptThreadPool_->queue_.pop();
|
49
|
+
if(task == NULL)
|
50
|
+
{
|
51
|
+
break;
|
52
|
+
}
|
53
|
+
task->run();
|
54
|
+
delete task;
|
55
|
+
}
|
56
|
+
}
|
57
|
+
};
|
58
|
+
private:
|
59
|
+
friend class Worker;
|
60
|
+
private:
|
61
|
+
vector<IThread*> threads_;
|
62
|
+
BoundedBlockingQueue<ITask*> queue_;
|
63
|
+
//mutable MutexLock mutex_;
|
64
|
+
//Condition isEmpty__;
|
65
|
+
public:
|
66
|
+
ThreadPool(size_t threadNum, size_t queueMaxSize): threads_(threadNum), queue_(queueMaxSize)//, mutex_(), isEmpty__(mutex_)
|
67
|
+
{
|
68
|
+
assert(threadNum);
|
69
|
+
assert(queueMaxSize);
|
70
|
+
for(size_t i = 0; i < threads_.size(); i ++)
|
71
|
+
{
|
72
|
+
threads_[i] = new Worker(this);
|
73
|
+
}
|
74
|
+
}
|
75
|
+
~ThreadPool()
|
76
|
+
{
|
77
|
+
for(size_t i = 0; i < threads_.size(); i ++)
|
78
|
+
{
|
79
|
+
queue_.push(NULL);
|
80
|
+
}
|
81
|
+
for(size_t i = 0; i < threads_.size(); i ++)
|
82
|
+
{
|
83
|
+
threads_[i]->join();
|
84
|
+
delete threads_[i];
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
public:
|
89
|
+
void start()
|
90
|
+
{
|
91
|
+
for(size_t i = 0; i < threads_.size(); i++)
|
92
|
+
{
|
93
|
+
threads_[i]->start();
|
94
|
+
}
|
95
|
+
}
|
96
|
+
|
97
|
+
void add(ITask* task)
|
98
|
+
{
|
99
|
+
assert(task);
|
100
|
+
queue_.push(task);
|
101
|
+
}
|
102
|
+
};
|
103
|
+
}
|
104
|
+
|
105
|
+
#endif
|