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) {
|
2025-05-05 15:11:12 +02:00
|
|
|
if (!removeEmpty && in.empty()) {
|
2024-06-08 22:24:55 +02:00
|
|
|
m_vArgs.emplace_back("");
|
2025-05-05 15:11:12 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 15:41:43 +02:00
|
|
|
std::vector<std::pair<size_t, size_t>> argIndices;
|
|
|
|
|
size_t currentStart = 0;
|
|
|
|
|
size_t idx = 0;
|
2024-06-08 22:24:55 +02:00
|
|
|
|
2025-05-05 15:11:12 +02:00
|
|
|
for (size_t i = 0; i < in.length(); ++i) {
|
2025-05-09 15:41:43 +02:00
|
|
|
if (handleEscape && in[i] == '\\' && i + 1 < in.length()) {
|
2025-05-09 09:05:17 +02:00
|
|
|
i++;
|
2025-05-09 15:41:43 +02:00
|
|
|
continue;
|
2024-06-08 22:24:55 +02:00
|
|
|
}
|
2025-05-05 15:11:12 +02:00
|
|
|
|
2025-05-09 15:41:43 +02:00
|
|
|
const char c = in[i];
|
|
|
|
|
const bool isDelim = (delim == 's' ? std::isspace(c) : c == delim);
|
2025-05-05 15:11:12 +02:00
|
|
|
|
|
|
|
|
if (isDelim) {
|
2025-05-09 15:41:43 +02:00
|
|
|
if (!removeEmpty || i > currentStart) {
|
|
|
|
|
argIndices.emplace_back(currentStart, i);
|
2025-05-05 15:11:12 +02:00
|
|
|
idx++;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 15:41:43 +02:00
|
|
|
currentStart = i + 1;
|
|
|
|
|
|
2025-05-05 15:11:12 +02:00
|
|
|
if (idx == lastArgNo - 1) {
|
2025-05-09 15:41:43 +02:00
|
|
|
argIndices.emplace_back(i + 1, in.length());
|
|
|
|
|
break;
|
2025-05-05 15:11:12 +02:00
|
|
|
}
|
2025-05-09 15:41:43 +02:00
|
|
|
}
|
|
|
|
|
}
|
2025-05-05 15:11:12 +02:00
|
|
|
|
2025-05-09 15:41:43 +02:00
|
|
|
if (currentStart < in.length() && (!removeEmpty || currentStart < in.length())) {
|
|
|
|
|
argIndices.emplace_back(currentStart, in.length());
|
2025-05-05 15:11:12 +02:00
|
|
|
}
|
|
|
|
|
|
2025-05-09 15:41:43 +02:00
|
|
|
m_vArgs.reserve(argIndices.size());
|
|
|
|
|
for (const auto& [start, end] : argIndices) {
|
|
|
|
|
if (handleEscape) {
|
|
|
|
|
std::string unescaped;
|
|
|
|
|
for (size_t i = start; i < end; ++i) {
|
|
|
|
|
if (in[i] == '\\' && i + 1 < end) {
|
|
|
|
|
unescaped += in[++i]; // Add the character being escaped
|
|
|
|
|
} else {
|
|
|
|
|
unescaped += in[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m_vArgs.emplace_back(trim(unescaped));
|
|
|
|
|
} else {
|
|
|
|
|
m_vArgs.emplace_back(trim(in.substr(start, end - start)));
|
|
|
|
|
}
|
|
|
|
|
}
|
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;
|
2025-02-03 00:36:28 +05:00
|
|
|
}
|