hyprutils/src/string/ConstVarList.cpp

39 lines
1.2 KiB
C++
Raw Normal View History

2025-05-07 17:26:37 +01:00
#include <ranges>
#include <algorithm>
#include <hyprutils/string/String.hpp>
2025-05-07 17:26:37 +01:00
#include <hyprutils/string/ConstVarList.hpp>
using namespace Hyprutils::String;
CConstVarList::CConstVarList(const std::string& in, const size_t lastArgNo, const char delim, const bool removeEmpty) : m_str(in) {
if (in.empty())
return;
2025-09-30 13:56:31 +01:00
size_t idx = 0;
size_t pos = 0;
2025-05-07 17:26:37 +01:00
std::ranges::replace_if(m_str, [&](const char& c) { return delim == 's' ? std::isspace(c) : c == delim; }, 0);
for (const auto& s : m_str | std::views::split(0)) {
if (removeEmpty && s.empty())
continue;
if (++idx == lastArgNo) {
2025-11-05 15:03:55 +00:00
m_args.emplace_back(trim(std::string_view{m_str}.substr(pos)));
2025-05-07 17:26:37 +01:00
break;
}
pos += s.size() + 1;
m_args.emplace_back(trim(std::string_view{s.data()}));
2025-05-07 17:26:37 +01:00
}
}
std::string CConstVarList::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) {
// cast can be removed once C++26's change to allow this is supported
rolling += std::string{m_args[i]} + (i + 1 < last ? joiner : "");
}
return rolling;
}