Make use of std algorithms

This commit is contained in:
Chocobo1
2019-01-11 16:05:57 +08:00
parent 40eb8a1f4a
commit 6d29a3af60
10 changed files with 67 additions and 61 deletions

View File

@@ -3460,18 +3460,18 @@ void Session::handleTorrentTrackerWarning(TorrentHandle *const torrent, const QS
bool Session::hasPerTorrentRatioLimit() const
{
for (TorrentHandle *const torrent : asConst(m_torrents))
if (torrent->ratioLimit() >= 0) return true;
return false;
return std::any_of(m_torrents.cbegin(), m_torrents.cend(), [](const TorrentHandle *torrent)
{
return (torrent->ratioLimit() >= 0);
});
}
bool Session::hasPerTorrentSeedingTimeLimit() const
{
for (TorrentHandle *const torrent : asConst(m_torrents))
if (torrent->seedingTimeLimit() >= 0) return true;
return false;
return std::any_of(m_torrents.cbegin(), m_torrents.cend(), [](const TorrentHandle *torrent)
{
return (torrent->seedingTimeLimit() >= 0);
});
}
void Session::initResumeFolder()

View File

@@ -841,11 +841,10 @@ bool TorrentHandle::hasError() const
bool TorrentHandle::hasFilteredPieces() const
{
const std::vector<int> pp = m_nativeHandle.piece_priorities();
for (const int priority : pp)
if (priority == 0) return true;
return false;
return std::any_of(pp.cbegin(), pp.cend(), [](const int priority)
{
return (priority == 0);
});
}
int TorrentHandle::queuePosition() const

View File

@@ -30,6 +30,8 @@
#include "requestparser.h"
#include <algorithm>
#include <QDebug>
#include <QRegularExpression>
#include <QStringList>
@@ -242,12 +244,10 @@ bool RequestParser::parsePostMessage(const QByteArray &data)
const QByteArray endDelimiter = QByteArray("--") + delimiter + QByteArray("--") + CRLF;
multipart.push_back(viewWithoutEndingWith(multipart.takeLast(), endDelimiter));
for (const auto &part : multipart) {
if (!parseFormData(part))
return false;
}
return true;
return std::all_of(multipart.cbegin(), multipart.cend(), [this](const QByteArray &part)
{
return this->parseFormData(part);
});
}
qWarning() << Q_FUNC_INFO << "unknown content type:" << contentType;

View File

@@ -50,22 +50,16 @@ namespace
QList<QSslCipher> safeCipherList()
{
const QStringList badCiphers = {"idea", "rc4"};
const QList<QSslCipher> allCiphers = QSslSocket::supportedCiphers();
const QStringList badCiphers {"idea", "rc4"};
const QList<QSslCipher> allCiphers {QSslSocket::supportedCiphers()};
QList<QSslCipher> safeCiphers;
for (const QSslCipher &cipher : allCiphers) {
bool isSafe = true;
for (const QString &badCipher : badCiphers) {
if (cipher.name().contains(badCipher, Qt::CaseInsensitive)) {
isSafe = false;
break;
}
}
if (isSafe)
safeCiphers += cipher;
}
std::copy_if(allCiphers.cbegin(), allCiphers.cend(), std::back_inserter(safeCiphers), [&badCiphers](const QSslCipher &cipher)
{
return std::none_of(badCiphers.cbegin(), badCiphers.cend(), [&cipher](const QString &badCipher)
{
return cipher.name().contains(badCipher, Qt::CaseInsensitive);
});
});
return safeCiphers;
}
}

View File

@@ -29,6 +29,8 @@
#include "rss_autodownloadrule.h"
#include <algorithm>
#include <QDebug>
#include <QDir>
#include <QHash>
@@ -237,13 +239,11 @@ bool AutoDownloadRule::matchesMustContainExpression(const QString &articleTitle)
// Each expression is either a regex, or a set of wildcards separated by whitespace.
// Accept if any complete expression matches.
for (const QString &expression : asConst(m_dataPtr->mustContain)) {
return std::any_of(m_dataPtr->mustContain.cbegin(), m_dataPtr->mustContain.cend(), [this, &articleTitle](const QString &expression)
{
// A regex of the form "expr|" will always match, so do the same for wildcards
if (matchesExpression(articleTitle, expression))
return true;
}
return false;
return matchesExpression(articleTitle, expression);
});
}
bool AutoDownloadRule::matchesMustNotContainExpression(const QString &articleTitle) const
@@ -253,13 +253,11 @@ bool AutoDownloadRule::matchesMustNotContainExpression(const QString &articleTit
// Each expression is either a regex, or a set of wildcards separated by whitespace.
// Reject if any complete expression matches.
for (const QString &expression : asConst(m_dataPtr->mustNotContain)) {
return std::none_of(m_dataPtr->mustNotContain.cbegin(), m_dataPtr->mustNotContain.cend(), [this, &articleTitle](const QString &expression)
{
// A regex of the form "expr|" will always match, so do the same for wildcards
if (matchesExpression(articleTitle, expression))
return false;
}
return true;
return matchesExpression(articleTitle, expression);
});
}
bool AutoDownloadRule::matchesEpisodeFilterExpression(const QString &articleTitle) const

View File

@@ -30,6 +30,8 @@
#include "rss_folder.h"
#include <algorithm>
#include <QJsonObject>
#include <QJsonValue>
@@ -69,10 +71,11 @@ QList<Article *> Folder::articles() const
int Folder::unreadCount() const
{
int count = 0;
for (Item *item : asConst(items()))
count += item->unreadCount();
return count;
const auto itemList = items();
return std::accumulate(itemList.cbegin(), itemList.cend(), 0, [](const int acc, const Item *item)
{
return (acc + item->unreadCount());
});
}
void Folder::markAsRead()