react-native-jieba 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Jieba.podspec +30 -0
- package/LICENSE +20 -0
- package/README.md +103 -0
- package/android/CMakeLists.txt +29 -0
- package/android/build.gradle +60 -0
- package/android/generated/java/com/jieba/NativeJiebaAndroidHelperSpec.java +38 -0
- package/android/generated/java/com/jieba/NativeJiebaSpec.java +74 -0
- package/android/generated/jni/CMakeLists.txt +28 -0
- package/android/generated/jni/JiebaSpec-generated.cpp +98 -0
- package/android/generated/jni/JiebaSpec.h +39 -0
- package/android/generated/jni/react/renderer/components/JiebaSpec/JiebaSpecJSI.h +144 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/com/jieba/JiebaAndroidHelperModule.kt +48 -0
- package/android/src/main/java/com/jieba/JiebaPackage.kt +32 -0
- package/cpp/JiebaImpl.cpp +154 -0
- package/cpp/JiebaImpl.h +37 -0
- package/cpp/cppjieba/LICENSE +20 -0
- package/cpp/cppjieba/deps/limonp/LICENSE +20 -0
- package/cpp/cppjieba/deps/limonp/include/limonp/ArgvContext.hpp +70 -0
- package/cpp/cppjieba/deps/limonp/include/limonp/Closure.hpp +206 -0
- package/cpp/cppjieba/deps/limonp/include/limonp/Colors.hpp +31 -0
- package/cpp/cppjieba/deps/limonp/include/limonp/Condition.hpp +38 -0
- package/cpp/cppjieba/deps/limonp/include/limonp/Config.hpp +103 -0
- package/cpp/cppjieba/deps/limonp/include/limonp/ForcePublic.hpp +7 -0
- package/cpp/cppjieba/deps/limonp/include/limonp/LocalVector.hpp +139 -0
- package/cpp/cppjieba/deps/limonp/include/limonp/Logging.hpp +92 -0
- package/cpp/cppjieba/deps/limonp/include/limonp/NonCopyable.hpp +21 -0
- package/cpp/cppjieba/deps/limonp/include/limonp/StdExtension.hpp +157 -0
- package/cpp/cppjieba/deps/limonp/include/limonp/StringUtil.hpp +386 -0
- package/cpp/cppjieba/dict/hmm_model.utf8 +34 -0
- package/cpp/cppjieba/dict/idf.utf8 +258826 -0
- package/cpp/cppjieba/dict/jieba.dict.utf8 +348982 -0
- package/cpp/cppjieba/dict/stop_words.utf8 +1534 -0
- package/cpp/cppjieba/dict/user.dict.utf8 +3 -0
- package/cpp/cppjieba/include/cppjieba/DictTrie.hpp +322 -0
- package/cpp/cppjieba/include/cppjieba/FullSegment.hpp +94 -0
- package/cpp/cppjieba/include/cppjieba/HMMModel.hpp +131 -0
- package/cpp/cppjieba/include/cppjieba/HMMSegment.hpp +211 -0
- package/cpp/cppjieba/include/cppjieba/Jieba.hpp +169 -0
- package/cpp/cppjieba/include/cppjieba/KeywordExtractor.hpp +152 -0
- package/cpp/cppjieba/include/cppjieba/MPSegment.hpp +137 -0
- package/cpp/cppjieba/include/cppjieba/MixSegment.hpp +109 -0
- package/cpp/cppjieba/include/cppjieba/PosTagger.hpp +77 -0
- package/cpp/cppjieba/include/cppjieba/PreFilter.hpp +54 -0
- package/cpp/cppjieba/include/cppjieba/QuerySegment.hpp +89 -0
- package/cpp/cppjieba/include/cppjieba/SegmentBase.hpp +46 -0
- package/cpp/cppjieba/include/cppjieba/SegmentTagged.hpp +23 -0
- package/cpp/cppjieba/include/cppjieba/TextRankExtractor.hpp +192 -0
- package/cpp/cppjieba/include/cppjieba/Trie.hpp +200 -0
- package/cpp/cppjieba/include/cppjieba/Unicode.hpp +231 -0
- package/cpp/cppjieba/include/cppjieba/UnicodeFile.hpp +68 -0
- package/ios/OnLoad.mm +35 -0
- package/ios/generated/ReactCodegen/JiebaSpec/JiebaSpec-generated.mm +126 -0
- package/ios/generated/ReactCodegen/JiebaSpec/JiebaSpec.h +103 -0
- package/ios/generated/ReactCodegen/JiebaSpecJSI.h +144 -0
- package/lib/module/NativeJieba.js +5 -0
- package/lib/module/NativeJieba.js.map +1 -0
- package/lib/module/NativeJiebaAndroidHelper.js +5 -0
- package/lib/module/NativeJiebaAndroidHelper.js.map +1 -0
- package/lib/module/index.js +5 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/init.js +24 -0
- package/lib/module/init.js.map +1 -0
- package/lib/module/jieba.js +31 -0
- package/lib/module/jieba.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/NativeJieba.d.ts +22 -0
- package/lib/typescript/src/NativeJieba.d.ts.map +1 -0
- package/lib/typescript/src/NativeJiebaAndroidHelper.d.ts +7 -0
- package/lib/typescript/src/NativeJiebaAndroidHelper.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +4 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/init.d.ts +2 -0
- package/lib/typescript/src/init.d.ts.map +1 -0
- package/lib/typescript/src/jieba.d.ts +18 -0
- package/lib/typescript/src/jieba.d.ts.map +1 -0
- package/package.json +197 -0
- package/react-native.config.js +17 -0
- package/src/NativeJieba.ts +19 -0
- package/src/NativeJiebaAndroidHelper.ts +7 -0
- package/src/index.tsx +13 -0
- package/src/init.ts +25 -0
- package/src/jieba.ts +43 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
package com.jieba
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.bridge.Promise
|
|
4
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
5
|
+
import com.facebook.react.module.annotations.ReactModule
|
|
6
|
+
import java.io.File
|
|
7
|
+
import java.io.FileOutputStream
|
|
8
|
+
|
|
9
|
+
@ReactModule(name = JiebaAndroidHelperModule.NAME)
|
|
10
|
+
class JiebaAndroidHelperModule(reactContext: ReactApplicationContext) :
|
|
11
|
+
NativeJiebaAndroidHelperSpec(reactContext) {
|
|
12
|
+
|
|
13
|
+
override fun getName(): String = NAME
|
|
14
|
+
|
|
15
|
+
override fun prepareDictDir(promise: Promise) {
|
|
16
|
+
val context = reactApplicationContext
|
|
17
|
+
Thread {
|
|
18
|
+
try {
|
|
19
|
+
val dictDir = File(context.filesDir, DICT_DIR_NAME)
|
|
20
|
+
if (!dictDir.exists()) dictDir.mkdirs()
|
|
21
|
+
for (filename in DICT_FILES) {
|
|
22
|
+
val target = File(dictDir, filename)
|
|
23
|
+
if (target.exists() && target.length() > 0) continue
|
|
24
|
+
context.assets.open(filename).use { input ->
|
|
25
|
+
FileOutputStream(target).use { output ->
|
|
26
|
+
input.copyTo(output)
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
promise.resolve(dictDir.absolutePath)
|
|
31
|
+
} catch (t: Throwable) {
|
|
32
|
+
promise.reject("E_JIEBA_DICT_EXTRACT", t.message, t)
|
|
33
|
+
}
|
|
34
|
+
}.start()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
companion object {
|
|
38
|
+
const val NAME = "JiebaAndroidHelper"
|
|
39
|
+
private const val DICT_DIR_NAME = "jieba-dict"
|
|
40
|
+
private val DICT_FILES = arrayOf(
|
|
41
|
+
"jieba.dict.utf8",
|
|
42
|
+
"hmm_model.utf8",
|
|
43
|
+
"user.dict.utf8",
|
|
44
|
+
"idf.utf8",
|
|
45
|
+
"stop_words.utf8",
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
package com.jieba
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.BaseReactPackage
|
|
4
|
+
import com.facebook.react.bridge.NativeModule
|
|
5
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfo
|
|
7
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
8
|
+
|
|
9
|
+
class JiebaPackage : BaseReactPackage() {
|
|
10
|
+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
|
|
11
|
+
return if (name == JiebaAndroidHelperModule.NAME) {
|
|
12
|
+
JiebaAndroidHelperModule(reactContext)
|
|
13
|
+
} else {
|
|
14
|
+
null
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
|
|
19
|
+
return ReactModuleInfoProvider {
|
|
20
|
+
mapOf(
|
|
21
|
+
JiebaAndroidHelperModule.NAME to ReactModuleInfo(
|
|
22
|
+
JiebaAndroidHelperModule.NAME,
|
|
23
|
+
JiebaAndroidHelperModule::class.java.name,
|
|
24
|
+
false,
|
|
25
|
+
false,
|
|
26
|
+
false,
|
|
27
|
+
true,
|
|
28
|
+
)
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
#include "JiebaImpl.h"
|
|
2
|
+
|
|
3
|
+
#include <cppjieba/Jieba.hpp>
|
|
4
|
+
|
|
5
|
+
#include <memory>
|
|
6
|
+
#include <mutex>
|
|
7
|
+
#include <stdexcept>
|
|
8
|
+
#include <string>
|
|
9
|
+
#include <utility>
|
|
10
|
+
#include <vector>
|
|
11
|
+
|
|
12
|
+
namespace facebook::react {
|
|
13
|
+
|
|
14
|
+
namespace {
|
|
15
|
+
|
|
16
|
+
std::mutex& dictMutex() {
|
|
17
|
+
static std::mutex m;
|
|
18
|
+
return m;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
std::string& dictPathStorage() {
|
|
22
|
+
static std::string p;
|
|
23
|
+
return p;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
std::unique_ptr<cppjieba::Jieba>& jiebaStorage() {
|
|
27
|
+
static std::unique_ptr<cppjieba::Jieba> j;
|
|
28
|
+
return j;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
std::string joinPath(const std::string& dir, const std::string& filename) {
|
|
32
|
+
if (dir.empty()) return filename;
|
|
33
|
+
char last = dir[dir.size() - 1];
|
|
34
|
+
if (last == '/' || last == '\\') return dir + filename;
|
|
35
|
+
return dir + "/" + filename;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
jsi::Array stringVectorToArray(jsi::Runtime& rt, const std::vector<std::string>& words) {
|
|
39
|
+
jsi::Array arr(rt, words.size());
|
|
40
|
+
for (size_t i = 0; i < words.size(); ++i) {
|
|
41
|
+
arr.setValueAtIndex(rt, i, jsi::String::createFromUtf8(rt, words[i]));
|
|
42
|
+
}
|
|
43
|
+
return arr;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
JiebaImpl::JiebaImpl(
|
|
49
|
+
std::shared_ptr<CallInvoker> jsInvoker
|
|
50
|
+
)
|
|
51
|
+
: NativeJiebaCxxSpec(std::move(jsInvoker)) {}
|
|
52
|
+
|
|
53
|
+
void JiebaImpl::setDictPathFromNative(const std::string& dictPath) {
|
|
54
|
+
std::lock_guard<std::mutex> lock(dictMutex());
|
|
55
|
+
if (dictPathStorage() != dictPath) {
|
|
56
|
+
dictPathStorage() = dictPath;
|
|
57
|
+
jiebaStorage().reset();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
void JiebaImpl::setDictPath(jsi::Runtime& rt, jsi::String path) {
|
|
62
|
+
setDictPathFromNative(path.utf8(rt));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
cppjieba::Jieba& JiebaImpl::getJieba() {
|
|
66
|
+
std::lock_guard<std::mutex> lock(dictMutex());
|
|
67
|
+
if (!jiebaStorage()) {
|
|
68
|
+
const std::string& base = dictPathStorage();
|
|
69
|
+
if (base.empty()) {
|
|
70
|
+
throw std::runtime_error(
|
|
71
|
+
"react-native-jieba: dictionary path not set. "
|
|
72
|
+
"The native module failed to locate bundled dict files."
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
jiebaStorage() = std::make_unique<cppjieba::Jieba>(
|
|
76
|
+
joinPath(base, "jieba.dict.utf8"),
|
|
77
|
+
joinPath(base, "hmm_model.utf8"),
|
|
78
|
+
joinPath(base, "user.dict.utf8"),
|
|
79
|
+
joinPath(base, "idf.utf8"),
|
|
80
|
+
joinPath(base, "stop_words.utf8")
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
return *jiebaStorage();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
jsi::Array JiebaImpl::cut(jsi::Runtime& rt, jsi::String sentence, bool hmm) {
|
|
87
|
+
std::vector<std::string> words;
|
|
88
|
+
getJieba().Cut(sentence.utf8(rt), words, hmm);
|
|
89
|
+
return stringVectorToArray(rt, words);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
jsi::Array JiebaImpl::cutAll(jsi::Runtime& rt, jsi::String sentence) {
|
|
93
|
+
std::vector<std::string> words;
|
|
94
|
+
getJieba().CutAll(sentence.utf8(rt), words);
|
|
95
|
+
return stringVectorToArray(rt, words);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
jsi::Array JiebaImpl::cutForSearch(jsi::Runtime& rt, jsi::String sentence, bool hmm) {
|
|
99
|
+
std::vector<std::string> words;
|
|
100
|
+
getJieba().CutForSearch(sentence.utf8(rt), words, hmm);
|
|
101
|
+
return stringVectorToArray(rt, words);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
jsi::Array JiebaImpl::cutHMM(jsi::Runtime& rt, jsi::String sentence) {
|
|
105
|
+
std::vector<std::string> words;
|
|
106
|
+
getJieba().CutHMM(sentence.utf8(rt), words);
|
|
107
|
+
return stringVectorToArray(rt, words);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
jsi::Array JiebaImpl::cutSmall(jsi::Runtime& rt, jsi::String sentence, double maxWordLen) {
|
|
111
|
+
std::vector<std::string> words;
|
|
112
|
+
size_t maxLen = maxWordLen <= 0 ? 0 : static_cast<size_t>(maxWordLen);
|
|
113
|
+
getJieba().CutSmall(sentence.utf8(rt), words, maxLen);
|
|
114
|
+
return stringVectorToArray(rt, words);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
jsi::Array JiebaImpl::tag(jsi::Runtime& rt, jsi::String sentence) {
|
|
118
|
+
std::vector<std::pair<std::string, std::string>> tagged;
|
|
119
|
+
getJieba().Tag(sentence.utf8(rt), tagged);
|
|
120
|
+
jsi::Array arr(rt, tagged.size());
|
|
121
|
+
for (size_t i = 0; i < tagged.size(); ++i) {
|
|
122
|
+
jsi::Object obj(rt);
|
|
123
|
+
obj.setProperty(rt, "word", jsi::String::createFromUtf8(rt, tagged[i].first));
|
|
124
|
+
obj.setProperty(rt, "tag", jsi::String::createFromUtf8(rt, tagged[i].second));
|
|
125
|
+
arr.setValueAtIndex(rt, i, std::move(obj));
|
|
126
|
+
}
|
|
127
|
+
return arr;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
jsi::Array JiebaImpl::extract(jsi::Runtime& rt, jsi::String sentence, double topK) {
|
|
131
|
+
std::vector<cppjieba::KeywordExtractor::Word> keywords;
|
|
132
|
+
size_t n = topK <= 0 ? 5 : static_cast<size_t>(topK);
|
|
133
|
+
getJieba().extractor.Extract(sentence.utf8(rt), keywords, n);
|
|
134
|
+
jsi::Array arr(rt, keywords.size());
|
|
135
|
+
for (size_t i = 0; i < keywords.size(); ++i) {
|
|
136
|
+
jsi::Object obj(rt);
|
|
137
|
+
obj.setProperty(rt, "word", jsi::String::createFromUtf8(rt, keywords[i].word));
|
|
138
|
+
obj.setProperty(rt, "weight", jsi::Value(keywords[i].weight));
|
|
139
|
+
arr.setValueAtIndex(rt, i, std::move(obj));
|
|
140
|
+
}
|
|
141
|
+
return arr;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
bool JiebaImpl::insertUserWord(jsi::Runtime& rt, jsi::String word, jsi::String tag) {
|
|
145
|
+
std::string t = tag.utf8(rt);
|
|
146
|
+
if (t.empty()) t = cppjieba::UNKNOWN_TAG;
|
|
147
|
+
return getJieba().InsertUserWord(word.utf8(rt), t);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
bool JiebaImpl::find(jsi::Runtime& rt, jsi::String word) {
|
|
151
|
+
return getJieba().Find(word.utf8(rt));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
}
|
package/cpp/JiebaImpl.h
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include <JiebaSpecJSI.h>
|
|
4
|
+
|
|
5
|
+
#include <memory>
|
|
6
|
+
#include <mutex>
|
|
7
|
+
#include <string>
|
|
8
|
+
|
|
9
|
+
namespace cppjieba {
|
|
10
|
+
class Jieba;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
namespace facebook::react {
|
|
14
|
+
|
|
15
|
+
class JiebaImpl
|
|
16
|
+
: public NativeJiebaCxxSpec<JiebaImpl> {
|
|
17
|
+
public:
|
|
18
|
+
JiebaImpl(std::shared_ptr<CallInvoker> jsInvoker);
|
|
19
|
+
|
|
20
|
+
static void setDictPathFromNative(const std::string& dictPath);
|
|
21
|
+
|
|
22
|
+
void setDictPath(jsi::Runtime& rt, jsi::String path);
|
|
23
|
+
jsi::Array cut(jsi::Runtime& rt, jsi::String sentence, bool hmm);
|
|
24
|
+
jsi::Array cutAll(jsi::Runtime& rt, jsi::String sentence);
|
|
25
|
+
jsi::Array cutForSearch(jsi::Runtime& rt, jsi::String sentence, bool hmm);
|
|
26
|
+
jsi::Array cutHMM(jsi::Runtime& rt, jsi::String sentence);
|
|
27
|
+
jsi::Array cutSmall(jsi::Runtime& rt, jsi::String sentence, double maxWordLen);
|
|
28
|
+
jsi::Array tag(jsi::Runtime& rt, jsi::String sentence);
|
|
29
|
+
jsi::Array extract(jsi::Runtime& rt, jsi::String sentence, double topK);
|
|
30
|
+
bool insertUserWord(jsi::Runtime& rt, jsi::String word, jsi::String tag);
|
|
31
|
+
bool find(jsi::Runtime& rt, jsi::String word);
|
|
32
|
+
|
|
33
|
+
private:
|
|
34
|
+
cppjieba::Jieba& getJieba();
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/************************************
|
|
2
|
+
* file enc : ascii
|
|
3
|
+
* author : wuyanyi09@gmail.com
|
|
4
|
+
************************************/
|
|
5
|
+
|
|
6
|
+
#ifndef LIMONP_ARGV_FUNCTS_H
|
|
7
|
+
#define LIMONP_ARGV_FUNCTS_H
|
|
8
|
+
|
|
9
|
+
#include <set>
|
|
10
|
+
#include <sstream>
|
|
11
|
+
#include "StringUtil.hpp"
|
|
12
|
+
|
|
13
|
+
namespace limonp {
|
|
14
|
+
|
|
15
|
+
using namespace std;
|
|
16
|
+
|
|
17
|
+
class ArgvContext {
|
|
18
|
+
public :
|
|
19
|
+
ArgvContext(int argc, const char* const * argv) {
|
|
20
|
+
for(int i = 0; i < argc; i++) {
|
|
21
|
+
if(StartsWith(argv[i], "-")) {
|
|
22
|
+
if(i + 1 < argc && !StartsWith(argv[i + 1], "-")) {
|
|
23
|
+
mpss_[argv[i]] = argv[i+1];
|
|
24
|
+
i++;
|
|
25
|
+
} else {
|
|
26
|
+
sset_.insert(argv[i]);
|
|
27
|
+
}
|
|
28
|
+
} else {
|
|
29
|
+
args_.push_back(argv[i]);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
~ArgvContext() {
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
friend ostream& operator << (ostream& os, const ArgvContext& args);
|
|
37
|
+
string operator [](size_t i) const {
|
|
38
|
+
if(i < args_.size()) {
|
|
39
|
+
return args_[i];
|
|
40
|
+
}
|
|
41
|
+
return "";
|
|
42
|
+
}
|
|
43
|
+
string operator [](const string& key) const {
|
|
44
|
+
map<string, string>::const_iterator it = mpss_.find(key);
|
|
45
|
+
if(it != mpss_.end()) {
|
|
46
|
+
return it->second;
|
|
47
|
+
}
|
|
48
|
+
return "";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
bool HasKey(const string& key) const {
|
|
52
|
+
if(mpss_.find(key) != mpss_.end() || sset_.find(key) != sset_.end()) {
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private:
|
|
59
|
+
vector<string> args_;
|
|
60
|
+
map<string, string> mpss_;
|
|
61
|
+
set<string> sset_;
|
|
62
|
+
}; // class ArgvContext
|
|
63
|
+
|
|
64
|
+
inline ostream& operator << (ostream& os, const ArgvContext& args) {
|
|
65
|
+
return os<<args.args_<<args.mpss_<<args.sset_;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
} // namespace limonp
|
|
69
|
+
|
|
70
|
+
#endif
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
#ifndef LIMONP_CLOSURE_HPP
|
|
2
|
+
#define LIMONP_CLOSURE_HPP
|
|
3
|
+
|
|
4
|
+
namespace limonp {
|
|
5
|
+
|
|
6
|
+
class ClosureInterface {
|
|
7
|
+
public:
|
|
8
|
+
virtual ~ClosureInterface() {
|
|
9
|
+
}
|
|
10
|
+
virtual void Run() = 0;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
template <class Funct>
|
|
14
|
+
class Closure0: public ClosureInterface {
|
|
15
|
+
public:
|
|
16
|
+
Closure0(Funct fun) {
|
|
17
|
+
fun_ = fun;
|
|
18
|
+
}
|
|
19
|
+
virtual ~Closure0() {
|
|
20
|
+
}
|
|
21
|
+
virtual void Run() {
|
|
22
|
+
(*fun_)();
|
|
23
|
+
}
|
|
24
|
+
private:
|
|
25
|
+
Funct fun_;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
template <class Funct, class Arg1>
|
|
29
|
+
class Closure1: public ClosureInterface {
|
|
30
|
+
public:
|
|
31
|
+
Closure1(Funct fun, Arg1 arg1) {
|
|
32
|
+
fun_ = fun;
|
|
33
|
+
arg1_ = arg1;
|
|
34
|
+
}
|
|
35
|
+
virtual ~Closure1() {
|
|
36
|
+
}
|
|
37
|
+
virtual void Run() {
|
|
38
|
+
(*fun_)(arg1_);
|
|
39
|
+
}
|
|
40
|
+
private:
|
|
41
|
+
Funct fun_;
|
|
42
|
+
Arg1 arg1_;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
template <class Funct, class Arg1, class Arg2>
|
|
46
|
+
class Closure2: public ClosureInterface {
|
|
47
|
+
public:
|
|
48
|
+
Closure2(Funct fun, Arg1 arg1, Arg2 arg2) {
|
|
49
|
+
fun_ = fun;
|
|
50
|
+
arg1_ = arg1;
|
|
51
|
+
arg2_ = arg2;
|
|
52
|
+
}
|
|
53
|
+
virtual ~Closure2() {
|
|
54
|
+
}
|
|
55
|
+
virtual void Run() {
|
|
56
|
+
(*fun_)(arg1_, arg2_);
|
|
57
|
+
}
|
|
58
|
+
private:
|
|
59
|
+
Funct fun_;
|
|
60
|
+
Arg1 arg1_;
|
|
61
|
+
Arg2 arg2_;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
template <class Funct, class Arg1, class Arg2, class Arg3>
|
|
65
|
+
class Closure3: public ClosureInterface {
|
|
66
|
+
public:
|
|
67
|
+
Closure3(Funct fun, Arg1 arg1, Arg2 arg2, Arg3 arg3) {
|
|
68
|
+
fun_ = fun;
|
|
69
|
+
arg1_ = arg1;
|
|
70
|
+
arg2_ = arg2;
|
|
71
|
+
arg3_ = arg3;
|
|
72
|
+
}
|
|
73
|
+
virtual ~Closure3() {
|
|
74
|
+
}
|
|
75
|
+
virtual void Run() {
|
|
76
|
+
(*fun_)(arg1_, arg2_, arg3_);
|
|
77
|
+
}
|
|
78
|
+
private:
|
|
79
|
+
Funct fun_;
|
|
80
|
+
Arg1 arg1_;
|
|
81
|
+
Arg2 arg2_;
|
|
82
|
+
Arg3 arg3_;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
template <class Obj, class Funct>
|
|
86
|
+
class ObjClosure0: public ClosureInterface {
|
|
87
|
+
public:
|
|
88
|
+
ObjClosure0(Obj* p, Funct fun) {
|
|
89
|
+
p_ = p;
|
|
90
|
+
fun_ = fun;
|
|
91
|
+
}
|
|
92
|
+
virtual ~ObjClosure0() {
|
|
93
|
+
}
|
|
94
|
+
virtual void Run() {
|
|
95
|
+
(p_->*fun_)();
|
|
96
|
+
}
|
|
97
|
+
private:
|
|
98
|
+
Obj* p_;
|
|
99
|
+
Funct fun_;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
template <class Obj, class Funct, class Arg1>
|
|
103
|
+
class ObjClosure1: public ClosureInterface {
|
|
104
|
+
public:
|
|
105
|
+
ObjClosure1(Obj* p, Funct fun, Arg1 arg1) {
|
|
106
|
+
p_ = p;
|
|
107
|
+
fun_ = fun;
|
|
108
|
+
arg1_ = arg1;
|
|
109
|
+
}
|
|
110
|
+
virtual ~ObjClosure1() {
|
|
111
|
+
}
|
|
112
|
+
virtual void Run() {
|
|
113
|
+
(p_->*fun_)(arg1_);
|
|
114
|
+
}
|
|
115
|
+
private:
|
|
116
|
+
Obj* p_;
|
|
117
|
+
Funct fun_;
|
|
118
|
+
Arg1 arg1_;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
template <class Obj, class Funct, class Arg1, class Arg2>
|
|
122
|
+
class ObjClosure2: public ClosureInterface {
|
|
123
|
+
public:
|
|
124
|
+
ObjClosure2(Obj* p, Funct fun, Arg1 arg1, Arg2 arg2) {
|
|
125
|
+
p_ = p;
|
|
126
|
+
fun_ = fun;
|
|
127
|
+
arg1_ = arg1;
|
|
128
|
+
arg2_ = arg2;
|
|
129
|
+
}
|
|
130
|
+
virtual ~ObjClosure2() {
|
|
131
|
+
}
|
|
132
|
+
virtual void Run() {
|
|
133
|
+
(p_->*fun_)(arg1_, arg2_);
|
|
134
|
+
}
|
|
135
|
+
private:
|
|
136
|
+
Obj* p_;
|
|
137
|
+
Funct fun_;
|
|
138
|
+
Arg1 arg1_;
|
|
139
|
+
Arg2 arg2_;
|
|
140
|
+
};
|
|
141
|
+
template <class Obj, class Funct, class Arg1, class Arg2, class Arg3>
|
|
142
|
+
class ObjClosure3: public ClosureInterface {
|
|
143
|
+
public:
|
|
144
|
+
ObjClosure3(Obj* p, Funct fun, Arg1 arg1, Arg2 arg2, Arg3 arg3) {
|
|
145
|
+
p_ = p;
|
|
146
|
+
fun_ = fun;
|
|
147
|
+
arg1_ = arg1;
|
|
148
|
+
arg2_ = arg2;
|
|
149
|
+
arg3_ = arg3;
|
|
150
|
+
}
|
|
151
|
+
virtual ~ObjClosure3() {
|
|
152
|
+
}
|
|
153
|
+
virtual void Run() {
|
|
154
|
+
(p_->*fun_)(arg1_, arg2_, arg3_);
|
|
155
|
+
}
|
|
156
|
+
private:
|
|
157
|
+
Obj* p_;
|
|
158
|
+
Funct fun_;
|
|
159
|
+
Arg1 arg1_;
|
|
160
|
+
Arg2 arg2_;
|
|
161
|
+
Arg3 arg3_;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
template<class R>
|
|
165
|
+
ClosureInterface* NewClosure(R (*fun)()) {
|
|
166
|
+
return new Closure0<R (*)()>(fun);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
template<class R, class Arg1>
|
|
170
|
+
ClosureInterface* NewClosure(R (*fun)(Arg1), Arg1 arg1) {
|
|
171
|
+
return new Closure1<R (*)(Arg1), Arg1>(fun, arg1);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
template<class R, class Arg1, class Arg2>
|
|
175
|
+
ClosureInterface* NewClosure(R (*fun)(Arg1, Arg2), Arg1 arg1, Arg2 arg2) {
|
|
176
|
+
return new Closure2<R (*)(Arg1, Arg2), Arg1, Arg2>(fun, arg1, arg2);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
template<class R, class Arg1, class Arg2, class Arg3>
|
|
180
|
+
ClosureInterface* NewClosure(R (*fun)(Arg1, Arg2, Arg3), Arg1 arg1, Arg2 arg2, Arg3 arg3) {
|
|
181
|
+
return new Closure3<R (*)(Arg1, Arg2, Arg3), Arg1, Arg2, Arg3>(fun, arg1, arg2, arg3);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
template<class R, class Obj>
|
|
185
|
+
ClosureInterface* NewClosure(Obj* obj, R (Obj::* fun)()) {
|
|
186
|
+
return new ObjClosure0<Obj, R (Obj::* )()>(obj, fun);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
template<class R, class Obj, class Arg1>
|
|
190
|
+
ClosureInterface* NewClosure(Obj* obj, R (Obj::* fun)(Arg1), Arg1 arg1) {
|
|
191
|
+
return new ObjClosure1<Obj, R (Obj::* )(Arg1), Arg1>(obj, fun, arg1);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
template<class R, class Obj, class Arg1, class Arg2>
|
|
195
|
+
ClosureInterface* NewClosure(Obj* obj, R (Obj::* fun)(Arg1, Arg2), Arg1 arg1, Arg2 arg2) {
|
|
196
|
+
return new ObjClosure2<Obj, R (Obj::*)(Arg1, Arg2), Arg1, Arg2>(obj, fun, arg1, arg2);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
template<class R, class Obj, class Arg1, class Arg2, class Arg3>
|
|
200
|
+
ClosureInterface* NewClosure(Obj* obj, R (Obj::* fun)(Arg1, Arg2, Arg3), Arg1 arg1, Arg2 arg2, Arg3 arg3) {
|
|
201
|
+
return new ObjClosure3<Obj, R (Obj::*)(Arg1, Arg2, Arg3), Arg1, Arg2, Arg3>(obj, fun, arg1, arg2, arg3);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
} // namespace limonp
|
|
205
|
+
|
|
206
|
+
#endif // LIMONP_CLOSURE_HPP
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
#ifndef LIMONP_COLOR_PRINT_HPP
|
|
2
|
+
#define LIMONP_COLOR_PRINT_HPP
|
|
3
|
+
|
|
4
|
+
#include <string>
|
|
5
|
+
#include <stdarg.h>
|
|
6
|
+
|
|
7
|
+
namespace limonp {
|
|
8
|
+
|
|
9
|
+
using std::string;
|
|
10
|
+
|
|
11
|
+
enum Color {
|
|
12
|
+
BLACK = 30,
|
|
13
|
+
RED,
|
|
14
|
+
GREEN,
|
|
15
|
+
YELLOW,
|
|
16
|
+
BLUE,
|
|
17
|
+
PURPLE
|
|
18
|
+
}; // enum Color
|
|
19
|
+
|
|
20
|
+
static void ColorPrintln(enum Color color, const char * fmt, ...) {
|
|
21
|
+
va_list ap;
|
|
22
|
+
printf("\033[0;%dm", color);
|
|
23
|
+
va_start(ap, fmt);
|
|
24
|
+
vprintf(fmt, ap);
|
|
25
|
+
va_end(ap);
|
|
26
|
+
printf("\033[0m\n"); // if not \n , in some situation , the next lines will be set the same color unexpectedly
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
} // namespace limonp
|
|
30
|
+
|
|
31
|
+
#endif // LIMONP_COLOR_PRINT_HPP
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
#ifndef LIMONP_CONDITION_HPP
|
|
2
|
+
#define LIMONP_CONDITION_HPP
|
|
3
|
+
|
|
4
|
+
#include "MutexLock.hpp"
|
|
5
|
+
|
|
6
|
+
namespace limonp {
|
|
7
|
+
|
|
8
|
+
class Condition : NonCopyable {
|
|
9
|
+
public:
|
|
10
|
+
explicit Condition(MutexLock& mutex)
|
|
11
|
+
: mutex_(mutex) {
|
|
12
|
+
XCHECK(!pthread_cond_init(&pcond_, NULL));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
~Condition() {
|
|
16
|
+
XCHECK(!pthread_cond_destroy(&pcond_));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
void Wait() {
|
|
20
|
+
XCHECK(!pthread_cond_wait(&pcond_, mutex_.GetPthreadMutex()));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
void Notify() {
|
|
24
|
+
XCHECK(!pthread_cond_signal(&pcond_));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
void NotifyAll() {
|
|
28
|
+
XCHECK(!pthread_cond_broadcast(&pcond_));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private:
|
|
32
|
+
MutexLock& mutex_;
|
|
33
|
+
pthread_cond_t pcond_;
|
|
34
|
+
}; // class Condition
|
|
35
|
+
|
|
36
|
+
} // namespace limonp
|
|
37
|
+
|
|
38
|
+
#endif // LIMONP_CONDITION_HPP
|