Skip to content

Commit 830ae16

Browse files
Improve splitString using std::string_view input
1 parent eccb727 commit 830ae16

2 files changed

Lines changed: 7 additions & 7 deletions

File tree

lib/utils.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,15 @@ std::string replaceEscapeSequences(const std::string &source) {
183183
}
184184

185185

186-
std::vector<std::string> splitString(const std::string& str, char sep)
186+
std::vector<std::string> splitString(std::string_view str, char sep)
187187
{
188188
std::vector<std::string> l;
189-
190-
std::string::size_type pos1 = 0;
191-
std::string::size_type pos2;
189+
std::string_view::size_type pos1 = 0;
190+
std::string_view::size_type pos2 = std::string_view::npos;
192191
while (true) {
193192
pos2 = str.find(sep, pos1);
194-
l.push_back(str.substr(pos1, pos2 - pos1));
195-
if (pos2 == std::string::npos)
193+
l.emplace_back(str.substr(pos1, pos2 - pos1));
194+
if (pos2 == std::string_view::npos)
196195
break;
197196
pos1 = pos2 + 1;
198197
}

lib/utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <type_traits>
3636
#include <utility>
3737
#include <vector>
38+
#include<string_view>
3839

3940
struct SelectMapKeys {
4041
template<class Pair>
@@ -428,7 +429,7 @@ static inline T* empty_if_null(T* p)
428429
* @param sep The separator
429430
* @return The list of separate strings (including empty ones). The whole input string if no separator found.
430431
*/
431-
CPPCHECKLIB std::vector<std::string> splitString(const std::string& str, char sep);
432+
CPPCHECKLIB std::vector<std::string> splitString(std::string_view str, char sep);
432433

433434
namespace utils {
434435
/**

0 commit comments

Comments
 (0)