From 9100d9bb0fc30df1046e58064e0293b13dbeda89 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Wed, 19 Dec 2018 16:27:41 +0100 Subject: Add ContentsMatcher class --- lib/urlfilter/formats/adblockrule.cpp | 1 + lib/urlfilter/formats/adblockrule.h | 58 +++++++++++++++++++++++++++++ lib/urlfilter/formats/adblockrule_parse.cpp | 2 +- lib/urlfilter/formats/adblockrule_parse.h | 4 +- 4 files changed, 62 insertions(+), 3 deletions(-) (limited to 'lib/urlfilter') diff --git a/lib/urlfilter/formats/adblockrule.cpp b/lib/urlfilter/formats/adblockrule.cpp index db1c3c5..60e817f 100644 --- a/lib/urlfilter/formats/adblockrule.cpp +++ b/lib/urlfilter/formats/adblockrule.cpp @@ -45,6 +45,7 @@ bool AdBlockRule::match(const QUrl &requestUrl, QWebEngineUrlRequestInfo::Resour case FilterLeaf::RegularExpressionMatch: return (regExp->indexIn(requestUrl.toString()) != -1); default: + qWarning("Match type not implemented, returning false!"); return false; } } 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 +#include +#include + +template +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) { + matcher.setPatternOptions(matcher.patternOptions() | QRegularExpression::CaseInsensitiveOption); + } else if constexpr(std::is_same_v) { + matcher.setCaseSensitivity(Qt::CaseInsensitive); + } + } + + bool hasMatch(const QString &where) const + { + if constexpr(std::is_same_v) { + 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) { + 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 { diff --git a/lib/urlfilter/formats/adblockrule_parse.cpp b/lib/urlfilter/formats/adblockrule_parse.cpp index 0e5bf05..927a6a3 100644 --- a/lib/urlfilter/formats/adblockrule_parse.cpp +++ b/lib/urlfilter/formats/adblockrule_parse.cpp @@ -16,7 +16,7 @@ // QString::chop(len) - Removes n characters from the end of the string. // QString::remove(pos, len) - Removes n characters from the string, starting at the given position index. -AdBlockRule *loadRule(const QString &filter) +AdBlockRule *parseRule_adblock(const QString &filter) { QString parsedLine = filter.trimmed(); diff --git a/lib/urlfilter/formats/adblockrule_parse.h b/lib/urlfilter/formats/adblockrule_parse.h index 7d380a8..01255ca 100644 --- a/lib/urlfilter/formats/adblockrule_parse.h +++ b/lib/urlfilter/formats/adblockrule_parse.h @@ -11,7 +11,7 @@ class AdBlockRule; -AdBlockRule *loadRule(const QString &filter); +AdBlockRule *parseRule_adblock(const QString &filter); std::optional> parseOption(const QString &option); -#endif // ADBLOCKRULE_PARSE_H \ No newline at end of file +#endif // ADBLOCKRULE_PARSE_H -- cgit v1.2.1