From 1a7cdb1284d0ef02bf2a5ac4be4dbf1f996ddf7a Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 13 Jul 2024 11:22:24 +0200 Subject: [PATCH 1/2] utils.cpp: simplified `splitString()` --- lib/utils.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/utils.cpp b/lib/utils.cpp index 0288dffe25a..378fa05bb4c 100644 --- a/lib/utils.cpp +++ b/lib/utils.cpp @@ -188,18 +188,16 @@ std::string replaceEscapeSequences(const std::string &source) { std::list splitString(const std::string& str, char sep) { - if (std::strchr(str.c_str(), sep) == nullptr) - return {str}; - std::list l; - std::string p(str); - for (;;) { - const std::string::size_type pos = p.find(sep); - if (pos == std::string::npos) + + std::string::size_type pos1 = 0; + std::string::size_type pos2; + while (true) { + pos2 = str.find(sep, pos1); + l.push_back(str.substr(pos1, pos2 - pos1)); + if (pos2 == std::string::npos) break; - l.push_back(p.substr(0,pos)); - p = p.substr(pos+1); + pos1 = pos2 + 1; } - l.push_back(std::move(p)); return l; } From 2f71aa4ddafed9fbe55c001aa707f7e5045aafce Mon Sep 17 00:00:00 2001 From: firewave Date: Mon, 13 Jan 2025 18:12:00 +0100 Subject: [PATCH 2/2] changed `splitString()` result to `std::vector` to allow usage of `operator[]` --- cli/cmdlineparser.cpp | 2 +- lib/utils.cpp | 4 ++-- lib/utils.h | 3 +-- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cli/cmdlineparser.cpp b/cli/cmdlineparser.cpp index 20b39179e8e..05f1bd71d86 100644 --- a/cli/cmdlineparser.cpp +++ b/cli/cmdlineparser.cpp @@ -949,7 +949,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a // --library else if (std::strncmp(argv[i], "--library=", 10) == 0) { - std::list libs = splitString(argv[i] + 10, ','); + std::vector libs = splitString(argv[i] + 10, ','); for (auto& l : libs) { if (l.empty()) { mLogger.printError("empty library specified."); diff --git a/lib/utils.cpp b/lib/utils.cpp index 378fa05bb4c..8d5c7509c56 100644 --- a/lib/utils.cpp +++ b/lib/utils.cpp @@ -186,9 +186,9 @@ std::string replaceEscapeSequences(const std::string &source) { } -std::list splitString(const std::string& str, char sep) +std::vector splitString(const std::string& str, char sep) { - std::list l; + std::vector l; std::string::size_type pos1 = 0; std::string::size_type pos2; diff --git a/lib/utils.h b/lib/utils.h index 563f8fd57b0..0c50b534090 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -30,7 +30,6 @@ #include #include #include -#include #include #include #include @@ -403,7 +402,7 @@ static inline T* empty_if_null(T* p) * @param sep The seperator * @return The list of seperate strings (including empty ones). The whole input string if no seperator found. */ -CPPCHECKLIB std::list splitString(const std::string& str, char sep); +CPPCHECKLIB std::vector splitString(const std::string& str, char sep); namespace utils { template