| 6 | |
| 7 | namespace waybar::util { |
| 8 | std::string rewriteString(const std::string& value, const Json::Value& rules) { |
| 9 | if (!rules.isObject()) { |
| 10 | return value; |
| 11 | } |
| 12 | |
| 13 | std::string res = value; |
| 14 | |
| 15 | for (auto it = rules.begin(); it != rules.end(); ++it) { |
| 16 | if (it.key().isString() && it->isString()) { |
| 17 | try { |
| 18 | // malformated regexes will cause an exception. |
| 19 | // in this case, log error and try the next rule. |
| 20 | const std::regex rule{it.key().asString(), std::regex_constants::icase}; |
| 21 | if (std::regex_match(value, rule)) { |
| 22 | res = std::regex_replace(res, rule, it->asString()); |
| 23 | } |
| 24 | } catch (const std::regex_error& e) { |
| 25 | spdlog::error("Invalid rule {}: {}", it.key().asString(), e.what()); |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | return res; |
| 31 | } |
| 32 | } // namespace waybar::util |
no test coverage detected