Skip to content

Commit

Permalink
format RegexCollection output using match results
Browse files Browse the repository at this point in the history
  • Loading branch information
Eisfunke committed Jun 5, 2024
1 parent 7163752 commit 76c2f31
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
4 changes: 2 additions & 2 deletions include/util/regex_collection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class RegexCollection {
std::map<std::string, std::string> regex_cache;
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:
RegexCollection() = default;
Expand All @@ -48,4 +48,4 @@ class RegexCollection {
std::string& get(std::string& value);
};

} // namespace waybar::util
} // namespace waybar::util
7 changes: 4 additions & 3 deletions src/util/regex_collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::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) {
if (std::regex_search(value, rule.rule)) {
std::smatch match;
if (std::regex_search(value, match, rule.rule)) {
matched_any = true;
return rule.repr;
return match.format(rule.repr.data());
}
}

Expand Down

0 comments on commit 76c2f31

Please sign in to comment.