mirror of
https://github.com/hyprwm/hyprutils.git
synced 2025-05-12 21:30:36 +01:00
string: add ConstVarList
This commit is contained in:
parent
05878d9470
commit
7f00411949
3 changed files with 123 additions and 0 deletions
64
include/hyprutils/string/ConstVarList.hpp
Normal file
64
include/hyprutils/string/ConstVarList.hpp
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
#pragma once
|
||||||
|
#include <functional>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace Hyprutils {
|
||||||
|
namespace String {
|
||||||
|
class CConstVarList {
|
||||||
|
public:
|
||||||
|
/** Split string into an immutable 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
|
||||||
|
*/
|
||||||
|
CConstVarList(const std::string& in, const size_t lastArgNo = 0, const char delim = ',', const bool removeEmpty = false);
|
||||||
|
|
||||||
|
~CConstVarList() = default;
|
||||||
|
|
||||||
|
size_t size() const {
|
||||||
|
return m_args.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string join(const std::string& joiner, size_t from = 0, size_t to = 0) const;
|
||||||
|
|
||||||
|
void map(std::function<void(const std::string_view&)> func) {
|
||||||
|
for (auto& s : m_args)
|
||||||
|
func(s);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string_view operator[](const size_t& idx) const {
|
||||||
|
if (idx >= m_args.size())
|
||||||
|
return "";
|
||||||
|
return m_args[idx];
|
||||||
|
}
|
||||||
|
|
||||||
|
// for range-based loops
|
||||||
|
std::vector<std::string_view>::iterator begin() {
|
||||||
|
return m_args.begin();
|
||||||
|
}
|
||||||
|
std::vector<std::string_view>::const_iterator begin() const {
|
||||||
|
return m_args.begin();
|
||||||
|
}
|
||||||
|
std::vector<std::string_view>::iterator end() {
|
||||||
|
return m_args.end();
|
||||||
|
}
|
||||||
|
std::vector<std::string_view>::const_iterator end() const {
|
||||||
|
return m_args.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool contains(const std::string_view& el) {
|
||||||
|
for (auto& a : m_args) {
|
||||||
|
if (a == el)
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::string m_str;
|
||||||
|
std::vector<std::string_view> m_args;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
54
src/string/ConstVarList.cpp
Normal file
54
src/string/ConstVarList.cpp
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
#include <ranges>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <hyprutils/string/ConstVarList.hpp>
|
||||||
|
|
||||||
|
using namespace Hyprutils::String;
|
||||||
|
|
||||||
|
static std::string_view trim(const std::string_view& sv) {
|
||||||
|
if (sv.empty())
|
||||||
|
return sv;
|
||||||
|
|
||||||
|
size_t countBefore = 0;
|
||||||
|
while (countBefore < sv.length() && std::isspace(sv.at(countBefore))) {
|
||||||
|
countBefore++;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t countAfter = 0;
|
||||||
|
while (countAfter < sv.length() - countBefore && std::isspace(sv.at(sv.length() - countAfter - 1))) {
|
||||||
|
countAfter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sv.substr(countBefore, sv.length() - countBefore - countAfter);
|
||||||
|
}
|
||||||
|
|
||||||
|
CConstVarList::CConstVarList(const std::string& in, const size_t lastArgNo, const char delim, const bool removeEmpty) : m_str(in) {
|
||||||
|
if (in.empty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
size_t idx = 0;
|
||||||
|
size_t pos = 0;
|
||||||
|
std::ranges::replace_if(m_str, [&](const char& c) { return delim == 's' ? std::isspace(c) : c == delim; }, 0);
|
||||||
|
|
||||||
|
for (const auto& s : m_str | std::views::split(0)) {
|
||||||
|
if (removeEmpty && s.empty())
|
||||||
|
continue;
|
||||||
|
if (++idx == lastArgNo) {
|
||||||
|
m_args.emplace_back(trim(in.substr(pos)));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
pos += s.size() + 1;
|
||||||
|
m_args.emplace_back(trim(s.data()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string CConstVarList::join(const std::string& joiner, size_t from, size_t to) const {
|
||||||
|
size_t last = to == 0 ? size() : to;
|
||||||
|
|
||||||
|
std::string rolling;
|
||||||
|
for (size_t i = from; i < last; ++i) {
|
||||||
|
// cast can be removed once C++26's change to allow this is supported
|
||||||
|
rolling += std::string{m_args[i]} + (i + 1 < last ? joiner : "");
|
||||||
|
}
|
||||||
|
|
||||||
|
return rolling;
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
#include <hyprutils/string/String.hpp>
|
#include <hyprutils/string/String.hpp>
|
||||||
#include <hyprutils/string/VarList.hpp>
|
#include <hyprutils/string/VarList.hpp>
|
||||||
|
#include <hyprutils/string/ConstVarList.hpp>
|
||||||
#include "shared.hpp"
|
#include "shared.hpp"
|
||||||
|
|
||||||
using namespace Hyprutils::String;
|
using namespace Hyprutils::String;
|
||||||
|
@ -38,6 +39,10 @@ int main(int argc, char** argv, char** envp) {
|
||||||
EXPECT(list[0], "hello");
|
EXPECT(list[0], "hello");
|
||||||
EXPECT(list[1], "world!");
|
EXPECT(list[1], "world!");
|
||||||
|
|
||||||
|
CConstVarList listConst("hello world!", 0, 's', true);
|
||||||
|
EXPECT(listConst[0], "hello");
|
||||||
|
EXPECT(listConst[1], "world!");
|
||||||
|
|
||||||
std::string hello = "hello world!";
|
std::string hello = "hello world!";
|
||||||
replaceInString(hello, "hello", "hi");
|
replaceInString(hello, "hello", "hi");
|
||||||
EXPECT(hello, "hi world!");
|
EXPECT(hello, "hi world!");
|
||||||
|
|
Loading…
Reference in a new issue