Skip to content

Commit

Permalink
utils.cpp: simplified splitString() (#7215)
Browse files Browse the repository at this point in the history
  • Loading branch information
firewave authored Jan 14, 2025
1 parent a615a28 commit 599f505
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
2 changes: 1 addition & 1 deletion cli/cmdlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> libs = splitString(argv[i] + 10, ',');
std::vector<std::string> libs = splitString(argv[i] + 10, ',');
for (auto& l : libs) {
if (l.empty()) {
mLogger.printError("empty library specified.");
Expand Down
22 changes: 10 additions & 12 deletions lib/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,18 @@ std::string replaceEscapeSequences(const std::string &source) {
}


std::list<std::string> splitString(const std::string& str, char sep)
std::vector<std::string> splitString(const std::string& str, char sep)
{
if (std::strchr(str.c_str(), sep) == nullptr)
return {str};

std::list<std::string> l;
std::string p(str);
for (;;) {
const std::string::size_type pos = p.find(sep);
if (pos == std::string::npos)
std::vector<std::string> l;

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;
}
3 changes: 1 addition & 2 deletions lib/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <functional>
#include <initializer_list>
#include <limits>
#include <list>
#include <stdexcept>
#include <string>
#include <type_traits>
Expand Down Expand Up @@ -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<std::string> splitString(const std::string& str, char sep);
CPPCHECKLIB std::vector<std::string> splitString(const std::string& str, char sep);

namespace utils {
template<class T>
Expand Down

0 comments on commit 599f505

Please sign in to comment.