diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-01-16 00:42:45 +0100 |
---|---|---|
committer | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2015-01-16 00:42:45 +0100 |
commit | dbf59a218edd684f93f9cf74cdddd6bebefe15e7 (patch) | |
tree | 36bee9342243d1cf12301c42e957b1d800caf13e | |
parent | a1245cf2ad80cba0a71ffb184966ee77a7e166cc (diff) |
Added Utils::split function for splitting strings at a delimiter
-rw-r--r-- | src/core/common/Utils.cpp | 19 | ||||
-rw-r--r-- | src/core/common/Utils.hpp | 11 |
2 files changed, 30 insertions, 0 deletions
diff --git a/src/core/common/Utils.cpp b/src/core/common/Utils.cpp index 5eaafe9..5fde29c 100644 --- a/src/core/common/Utils.cpp +++ b/src/core/common/Utils.cpp @@ -55,5 +55,24 @@ bool Utils::isIdentifier(const std::string &name) } return true; } + +std::vector<std::string> Utils::split(const std::string &s, char delim) +{ + std::vector<std::string> res; + const size_t totalLen = s.size(); + size_t start = 0; + size_t len = 0; + for (size_t i = 0; i <= totalLen; i++) { + if (i == totalLen || s[i] == delim) { + res.push_back(s.substr(start, len)); + start = i + 1; + len = 0; + } else { + len++; + } + } + return res; +} + } diff --git a/src/core/common/Utils.hpp b/src/core/common/Utils.hpp index 5332b50..1f7f142 100644 --- a/src/core/common/Utils.hpp +++ b/src/core/common/Utils.hpp @@ -21,6 +21,7 @@ #include <sstream> #include <string> +#include <vector> namespace ousia { @@ -103,6 +104,16 @@ public: res << end; return res.str(); } + + /** + * Splits the given string at the delimiter and returns an array of + * substrings without the delimiter. + * + * @param s is the string that should be splitted. + * @param delim is the delimiter at which the string should be splitted. + * @return a vector of strings containing the splitted sub-strings. + */ + static std::vector<std::string> split(const std::string &s, char delim); }; } |