mirror of
https://github.com/hyprwm/hyprutils.git
synced 2025-05-13 13:50:44 +01:00
make it optional
This commit is contained in:
parent
6770c062bf
commit
401b0cf406
3 changed files with 14 additions and 13 deletions
|
@ -8,11 +8,13 @@ namespace Hyprutils {
|
||||||
class CVarList {
|
class CVarList {
|
||||||
public:
|
public:
|
||||||
/** Split string into arg list
|
/** Split string into arg list
|
||||||
@param lastArgNo stop splitting after argv reaches maximum size, last arg will contain rest of unsplit args
|
* @param in The string to split
|
||||||
@param delim if delimiter is 's', use std::isspace
|
* @param lastArgNo The number of arguments to split into
|
||||||
@param removeEmpty remove empty args from argv
|
* @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;
|
~CVarList() = default;
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
using namespace Hyprutils::String;
|
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()) {
|
if (!removeEmpty && in.empty()) {
|
||||||
m_vArgs.emplace_back("");
|
m_vArgs.emplace_back("");
|
||||||
return;
|
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) {
|
for (size_t i = 0; i < in.length(); ++i) {
|
||||||
char c = in[i];
|
char c = in[i];
|
||||||
|
|
||||||
// Handle escape character
|
// Handle escape character if enabled
|
||||||
if (c == '\\' && !escaped) {
|
if (handleEscape && c == '\\' && !escaped) {
|
||||||
escaped = true;
|
escaped = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if we've hit a delimiter that's not escaped
|
// Determine if this char is a delimiter (respect escape setting)
|
||||||
bool isDelim = (delim == 's' ? std::isspace(c) : c == delim) && !escaped;
|
bool isDelim = (delim == 's' ? std::isspace(c) : c == delim) && !(handleEscape && escaped);
|
||||||
|
|
||||||
if (isDelim) {
|
if (isDelim) {
|
||||||
if (!removeEmpty || !currentArg.empty()) {
|
if (!removeEmpty || !currentArg.empty()) {
|
||||||
|
@ -34,7 +34,6 @@ Hyprutils::String::CVarList::CVarList(const std::string& in, const size_t lastAr
|
||||||
idx++;
|
idx++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If this is the last argument we want to parse separately
|
|
||||||
if (idx == lastArgNo - 1) {
|
if (idx == lastArgNo - 1) {
|
||||||
m_vArgs.emplace_back(trim(in.substr(i + 1)));
|
m_vArgs.emplace_back(trim(in.substr(i + 1)));
|
||||||
return;
|
return;
|
||||||
|
@ -43,10 +42,10 @@ Hyprutils::String::CVarList::CVarList(const std::string& in, const size_t lastAr
|
||||||
currentArg += c;
|
currentArg += c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (handleEscape)
|
||||||
escaped = false;
|
escaped = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the last argument if there is one
|
|
||||||
if (!removeEmpty || !currentArg.empty()) {
|
if (!removeEmpty || !currentArg.empty()) {
|
||||||
m_vArgs.emplace_back(trim(currentArg));
|
m_vArgs.emplace_back(trim(currentArg));
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@ int main(int argc, char** argv, char** envp) {
|
||||||
EXPECT(list[0], "hello");
|
EXPECT(list[0], "hello");
|
||||||
EXPECT(list[1], "world!");
|
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[0], "test");
|
||||||
EXPECT(list2[1], "test:test");
|
EXPECT(list2[1], "test:test");
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue