diff options
| author | vaxerski <vaxry@vaxry.net> | 2023-09-12 16:46:22 +0100 |
|---|---|---|
| committer | vaxerski <vaxry@vaxry.net> | 2023-09-12 16:50:10 +0100 |
| commit | 2e34548aea5b4a0e131a365693227f8739c0af6d (patch) | |
| tree | 755006c60ef5120a067715d45040406a33511815 /src/helpers/VarList.cpp | |
| parent | 5cc53c14d9d904fe9b97e72ed974bbea9d68eb64 (diff) | |
varlist: move to a separate header, add join
Diffstat (limited to 'src/helpers/VarList.cpp')
| -rw-r--r-- | src/helpers/VarList.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/helpers/VarList.cpp b/src/helpers/VarList.cpp new file mode 100644 index 00000000..0da86e15 --- /dev/null +++ b/src/helpers/VarList.cpp @@ -0,0 +1,46 @@ +#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; + } + }; + + nextItem(); + + while (curitem != STRVAL_EMPTY) { + m_vArgs.push_back(removeBeginEndSpacesTabs(curitem)); + nextItem(); + } +} + +std::string CVarList::join(const std::string& joiner, size_t from, size_t to) const { + size_t last = to == 0 ? size() : to; + + std::string rolling; + for (size_t i = from; i < last; ++i) { + rolling += m_vArgs[i] + (i + 1 < last ? joiner : ""); + } + + return rolling; +}
\ No newline at end of file |
