From 8e9d4c3f0e133299273084fb35df71669c05a00f Mon Sep 17 00:00:00 2001 From: Michael Uleysky Date: Tue, 20 Oct 2015 15:23:45 +1000 Subject: [PATCH] Function Split to break strings by delimiters. --- include/common.h | 18 +++++++++++++++++- src/globals.cpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/include/common.h b/include/common.h index 0c3412d..0923735 100644 --- a/include/common.h +++ b/include/common.h @@ -1,8 +1,8 @@ #ifndef COMMON_H #define COMMON_H -//#include #include #include +#include #include #include #include @@ -397,4 +397,20 @@ inline bool str2uint(const std::string& str, int64_t* res) {return str2uint(str. inline void tolower(std::string& str) {for(auto& p: str) p=tolower(p);} inline void tolower(std::string* str) {for(auto& p:*str) p=tolower(p);} inline void tolower(char* str) {for(*str=tolower(*str);'\0'!=*str++;*str=tolower(*str));} + +typedef std::list WordList; + +EXPORT WordList Split(const std::string& str, const std::string& delims, bool allowempty); + +inline WordList Split(const std::string& str) {return Split(str,std::string(" \t",3),false);} +inline WordList Split(const char* str) {return Split(std::string(str),std::string(" \t",3),false);} + +inline WordList Split(const std::string& str, bool allowempty) {return Split(str,std::string(" \t",3),allowempty);} +inline WordList Split(const char* str, bool allowempty) {return Split(std::string(str),std::string(" \t",3),allowempty);} + +inline WordList Split(const std::string& str, const std::string& delims) {return Split(str,delims,false);} +inline WordList Split(const char* str, const std::string& delims) {return Split(std::string(str),delims,false);} +inline WordList Split(const std::string& str, const char* delims) {return Split(str,std::string(delims),false);} +inline WordList Split(const char* str, const char* delims) {return Split(std::string(str),std::string(delims),false);} + #endif diff --git a/src/globals.cpp b/src/globals.cpp index 1cd0002..947ca61 100644 --- a/src/globals.cpp +++ b/src/globals.cpp @@ -159,3 +159,45 @@ bool Print(const ObjectList* input) return true; } + +WordList Split(const std::string& str, const std::string& delims, bool allowempty) +{ + WordList wl; + size_t pos=0,bpos; + + if(0==str.size()) return wl; + if(0==delims.size()) + { + wl.push_back(str); + return wl; + } + + if(allowempty) + { + // Find first delimiter symbol + pos=str.find_first_of(delims); + wl.push_back(str.substr(0,pos)); // pos can be npos + while(std::string::npos!=pos) + { + bpos=pos+1; + pos=str.find_first_of(delims,bpos); // bpos can be greater then length + wl.push_back(str.substr(bpos,(std::string::npos==pos)?pos:(pos-bpos))); + } + } + else + { + while(true) + { + // Find first nondelimiter symbol + bpos=str.find_first_not_of(delims,pos); + if(std::string::npos==bpos) break; // no nondelimiters + // Find first delimiter symbol + pos=str.find_first_of(delims,bpos); + wl.push_back(str.substr(bpos,(std::string::npos==pos)?pos:(pos-bpos))); + if(std::string::npos!=pos) pos++; + else break; + } + } + + return wl; +}