make it optional

This commit is contained in:
LOSEARDES77 2025-05-06 14:52:55 +02:00
parent 6770c062bf
commit 401b0cf406
3 changed files with 14 additions and 13 deletions

View file

@ -8,11 +8,13 @@ namespace Hyprutils {
class CVarList {
public:
/** Split string into arg list
@param lastArgNo stop splitting after argv reaches maximum size, last arg will contain rest of unsplit args
@param delim if delimiter is 's', use std::isspace
@param removeEmpty remove empty args from argv
* @param in The string to split
* @param lastArgNo The number of arguments to split into
* @param delim The delimiter to use for splitting
* @param removeEmpty Whether to remove empty arguments
* @param handleEscape Whether to handle escape characters
*/
CVarList(const std::string& in, const size_t lastArgNo = 0, const char delim = ',', const bool removeEmpty = false);
CVarList(const std::string& in, const size_t lastArgNo = 0, const char delim = ',', const bool removeEmpty = false, const bool handleEscape = false);
~CVarList() = default;

View file

@ -5,7 +5,7 @@
using namespace Hyprutils::String;
Hyprutils::String::CVarList::CVarList(const std::string& in, const size_t lastArgNo, const char delim, const bool removeEmpty) {
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()) {
m_vArgs.emplace_back("");
return;
@ -18,14 +18,14 @@ Hyprutils::String::CVarList::CVarList(const std::string& in, const size_t lastAr
for (size_t i = 0; i < in.length(); ++i) {
char c = in[i];
// Handle escape character
if (c == '\\' && !escaped) {
// Handle escape character if enabled
if (handleEscape && c == '\\' && !escaped) {
escaped = true;
continue;
}
// Check if we've hit a delimiter that's not escaped
bool isDelim = (delim == 's' ? std::isspace(c) : c == delim) && !escaped;
// 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()) {
@ -34,7 +34,6 @@ Hyprutils::String::CVarList::CVarList(const std::string& in, const size_t lastAr
idx++;
}
// If this is the last argument we want to parse separately
if (idx == lastArgNo - 1) {
m_vArgs.emplace_back(trim(in.substr(i + 1)));
return;
@ -43,10 +42,10 @@ Hyprutils::String::CVarList::CVarList(const std::string& in, const size_t lastAr
currentArg += c;
}
escaped = false;
if (handleEscape)
escaped = false;
}
// Add the last argument if there is one
if (!removeEmpty || !currentArg.empty()) {
m_vArgs.emplace_back(trim(currentArg));
}

View file

@ -38,7 +38,7 @@ int main(int argc, char** argv, char** envp) {
EXPECT(list[0], "hello");
EXPECT(list[1], "world!");
CVarList list2("test:test\\:test", 0, ':', true);
CVarList list2("test:test\\:test", 0, ':', true, true);
EXPECT(list2[0], "test");
EXPECT(list2[1], "test:test");