From 761446e34af7a81d0516e322228616ebd046c9ba Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Wed, 15 Apr 2020 11:56:04 +0300 Subject: Fix MatcherRule with DomainMatch --- staging/adblock/rule.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'staging/adblock/rule.h') diff --git a/staging/adblock/rule.h b/staging/adblock/rule.h index 90062ba..0dbff21 100644 --- a/staging/adblock/rule.h +++ b/staging/adblock/rule.h @@ -39,10 +39,19 @@ protected: const Options options; }; +// The separator character can be anything but +// a letter, a digit, or one of the following: _, -, ., %. +// The end of the address is also accepted as a separator. +inline bool isSeparator(const QChar &c) +{ + return !c.isLetter() && !c.isDigit() && c != '_' && c != '-' && c != '.' && c != '%'; +} + class MatcherRule : public Rule { public: enum MatchPosition { + DomainMatch, UrlStartsWith, UrlEndsWith, UrlContains, @@ -66,8 +75,17 @@ public: bool hasMatch(const QStringRef &url) const override { const auto index = matcher.indexIn(url); + if(index == -1) { + return false; + } switch(position) { + case DomainMatch: + // match if + // there is only one : left of the index + // and + // character after pattern is separator or end of string + return (url.left(index).count(':') <= 1 && (index + patternLength == url.length() || isSeparator(url[index + patternLength]))); case UrlStartsWith: return (index == 0); case UrlEndsWith: @@ -77,6 +95,8 @@ public: case UrlEquals: return (index == 0 && patternLength == url.length()); } + + return false; } private: -- cgit v1.2.1