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/AutoTypeSelectView.h
autotype/ShortcutWidget.h
autotype/WildcardMatcher.h
autotype/WindowSelectComboBox.h
core/AutoTypeAssociations.h
core/Config.h

View File

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

View File

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