From 36ba355f67dcb21b3f0e99316d4a1e17d8aa17f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Pereira?= Date: Fri, 3 May 2024 17:36:40 +0100 Subject: [PATCH] Use std if (#545) * Use std library find algorithm Also avoids mixing const and non-const iterators. * Use initializer list --- lib/kprocess.cpp | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/lib/kprocess.cpp b/lib/kprocess.cpp index 6e8406ec..e2ab2430 100644 --- a/lib/kprocess.cpp +++ b/lib/kprocess.cpp @@ -73,7 +73,7 @@ void KProcess::setNextOpenMode(QIODevice::OpenMode mode) void KProcess::clearEnvironment() { - setEnvironment(QStringList() << QString::fromLatin1(DUMMYENV)); + setEnvironment(QStringList{QString::fromLatin1(DUMMYENV)}); } void KProcess::setEnv(const QString &name, const QString &value, bool overwrite) @@ -85,14 +85,16 @@ void KProcess::setEnv(const QString &name, const QString &value, bool overwrite) } QString fname(name); fname.append(QLatin1Char('=')); - for (QStringList::Iterator it = env.begin(); it != env.end(); ++it) - if ((*it).startsWith(fname)) { - if (overwrite) { - *it = fname.append(value); - setEnvironment(env); - } - return; + auto it = std::find_if(env.begin(), env.end(), [&fname](const QString &s) { + return s.startsWith(fname); + }); + if (it != env.end()) { + if (overwrite) { + *it = fname.append(value); + setEnvironment(env); } + return; + } env.append(fname.append(value)); setEnvironment(env); } @@ -106,14 +108,16 @@ void KProcess::unsetEnv(const QString &name) } QString fname(name); fname.append(QLatin1Char('=')); - for (QStringList::Iterator it = env.begin(); it != env.end(); ++it) - if ((*it).startsWith(fname)) { - env.erase(it); - if (env.isEmpty()) - env.append(QString::fromLatin1(DUMMYENV)); - setEnvironment(env); - return; + auto it = std::find_if(env.begin(), env.end(), [&fname](const QString &s) { + return s.startsWith(fname); + }); + if (it != env.end()) { + env.erase(it); + if (env.isEmpty()) { + env.append(QString::fromLatin1(DUMMYENV)); } + setEnvironment(env); + } } void KProcess::setProgram(const QString &exe, const QStringList &args)