Skip to content

Commit

Permalink
feat: sync lib with upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Dec 13, 2023
1 parent b070d1b commit 3b0ca24
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 12 deletions.
9 changes: 9 additions & 0 deletions deps/cppjieba/DictTrie.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ class DictTrie {
return true;
}

bool DeleteUserWord(const string& word, const string& tag = UNKNOWN_TAG) {
DictUnit node_info;
if (!MakeNodeInfo(node_info, word, user_word_default_weight_, tag)) {
return false;
}
trie_->DeleteNode(node_info.word, &node_info);
return true;
}

const DictUnit* Find(RuneStrArray::const_iterator begin, RuneStrArray::const_iterator end) const {
return trie_->Find(begin, end);
}
Expand Down
4 changes: 4 additions & 0 deletions deps/cppjieba/Jieba.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ class Jieba {
return dict_trie_.InsertUserWord(word,freq, tag);
}

bool DeleteUserWord(const string& word, const string& tag = UNKNOWN_TAG) {
return dict_trie_.DeleteUserWord(word, tag);
}

bool Find(const string& word)
{
return dict_trie_.Find(word);
Expand Down
2 changes: 0 additions & 2 deletions deps/cppjieba/KeywordExtractor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,3 @@ inline ostream& operator << (ostream& os, const KeywordExtractor::Word& word) {
} // namespace cppjieba

#endif


28 changes: 27 additions & 1 deletion deps/cppjieba/Trie.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,33 @@ class Trie {
assert(ptNode != NULL);
ptNode->ptValue = ptValue;
}

void DeleteNode(const Unicode& key, const DictUnit* ptValue) {
if (key.begin() == key.end()) {
return;
}
//定义一个NextMap迭代器
TrieNode::NextMap::const_iterator kmIter;
//定义一个指向root的TrieNode指针
TrieNode *ptNode = root_;
for (Unicode::const_iterator citer = key.begin(); citer != key.end(); ++citer) {
//链表不存在元素
if (NULL == ptNode->next) {
return;
}
kmIter = ptNode->next->find(*citer);
//如果map中不存在,跳出循环
if (ptNode->next->end() == kmIter) {
break;
}
//从unordered_map中擦除该项
ptNode->next->erase(*citer);
//删除该node
ptNode = kmIter->second;
delete ptNode;
break;
}
return;
}
private:
void CreateTrie(const vector<Unicode>& keys, const vector<const DictUnit*>& valuePointers) {
if (valuePointers.empty() || keys.empty()) {
Expand Down
6 changes: 3 additions & 3 deletions deps/limonp/LocalVector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ class LocalVector {
size_ = vec.size();
capacity_ = vec.capacity();
if(vec.buffer_ == vec.ptr_) {
memcpy(buffer_, vec.buffer_, sizeof(T) * size_);
memcpy(static_cast<void*>(buffer_), vec.buffer_, sizeof(T) * size_);
ptr_ = buffer_;
} else {
ptr_ = (T*) malloc(vec.capacity() * sizeof(T));
assert(ptr_);
memcpy(ptr_, vec.ptr_, vec.size() * sizeof(T));
memcpy(static_cast<void*>(ptr_), vec.ptr_, vec.size() * sizeof(T));
}
return *this;
}
Expand Down Expand Up @@ -92,7 +92,7 @@ class LocalVector {
assert(next);
T * old = ptr_;
ptr_ = next;
memcpy(ptr_, old, sizeof(T) * capacity_);
memcpy(static_cast<void*>(ptr_), old, sizeof(T) * capacity_);
capacity_ = size;
if(old != buffer_) {
free(old);
Expand Down
20 changes: 17 additions & 3 deletions deps/limonp/Logging.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,24 @@ class Logger {
}
#endif
assert(level_ <= sizeof(LOG_LEVEL_ARRAY)/sizeof(*LOG_LEVEL_ARRAY));

char buf[32];
time_t now;
time(&now);
strftime(buf, sizeof(buf), LOG_TIME_FORMAT, localtime(&now));

time_t timeNow;
time(&timeNow);

struct tm tmNow;

#if defined(_WIN32) || defined(_WIN64)
errno_t e = localtime_s(&tmNow, &timeNow);
assert(e == 0);
#else
struct tm * tm_tmp = localtime_r(&timeNow, &tmNow);
assert(tm_tmp != nullptr);
#endif

strftime(buf, sizeof(buf), LOG_TIME_FORMAT, &tmNow);

stream_ << buf
<< " " << filename
<< ":" << lineno
Expand Down
19 changes: 16 additions & 3 deletions deps/limonp/StringUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include <algorithm>
#include <cctype>
#include <map>
#include <cassert>
#include <ctime>
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
Expand Down Expand Up @@ -95,8 +97,6 @@ inline std::string& LTrim(std::string &s) {
return s;
}



inline std::string& RTrim(std::string &s) {
#if defined(_MSC_VER) && _MSC_VER >= 1910
// Use MSVC 2017 or higher version
Expand Down Expand Up @@ -376,8 +376,21 @@ void GBKTrans(Uint16ContainerConIter begin, Uint16ContainerConIter end, string&
inline void GetTime(const string& format, string& timeStr) {
time_t timeNow;
time(&timeNow);

struct tm tmNow;

#if defined(_WIN32) || defined(_WIN64)
errno_t e = localtime_s(&tmNow, &timeNow);
assert(e = 0);
#else
struct tm * tm_tmp = localtime_r(&timeNow, &tmNow);
assert(tm_tmp != nullptr);
#endif

timeStr.resize(64);
size_t len = strftime((char*)timeStr.c_str(), timeStr.size(), format.c_str(), localtime(&timeNow));

size_t len = strftime((char*)timeStr.c_str(), timeStr.size(), format.c_str(), &tmNow);

timeStr.resize(len);
}

Expand Down

0 comments on commit 3b0ca24

Please sign in to comment.