Skip to content

Commit

Permalink
Use std if (#545)
Browse files Browse the repository at this point in the history
* Use std library find algorithm

Also avoids mixing const and non-const iterators.

* Use initializer list
  • Loading branch information
luis-pereira authored May 3, 2024
1 parent a8d1e67 commit 36ba355
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions lib/kprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
Expand All @@ -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)
Expand Down

0 comments on commit 36ba355

Please sign in to comment.