From d5bb4e48ece5acc679e90efc3da45c4f5ad3723b Mon Sep 17 00:00:00 2001 From: wyy Date: Wed, 29 Jan 2014 20:37:26 +0800 Subject: [PATCH] use InitOnOff --- src/Limonp/CMakeLists.txt | 4 +++- src/Limonp/InitOnOff.hpp | 21 +++++++++++++++++++++ src/Limonp/str_functs.hpp | 26 +++++++++++++++----------- src/SegmentBase.hpp | 11 +++-------- 4 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 src/Limonp/InitOnOff.hpp diff --git a/src/Limonp/CMakeLists.txt b/src/Limonp/CMakeLists.txt index 7ed15ae..51f62d7 100644 --- a/src/Limonp/CMakeLists.txt +++ b/src/Limonp/CMakeLists.txt @@ -1 +1,3 @@ -INSTALL(FILES ArgvContext.hpp io_functs.hpp macro_def.hpp MysqlClient.hpp str_functs.hpp cast_functs.hpp Config.hpp logger.hpp map_functs.hpp std_outbound.hpp DESTINATION include/CppJieba/Limonp) +INSTALL(FILES ArgvContext.hpp io_functs.hpp macro_def.hpp MysqlClient.hpp + str_functs.hpp cast_functs.hpp Config.hpp logger.hpp map_functs.hpp + std_outbound.hpp InitOnOff.hpp DESTINATION include/CppJieba/Limonp) diff --git a/src/Limonp/InitOnOff.hpp b/src/Limonp/InitOnOff.hpp new file mode 100644 index 0000000..926daab --- /dev/null +++ b/src/Limonp/InitOnOff.hpp @@ -0,0 +1,21 @@ +#ifndef LIMONP_INITONOFF_H +#define LIMONP_INITONOFF_H + +namespace Limonp +{ + class InitOnOff + { + public: + InitOnOff(){_setInitFlag(false);}; + ~InitOnOff(){}; + protected: + bool _isInited; + bool _getInitFlag()const{return _isInited;}; + bool _setInitFlag(bool flag){return _isInited = flag;}; + public: + operator bool(){return _getInitFlag();}; + + }; +} + +#endif diff --git a/src/Limonp/str_functs.hpp b/src/Limonp/str_functs.hpp index 7ebb6b4..e610232 100644 --- a/src/Limonp/str_functs.hpp +++ b/src/Limonp/str_functs.hpp @@ -100,7 +100,7 @@ namespace Limonp - inline bool split(const string& src, vector& res, const string& pattern) + inline bool split(const string& src, vector& res, const string& pattern, size_t offset = 0, size_t len = string::npos) { if(src.empty()) { @@ -110,20 +110,28 @@ namespace Limonp size_t start = 0; size_t end = 0; - while(start < src.size()) + size_t cnt = 0; + while(start < src.size() && res.size() < len) { end = src.find_first_of(pattern, start); if(string::npos == end) { - res.push_back(src.substr(start)); + if(cnt >= offset) + { + res.push_back(src.substr(start)); + } return true; } - res.push_back(src.substr(start, end - start)); - if(end == src.size() - 1) + //if(end == src.size() - 1) + //{ + // res.push_back(""); + // return true; + //} + if(cnt >= offset) { - res.push_back(""); - break; + res.push_back(src.substr(start, end - start)); } + cnt ++; start = end + 1; } return true; @@ -158,12 +166,8 @@ namespace Limonp return ltrim(rtrim(s)); } - - - inline bool startsWith(const string& str, const string& prefix) { - //return str.substr(0, prefix.size()) == prefix; if(prefix.length() > str.length()) { return false; diff --git a/src/SegmentBase.hpp b/src/SegmentBase.hpp index 12938cd..dabee0c 100644 --- a/src/SegmentBase.hpp +++ b/src/SegmentBase.hpp @@ -3,6 +3,7 @@ #include "TransCode.hpp" #include "Limonp/logger.hpp" +#include "Limonp/InitOnOff.hpp" #include "ISegment.hpp" #include @@ -10,17 +11,11 @@ namespace CppJieba { using namespace Limonp; - class SegmentBase: public ISegment + class SegmentBase: public ISegment, public InitOnOff { public: - SegmentBase(){_setInitFlag(false);}; + SegmentBase(){}; virtual ~SegmentBase(){}; - protected: - bool _isInited; - bool _getInitFlag()const{return _isInited;}; - bool _setInitFlag(bool flag){return _isInited = flag;}; - public: - operator bool(){return _getInitFlag();}; public: virtual bool cut(Unicode::const_iterator begin, Unicode::const_iterator end, vector& res)const = 0;