mirror of
https://github.com/EQEmu/Server.git
synced 2026-06-11 15:58:36 +00:00
[Strings] Split String Optimizations (#1325)
* Switch the 2 split calls to SplitString * Nuke duplicate split in favor of SplitString #1263 * Add a test for SplitString * Optimize SplitString Benchmarking: -------------------------------------------------------------- Benchmark Time CPU Iterations -------------------------------------------------------------- bench_oldsplit 5201 ns 5201 ns 129500 bench_split 1269 ns 1269 ns 548906 This is splitting a VERY long SpecialAbilities string. This is ~75% speed up.
This commit is contained in:
committed by
GitHub
parent
00fb9bc9f9
commit
dba3010c89
+11
-20
@@ -74,18 +74,6 @@ const std::string str_tolower(std::string s)
|
||||
return s;
|
||||
}
|
||||
|
||||
std::vector<std::string> split(std::string str_to_split, char delimiter)
|
||||
{
|
||||
std::stringstream ss(str_to_split);
|
||||
std::string item;
|
||||
std::vector<std::string> exploded_values;
|
||||
while (std::getline(ss, item, delimiter)) {
|
||||
exploded_values.push_back(item);
|
||||
}
|
||||
|
||||
return exploded_values;
|
||||
}
|
||||
|
||||
const std::string str_toupper(std::string s)
|
||||
{
|
||||
std::transform(
|
||||
@@ -113,15 +101,18 @@ const std::string StringFormat(const char *format, ...)
|
||||
return output;
|
||||
}
|
||||
|
||||
std::vector<std::string> SplitString(const std::string &str, char delim) {
|
||||
std::vector<std::string> SplitString(const std::string &str, const char delim) {
|
||||
std::vector<std::string> ret;
|
||||
std::stringstream ss(str);
|
||||
std::string item;
|
||||
|
||||
while(std::getline(ss, item, delim)) {
|
||||
ret.push_back(item);
|
||||
}
|
||||
|
||||
std::string::size_type start = 0;
|
||||
auto end = str.find(delim);
|
||||
while (end != std::string::npos) {
|
||||
ret.emplace_back(str, start, end - start);
|
||||
start = end + 1;
|
||||
end = str.find(delim, start);
|
||||
}
|
||||
// this will catch the last word since the string is unlikely to end with a delimiter
|
||||
if (str.length() > start)
|
||||
ret.emplace_back(str, start, str.length() - start);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,6 @@
|
||||
const std::string str_tolower(std::string s);
|
||||
const std::string str_toupper(std::string s);
|
||||
const std::string ucfirst(std::string s);
|
||||
std::vector<std::string> split(std::string str_to_split, char delimiter);
|
||||
const std::string StringFormat(const char* format, ...);
|
||||
const std::string vStringFormat(const char* format, va_list args);
|
||||
std::vector<std::string> wrap(std::vector<std::string> &src, std::string character);
|
||||
@@ -177,7 +176,7 @@ std::vector<std::string> join_tuple(const std::string &glue, const std::pair<cha
|
||||
return output;
|
||||
}
|
||||
|
||||
std::vector<std::string> SplitString(const std::string &s, char delim);
|
||||
std::vector<std::string> SplitString(const std::string &s, const char delim = ',');
|
||||
std::string::size_type search_deliminated_string(const std::string &haystack, const std::string &needle, const char deliminator = ',');
|
||||
std::string EscapeString(const char *src, size_t sz);
|
||||
std::string EscapeString(const std::string &s);
|
||||
|
||||
Reference in New Issue
Block a user