From a2c85bffbeaa027e98fb6c23b2d7919adc8d28b7 Mon Sep 17 00:00:00 2001 From: Samuel Gaist Date: Thu, 19 Jul 2018 00:21:49 +0200 Subject: Migrate QDir to use QRegularExpression The match method still uses QRegExp. This patch updates the code to use QRegularExpression and translates the wildcard patterns to a suitable form for QRegularExpression. [ChangeLog][Core][QDir] QDir now uses QRegularExpression internally for wildcard matching. Note that QRegularExpression might not give the exact same result as QRegExp as its implementation follows strictly the glob patterns definition for wildcard expressions. Nevertheless, the tests for QDir return the same results as before. Change-Id: I095959443ac7362f7534e35454eff038061fca82 Reviewed-by: Ulf Hermann --- src/corelib/io/qdir.cpp | 24 ++++++++++++++++-------- src/corelib/io/qdir.h | 2 +- tests/auto/corelib/io/qdir/tst_qdir.cpp | 1 - 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp index 7c0a48f8f2..85ea2cb139 100644 --- a/src/corelib/io/qdir.cpp +++ b/src/corelib/io/qdir.cpp @@ -48,7 +48,9 @@ #include "qdiriterator.h" #include "qdatetime.h" #include "qstring.h" -#include "qregexp.h" +#if QT_CONFIG(regularexpression) +# include +#endif #include "qvector.h" #include "qvarlengtharray.h" #include "qfilesystementry_p.h" @@ -1038,7 +1040,7 @@ QStringList QDir::nameFilters() const list of filters specified by \a nameFilters. Each name filter is a wildcard (globbing) filter that understands - \c{*} and \c{?} wildcards. (See \l{QRegExp wildcard matching}.) + \c{*} and \c{?} wildcards. (See \l{QRegularExpression wildcard matching}.) For example, the following code sets three name filters on a QDir to ensure that only files with extensions typically used for C++ @@ -2110,7 +2112,7 @@ QString QDir::rootPath() return QFileSystemEngine::rootPath(); } -#ifndef QT_NO_REGEXP +#if QT_CONFIG(regularexpression) /*! \overload @@ -2118,13 +2120,18 @@ QString QDir::rootPath() patterns in the list of \a filters; otherwise returns \c false. The matching is case insensitive. - \sa {QRegExp wildcard matching}, QRegExp::exactMatch(), entryList(), entryInfoList() + \sa {QRegularExpression Wildcard matching}, QRegularExpression::wildcardToRegularExpression(), + entryList(), entryInfoList() */ bool QDir::match(const QStringList &filters, const QString &fileName) { for (QStringList::ConstIterator sit = filters.constBegin(); sit != filters.constEnd(); ++sit) { - QRegExp rx(*sit, Qt::CaseInsensitive, QRegExp::Wildcard); - if (rx.exactMatch(fileName)) + QString wildcard = QRegularExpression::wildcardToRegularExpression(*sit); + // Insensitive exact match + // (see Notes for QRegExp Users in QRegularExpression's documentation) + QRegularExpression rx(QLatin1String("\\A(?:") + wildcard + QLatin1String(")\\z"), + QRegularExpression::CaseInsensitiveOption); + if (rx.match(fileName).hasMatch()) return true; } return false; @@ -2136,13 +2143,14 @@ bool QDir::match(const QStringList &filters, const QString &fileName) contain multiple patterns separated by spaces or semicolons. The matching is case insensitive. - \sa {QRegExp wildcard matching}, QRegExp::exactMatch(), entryList(), entryInfoList() + \sa {QRegularExpression wildcard matching}, QRegularExpression::wildcardToRegularExpression, + entryList(), entryInfoList() */ bool QDir::match(const QString &filter, const QString &fileName) { return match(nameFiltersFromString(filter), fileName); } -#endif // QT_NO_REGEXP +#endif // QT_CONFIG(regularexpression) /*! \internal diff --git a/src/corelib/io/qdir.h b/src/corelib/io/qdir.h index 950a26f327..45c59d9e1d 100644 --- a/src/corelib/io/qdir.h +++ b/src/corelib/io/qdir.h @@ -206,7 +206,7 @@ public: static inline QDir temp() { return QDir(tempPath()); } static QString tempPath(); -#ifndef QT_NO_REGEXP +#if QT_CONFIG(regularexpression) static bool match(const QStringList &filters, const QString &fileName); static bool match(const QString &filter, const QString &fileName); #endif diff --git a/tests/auto/corelib/io/qdir/tst_qdir.cpp b/tests/auto/corelib/io/qdir/tst_qdir.cpp index afa15fe895..30f0e447ad 100644 --- a/tests/auto/corelib/io/qdir/tst_qdir.cpp +++ b/tests/auto/corelib/io/qdir/tst_qdir.cpp @@ -33,7 +33,6 @@ #include #include #include -#include #include #if defined(Q_OS_WIN) -- cgit v1.2.3