aboutsummaryrefslogtreecommitdiff
path: root/lib/urlfilter/formats/adblockrule.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/urlfilter/formats/adblockrule.h')
-rw-r--r--lib/urlfilter/formats/adblockrule.h58
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/urlfilter/formats/adblockrule.h b/lib/urlfilter/formats/adblockrule.h
index 9c89dac..3331cac 100644
--- a/lib/urlfilter/formats/adblockrule.h
+++ b/lib/urlfilter/formats/adblockrule.h
@@ -11,6 +11,64 @@
#include "../filterleaf.h"
#include <optional>
+#include <QRegularExpression>
+#include <QStringMatcher>
+
+template <typename T>
+class ContentsMatcher
+{
+public:
+ ContentsMatcher(const QString &pattern, FilterLeaf::UrlMatchType matchType)
+ {
+ this->matchType = matchType;
+ patternLength = pattern.length();
+ matcher.setPattern(pattern);
+
+ if constexpr(std::is_same_v<T, QRegularExpression>) {
+ matcher.setPatternOptions(matcher.patternOptions() | QRegularExpression::CaseInsensitiveOption);
+ } else if constexpr(std::is_same_v<T, QStringMatcher>) {
+ matcher.setCaseSensitivity(Qt::CaseInsensitive);
+ }
+ }
+
+ bool hasMatch(const QString &where) const
+ {
+ if constexpr(std::is_same_v<T, QStringMatcher>) {
+ switch (matchType) {
+ case FilterLeaf::InvalidMatch:
+ case FilterLeaf::RegularExpressionMatch:
+ case FilterLeaf::DomainMatch:
+ qWarning("ContentsMatcher is a String Matcher, but not doing string matching!");
+ return false;
+
+ case FilterLeaf::StringContains:
+ return (matcher.indexIn(where) != -1);
+
+ case FilterLeaf::StringStartsWith:
+ return (matcher.indexIn(where) == 0);
+
+ case FilterLeaf::StringEndsWith:
+ return (matcher.indexIn(where) == where.length() - patternLength);
+
+ case FilterLeaf::StringEquals:
+ return (matcher.indexIn(where) == 0) && (patternLength == where.length());
+ }
+
+ } else if constexpr(std::is_same_v<T, QRegularExpression>) {
+ if(matchType != FilterLeaf::RegularExpressionMatch)
+ qWarning("ContentsMatcher is a regular expression, but not doing a regular expression match!");
+ return matcher.match(where).hasMatch();
+ } else {
+ qWarning("Matcher has no backend, returning false");
+ return false;
+ }
+ }
+
+private:
+ int patternLength;
+ T matcher;
+ FilterLeaf::UrlMatchType matchType;
+};
class AdBlockRule : public FilterLeaf
{