diff options
Diffstat (limited to 'src/widgets/util/qcompleter.cpp')
-rw-r--r-- | src/widgets/util/qcompleter.cpp | 47 |
1 files changed, 32 insertions, 15 deletions
diff --git a/src/widgets/util/qcompleter.cpp b/src/widgets/util/qcompleter.cpp index 5a31eb4e52..e22f0e7f28 100644 --- a/src/widgets/util/qcompleter.cpp +++ b/src/widgets/util/qcompleter.cpp @@ -490,18 +490,25 @@ QMatchData QCompletionEngine::filterHistory() } // Returns a match hint from the cache by chopping the search string -bool QCompletionEngine::matchHint(QString part, const QModelIndex& parent, QMatchData *hint) +bool QCompletionEngine::matchHint(const QString &part, const QModelIndex &parent, QMatchData *hint) const { - if (c->cs == Qt::CaseInsensitive) - part = std::move(part).toLower(); + if (part.isEmpty()) + return false; // early out to avoid cache[parent] lookup costs - const CacheItem& map = cache[parent]; + const auto cit = cache.find(parent); + if (cit == cache.end()) + return false; + + const CacheItem& map = *cit; + const auto mapEnd = map.end(); + + QString key = c->cs == Qt::CaseInsensitive ? part.toLower() : part; - QString key = part; while (!key.isEmpty()) { key.chop(1); - if (map.contains(key)) { - *hint = map[key]; + const auto it = map.find(key); + if (it != mapEnd) { + *hint = *it; return true; } } @@ -509,15 +516,25 @@ bool QCompletionEngine::matchHint(QString part, const QModelIndex& parent, QMatc return false; } -bool QCompletionEngine::lookupCache(QString part, const QModelIndex& parent, QMatchData *m) +bool QCompletionEngine::lookupCache(const QString &part, const QModelIndex &parent, QMatchData *m) const { - if (c->cs == Qt::CaseInsensitive) - part = std::move(part).toLower(); - const CacheItem& map = cache[parent]; - if (!map.contains(part)) - return false; - *m = map[part]; - return true; + if (part.isEmpty()) + return false; // early out to avoid cache[parent] lookup costs + + const auto cit = cache.find(parent); + if (cit == cache.end()) + return false; + + const CacheItem& map = *cit; + + const QString key = c->cs == Qt::CaseInsensitive ? part.toLower() : part; + + const auto it = map.find(key); + if (it == map.end()) + return false; + + *m = it.value(); + return true; } // When the cache size exceeds 1MB, it clears out about 1/2 of the cache. |