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 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string currentArg;
|
|
|
|
|
bool escaped = false;
|
|
|
|
|
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) {
|
|
|
|
|
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) {
|
2025-05-05 15:11:12 +02:00
|
|
|
escaped = true;
|
2024-06-08 22:24:55 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
2025-05-05 15:11:12 +02:00
|
|
|
|
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);
|
2025-05-05 15:11:12 +02:00
|
|
|
|
|
|
|
|
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;
|
2025-05-05 15:11:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
2025-02-03 00:36:28 +05:00
|
|
|
}
|