diff options
| author | memchr <118117622+memchr@users.noreply.github.com> | 2023-09-19 08:44:54 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-19 09:44:54 +0100 |
| commit | c50072b108198a5c278c16967b700c713b0140a8 (patch) | |
| tree | 96f6115e14070962679c19025385d62cec334112 /src/helpers/VarList.cpp | |
| parent | 60f10e60375612a378c57556bbef9a9c62cfc1b3 (diff) | |
varlist: cleanup unintuitive string splitting (#3369)
* refactor(varlist): replace unintuitive string splitting
* refactor(varlist): remove test asserts
Diffstat (limited to 'src/helpers/VarList.cpp')
| -rw-r--r-- | src/helpers/VarList.cpp | 52 |
1 files changed, 21 insertions, 31 deletions
diff --git a/src/helpers/VarList.cpp b/src/helpers/VarList.cpp index 0da86e15..c29a1e96 100644 --- a/src/helpers/VarList.cpp +++ b/src/helpers/VarList.cpp @@ -1,36 +1,26 @@ #include "VarList.hpp" - -CVarList::CVarList(const std::string& in, long unsigned int lastArgNo, const char separator) { - std::string curitem = ""; - std::string argZ = in; - const bool SPACESEP = separator == 's'; - - auto nextItem = [&]() { - auto idx = lastArgNo != 0 && m_vArgs.size() >= lastArgNo - 1 ? std::string::npos : ([&]() -> size_t { - if (!SPACESEP) - return argZ.find_first_of(separator); - - uint64_t pos = -1; - while (!std::isspace(argZ[++pos]) && pos < argZ.length()) - ; - - return pos < argZ.length() ? pos : std::string::npos; - }()); - - if (idx != std::string::npos) { - curitem = argZ.substr(0, idx); - argZ = argZ.substr(idx + 1); - } else { - curitem = argZ; - argZ = STRVAL_EMPTY; +#include <ranges> +#include <algorithm> + +CVarList::CVarList(const std::string& in, const size_t lastArgNo, const char delim, const bool removeEmpty) { + if (in.empty()) + m_vArgs.emplace_back(""); + + std::string args{in}; + size_t idx = 0; + size_t pos = 0; + std::ranges::replace_if( + args, [&](const char& c) { return delim == 's' ? std::isspace(c) : c == delim; }, 0); + + for (const auto& s : args | std::views::split(0)) { + if (removeEmpty && s.empty()) + continue; + if (++idx == lastArgNo) { + m_vArgs.emplace_back(removeBeginEndSpacesTabs(in.substr(pos))); + break; } - }; - - nextItem(); - - while (curitem != STRVAL_EMPTY) { - m_vArgs.push_back(removeBeginEndSpacesTabs(curitem)); - nextItem(); + pos += s.size() + 1; + m_vArgs.emplace_back(removeBeginEndSpacesTabs(std::string_view{s}.data())); } } |
