replaced explode() function with a more efficient version

git-svn-id: http://svn.berlios.de/svnroot/repos/sems/trunk@1556 8eb893ce-cfd4-0310-b710-fb5ebe64c474
sayer/1.4-spce2.6
Stefan Sayer 17 years ago
parent 6856ffefe5
commit 7f30a69973

@ -972,23 +972,33 @@ unsigned int get_random()
return r;
}
// Explode string by a separator to a vector
vector <string> explode(string s, string e) {
vector <string> ret;
int iPos = s.find(e, 0);
int iLen = e.length();
while (iPos > -1) {
if (iPos != 0)
ret.push_back(s.substr(0, iPos));
s.erase(0, iPos+iLen);
iPos = s.find(e, 0);
// see http://stackoverflow.com/questions/236129/c-how-to-split-a-string
std::vector<string> explode(const string& s, const string& delim,
const bool keep_empty) {
vector<string> result;
if (delim.empty()) {
result.push_back(s);
return result;
}
if (s != "")
ret.push_back(s);
return ret;
string::const_iterator substart = s.begin(), subend;
while (true) {
subend = search(substart, s.end(), delim.begin(), delim.end());
string temp(substart, subend);
if (keep_empty || !temp.empty()) {
result.push_back(temp);
}
if (subend == s.end()) {
break;
}
substart = subend + delim.size();
}
return result;
}
// Warning: static var is not mutexed
// Call this func only in init code.
//

@ -286,7 +286,7 @@ void init_random();
unsigned int get_random();
/** Explode string by a separator to a vector */
std::vector <string> explode(string s, string e);
std::vector<string> explode(const string& s, const string& delim, const bool keep_empty = false);
/** add a directory to an environement variable */
void add_env_path(const char* name, const string& path);

Loading…
Cancel
Save