commit 856f5c0198f132b2bdc8134833aa078e92fe33b9
parent a5430e2d5db849489162637907161590f02f5a37
Author: cfillion <cfillion@users.noreply.github.com>
Date: Mon, 27 Jan 2020 01:05:15 -0500
filter: allow matching literal ^ and $ when quoted
Diffstat:
3 files changed, 23 insertions(+), 22 deletions(-)
diff --git a/src/filter.cpp b/src/filter.cpp
@@ -35,7 +35,9 @@ void Filter::set(const std::string &input)
int flags = 0;
Group *group = &m_root;
- for(const char c : input) {
+ for(size_t i = 0; i < input.size(); ++i) {
+ const char c = input[i];
+
if((c == '"' || c == '\'') && (!quote || quote == c)) {
if(quote)
quote = 0;
@@ -54,6 +56,16 @@ void Filter::set(const std::string &input)
continue;
}
}
+ else if(!quote) {
+ if(c == '^' && buf.empty()) {
+ flags |= Node::StartAnchorFlag;
+ continue;
+ }
+ else if(c == '$' && (i+1 == input.size() || input[i+1] == '\x20')) {
+ flags |= Node::EndAnchorFlag;
+ continue;
+ }
+ }
buf += c;
}
@@ -74,7 +86,7 @@ Filter::Group::Group(Type type, int flags, Group *parent)
{
}
-Filter::Group *Filter::Group::push(std::string buf, int *flags)
+Filter::Group *Filter::Group::push(const std::string &buf, int *flags)
{
if(buf.empty())
return this;
@@ -114,15 +126,6 @@ Filter::Group *Filter::Group::push(std::string buf, int *flags)
}
}
- if(buf.size() > 1 && buf.front() == '^') {
- *flags |= Node::StartAnchorFlag;
- buf.erase(0, 1); // we need to recheck the size below, for '$'
- }
- if(buf.size() > 1 && buf.back() == '$') {
- *flags |= Node::EndAnchorFlag;
- buf.pop_back();
- }
-
Group *group = m_open ? this : m_parent;
group->m_nodes.push_back(std::make_unique<Token>(buf, *flags));
*flags = 0;
diff --git a/src/filter.hpp b/src/filter.hpp
@@ -65,7 +65,7 @@ private:
Group(Type type, int flags = 0, Group *parent = nullptr);
void clear() { m_nodes.clear(); }
- Group *push(std::string, int *flags);
+ Group *push(const std::string &, int *flags);
bool match(const std::vector<std::string> &) const override;
diff --git a/test/filter.cpp b/test/filter.cpp
@@ -123,17 +123,16 @@ TEST_CASE("start of string", M) {
SECTION("single") {
f.set("^");
- REQUIRE(f.match({"hel^lo world"}));
- REQUIRE_FALSE(f.match({"hello world"}));
+ REQUIRE(f.match({"hello world"}));
}
- SECTION("quote before") {
+ SECTION("literal ^") {
f.set("'^hello'");
- REQUIRE(f.match({"hello world"}));
+ REQUIRE(f.match({"^hello world"}));
REQUIRE_FALSE(f.match({"world hello"}));
}
- SECTION("quote after") {
+ SECTION("full word") {
f.set("^'hello");
REQUIRE(f.match({"hello world"}));
REQUIRE_FALSE(f.match({"world hello"}));
@@ -159,19 +158,18 @@ TEST_CASE("end of string", M) {
SECTION("single") {
f.set("$");
- REQUIRE(f.match({"hel$lo world"}));
- REQUIRE_FALSE(f.match({"hello world"}));
+ REQUIRE(f.match({"hello world"}));
}
- SECTION("quote before") {
+ SECTION("full word") {
f.set("'hello'$");
REQUIRE(f.match({"hello"}));
REQUIRE_FALSE(f.match({"hello world"}));
}
- SECTION("quote after") {
+ SECTION("literal $") {
f.set("'hello$'");
- REQUIRE(f.match({"hello"}));
+ REQUIRE(f.match({"hello$"}));
REQUIRE_FALSE(f.match({"hello world"}));
}
}