Use efficient algorithm for removing entries

PR #19351.
This commit is contained in:
Chocobo1 2023-07-23 12:54:31 +08:00 committed by GitHub
parent 938adca47d
commit 766fce82b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -62,11 +62,10 @@ public:
{ {
const QDateTime now = QDateTime::currentDateTime(); const QDateTime now = QDateTime::currentDateTime();
QList<QNetworkCookie> cookies = Preferences::instance()->getNetworkCookies(); QList<QNetworkCookie> cookies = Preferences::instance()->getNetworkCookies();
for (const QNetworkCookie &cookie : asConst(Preferences::instance()->getNetworkCookies())) cookies.erase(std::remove_if(cookies.begin(), cookies.end(), [&now](const QNetworkCookie &cookie)
{ {
if (cookie.isSessionCookie() || (cookie.expirationDate() <= now)) return cookie.isSessionCookie() || (cookie.expirationDate() <= now);
cookies.removeAll(cookie); }), cookies.end());
}
setAllCookies(cookies); setAllCookies(cookies);
} }
@ -75,11 +74,10 @@ public:
{ {
const QDateTime now = QDateTime::currentDateTime(); const QDateTime now = QDateTime::currentDateTime();
QList<QNetworkCookie> cookies = allCookies(); QList<QNetworkCookie> cookies = allCookies();
for (const QNetworkCookie &cookie : asConst(allCookies())) cookies.erase(std::remove_if(cookies.begin(), cookies.end(), [&now](const QNetworkCookie &cookie)
{ {
if (cookie.isSessionCookie() || (cookie.expirationDate() <= now)) return cookie.isSessionCookie() || (cookie.expirationDate() <= now);
cookies.removeAll(cookie); }), cookies.end());
}
Preferences::instance()->setNetworkCookies(cookies); Preferences::instance()->setNetworkCookies(cookies);
} }
@ -91,11 +89,10 @@ public:
{ {
const QDateTime now = QDateTime::currentDateTime(); const QDateTime now = QDateTime::currentDateTime();
QList<QNetworkCookie> cookies = QNetworkCookieJar::cookiesForUrl(url); QList<QNetworkCookie> cookies = QNetworkCookieJar::cookiesForUrl(url);
for (const QNetworkCookie &cookie : asConst(QNetworkCookieJar::cookiesForUrl(url))) cookies.erase(std::remove_if(cookies.begin(), cookies.end(), [&now](const QNetworkCookie &cookie)
{ {
if (!cookie.isSessionCookie() && (cookie.expirationDate() <= now)) return !cookie.isSessionCookie() && (cookie.expirationDate() <= now);
cookies.removeAll(cookie); }), cookies.end());
}
return cookies; return cookies;
} }
@ -104,11 +101,10 @@ public:
{ {
const QDateTime now = QDateTime::currentDateTime(); const QDateTime now = QDateTime::currentDateTime();
QList<QNetworkCookie> cookies = cookieList; QList<QNetworkCookie> cookies = cookieList;
for (const QNetworkCookie &cookie : cookieList) cookies.erase(std::remove_if(cookies.begin(), cookies.end(), [&now](const QNetworkCookie &cookie)
{ {
if (!cookie.isSessionCookie() && (cookie.expirationDate() <= now)) return !cookie.isSessionCookie() && (cookie.expirationDate() <= now);
cookies.removeAll(cookie); }), cookies.end());
}
return QNetworkCookieJar::setCookiesFromUrl(cookies, url); return QNetworkCookieJar::setCookiesFromUrl(cookies, url);
} }