QRegularExpression: anchor wildcard pattern

The current implementation of wildcardToRegularExpression doesn't
anchor the pattern which makes it not narrow enough for globbing
patterns. This patch fixes that by applying anchoredPattern before
returning the wildcard pattern.

[ChangeLog][QtCore][QRegularExpression] The wildcardToRegularExpression
method now returns a properly anchored pattern.

Change-Id: I7bee73389d408cf42499652e4fb854517a8125b5
Fixes: QTBUG-72539
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Samuel Gaist 2018-12-14 20:59:55 +01:00
parent e80bf655e9
commit 649ee12aba
4 changed files with 40 additions and 42 deletions

View File

@ -2127,10 +2127,9 @@ QString QDir::rootPath()
bool QDir::match(const QStringList &filters, const QString &fileName)
{
for (QStringList::ConstIterator sit = filters.constBegin(); sit != filters.constEnd(); ++sit) {
QString wildcard = QRegularExpression::wildcardToRegularExpression(*sit);
// Insensitive exact match
// (see Notes for QRegExp Users in QRegularExpression's documentation)
QRegularExpression rx(QRegularExpression::anchoredPattern(wildcard),
QRegularExpression rx(QRegularExpression::wildcardToRegularExpression(*sit),
QRegularExpression::CaseInsensitiveOption);
if (rx.match(fileName).hasMatch())
return true;

View File

@ -1992,7 +1992,7 @@ QString QRegularExpression::wildcardToRegularExpression(const QString &pattern)
}
}
return rx;
return anchoredPattern(rx);
}
/*!

View File

@ -2032,8 +2032,7 @@ bool QFileSystemModelPrivate::passNameFilters(const QFileSystemNode *node) const
: QRegularExpression::CaseInsensitiveOption;
for (const auto &nameFilter : nameFilters) {
const QString wildcard = QRegularExpression::wildcardToRegularExpression(nameFilter);
QRegularExpression rx(QRegularExpression::anchoredPattern(wildcard), options);
QRegularExpression rx(QRegularExpression::wildcardToRegularExpression(nameFilter), options);
QRegularExpressionMatch match = rx.match(node->fileName);
if (match.hasMatch())
return true;

View File

@ -2169,47 +2169,47 @@ void tst_QRegularExpression::wildcard_data()
addRow("*.html", "test.html", 0);
addRow("*.html", "test.htm", -1);
addRow("bar*", "foobarbaz", 3);
addRow("*bar*", "foobarbaz", 0);
addRow("*", "Qt Rocks!", 0);
addRow(".html", "test.html", 4);
addRow(".h", "test.cpp", -1);
addRow(".???l", "test.html", 4);
addRow("?", "test.html", 0);
addRow("?m", "test.html", 6);
addRow("[*]", "test.html", -1);
addRow("[?]","test.html", -1);
addRow("[?]","test.h?ml", 6);
addRow("[[]","test.h[ml", 6);
addRow("[]]","test.h]ml", 6);
addRow(".h[a-z]ml", "test.html", 4);
addRow(".h[A-Z]ml", "test.html", -1);
addRow(".h[A-Z]ml", "test.hTml", 4);
addRow(".h[!A-Z]ml", "test.hTml", -1);
addRow(".h[!A-Z]ml", "test.html", 4);
addRow(".h[!T]ml", "test.hTml", -1);
addRow(".h[!T]ml", "test.html", 4);
addRow(".h[!T]m[!L]", "test.htmL", -1);
addRow(".h[!T]m[!L]", "test.html", 4);
addRow(".h[][!]", "test.h]ml", 4);
addRow(".h[][!]", "test.h[ml", 4);
addRow(".h[][!]", "test.h!ml", 4);
addRow("*.html", "test.html", 0);
addRow("*.h", "test.cpp", -1);
addRow("*.???l", "test.html", 0);
addRow("*?", "test.html", 0);
addRow("*?ml", "test.html", 0);
addRow("*[*]", "test.html", -1);
addRow("*[?]","test.html", -1);
addRow("*[?]ml","test.h?ml", 0);
addRow("*[[]ml","test.h[ml", 0);
addRow("*[]]ml","test.h]ml", 0);
addRow("*.h[a-z]ml", "test.html", 0);
addRow("*.h[A-Z]ml", "test.html", -1);
addRow("*.h[A-Z]ml", "test.hTml", 0);
addRow("*.h[!A-Z]ml", "test.hTml", -1);
addRow("*.h[!A-Z]ml", "test.html", 0);
addRow("*.h[!T]ml", "test.hTml", -1);
addRow("*.h[!T]ml", "test.html", 0);
addRow("*.h[!T]m[!L]", "test.htmL", -1);
addRow("*.h[!T]m[!L]", "test.html", 0);
addRow("*.h[][!]ml", "test.h]ml", 0);
addRow("*.h[][!]ml", "test.h[ml", 0);
addRow("*.h[][!]ml", "test.h!ml", 0);
addRow("foo/*/bar", "Qt/foo/baz/bar", 3);
addRow("foo/(*)/bar", "Qt/foo/baz/bar", -1);
addRow("foo/(*)/bar", "Qt/foo/(baz)/bar", 3);
addRow("foo/?/bar", "Qt/foo/Q/bar", 3);
addRow("foo/?/bar", "Qt/foo/Qt/bar", -1);
addRow("foo/(?)/bar", "Qt/foo/Q/bar", -1);
addRow("foo/(?)/bar", "Qt/foo/(Q)/bar", 3);
addRow("foo/*/bar", "foo/baz/bar", 0);
addRow("foo/(*)/bar", "foo/baz/bar", -1);
addRow("foo/(*)/bar", "foo/(baz)/bar", 0);
addRow("foo/?/bar", "foo/Q/bar", 0);
addRow("foo/?/bar", "foo/Qt/bar", -1);
addRow("foo/(?)/bar", "foo/Q/bar", -1);
addRow("foo/(?)/bar", "foo/(Q)/bar", 0);
#ifdef Q_OS_WIN
addRow("foo\\*\\bar", "Qt\\foo\\baz\\bar", 3);
addRow("foo\\(*)\\bar", "Qt\\foo\\baz\\bar", -1);
addRow("foo\\(*)\\bar", "Qt\\foo\\(baz)\\bar", 3);
addRow("foo\\?\\bar", "Qt\\foo\\Q\\bar", 3);
addRow("foo\\?\\bar", "Qt\\foo\\Qt\\bar", -1);
addRow("foo\\(?)\\bar", "Qt\\foo\\Q\\bar", -1);
addRow("foo\\(?)\\bar", "Qt\\foo\\(Q)\\bar", 3);
addRow("foo\\*\\bar", "foo\\baz\\bar", 0);
addRow("foo\\(*)\\bar", "foo\\baz\\bar", -1);
addRow("foo\\(*)\\bar", "foo\\(baz)\\bar", 0);
addRow("foo\\?\\bar", "foo\\Q\\bar", 0);
addRow("foo\\?\\bar", "foo\\Qt\\bar", -1);
addRow("foo\\(?)\\bar", "foo\\Q\\bar", -1);
addRow("foo\\(?)\\bar", "foo\\(Q)\\bar", 0);
#endif
}