diff --git a/lib/utils.cpp b/lib/utils.cpp index b840fc3194b..dd9f4d2ee39 100644 --- a/lib/utils.cpp +++ b/lib/utils.cpp @@ -183,16 +183,15 @@ std::string replaceEscapeSequences(const std::string &source) { } -std::vector splitString(const std::string& str, char sep) +std::vector splitString(std::string_view str, char sep) { std::vector l; - - std::string::size_type pos1 = 0; - std::string::size_type pos2; + std::string_view::size_type pos1 = 0; + std::string_view::size_type pos2 = std::string_view::npos; while (true) { pos2 = str.find(sep, pos1); - l.push_back(str.substr(pos1, pos2 - pos1)); - if (pos2 == std::string::npos) + l.emplace_back(str.substr(pos1, pos2 - pos1)); + if (pos2 == std::string_view::npos) break; pos1 = pos2 + 1; } diff --git a/lib/utils.h b/lib/utils.h index 3f9c4ab949f..e7eae153462 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -35,6 +35,7 @@ #include #include #include +#include struct SelectMapKeys { template @@ -428,7 +429,7 @@ static inline T* empty_if_null(T* p) * @param sep The separator * @return The list of separate strings (including empty ones). The whole input string if no separator found. */ -CPPCHECKLIB std::vector splitString(const std::string& str, char sep); +CPPCHECKLIB std::vector splitString(std::string_view str, char sep); namespace utils { /**