aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-12-19 16:27:41 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2018-12-19 16:27:41 +0100
commit9100d9bb0fc30df1046e58064e0293b13dbeda89 (patch)
tree074d35fbb0c278d26b3f2d0177558173f47fdbcd /lib
parentAdd subwindow list to Window menu (diff)
downloadsmolbote-9100d9bb0fc30df1046e58064e0293b13dbeda89.tar.xz
Add ContentsMatcher class
Diffstat (limited to 'lib')
-rw-r--r--lib/urlfilter/formats/adblockrule.cpp1
-rw-r--r--lib/urlfilter/formats/adblockrule.h58
-rw-r--r--lib/urlfilter/formats/adblockrule_parse.cpp2
-rw-r--r--lib/urlfilter/formats/adblockrule_parse.h4
4 files changed, 62 insertions, 3 deletions
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 <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
{
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<QPair<QWebEngineUrlRequestInfo::ResourceType, bool>> parseOption(const QString &option);
-#endif // ADBLOCKRULE_PARSE_H \ No newline at end of file
+#endif // ADBLOCKRULE_PARSE_H