diff --git a/src/common/splitString.cpp b/src/common/splitString.cpp new file mode 100644 index 000000000..0cb9bfc92 --- /dev/null +++ b/src/common/splitString.cpp @@ -0,0 +1,31 @@ +#include "splitString.h" + +std::vector split(const std::string & str, + const std::string & delims=" *-") +{ + string::size_type lastPos = str.find_first_not_of(delims, 0); + string::size_type pos = str.find_first_of(delims, lastPos); + vector tokens; + + while (string::npos != pos || string::npos != lastPos) + { + tokens.push_back(str.substr(lastPos, pos - lastPos)); + lastPos = str.find_first_not_of(delims, pos); + pos = str.find_first_of(delims, lastPos); + } + + return tokens; +} + +std::vector split(const char* lhs, const char* rhs){ + const std::string m1 (lhs), m2 (rhs); + return split(m1, m2); +} + +std::vector split(const char* lhs, const std::string& rhs){ + return split(lhs, rhs.c_str()); +} + +std::vector split(const std::string& lhs, const char* rhs){ + return split(lhs.c_str(), rhs); +} diff --git a/src/common/splitString.h b/src/common/splitString.h new file mode 100644 index 000000000..df325319b --- /dev/null +++ b/src/common/splitString.h @@ -0,0 +1,10 @@ +#include +#include +#include + +using namespace std; + +std::vector split(const std::string&, const std::string&); +std::vector split(const char*, const char*); +std::vector split(const std::string&, const char*); +std::vector split(const char*, std::string&);