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

@@ -1,6 +1,5 @@
#include "util/rewrite_string.hpp"
#include <fmt/core.h>
#include <spdlog/spdlog.h>
#include <regex>
@@ -30,31 +29,4 @@ std::string rewriteString(const std::string& value, const Json::Value& rules) {
return res;
}
std::string rewriteStringOnce(const std::string& value, const Json::Value& rules,
bool& matched_any) {
if (!rules.isObject()) {
return value;
}
matched_any = false;
std::string res = value;
for (auto it = rules.begin(); it != rules.end(); ++it) {
if (it.key().isString() && it->isString()) {
try {
const std::regex rule{it.key().asString(), std::regex_constants::icase};
if (std::regex_match(value, rule)) {
matched_any = true;
return std::regex_replace(res, rule, it->asString());
}
} catch (const std::regex_error& e) {
spdlog::error("Invalid rule {}: {}", it.key().asString(), e.what());
}
}
}
return value;
}
} // namespace waybar::util