string/varlist2: add view ctors

This commit is contained in:
Vaxry 2026-03-26 14:20:48 +00:00
parent cb4e152dc7
commit 762166b516
Signed by: vaxry
GPG key ID: 665806380871D640
3 changed files with 40 additions and 6 deletions

View file

@ -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<CGenericAnimatedVariable<VarType, AnimationContext>> pSelf,
const VarType& initialValue) {
const VarType& initialValue) {
m_Begun = initialValue;
m_Value = initialValue;
m_Goal = initialValue;

View file

@ -2,6 +2,7 @@
#include <functional>
#include <vector>
#include <string>
#include <string_view>
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<std::string> m_copyStrings;
std::vector<std::string_view> m_args;
};

View file

@ -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);
}