format RegexCollection output using match results

This commit is contained in:
Nicolas Lenz 2024-06-05 19:58:27 +02:00
parent 7163752aa0
commit 76c2f3166e
No known key found for this signature in database
2 changed files with 6 additions and 5 deletions

View File

@ -36,7 +36,7 @@ class RegexCollection {
std::map<std::string, std::string> regex_cache; std::map<std::string, std::string> regex_cache;
std::string default_repr; std::string default_repr;
std::string& find_match(std::string& value, bool& matched_any); std::string find_match(std::string& value, bool& matched_any);
public: public:
RegexCollection() = default; RegexCollection() = default;
@ -48,4 +48,4 @@ class RegexCollection {
std::string& get(std::string& value); std::string& get(std::string& value);
}; };
} // namespace waybar::util } // namespace waybar::util

View File

@ -31,11 +31,12 @@ RegexCollection::RegexCollection(const Json::Value& map, std::string default_rep
std::sort(rules.begin(), rules.end(), [](Rule& a, Rule& b) { return a.priority > b.priority; }); std::sort(rules.begin(), rules.end(), [](Rule& a, Rule& b) { return a.priority > b.priority; });
} }
std::string& RegexCollection::find_match(std::string& value, bool& matched_any) { std::string RegexCollection::find_match(std::string& value, bool& matched_any) {
for (auto& rule : rules) { for (auto& rule : rules) {
if (std::regex_search(value, rule.rule)) { std::smatch match;
if (std::regex_search(value, match, rule.rule)) {
matched_any = true; matched_any = true;
return rule.repr; return match.format(rule.repr.data());
} }
} }