Skip to content

Commit 86cee80

Browse files
committed
add vector variant of splitString
1 parent 5b912b6 commit 86cee80

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/utils.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,21 @@ std::list<std::string> splitString(const std::string& str, char sep)
203203
l.push_back(std::move(p));
204204
return l;
205205
}
206+
207+
std::vector<std::string> splitStringVector(const std::string& str, char sep)
208+
{
209+
if (std::strchr(str.c_str(), sep) == nullptr)
210+
return {str};
211+
212+
std::vector<std::string> l;
213+
std::string p(str);
214+
for (;;) {
215+
const std::string::size_type pos = p.find(sep);
216+
if (pos == std::string::npos)
217+
break;
218+
l.push_back(p.substr(0,pos));
219+
p = p.substr(pos+1);
220+
}
221+
l.push_back(std::move(p));
222+
return l;
223+
}

lib/utils.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,14 @@ static inline T* empty_if_null(T* p)
405405
*/
406406
CPPCHECKLIB std::list<std::string> splitString(const std::string& str, char sep);
407407

408+
/**
409+
* Split string by given sperator.
410+
* @param str The string to split
411+
* @param sep The seperator
412+
* @return The vector of seperate strings (including empty ones). The whole input string if no seperator found.
413+
*/
414+
CPPCHECKLIB std::vector<std::string> splitStringVector(const std::string& str, char sep);
415+
408416
namespace utils {
409417
template<class T>
410418
constexpr typename std::add_const<T>::type & as_const(T& t) noexcept

0 commit comments

Comments
 (0)