refactor: separate regex rule matching and caching in separate class

This commit is contained in:
Brenno Lemos
2023-10-09 13:53:00 -03:00
parent 30cc88a4c5
commit 8d057e6f96
6 changed files with 149 additions and 62 deletions

View File

@ -0,0 +1,37 @@
#pragma once
#include <json/json.h>
#include <functional>
#include <regex>
#include <string>
namespace waybar::util {
struct Rule {
std::regex rule;
std::string repr;
int priority;
};
int default_priority_function(std::string& key);
class RegexCollection {
private:
std::vector<Rule> rules;
std::map<std::string, std::string> regex_cache;
std::string default_repr;
std::string& find_match(std::string& value, bool& matched_any);
public:
RegexCollection() = default;
RegexCollection(const Json::Value& map, std::string default_repr = "",
std::function<int(std::string&)> priority_function = default_priority_function);
~RegexCollection() = default;
std::string& get(std::string& value, bool& matched_any);
std::string& get(std::string& value);
};
} // namespace waybar::util