Performance improvement
Igor Mironchik
igor.mironchik at gmail.com
Thu May 22 17:21:03 BST 2025
Hi.
I'd like to ask you, guys, maybe I'm a dumb a little, and you know how
to optimize a following function.
inline long long int
skipIf(long long int startPos, const QString &line,
const std::function<bool(const QChar &)> &pred,
long long int endPos = -1)
{
const auto length = (endPos < line.length() && endPos > -1 ? endPos
: line.length());
while (startPos < length && pred(line[startPos])) {
++startPos;
}
return startPos;
}
This function is needed to walk through the string with some predicate.
It's needed in some cases. I know that QString::indexOf(QChar) is very
efficient, but consider I need to skip all QChar::isSpace()...
QString::indexOf() won't help in this situation, but skipIf() with
predicate is very helpful here. For example.
inline long long int
skipSpaces(long long int i, const QString &line)
{
return skipIf(i, line, [](const QChar &ch){ return ch.isSpace(); });
}
Do you know a way to optimize it?
Thank you.
More information about the kde-devel
mailing list