mirror of
https://github.com/zeek/zeek.git
synced 2025-10-06 16:48:19 +00:00
util: add a tokenize_string() overload which returns string_views
Additionally, it uses a single "char" as delimiter, which is also faster. This patch speeds up Zeek startup by 10%.
This commit is contained in:
parent
763afe6f5f
commit
26da10ca05
2 changed files with 24 additions and 6 deletions
28
src/util.cc
28
src/util.cc
|
@ -837,8 +837,7 @@ bool ensure_intermediate_dirs(const char* dirname)
|
|||
bool absolute = dirname[0] == '/';
|
||||
string path = normalize_path(dirname);
|
||||
|
||||
vector<string> path_components;
|
||||
tokenize_string(path, "/", &path_components);
|
||||
const auto path_components = tokenize_string(path, '/');
|
||||
|
||||
string current_dir;
|
||||
|
||||
|
@ -1525,6 +1524,25 @@ vector<string>* tokenize_string(const std::string_view input, const std::string_
|
|||
return rval;
|
||||
}
|
||||
|
||||
vector<std::string_view> tokenize_string(const std::string_view input, const char delim) noexcept
|
||||
{
|
||||
vector<std::string_view> rval;
|
||||
|
||||
size_t pos = 0;
|
||||
size_t n;
|
||||
auto found = 0;
|
||||
|
||||
while ( (n = input.find(delim, pos)) != string::npos )
|
||||
{
|
||||
++found;
|
||||
rval.push_back(input.substr(pos, n - pos));
|
||||
pos = n + 1;
|
||||
}
|
||||
|
||||
rval.push_back(input.substr(pos));
|
||||
return rval;
|
||||
}
|
||||
|
||||
TEST_CASE("util normalize_path")
|
||||
{
|
||||
CHECK(normalize_path("/1/2/3") == "/1/2/3");
|
||||
|
@ -1555,7 +1573,6 @@ TEST_CASE("util normalize_path")
|
|||
string normalize_path(const std::string_view path)
|
||||
{
|
||||
size_t n;
|
||||
vector<string> components;
|
||||
vector<std::string_view> final_components;
|
||||
string new_path;
|
||||
new_path.reserve(path.size());
|
||||
|
@ -1563,7 +1580,7 @@ string normalize_path(const std::string_view path)
|
|||
if ( !path.empty() && path[0] == '/' )
|
||||
new_path = "/";
|
||||
|
||||
tokenize_string(path, "/", &components);
|
||||
const auto components = tokenize_string(path, '/');
|
||||
final_components.reserve(components.size());
|
||||
|
||||
for ( auto it = components.begin(); it != components.end(); ++it )
|
||||
|
@ -1616,8 +1633,7 @@ string without_bropath_component(const string& path)
|
|||
{
|
||||
string rval = normalize_path(path);
|
||||
|
||||
vector<string> paths;
|
||||
tokenize_string(bro_path(), ":", &paths);
|
||||
const auto paths = tokenize_string(bro_path(), ':');
|
||||
|
||||
for ( size_t i = 0; i < paths.size(); ++i )
|
||||
{
|
||||
|
|
|
@ -150,6 +150,8 @@ std::vector<std::string>* tokenize_string(std::string_view input,
|
|||
std::string_view delim,
|
||||
std::vector<std::string>* rval = 0, int limit = 0);
|
||||
|
||||
std::vector<std::string_view> tokenize_string(const std::string_view input, const char delim) noexcept;
|
||||
|
||||
extern char* copy_string(const char* s);
|
||||
extern int streq(const char* s1, const char* s2);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue