Make SplitString significantly faster.

This takes about 10% as long or less with simple strings, which really
improves UI layout performance when wrapping text.
This commit is contained in:
Unknown W. Brackets 2016-09-04 09:20:13 -07:00
parent 57e68be754
commit 740365235b

View file

@ -250,13 +250,20 @@ bool TryParse(const std::string &str, bool *const output)
void SplitString(const std::string& str, const char delim, std::vector<std::string>& output)
{
std::istringstream iss(str);
output.resize(1);
size_t next = 0;
for (size_t pos = 0, len = str.length(); pos < len; ++pos) {
if (str[pos] == delim) {
output.push_back(str.substr(next, pos - next));
// Skip the delimiter itself.
next = pos + 1;
}
}
while (std::getline(iss, *output.rbegin(), delim))
output.push_back("");
output.pop_back();
if (next == 0) {
output.push_back(str);
} else if (next < str.length()) {
output.push_back(str.substr(next));
}
}
std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest)
@ -281,4 +288,4 @@ int strcmpIgnore(std::string str1, std::string str2, std::string ignorestr1, std
str1 = ReplaceAll(str1, ignorestr1, ignorestr2);
str2 = ReplaceAll(str2, ignorestr1, ignorestr2);
return strcmp(str1.c_str(),str2.c_str());
}
}