diff options
author | Lars Knoll <lars.knoll@qt.io> | 2020-04-15 10:52:51 +0200 |
---|---|---|
committer | Lars Knoll <lars.knoll@qt.io> | 2020-05-06 09:57:40 +0200 |
commit | adf829e65d0837c043dbfdfaba49dd145debe954 (patch) | |
tree | c45291cbe4ff82b9ec2d5f2ae008066b839dfd1a /src | |
parent | 56a7984a90992371cd7f54a061dcc791e6f22cfd (diff) |
Add a QRegularExpression::fromWildcard() convenience method
Simplify constructing QRegularExpression objects from a glob
pattern.
Change-Id: I06f60b1dfea3da969e2474dedd44b6ca5d456d7d
Reviewed-by: Simon Hausmann <hausmann@gmail.com>
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/io/qdir.cpp | 3 | ||||
-rw-r--r-- | src/corelib/io/qdiriterator.cpp | 6 | ||||
-rw-r--r-- | src/corelib/mimetypes/qmimeglobpattern.cpp | 2 | ||||
-rw-r--r-- | src/corelib/text/qregularexpression.cpp | 19 | ||||
-rw-r--r-- | src/corelib/text/qregularexpression.h | 3 | ||||
-rw-r--r-- | src/network/kernel/qnetworkproxy_mac.cpp | 3 | ||||
-rw-r--r-- | src/network/kernel/qnetworkproxy_win.cpp | 3 | ||||
-rw-r--r-- | src/widgets/dialogs/qfilesystemmodel.cpp | 6 |
8 files changed, 31 insertions, 14 deletions
diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp index fa559a22bb..31a3eda5ed 100644 --- a/src/corelib/io/qdir.cpp +++ b/src/corelib/io/qdir.cpp @@ -2150,8 +2150,7 @@ bool QDir::match(const QStringList &filters, const QString &fileName) { for (QStringList::ConstIterator sit = filters.constBegin(); sit != filters.constEnd(); ++sit) { // Insensitive exact match - QRegularExpression rx(QRegularExpression::wildcardToRegularExpression(*sit), - QRegularExpression::CaseInsensitiveOption); + auto rx = QRegularExpression::fromWildcard(*sit, Qt::CaseInsensitive); if (rx.match(fileName).hasMatch()) return true; } diff --git a/src/corelib/io/qdiriterator.cpp b/src/corelib/io/qdiriterator.cpp index d77829e9f3..0c60ca8920 100644 --- a/src/corelib/io/qdiriterator.cpp +++ b/src/corelib/io/qdiriterator.cpp @@ -170,9 +170,9 @@ QDirIteratorPrivate::QDirIteratorPrivate(const QFileSystemEntry &entry, const QS #if QT_CONFIG(regularexpression) nameRegExps.reserve(nameFilters.size()); for (const auto &filter : nameFilters) { - QString re = QRegularExpression::wildcardToRegularExpression(filter); - nameRegExps.append( - QRegularExpression(re, (filters & QDir::CaseSensitive) ? QRegularExpression::NoPatternOption : QRegularExpression::CaseInsensitiveOption)); + auto re = QRegularExpression::fromWildcard(filter, (filters & QDir::CaseSensitive ? + Qt::CaseSensitive : Qt::CaseInsensitive)); + nameRegExps.append(re); } #endif QFileSystemMetaData metaData; diff --git a/src/corelib/mimetypes/qmimeglobpattern.cpp b/src/corelib/mimetypes/qmimeglobpattern.cpp index ad78cd9ffa..6d2a1f5425 100644 --- a/src/corelib/mimetypes/qmimeglobpattern.cpp +++ b/src/corelib/mimetypes/qmimeglobpattern.cpp @@ -145,7 +145,7 @@ bool QMimeGlobPattern::matchFileName(const QString &inputFilename) const // Other (quite rare) patterns, like "*.anim[1-9j]": use slow but correct method #if QT_CONFIG(regularexpression) - QRegularExpression rx(QRegularExpression::wildcardToRegularExpression(m_pattern)); + auto rx = QRegularExpression::fromWildcard(m_pattern); return rx.match(filename).hasMatch(); #else return false; diff --git a/src/corelib/text/qregularexpression.cpp b/src/corelib/text/qregularexpression.cpp index 6b81f5cc79..25e2169b06 100644 --- a/src/corelib/text/qregularexpression.cpp +++ b/src/corelib/text/qregularexpression.cpp @@ -1935,6 +1935,25 @@ QString QRegularExpression::wildcardToRegularExpression(QStringView pattern, Wil return rx; } +/*! + \since 6.0 + Returns a regular expression of the glob pattern \a pattern. + + Equivalent to + \code + auto reOptions = cs == Qt::CaseSensitive ? QRegularExpression::NoPatternOption : + QRegularExpression::CaseInsensitiveOption; + return QRegularExpression(wildcardToRegularExpression(str, options), reOptions); + \endcode +*/ +QRegularExpression QRegularExpression::fromWildcard(QStringView str, Qt::CaseSensitivity cs, + WildcardConversionOptions options) +{ + auto reOptions = cs == Qt::CaseSensitive ? QRegularExpression::NoPatternOption : + QRegularExpression::CaseInsensitiveOption; + return QRegularExpression(wildcardToRegularExpression(str, options), reOptions); +} + #if QT_STRINGVIEW_LEVEL < 2 /*! \fn QRegularExpression::anchoredPattern(const QString &expression) diff --git a/src/corelib/text/qregularexpression.h b/src/corelib/text/qregularexpression.h index cfb7f34d9d..10ff2e94cb 100644 --- a/src/corelib/text/qregularexpression.h +++ b/src/corelib/text/qregularexpression.h @@ -165,6 +165,9 @@ public: static QString wildcardToRegularExpression(QStringView str, WildcardConversionOptions options = DefaultWildcardConversion); static QString anchoredPattern(QStringView expression); + static QRegularExpression fromWildcard(QStringView str, Qt::CaseSensitivity cs = Qt::CaseInsensitive, + WildcardConversionOptions options = DefaultWildcardConversion); + bool operator==(const QRegularExpression &re) const; inline bool operator!=(const QRegularExpression &re) const { return !operator==(re); } diff --git a/src/network/kernel/qnetworkproxy_mac.cpp b/src/network/kernel/qnetworkproxy_mac.cpp index 4c582bfa71..3fcd9299f3 100644 --- a/src/network/kernel/qnetworkproxy_mac.cpp +++ b/src/network/kernel/qnetworkproxy_mac.cpp @@ -110,8 +110,7 @@ static bool isHostExcluded(CFDictionaryRef dict, const QString &host) return true; // excluded } else { // do wildcard matching - QRegularExpression rx(QRegularExpression::wildcardToRegularExpression(entry), - QRegularExpression::CaseInsensitiveOption); + auto rx = QRegularExpression::fromWildcard(entry, Qt::CaseInsensitive); if (rx.match(host).hasMatch()) return true; } diff --git a/src/network/kernel/qnetworkproxy_win.cpp b/src/network/kernel/qnetworkproxy_win.cpp index dffc0adf8f..9dfe32d5f7 100644 --- a/src/network/kernel/qnetworkproxy_win.cpp +++ b/src/network/kernel/qnetworkproxy_win.cpp @@ -212,8 +212,7 @@ static bool isBypassed(const QString &host, const QStringList &bypassList) return true; // excluded } else { // do wildcard matching - QRegularExpression rx(QRegularExpression::wildcardToRegularExpression(entry), - QRegularExpression::CaseInsensitiveOption); + auto rx = QRegularExpression::fromWildcard(entry, Qt::CaseInsensitive); if (rx.match(host).hasMatch()) return true; } diff --git a/src/widgets/dialogs/qfilesystemmodel.cpp b/src/widgets/dialogs/qfilesystemmodel.cpp index 6afdfa6eb9..30d1324248 100644 --- a/src/widgets/dialogs/qfilesystemmodel.cpp +++ b/src/widgets/dialogs/qfilesystemmodel.cpp @@ -2148,12 +2148,10 @@ bool QFileSystemModelPrivate::passNameFilters(const QFileSystemNode *node) const // Check the name regularexpression filters if (!(node->isDir() && (filters & QDir::AllDirs))) { - const QRegularExpression::PatternOptions options = - (filters & QDir::CaseSensitive) ? QRegularExpression::NoPatternOption - : QRegularExpression::CaseInsensitiveOption; + auto cs = (filters & QDir::CaseSensitive) ? Qt::CaseSensitive : Qt::CaseInsensitive; for (const auto &nameFilter : nameFilters) { - QRegularExpression rx(QRegularExpression::wildcardToRegularExpression(nameFilter), options); + auto rx = QRegularExpression::fromWildcard(nameFilter, cs); QRegularExpressionMatch match = rx.match(node->fileName); if (match.hasMatch()) return true; |