From edc41d6f5911359a55fd37268d9dcee215b70d76 Mon Sep 17 00:00:00 2001 From: Albert S Date: Tue, 31 May 2022 10:13:34 +0200 Subject: [PATCH] shared: Introduce WildcardMatcher --- shared/shared.pro | 6 ++++-- shared/wildcardmatcher.cpp | 29 +++++++++++++++++++++++++++++ shared/wildcardmatcher.h | 17 +++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 shared/wildcardmatcher.cpp create mode 100644 shared/wildcardmatcher.h diff --git a/shared/shared.pro b/shared/shared.pro index 9d68f02..3c80b41 100644 --- a/shared/shared.pro +++ b/shared/shared.pro @@ -52,7 +52,8 @@ SOURCES += sqlitesearch.cpp \ sqlitedbservice.cpp \ tagstripperprocessor.cpp \ utils.cpp \ - ../submodules/exile.h/exile.c + ../submodules/exile.h/exile.c \ + wildcardmatcher.cpp HEADERS += sqlitesearch.h \ concurrentqueue.h \ @@ -81,7 +82,8 @@ HEADERS += sqlitesearch.h \ tagstripperprocessor.h \ token.h \ common.h \ - utils.h + utils.h \ + wildcardmatcher.h unix { target.path = /usr/lib INSTALLS += target diff --git a/shared/wildcardmatcher.cpp b/shared/wildcardmatcher.cpp new file mode 100644 index 0000000..e4b24ba --- /dev/null +++ b/shared/wildcardmatcher.cpp @@ -0,0 +1,29 @@ +#include "wildcardmatcher.h" + +void WildcardMatcher::setPatterns(QStringList patterns) +{ + this->regexes.clear(); + for(QString &str : patterns) + { + QRegExp regexp; + regexp.setPattern(str); + regexp.setPatternSyntax(QRegExp::WildcardUnix); + this->regexes.append(regexp); + } +} + +WildcardMatcher::WildcardMatcher() +{ +} + +bool WildcardMatcher::match(QString haystack) const +{ + for(const QRegExp ®exp : this->regexes) + { + if(regexp.exactMatch(haystack)) + { + return true; + } + } + return false; +} diff --git a/shared/wildcardmatcher.h b/shared/wildcardmatcher.h new file mode 100644 index 0000000..dd6dd60 --- /dev/null +++ b/shared/wildcardmatcher.h @@ -0,0 +1,17 @@ +#ifndef WILDCARDMATCHER_H +#define WILDCARDMATCHER_H +#include +#include +class WildcardMatcher +{ + private: + QVector regexes; + QStringList patterns; + + public: + WildcardMatcher(); + bool match(QString haystack) const; + void setPatterns(QStringList patterns); +}; + +#endif // WILDCARDMATCHER_H