Rename function name

This commit is contained in:
TheRealLorenz
2023-03-25 17:33:01 +01:00
parent 473eb0982b
commit 63525ec956
5 changed files with 11 additions and 11 deletions

View File

@@ -0,0 +1,32 @@
#include "util/rewrite_string.hpp"
#include <spdlog/spdlog.h>
#include <regex>
namespace waybar::util {
std::string rewriteString(const std::string& value, const Json::Value& rules) {
if (!rules.isObject()) {
return value;
}
std::string res = value;
for (auto it = rules.begin(); it != rules.end(); ++it) {
if (it.key().isString() && it->isString()) {
try {
// malformated regexes will cause an exception.
// in this case, log error and try the next rule.
const std::regex rule{it.key().asString()};
if (std::regex_match(value, rule)) {
res = std::regex_replace(res, rule, it->asString());
}
} catch (const std::regex_error& e) {
spdlog::error("Invalid rule {}: {}", it.key().asString(), e.what());
}
}
}
return res;
}
} // namespace waybar::util