Commit 37cf8836 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Replace all usages of the deprecated qBinaryFind

Where possible qBinaryFind is replaced with std::binary_search. All
other occurrences are replaced with a custom replacement.

I think using this replacement is much more convenient and results
in better readable and less errorprone code than replacing all usages of
qBinaryFind with std::lower_bound() and the necessary additional check
of the resulting iterator, in particular if a lambda expression is used
as custom comparison function.
parent 16733a65
......@@ -17,6 +17,7 @@
#include "kleo/predicates.h"
#include "kleo/keyfiltermanager.h"
#include "kleo/keyfilter.h"
#include "utils/algorithm.h"
#include "utils/formatting.h"
#ifdef KLEO_MODEL_TEST
......@@ -732,8 +733,8 @@ QList<QModelIndex> FlatKeyListModel::doAddKeys(const std::vector<Key> &keys)
void FlatKeyListModel::doRemoveKey(const Key &key)
{
const std::vector<Key>::iterator it
= qBinaryFind(mKeysByFingerprint.begin(), mKeysByFingerprint.end(),
key, _detail::ByFingerprint<std::less>());
= Kleo::binary_find(mKeysByFingerprint.begin(), mKeysByFingerprint.end(),
key, _detail::ByFingerprint<std::less>());
if (it == mKeysByFingerprint.end()) {
return;
}
......@@ -895,8 +896,8 @@ QModelIndex HierarchicalKeyListModel::parent(const QModelIndex &idx) const
return {};
}
const std::vector<Key>::const_iterator it
= qBinaryFind(mKeysByFingerprint.begin(), mKeysByFingerprint.end(),
cleanChainID(key), _detail::ByFingerprint<std::less>());
= Kleo::binary_find(mKeysByFingerprint.begin(), mKeysByFingerprint.end(),
cleanChainID(key), _detail::ByFingerprint<std::less>());
return it != mKeysByFingerprint.end() ? index(*it) : QModelIndex();
}
......@@ -975,7 +976,8 @@ void HierarchicalKeyListModel::addKeyWithParent(const char *issuer_fpr, const Ke
Q_EMIT dataChanged(createIndex(row, 0, const_cast<char *>(issuer_fpr)), createIndex(row, NumColumns - 1, const_cast<char *>(issuer_fpr)));
} else {
// doesn't exist -> insert
const std::vector<Key>::const_iterator pos = qBinaryFind(mKeysByFingerprint.begin(), mKeysByFingerprint.end(), issuer_fpr, _detail::ByFingerprint<std::less>());
const std::vector<Key>::const_iterator pos = Kleo::binary_find(mKeysByFingerprint.begin(), mKeysByFingerprint.end(),
issuer_fpr, _detail::ByFingerprint<std::less>());
Q_ASSERT(pos != mKeysByFingerprint.end());
beginInsertRows(index(*pos), row, row);
subjects.insert(it, key);
......@@ -1039,7 +1041,7 @@ static std::vector<Key> topological_sort(const std::vector<Key> &keys)
continue;
}
const std::vector<Key>::const_iterator it
= qBinaryFind(keys.begin(), keys.end(), issuer_fpr, _detail::ByFingerprint<std::less>());
= Kleo::binary_find(keys.begin(), keys.end(), issuer_fpr, _detail::ByFingerprint<std::less>());
if (it == keys.end()) {
continue;
}
......@@ -1089,7 +1091,7 @@ QList<QModelIndex> HierarchicalKeyListModel::doAddKeys(const std::vector<Key> &k
continue;
}
const bool keyAlreadyExisted = qBinaryFind(oldKeys.begin(), oldKeys.end(), key, _detail::ByFingerprint<std::less>()) != oldKeys.end();
const bool keyAlreadyExisted = std::binary_search(oldKeys.begin(), oldKeys.end(), key, _detail::ByFingerprint<std::less>());
const Map::iterator it = mKeysByNonExistingParent.find(fpr);
const std::vector<Key> children = it != mKeysByNonExistingParent.end() ? it->second : std::vector<Key>();
......@@ -1104,11 +1106,11 @@ QList<QModelIndex> HierarchicalKeyListModel::doAddKeys(const std::vector<Key> &k
auto lastFP = mKeysByFingerprint.begin();
for (const Key &k : qAsConst(children)) {
last = qBinaryFind(last, mTopLevels.end(), k, _detail::ByFingerprint<std::less>());
last = Kleo::binary_find(last, mTopLevels.end(), k, _detail::ByFingerprint<std::less>());
Q_ASSERT(last != mTopLevels.end());
const int row = std::distance(mTopLevels.begin(), last);
lastFP = qBinaryFind(lastFP, mKeysByFingerprint.end(), k, _detail::ByFingerprint<std::less>());
lastFP = Kleo::binary_find(lastFP, mKeysByFingerprint.end(), k, _detail::ByFingerprint<std::less>());
Q_ASSERT(lastFP != mKeysByFingerprint.end());
Q_EMIT rowAboutToBeMoved(QModelIndex(), row);
......@@ -1176,8 +1178,8 @@ void HierarchicalKeyListModel::doRemoveKey(const Key &key)
if (mKeysByExistingParent.find(fpr) != mKeysByExistingParent.end()) {
//handle non-leave nodes:
std::vector<Key> keys = mKeysByFingerprint;
const std::vector<Key>::iterator it = qBinaryFind(keys.begin(), keys.end(),
key, _detail::ByFingerprint<std::less>());
const std::vector<Key>::iterator it = Kleo::binary_find(keys.begin(), keys.end(),
key, _detail::ByFingerprint<std::less>());
if (it == keys.end()) {
return;
}
......@@ -1191,8 +1193,8 @@ void HierarchicalKeyListModel::doRemoveKey(const Key &key)
//handle leave nodes:
const std::vector<Key>::iterator it = qBinaryFind(mKeysByFingerprint.begin(), mKeysByFingerprint.end(),
key, _detail::ByFingerprint<std::less>());
const std::vector<Key>::iterator it = Kleo::binary_find(mKeysByFingerprint.begin(), mKeysByFingerprint.end(),
key, _detail::ByFingerprint<std::less>());
Q_ASSERT(it != mKeysByFingerprint.end());
Q_ASSERT(mKeysByNonExistingParent.find(fpr) == mKeysByNonExistingParent.end());
......@@ -1203,7 +1205,8 @@ void HierarchicalKeyListModel::doRemoveKey(const Key &key)
const char *const issuer_fpr = cleanChainID(key);
const std::vector<Key>::iterator tlIt = qBinaryFind(mTopLevels.begin(), mTopLevels.end(), key, _detail::ByFingerprint<std::less>());
const std::vector<Key>::iterator tlIt = Kleo::binary_find(mTopLevels.begin(), mTopLevels.end(),
key, _detail::ByFingerprint<std::less>());
if (tlIt != mTopLevels.end()) {
mTopLevels.erase(tlIt);
}
......@@ -1211,7 +1214,8 @@ void HierarchicalKeyListModel::doRemoveKey(const Key &key)
if (issuer_fpr && *issuer_fpr) {
const Map::iterator nexIt = mKeysByNonExistingParent.find(issuer_fpr);
if (nexIt != mKeysByNonExistingParent.end()) {
const std::vector<Key>::iterator eit = qBinaryFind(nexIt->second.begin(), nexIt->second.end(), key, _detail::ByFingerprint<std::less>());
const std::vector<Key>::iterator eit = Kleo::binary_find(nexIt->second.begin(), nexIt->second.end(),
key, _detail::ByFingerprint<std::less>());
if (eit != nexIt->second.end()) {
nexIt->second.erase(eit);
}
......@@ -1222,7 +1226,8 @@ void HierarchicalKeyListModel::doRemoveKey(const Key &key)
const Map::iterator exIt = mKeysByExistingParent.find(issuer_fpr);
if (exIt != mKeysByExistingParent.end()) {
const std::vector<Key>::iterator eit = qBinaryFind(exIt->second.begin(), exIt->second.end(), key, _detail::ByFingerprint<std::less>());
const std::vector<Key>::iterator eit = Kleo::binary_find(exIt->second.begin(), exIt->second.end(),
key, _detail::ByFingerprint<std::less>());
if (eit != exIt->second.end()) {
exIt->second.erase(eit);
}
......
/*
utils/algorithm.h
This file is part of libkleopatra, the KDE keymanagement library
SPDX-FileCopyrightText: 2021 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef LIBKLEO_ALGORITHM_H
#define LIBKLEO_ALGORITHM_H
#include <algorithm>
namespace Kleo
{
template<typename ForwardIterator, typename T>
ForwardIterator binary_find(ForwardIterator first, ForwardIterator last, const T &value)
{
const ForwardIterator it = std::lower_bound(first, last, value);
return (it == last || value < *it) ? last : it;
}
template<typename ForwardIterator, typename T, typename Compare>
ForwardIterator binary_find(ForwardIterator first, ForwardIterator last, const T &value, Compare comp)
{
const ForwardIterator it = std::lower_bound(first, last, value, comp);
return (it == last || comp(value, *it)) ? last : it;
}
}
#endif // LIBKLEO_ALGORITHM_H
......@@ -11,12 +11,12 @@
#include "libkleo_debug.h"
#include "kleo/checksumdefinition.h"
#include "utils/algorithm.h"
#include <QString>
#include <QStringList>
#include <QFile>
#include <QFileInfo>
#include <QtAlgorithms>
#include <QByteArrayMatcher>
#include <QMap>
#include <QRegularExpression>
......@@ -158,9 +158,9 @@ unsigned int Kleo::classify(const QStringList &fileNames)
static unsigned int classifyExtension(const QFileInfo &fi)
{
const _classification *const it = qBinaryFind(std::begin(classifications), std::end(classifications),
fi.suffix().toLatin1().constData(),
ByExtension<std::less>());
const _classification *const it = Kleo::binary_find(std::begin(classifications), std::end(classifications),
fi.suffix().toLatin1().constData(),
ByExtension<std::less>());
if (it != std::end(classifications))
if (!(it->classification & ExamineContentHint)) {
return it->classification;
......@@ -233,8 +233,9 @@ static unsigned int classifyContentInteral(const QByteArray &data)
}
const _content_classification *const cit
= qBinaryFind(std::begin(content_classifications), std::end(content_classifications),
data.data() + pos, ByContent<std::less>(epos - pos));
= Kleo::binary_find(std::begin(content_classifications), std::end(content_classifications),
data.data() + pos,
ByContent<std::less>(epos - pos));
if (cit != std::end(content_classifications)) {
return cit->classification | (pgp ? Kleo::Class::OpenPGP : Kleo::Class::CMS);
......@@ -350,9 +351,9 @@ QString Kleo::outputFileName(const QString &inputFileName)
{
const QFileInfo fi(inputFileName);
if (qBinaryFind(std::begin(classifications), std::end(classifications),
fi.suffix().toLatin1().constData(),
ByExtension<std::less>()) == std::end(classifications)) {
if (!std::binary_search(std::begin(classifications), std::end(classifications),
fi.suffix().toLatin1().constData(),
ByExtension<std::less>())) {
return inputFileName + QLatin1String(".out");
} else {
return chopped(inputFileName, 4);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment