diff --git a/include/hyprutils/string/VarList.hpp b/include/hyprutils/string/VarList.hpp index 697b51e..e106c3b 100644 --- a/include/hyprutils/string/VarList.hpp +++ b/include/hyprutils/string/VarList.hpp @@ -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; diff --git a/src/string/VarList.cpp b/src/string/VarList.cpp index 08177de..9504797 100644 --- a/src/string/VarList.cpp +++ b/src/string/VarList.cpp @@ -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)); } diff --git a/tests/string.cpp b/tests/string.cpp index 54772c8..80c2cf8 100644 --- a/tests/string.cpp +++ b/tests/string.cpp @@ -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");