Optimize WildcardMatcher a bit.

This commit is contained in:
Felix Geyer 2012-07-23 12:33:37 +02:00
parent 9ad4cc8783
commit cf15d1741f
3 changed files with 21 additions and 28 deletions

View File

@ -106,7 +106,6 @@ set(keepassx_MOC
autotype/AutoTypeSelectDialog.h autotype/AutoTypeSelectDialog.h
autotype/AutoTypeSelectView.h autotype/AutoTypeSelectView.h
autotype/ShortcutWidget.h autotype/ShortcutWidget.h
autotype/WildcardMatcher.h
autotype/WindowSelectComboBox.h autotype/WindowSelectComboBox.h
core/AutoTypeAssociations.h core/AutoTypeAssociations.h
core/Config.h core/Config.h

View File

@ -19,16 +19,15 @@
#include <QtCore/QStringList> #include <QtCore/QStringList>
const QString WildcardMatcher::Wildcard = "*"; const QChar WildcardMatcher::Wildcard = '*';
const Qt::CaseSensitivity WildcardMatcher::Sensitivity = Qt::CaseInsensitive; const Qt::CaseSensitivity WildcardMatcher::Sensitivity = Qt::CaseInsensitive;
WildcardMatcher::WildcardMatcher(QString text, QObject* parent) : WildcardMatcher::WildcardMatcher(const QString& text)
QObject(parent) : m_text(text)
{ {
m_text = text;
} }
bool WildcardMatcher::match(QString pattern) bool WildcardMatcher::match(const QString& pattern)
{ {
m_pattern = pattern; m_pattern = pattern;
@ -62,16 +61,16 @@ bool WildcardMatcher::matchWithWildcards()
return partsMatch(parts); return partsMatch(parts);
} }
bool WildcardMatcher::startOrEndDoesNotMatch(QStringList parts) bool WildcardMatcher::startOrEndDoesNotMatch(const QStringList& parts)
{ {
return !m_text.startsWith(parts.first(), Sensitivity) || return !m_text.startsWith(parts.first(), Sensitivity) ||
!m_text.endsWith(parts.last(), Sensitivity); !m_text.endsWith(parts.last(), Sensitivity);
} }
bool WildcardMatcher::partsMatch(QStringList parts) bool WildcardMatcher::partsMatch(const QStringList& parts)
{ {
int index = 0; int index = 0;
Q_FOREACH (QString part, parts) { Q_FOREACH (const QString& part, parts) {
int matchIndex = getMatchIndex(part, index); int matchIndex = getMatchIndex(part, index);
if (noMatchFound(matchIndex)) { if (noMatchFound(matchIndex)) {
return false; return false;
@ -82,14 +81,14 @@ bool WildcardMatcher::partsMatch(QStringList parts)
return true; return true;
} }
int WildcardMatcher::getMatchIndex(QString part, int startIndex) int WildcardMatcher::getMatchIndex(const QString& part, int startIndex)
{ {
return m_text.indexOf(part, startIndex, Sensitivity); return m_text.indexOf(part, startIndex, Sensitivity);
} }
bool WildcardMatcher::noMatchFound(int index) bool WildcardMatcher::noMatchFound(int index)
{ {
return index < 0; return index == -1;
} }
int WildcardMatcher::calculateNewIndex(int matchIndex, int partLength) int WildcardMatcher::calculateNewIndex(int matchIndex, int partLength)

View File

@ -18,34 +18,29 @@
#ifndef KEEPASSX_WILDCARDMATCHER_H #ifndef KEEPASSX_WILDCARDMATCHER_H
#define KEEPASSX_WILDCARDMATCHER_H #define KEEPASSX_WILDCARDMATCHER_H
#include <QtCore/QObject> #include <QtCore/QStringList>
#include "core/Global.h" class WildcardMatcher
class WildcardMatcher : public QObject
{ {
Q_OBJECT
public: public:
explicit WildcardMatcher(QString text, QObject* parent = Q_NULLPTR); explicit WildcardMatcher(const QString& text);
bool match(const QString& pattern);
static const QString Wildcard; static const QChar Wildcard;
bool match(QString pattern);
private: private:
static const Qt::CaseSensitivity Sensitivity;
QString m_text;
QString m_pattern;
bool patternEqualsText(); bool patternEqualsText();
bool patternContainsWildcard(); bool patternContainsWildcard();
bool matchWithWildcards(); bool matchWithWildcards();
bool startOrEndDoesNotMatch(QStringList parts); bool startOrEndDoesNotMatch(const QStringList& parts);
bool partsMatch(QStringList parts); bool partsMatch(const QStringList& parts);
int getMatchIndex(QString part, int startIndex); int getMatchIndex(const QString& part, int startIndex);
bool noMatchFound(int index); bool noMatchFound(int index);
int calculateNewIndex(int matchIndex, int partLength); int calculateNewIndex(int matchIndex, int partLength);
static const Qt::CaseSensitivity Sensitivity;
const QString m_text;
QString m_pattern;
}; };
#endif // KEEPASSX_WILDCARDMATCHER_H #endif // KEEPASSX_WILDCARDMATCHER_H