mirror of
https://github.com/zeek/zeek.git
synced 2025-10-07 00:58:19 +00:00
util: don't modify the input string in tokenize_string()
This saves one full copy of the input string and avoids moving memory around at O(n^2) in the erase() call in each loop iteration.
This commit is contained in:
parent
70b45d1aba
commit
e068ad8a53
2 changed files with 7 additions and 6 deletions
11
src/util.cc
11
src/util.cc
|
@ -1501,26 +1501,27 @@ TEST_CASE("util tokenize_string")
|
|||
CHECK(v2.size() == 1);
|
||||
}
|
||||
|
||||
vector<string>* tokenize_string(string input, const string& delim,
|
||||
vector<string>* tokenize_string(const string &input, const string& delim,
|
||||
vector<string>* rval, int limit)
|
||||
{
|
||||
if ( ! rval )
|
||||
rval = new vector<string>();
|
||||
|
||||
size_t pos = 0;
|
||||
size_t n;
|
||||
auto found = 0;
|
||||
|
||||
while ( (n = input.find(delim)) != string::npos )
|
||||
while ( (n = input.find(delim, pos)) != string::npos )
|
||||
{
|
||||
++found;
|
||||
rval->push_back(input.substr(0, n));
|
||||
input.erase(0, n + 1);
|
||||
rval->emplace_back(input.substr(pos, n - pos));
|
||||
pos = n + 1;
|
||||
|
||||
if ( limit && found == limit )
|
||||
break;
|
||||
}
|
||||
|
||||
rval->push_back(input);
|
||||
rval->emplace_back(input.substr(pos));
|
||||
return rval;
|
||||
}
|
||||
|
||||
|
|
|
@ -145,7 +145,7 @@ inline std::string get_escaped_string(const std::string& str, bool escape_all)
|
|||
return get_escaped_string(str.data(), str.length(), escape_all);
|
||||
}
|
||||
|
||||
std::vector<std::string>* tokenize_string(std::string input,
|
||||
std::vector<std::string>* tokenize_string(const std::string &input,
|
||||
const std::string& delim,
|
||||
std::vector<std::string>* rval = 0, int limit = 0);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue