diff --git a/include/hyprutils/animation/AnimatedVariable.hpp b/include/hyprutils/animation/AnimatedVariable.hpp index 668661b..3909cb5 100644 --- a/include/hyprutils/animation/AnimatedVariable.hpp +++ b/include/hyprutils/animation/AnimatedVariable.hpp @@ -149,7 +149,7 @@ namespace Hyprutils { /* Equivalent to create, except that it allows animated variables to be UP's */ void create2(const int typeInfo, CAnimationManager* pAnimationManager, Memory::CWeakPointer> pSelf, - const VarType& initialValue) { + const VarType& initialValue) { m_Begun = initialValue; m_Value = initialValue; m_Goal = initialValue; diff --git a/include/hyprutils/string/VarList2.hpp b/include/hyprutils/string/VarList2.hpp index 04b0faa..7d79f48 100644 --- a/include/hyprutils/string/VarList2.hpp +++ b/include/hyprutils/string/VarList2.hpp @@ -2,6 +2,7 @@ #include #include #include +#include namespace Hyprutils { namespace String { @@ -15,7 +16,24 @@ namespace Hyprutils { @param removeEmpty remove empty args from argv @param allowEscape whether to allow escaping the delimiter */ - CVarList2(std::string&& in, const size_t lastArgNo = 0, const char delim = ',', const bool removeEmpty = false, const bool allowEscape = true); + explicit CVarList2(std::string&& in, const size_t lastArgNo = 0, const char delim = ',', const bool removeEmpty = false, const bool allowEscape = true); + + /** Split string into arg list + Prefer this over CConstVarList / CVarList, this is better. + + Warning: passing a string_view will make this list assume sv is kept alive as long as this vl is alive. + + @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 allowEscape whether to allow escaping the delimiter + */ + explicit CVarList2(std::string_view in, const size_t lastArgNo = 0, const char delim = ',', const bool removeEmpty = false, const bool allowEscape = true); + + /** + Same as CVarList2(std::string_view, ...) + */ + explicit CVarList2(const char* in, const size_t lastArgNo = 0, const char delim = ',', const bool removeEmpty = false, const bool allowEscape = true); ~CVarList2() = default; @@ -42,7 +60,10 @@ namespace Hyprutils { } private: - std::string m_inString; + void construct(std::string_view in, const size_t lastArgNo, const char delim, const bool removeEmpty, const bool allowEscape); + + std::string m_inStringCopy; + std::string_view m_inString; std::vector m_copyStrings; std::vector m_args; }; diff --git a/src/string/VarList2.cpp b/src/string/VarList2.cpp index 9f845ae..4734694 100644 --- a/src/string/VarList2.cpp +++ b/src/string/VarList2.cpp @@ -5,7 +5,20 @@ using namespace Hyprutils::String; -CVarList2::CVarList2(std::string&& in, const size_t lastArgNo, const char delim, const bool removeEmpty, const bool allowEscape) : m_inString(std::move(in)) { +CVarList2::CVarList2(std::string&& in, const size_t lastArgNo, const char delim, const bool removeEmpty, const bool allowEscape) : + m_inStringCopy(std::move(in)), m_inString(m_inStringCopy) { + construct(m_inString, lastArgNo, delim, removeEmpty, allowEscape); +} + +CVarList2::CVarList2(std::string_view in, const size_t lastArgNo, const char delim, const bool removeEmpty, const bool allowEscape) : m_inString(in) { + construct(m_inString, lastArgNo, delim, removeEmpty, allowEscape); +} + +CVarList2::CVarList2(const char* in, const size_t lastArgNo, const char delim, const bool removeEmpty, const bool allowEscape) : m_inString(in) { + construct(m_inString, lastArgNo, delim, removeEmpty, allowEscape); +} + +void CVarList2::construct(std::string_view in, const size_t lastArgNo, const char delim, const bool removeEmpty, const bool allowEscape) { if (m_inString.empty()) return; @@ -57,7 +70,7 @@ CVarList2::CVarList2(std::string&& in, const size_t lastArgNo, const char delim, m_args.emplace_back(ARG); } else { // we escaped something, fixup the string, add to copies, then emplace - std::string cpy = m_inString.substr(argBegin, i - argBegin); + std::string cpy = std::string{m_inString.substr(argBegin, i - argBegin)}; for (size_t i = 0; i < escapedIndices.size(); ++i) { cpy = cpy.substr(0, escapedIndices[i] - i) + cpy.substr(escapedIndices[i] - i + 1); } @@ -80,7 +93,7 @@ CVarList2::CVarList2(std::string&& in, const size_t lastArgNo, const char delim, m_args.emplace_back(ARG); } else { // we escaped something, fixup the string, add to copies, then emplace - std::string cpy = m_inString.substr(argBegin, m_inString.size() - argBegin); + std::string cpy = std::string{m_inString.substr(argBegin, m_inString.size() - argBegin)}; for (size_t i = 0; i < escapedIndices.size(); ++i) { cpy = cpy.substr(0, escapedIndices[i] - i) + cpy.substr(escapedIndices[i] - i + 1); }