2017-05-21 19:51:15 +00:00
|
|
|
#include "match.h"
|
2017-04-15 01:40:01 +00:00
|
|
|
|
|
|
|
#include <doctest/doctest.h>
|
2017-05-21 21:00:48 +00:00
|
|
|
#include <iostream>
|
2017-04-15 01:40:01 +00:00
|
|
|
|
2017-05-21 21:00:48 +00:00
|
|
|
// static
|
|
|
|
optional<Matcher> Matcher::Create(const std::string& search) {
|
2017-04-16 23:52:42 +00:00
|
|
|
/*
|
2017-04-15 01:40:01 +00:00
|
|
|
std::string real_search;
|
|
|
|
real_search.reserve(search.size() * 3 + 2);
|
|
|
|
for (auto c : search) {
|
|
|
|
real_search += ".*";
|
|
|
|
real_search += c;
|
|
|
|
}
|
|
|
|
real_search += ".*";
|
2017-04-16 23:52:42 +00:00
|
|
|
*/
|
2017-04-15 01:40:01 +00:00
|
|
|
|
2017-05-21 21:00:48 +00:00
|
|
|
try {
|
|
|
|
Matcher m;
|
|
|
|
m.regex_string = search;
|
|
|
|
m.regex = std::regex(search,
|
2017-04-15 01:40:01 +00:00
|
|
|
std::regex_constants::ECMAScript |
|
|
|
|
std::regex_constants::icase |
|
|
|
|
std::regex_constants::optimize
|
|
|
|
//std::regex_constants::nosubs
|
2017-05-21 21:00:48 +00:00
|
|
|
);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
catch (std::exception e) {
|
|
|
|
// TODO/FIXME: show a warning message, we need to access IpcManager so we can print it safely to stdout.
|
|
|
|
std::cerr << "Building matcher for " << search << " failed; " << e.what() << std::endl;
|
|
|
|
return nullopt;
|
|
|
|
}
|
2017-04-15 01:40:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-16 23:52:42 +00:00
|
|
|
bool Matcher::IsMatch(const std::string& value) const {
|
2017-04-15 01:40:01 +00:00
|
|
|
//std::smatch match;
|
|
|
|
//return std::regex_match(value, match, regex);
|
|
|
|
return std::regex_match(value, regex, std::regex_constants::match_any);
|
|
|
|
}
|
|
|
|
|
2017-05-21 19:51:15 +00:00
|
|
|
GroupMatch::GroupMatch(
|
|
|
|
const std::vector<std::string>& whitelist,
|
|
|
|
const std::vector<std::string>& blacklist) {
|
2017-05-21 21:00:48 +00:00
|
|
|
for (const std::string& entry : whitelist) {
|
|
|
|
optional<Matcher> m = Matcher::Create(entry);
|
|
|
|
if (m)
|
|
|
|
this->whitelist.push_back(*m);
|
|
|
|
}
|
|
|
|
for (const std::string& entry : blacklist) {
|
|
|
|
optional<Matcher> m = Matcher::Create(entry);
|
|
|
|
if (m)
|
|
|
|
this->blacklist.push_back(*m);
|
|
|
|
}
|
2017-05-21 19:51:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GroupMatch::IsMatch(const std::string& value, std::string* match_failure_reason) const {
|
|
|
|
for (const Matcher& m : whitelist) {
|
|
|
|
if (!m.IsMatch(value)) {
|
|
|
|
if (match_failure_reason)
|
|
|
|
*match_failure_reason = "whitelist \"" + m.regex_string + "\"";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const Matcher& m : blacklist) {
|
|
|
|
if (m.IsMatch(value)) {
|
|
|
|
if (match_failure_reason)
|
|
|
|
*match_failure_reason = "blacklist \"" + m.regex_string + "\"";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-15 01:40:01 +00:00
|
|
|
TEST_SUITE("Matcher");
|
|
|
|
|
|
|
|
TEST_CASE("sanity") {
|
2017-05-21 21:00:48 +00:00
|
|
|
//Matcher m("abc");
|
2017-04-15 01:40:01 +00:00
|
|
|
// TODO: check case
|
2017-04-18 02:59:48 +00:00
|
|
|
//CHECK(m.IsMatch("abc"));
|
|
|
|
//CHECK(m.IsMatch("fooabc"));
|
|
|
|
//CHECK(m.IsMatch("abc"));
|
|
|
|
//CHECK(m.IsMatch("abcfoo"));
|
|
|
|
//CHECK(m.IsMatch("11a11b11c11"));
|
2017-04-15 01:40:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_SUITE_END();
|