its to sort files comparing numbers if they exist (the files are roughly in the same format, but sometimes they don't have 0-padding), i want file2 before file10, and file2-2 before file2-10
bool compare(const QString &v1, const QString &v2) {
QRegularExpression r("(\\d+)");
auto match1 = r.globalMatch(v1);
auto match2 = r.globalMatch(v2);
while (match1.hasNext() && match2.hasNext()) {
int num1 = match1.next().captured(1).toInt();
int num2 = match2.next().captured(1).toInt();
if (num1 != num2) {
return (num1 < num2);
}
}
return v1 < v2;
}
called from std::sort :
//fileList is a QStringList
std::sort(fileList.begin(), fileList.end(), compare);
The warning is
Don't create temporary QRegularExpression objects. Use a static QRegularExpression object instead [clazy-use-static-qregularexpression] on both lines starting with auto match...
edit: the function does what i want, but i wanna remove the warning
Did you try "not creating temporary QRegularExpression objects. Use a static QRegularExpression object instead"? Every time you call compare you're creating new class for no reason.
How can i not create it? Don't i need a new match every compare? Unless it means QRegularExpression r needs to be static, but the warning shows on match1 and match2 which are QRegularExpressionMatchIterator
Обсуждают сегодня