Skip to content

Commit 599f505

Browse files
authored
utils.cpp: simplified splitString() (#7215)
1 parent a615a28 commit 599f505

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

Diff for: cli/cmdlineparser.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ CmdLineParser::Result CmdLineParser::parseFromArgs(int argc, const char* const a
949949

950950
// --library
951951
else if (std::strncmp(argv[i], "--library=", 10) == 0) {
952-
std::list<std::string> libs = splitString(argv[i] + 10, ',');
952+
std::vector<std::string> libs = splitString(argv[i] + 10, ',');
953953
for (auto& l : libs) {
954954
if (l.empty()) {
955955
mLogger.printError("empty library specified.");

Diff for: lib/utils.cpp

+10-12
Original file line numberDiff line numberDiff line change
@@ -186,20 +186,18 @@ std::string replaceEscapeSequences(const std::string &source) {
186186
}
187187

188188

189-
std::list<std::string> splitString(const std::string& str, char sep)
189+
std::vector<std::string> splitString(const std::string& str, char sep)
190190
{
191-
if (std::strchr(str.c_str(), sep) == nullptr)
192-
return {str};
193-
194-
std::list<std::string> l;
195-
std::string p(str);
196-
for (;;) {
197-
const std::string::size_type pos = p.find(sep);
198-
if (pos == std::string::npos)
191+
std::vector<std::string> l;
192+
193+
std::string::size_type pos1 = 0;
194+
std::string::size_type pos2;
195+
while (true) {
196+
pos2 = str.find(sep, pos1);
197+
l.push_back(str.substr(pos1, pos2 - pos1));
198+
if (pos2 == std::string::npos)
199199
break;
200-
l.push_back(p.substr(0,pos));
201-
p = p.substr(pos+1);
200+
pos1 = pos2 + 1;
202201
}
203-
l.push_back(std::move(p));
204202
return l;
205203
}

Diff for: lib/utils.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include <functional>
3131
#include <initializer_list>
3232
#include <limits>
33-
#include <list>
3433
#include <stdexcept>
3534
#include <string>
3635
#include <type_traits>
@@ -403,7 +402,7 @@ static inline T* empty_if_null(T* p)
403402
* @param sep The seperator
404403
* @return The list of seperate strings (including empty ones). The whole input string if no seperator found.
405404
*/
406-
CPPCHECKLIB std::list<std::string> splitString(const std::string& str, char sep);
405+
CPPCHECKLIB std::vector<std::string> splitString(const std::string& str, char sep);
407406

408407
namespace utils {
409408
template<class T>

0 commit comments

Comments
 (0)