diff --git a/src/utilities.cpp b/src/utilities.cpp index 527acf4..7eab4a5 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -202,39 +202,20 @@ int evalIntConf (ConfIntHash& hash, const std::string &key, int def, int imin, i } } -bool split (vector &vec, const string &str, const string &delim, bool destructive) { +void split(vector& vec, string const& str, string const& delim) { vec.clear(); if (delim.empty()) { vec.push_back(str); - return false; + return; } - std::string::size_type i = 0; - std::string::size_type j = 0; - - while(1) { - j = str.find(delim,i); - if (j==std::string::npos) { - vec.push_back(str.substr(i)); - break; - } - - if (!destructive) - j += delim.size(); - - vec.push_back(str.substr(i,j-i)); - - if (destructive) - i = j + delim.size(); - - if (i==str.size()) { - vec.push_back(std::string()); - break; - } + string::size_type i = 0, j; + while ((j = str.find(delim, i)) != string::npos) { + vec.push_back(str.substr(i, j - i)); + i = j + delim.size(); } - - return true; + vec.push_back(str.substr(i)); } string strreplace (string orig, const string &search, const string &replace) { diff --git a/src/utilities.h b/src/utilities.h index 0fc76b2..206fa66 100644 --- a/src/utilities.h +++ b/src/utilities.h @@ -85,8 +85,16 @@ int constrain(int x, int imin, int imax); int evalIntConf(ConfIntHash& hash, const std::string &key, int def, int imin, int imax); -bool split(std::vector &vec, const std::string &str, - const std::string &delim, bool destructive=true); +/** + * Splits the given string on the given delimiter, returning the split elements + * in the given vector. + * A delimiter can be a string of multiple characters, in which case that + * entire delimiter string acts as a single delimiter. + * If the delimiter is empty, the entire string is returned as a single element. + * Any previous contents of the vector are discarded. + */ +void split(std::vector& vec, std::string const& str, + std::string const& delim); int intTransition(int from, int to, long int tickStart, long duration=500, long tickNow=-1);