Browse Source

Function Split to break strings by delimiters.

ObjPtr
Michael Uleysky 9 years ago
parent
commit
8e9d4c3f0e
  1. 18
      include/common.h
  2. 42
      src/globals.cpp

18
include/common.h

@ -1,8 +1,8 @@
#ifndef COMMON_H #ifndef COMMON_H
#define COMMON_H #define COMMON_H
//#include <algorithm>
#include <cctype> #include <cctype>
#include <iostream> #include <iostream>
#include <list>
#include <memory> #include <memory>
#include <set> #include <set>
#include <string> #include <string>
@ -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(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));} inline void tolower(char* str) {for(*str=tolower(*str);'\0'!=*str++;*str=tolower(*str));}
typedef std::list<std::string> 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 #endif

42
src/globals.cpp

@ -159,3 +159,45 @@ bool Print(const ObjectList* input)
return true; 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;
}

Loading…
Cancel
Save