diff --git a/src/util.cc b/src/util.cc index db91af4f54..d566c588a9 100644 --- a/src/util.cc +++ b/src/util.cc @@ -837,8 +837,7 @@ bool ensure_intermediate_dirs(const char* dirname) bool absolute = dirname[0] == '/'; string path = normalize_path(dirname); - vector path_components; - tokenize_string(path, "/", &path_components); + const auto path_components = tokenize_string(path, '/'); string current_dir; @@ -1525,6 +1524,25 @@ vector* tokenize_string(const std::string_view input, const std::string_ return rval; } +vector tokenize_string(const std::string_view input, const char delim) noexcept + { + vector 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 components; vector 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 paths; - tokenize_string(bro_path(), ":", &paths); + const auto paths = tokenize_string(bro_path(), ':'); for ( size_t i = 0; i < paths.size(); ++i ) { diff --git a/src/util.h b/src/util.h index b2b346f727..bd2447c23f 100644 --- a/src/util.h +++ b/src/util.h @@ -150,6 +150,8 @@ std::vector* tokenize_string(std::string_view input, std::string_view delim, std::vector* rval = 0, int limit = 0); +std::vector 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);