1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-09-29 02:04:46 +03:00

Clean up split() utility function

Remove fourth argument, which was never used and had a confusing name.
Change return type to void, since the returned value was never used.
Simplify the actual splitting code.
Add documentation comment.
This commit is contained in:
Maarten ter Huurne 2014-10-04 07:46:07 +02:00
parent a03f8957aa
commit f0f3684826
2 changed files with 17 additions and 28 deletions

View File

@ -202,39 +202,20 @@ int evalIntConf (ConfIntHash& hash, const std::string &key, int def, int imin, i
} }
} }
bool split (vector<string> &vec, const string &str, const string &delim, bool destructive) { void split(vector<string>& vec, string const& str, string const& delim) {
vec.clear(); vec.clear();
if (delim.empty()) { if (delim.empty()) {
vec.push_back(str); vec.push_back(str);
return false; return;
} }
std::string::size_type i = 0; string::size_type i = 0, j;
std::string::size_type j = 0; while ((j = str.find(delim, i)) != string::npos) {
vec.push_back(str.substr(i, j - i));
while(1) { i = j + delim.size();
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;
}
} }
vec.push_back(str.substr(i));
return true;
} }
string strreplace (string orig, const string &search, const string &replace) { string strreplace (string orig, const string &search, const string &replace) {

View File

@ -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); int evalIntConf(ConfIntHash& hash, const std::string &key, int def, int imin, int imax);
bool split(std::vector<std::string> &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<std::string>& vec, std::string const& str,
std::string const& delim);
int intTransition(int from, int to, long int tickStart, long duration=500, int intTransition(int from, int to, long int tickStart, long duration=500,
long tickNow=-1); long tickNow=-1);