From 0f862fcf9f72e743d0234ec1d72fc1fb21b46824 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Fri, 21 Jul 2023 12:18:26 +0800 Subject: [PATCH] Use efficient algorithm for removing entries PR #19345. --- src/base/net/downloadmanager.cpp | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/base/net/downloadmanager.cpp b/src/base/net/downloadmanager.cpp index 82c3fb143..55192ec60 100644 --- a/src/base/net/downloadmanager.cpp +++ b/src/base/net/downloadmanager.cpp @@ -42,6 +42,7 @@ #include #include +#include "base/algorithm.h" #include "base/global.h" #include "base/logger.h" #include "base/preferences.h" @@ -62,11 +63,10 @@ public: { const QDateTime now = QDateTime::currentDateTime(); QList cookies = Preferences::instance()->getNetworkCookies(); - for (const QNetworkCookie &cookie : asConst(Preferences::instance()->getNetworkCookies())) + Algorithm::removeIf(cookies, [&now](const QNetworkCookie &cookie) { - if (cookie.isSessionCookie() || (cookie.expirationDate() <= now)) - cookies.removeAll(cookie); - } + return cookie.isSessionCookie() || (cookie.expirationDate() <= now); + }); setAllCookies(cookies); } @@ -75,11 +75,10 @@ public: { const QDateTime now = QDateTime::currentDateTime(); QList cookies = allCookies(); - for (const QNetworkCookie &cookie : asConst(allCookies())) + Algorithm::removeIf(cookies, [&now](const QNetworkCookie &cookie) { - if (cookie.isSessionCookie() || (cookie.expirationDate() <= now)) - cookies.removeAll(cookie); - } + return cookie.isSessionCookie() || (cookie.expirationDate() <= now); + }); Preferences::instance()->setNetworkCookies(cookies); } @@ -91,11 +90,10 @@ public: { const QDateTime now = QDateTime::currentDateTime(); QList cookies = QNetworkCookieJar::cookiesForUrl(url); - for (const QNetworkCookie &cookie : asConst(QNetworkCookieJar::cookiesForUrl(url))) + Algorithm::removeIf(cookies, [&now](const QNetworkCookie &cookie) { - if (!cookie.isSessionCookie() && (cookie.expirationDate() <= now)) - cookies.removeAll(cookie); - } + return !cookie.isSessionCookie() && (cookie.expirationDate() <= now); + }); return cookies; } @@ -104,11 +102,10 @@ public: { const QDateTime now = QDateTime::currentDateTime(); QList cookies = cookieList; - for (const QNetworkCookie &cookie : cookieList) + Algorithm::removeIf(cookies, [&now](const QNetworkCookie &cookie) { - if (!cookie.isSessionCookie() && (cookie.expirationDate() <= now)) - cookies.removeAll(cookie); - } + return !cookie.isSessionCookie() && (cookie.expirationDate() <= now); + }); return QNetworkCookieJar::setCookiesFromUrl(cookies, url); }