| 11 | int default_priority_function(std::string& key) { return 0; } |
| 12 | |
| 13 | RegexCollection::RegexCollection(const Json::Value& map, std::string default_repr, |
| 14 | const std::function<int(std::string&)>& priority_function) |
| 15 | : default_repr(std::move(default_repr)) { |
| 16 | if (!map.isObject()) { |
| 17 | spdlog::warn("Mapping is not an object"); |
| 18 | return; |
| 19 | } |
| 20 | |
| 21 | for (auto it = map.begin(); it != map.end(); ++it) { |
| 22 | if (it.key().isString() && it->isString()) { |
| 23 | std::string key = it.key().asString(); |
| 24 | int priority = priority_function(key); |
| 25 | try { |
| 26 | const std::regex rule{key, std::regex_constants::icase}; |
| 27 | rules.emplace_back(rule, it->asString(), priority); |
| 28 | } catch (const std::regex_error& e) { |
| 29 | spdlog::error("Invalid rule '{}': {}", key, e.what()); |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | std::sort(rules.begin(), rules.end(), [](Rule& a, Rule& b) { return a.priority > b.priority; }); |
| 35 | } |
| 36 | |
| 37 | std::string RegexCollection::find_match(std::string& value, bool& matched_any) { |
| 38 | for (auto& rule : rules) { |