From ccf5568a21c1307d20b3a5ed65d60edf912e1e8b Mon Sep 17 00:00:00 2001 From: Roman Kashitsyn Date: Sun, 19 Jan 2014 14:00:29 +0400 Subject: [PATCH] fix(#77): Make docset filter case-insensitive --- zeal/zealdocsetsregistry.cpp | 8 ++++---- zeal/zealsearchresult.cpp | 31 +++++++++++++++++++++++++++++-- zeal/zealsearchresult.h | 36 +++++++++++++++++++----------------- 3 files changed, 52 insertions(+), 23 deletions(-) diff --git a/zeal/zealdocsetsregistry.cpp b/zeal/zealdocsetsregistry.cpp index 7cd706c..c5d33c5 100644 --- a/zeal/zealdocsetsregistry.cpp +++ b/zeal/zealdocsetsregistry.cpp @@ -59,14 +59,14 @@ void ZealDocsetsRegistry::_runQuery(const QString& rawQuery, int queryNum) if(queryNum != lastQuery) return; // some other queries pending - ignore this one QList results; - - ZealSearchQuery query = ZealSearchQuery(rawQuery); + ZealSearchQuery query(rawQuery); QString docsetPrefix = query.getDocsetFilter(); QString preparedQuery = query.getSanitizedQuery(); + bool hasPrefixFilter = !docsetPrefix.isEmpty(); - for(QString name : names()) { - if(!docsetPrefix.isEmpty() && !name.toLower().contains(docsetPrefix)) { + for (const QString &name : names()) { + if (hasPrefixFilter && !name.contains(docsetPrefix, Qt::CaseInsensitive)) { // Filter out this docset as the names don't match the docset prefix continue; } diff --git a/zeal/zealsearchresult.cpp b/zeal/zealsearchresult.cpp index 83375f4..b4468f0 100644 --- a/zeal/zealsearchresult.cpp +++ b/zeal/zealsearchresult.cpp @@ -1,6 +1,33 @@ #include "zealsearchresult.h" -ZealSearchResult::ZealSearchResult(const QString& name_, const QString& parentName_, const QString& path_, const QString& docset_, const QString& query_) - : name(name_), parentName(parentName_), path(path_), docset(docset_), query(query_) +ZealSearchResult::ZealSearchResult() +{} + +ZealSearchResult::ZealSearchResult(const QString &name_, + const QString &parentName_, + const QString &path_, + const QString &docset_, + const QString &query_) + : name(name_) + , parentName(parentName_) + , path(path_) + , docset(docset_) + , query(query_) +{} + +bool ZealSearchResult::operator<(const ZealSearchResult &r) const { + const bool lhsStartsWithQuery = name.startsWith(query, Qt::CaseInsensitive); + const bool rhsStartsWithQuery = r.name.startsWith(query, Qt::CaseInsensitive); + + if (lhsStartsWithQuery != rhsStartsWithQuery) { + return lhsStartsWithQuery > rhsStartsWithQuery; + } + + const int namesCmp = QString::compare(name, r.name, Qt::CaseInsensitive); + if (namesCmp) { + return namesCmp < 0; + } + + return QString::compare(name, r.name, Qt::CaseInsensitive) < 0; } diff --git a/zeal/zealsearchresult.h b/zeal/zealsearchresult.h index d5a0388..2581d3b 100644 --- a/zeal/zealsearchresult.h +++ b/zeal/zealsearchresult.h @@ -6,23 +6,25 @@ class ZealSearchResult { public: - ZealSearchResult(const QString& name_, const QString& parentName_, const QString& path_, const QString& docset_, const QString& query_); - const QString& getName() const { return name; }; - const QString& getParentName() const { return parentName; }; - const QString& getPath() const { return path; }; - const QString& getDocsetName() const { return docset; }; - bool operator<(const ZealSearchResult& r) const { - if (name.toLower().startsWith(query.toLower()) > r.name.toLower().startsWith(r.query.toLower())) { - // return results that are prefixed with query first - return true; - } else if (name.toLower().startsWith(query.toLower()) < r.name.toLower().startsWith(r.query.toLower())) { - return false; - } else { - return name.toLower() < r.name.toLower() || - (name.toLower() == r.name.toLower() - && parentName.toLower() < r.parentName.toLower()); - } - } + + ZealSearchResult(); + + ZealSearchResult(const QString &name_, + const QString &parentName_, + const QString &path_, + const QString &docset_, + const QString &query_); + + QString getName() const { return name; } + + QString getParentName() const { return parentName; } + + QString getPath() const { return path; } + + QString getDocsetName() const { return docset; } + + bool operator<(const ZealSearchResult& r) const; + private: QString name; QString parentName;