hyprutils/src/string/VarList.cpp

69 lines
2.1 KiB
C++
Raw Normal View History

2024-06-08 22:24:55 +02:00
#include <ranges>
#include <algorithm>
#include <hyprutils/string/VarList.hpp>
#include <hyprutils/string/String.hpp>
using namespace Hyprutils::String;
2025-05-06 15:18:28 +02:00
// Original constructor calls the extended one with handleEscape = false
Hyprutils::String::CVarList::CVarList(const std::string& in, const size_t lastArgNo, const char delim, const bool removeEmpty) :
CVarList(in, lastArgNo, delim, removeEmpty, false) {}
// Extended constructor with escape handling parameter
2025-05-06 14:52:55 +02:00
Hyprutils::String::CVarList::CVarList(const std::string& in, const size_t lastArgNo, const char delim, const bool removeEmpty, const bool handleEscape) {
if (!removeEmpty && in.empty()) {
2024-06-08 22:24:55 +02:00
m_vArgs.emplace_back("");
return;
}
std::string currentArg;
bool escaped = false;
size_t idx = 0;
2024-06-08 22:24:55 +02:00
for (size_t i = 0; i < in.length(); ++i) {
char c = in[i];
2024-06-08 22:24:55 +02:00
2025-05-06 14:52:55 +02:00
// Handle escape character if enabled
if (handleEscape && c == '\\' && !escaped) {
escaped = true;
2024-06-08 22:24:55 +02:00
continue;
}
2025-05-06 14:52:55 +02:00
// Determine if this char is a delimiter (respect escape setting)
bool isDelim = (delim == 's' ? std::isspace(c) : c == delim) && !(handleEscape && escaped);
if (isDelim) {
if (!removeEmpty || !currentArg.empty()) {
m_vArgs.emplace_back(trim(currentArg));
currentArg.clear();
idx++;
}
if (idx == lastArgNo - 1) {
m_vArgs.emplace_back(trim(in.substr(i + 1)));
return;
}
} else {
currentArg += c;
}
2025-05-06 14:52:55 +02:00
if (handleEscape)
escaped = false;
}
if (!removeEmpty || !currentArg.empty()) {
m_vArgs.emplace_back(trim(currentArg));
2024-06-08 22:24:55 +02:00
}
}
std::string Hyprutils::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;
}