Utils: Fix matching wildcards to full paths

Fixes: QTCREATORBUG-24792
Change-Id: I82b4edea6260b07e1bdff065b157a4cd044ec629
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Christian Stenger
2020-10-16 13:38:57 +02:00
parent 9f6eb972b7
commit 6c5eaed92c
4 changed files with 75 additions and 2 deletions

View File

@@ -393,4 +393,68 @@ QString formatElapsedTime(qint64 elapsed)
return QCoreApplication::translate("StringUtils", "Elapsed time: %1.").arg(time);
}
/*
* Basically QRegularExpression::wildcardToRegularExpression(), but let wildcards match
* path separators as well
*/
QString wildcardToRegularExpression(const QString &original)
{
const qsizetype wclen = original.size();
QString rx;
rx.reserve(wclen + wclen / 16);
qsizetype i = 0;
const QChar *wc = original.data();
const QLatin1String starEscape(".*");
const QLatin1String questionMarkEscape(".");
while (i < wclen) {
const QChar c = wc[i++];
switch (c.unicode()) {
case '*':
rx += starEscape;
break;
case '?':
rx += questionMarkEscape;
break;
case '\\':
case '$':
case '(':
case ')':
case '+':
case '.':
case '^':
case '{':
case '|':
case '}':
rx += QLatin1Char('\\');
rx += c;
break;
case '[':
rx += c;
// Support for the [!abc] or [!a-c] syntax
if (i < wclen) {
if (wc[i] == QLatin1Char('!')) {
rx += QLatin1Char('^');
++i;
}
if (i < wclen && wc[i] == QLatin1Char(']'))
rx += wc[i++];
while (i < wclen && wc[i] != QLatin1Char(']')) {
if (wc[i] == QLatin1Char('\\'))
rx += QLatin1Char('\\');
rx += wc[i++];
}
}
break;
default:
rx += c;
break;
}
}
return QRegularExpression::anchoredPattern(rx);
}
} // namespace Utils