commit b71e5f0c8123cc3d8c6a19e1761ef0e9c4a644cd
parent 3b1981e5fd944d73ffdbad477a0db09172f93ed2
Author: cfillion <cfillion@users.noreply.github.com>
Date: Thu, 5 Jan 2017 01:48:12 -0500
filter: refactoring and optimization
Diffstat:
3 files changed, 30 insertions(+), 20 deletions(-)
diff --git a/src/filter.cpp b/src/filter.cpp
@@ -67,6 +67,12 @@ void Filter::set(const string &input)
group->push(buf, &flags);
}
+bool Filter::match(vector<string> rows) const
+{
+ for_each(rows.begin(), rows.end(), [](string &str) { boost::to_lower(str); });
+ return m_root.match(rows);
+}
+
Filter::Group::Group(Type type, int flags, Group *parent)
: Node(flags), m_parent(parent), m_type(type), m_open(true)
{
@@ -168,9 +174,10 @@ bool Filter::Group::match(const vector<string> &rows) const
return m_type == MatchAll && !test(NotFlag);
}
-Filter::Token::Token(const std::string &buf, int flags)
+Filter::Token::Token(const string &buf, int flags)
: Node(flags), m_buf(buf)
{
+ boost::to_lower(m_buf);
}
bool Filter::Token::match(const vector<string> &rows) const
@@ -189,14 +196,13 @@ bool Filter::Token::match(const vector<string> &rows) const
bool Filter::Token::matchRow(const string &str) const
{
- bool match = true;
-
- if(test(StartAnchorFlag))
- match = match && boost::istarts_with(str, m_buf);
- if(test(EndAnchorFlag))
- match = match && boost::iends_with(str, m_buf);
+ const size_t pos = str.find(m_buf);
+ const bool fail = test(NotFlag);
- match = match && boost::icontains(str, m_buf);
+ if(test(StartAnchorFlag) && pos != 0)
+ return fail;
+ if(test(EndAnchorFlag) && pos + m_buf.size() != str.size())
+ return fail;
- return match ^ test(NotFlag);
+ return (pos != string::npos) ^ fail;
}
diff --git a/src/filter.hpp b/src/filter.hpp
@@ -29,7 +29,7 @@ public:
const std::string get() const { return m_input; }
void set(const std::string &);
- bool match(const std::vector<std::string> &rows) const { return m_root.match(rows); }
+ bool match(std::vector<std::string> rows) const;
Filter &operator=(const std::string &f) { set(f); return *this; }
bool operator==(const std::string &f) const { return m_input == f; }
diff --git a/test/filter.cpp b/test/filter.cpp
@@ -59,18 +59,13 @@ TEST_CASE("word matching", M) {
REQUIRE(f.match({"hello test world"}));
}
-TEST_CASE("double quote matching", M) {
+TEST_CASE("quote matching", M) {
Filter f;
- f.set("\"hello world\"");
- REQUIRE(f.match({"hello world"}));
- REQUIRE_FALSE(f.match({"helloworld"}));
- REQUIRE_FALSE(f.match({"hello test world"}));
-}
-
-TEST_CASE("single quote matching", M) {
- Filter f;
- f.set("'hello world'");
+ SECTION("double quotes")
+ f.set("\"hello world\"");
+ SECTION("single quotes")
+ f.set("'hello world'");
REQUIRE(f.match({"hello world"}));
REQUIRE_FALSE(f.match({"helloworld"}));
@@ -167,6 +162,15 @@ TEST_CASE("end of string", M) {
}
}
+TEST_CASE("both anchors", M) {
+ Filter f;
+ f.set("^word$");
+
+ REQUIRE(f.match({"word"}));
+ REQUIRE_FALSE(f.match({"word after"}));
+ REQUIRE_FALSE(f.match({"before word"}));
+}
+
TEST_CASE("row matching", M) {
Filter f;
f.set("hello world");