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>
This commit is contained in:
Lars Knoll 2020-04-15 10:52:51 +02:00
parent 56a7984a90
commit adf829e65d
11 changed files with 36 additions and 21 deletions

View File

@ -2169,8 +2169,7 @@ MakefileGenerator::writeExtraVariables(QTextStream &t)
const ProValueMap &vars = project->variables(); const ProValueMap &vars = project->variables();
const ProStringList &exports = project->values("QMAKE_EXTRA_VARIABLES"); const ProStringList &exports = project->values("QMAKE_EXTRA_VARIABLES");
for (ProStringList::ConstIterator exp_it = exports.begin(); exp_it != exports.end(); ++exp_it) { for (ProStringList::ConstIterator exp_it = exports.begin(); exp_it != exports.end(); ++exp_it) {
auto pattern = QRegularExpression::wildcardToRegularExpression((*exp_it).toQString()); auto rx = QRegularExpression::fromWildcard((*exp_it).toQString(), Qt::CaseInsensitive);
QRegularExpression rx(pattern, QRegularExpression::CaseInsensitiveOption);
for (ProValueMap::ConstIterator it = vars.begin(); it != vars.end(); ++it) { for (ProValueMap::ConstIterator it = vars.begin(); it != vars.end(); ++it) {
if (rx.match(it.key().toQString()).hasMatch()) if (rx.match(it.key().toQString()).hasMatch())
outlist << ("EXPORT_" + it.key() + " = " + it.value().join(' ')); outlist << ("EXPORT_" + it.key() + " = " + it.value().join(' '));

View File

@ -1533,10 +1533,9 @@ std::pair<bool, QString> UnixMakefileGenerator::writeObjectsPart(QTextStream &t,
for (ProStringList::ConstIterator objit = objs.begin(); objit != objs.end(); ++objit) { for (ProStringList::ConstIterator objit = objs.begin(); objit != objs.end(); ++objit) {
bool increment = false; bool increment = false;
for (ProStringList::ConstIterator incrit = incrs.begin(); incrit != incrs.end(); ++incrit) { for (ProStringList::ConstIterator incrit = incrs.begin(); incrit != incrs.end(); ++incrit) {
auto pattern = auto regexp = QRegularExpression::fromWildcard((*incrit).toQString(), Qt::CaseSensitive,
QRegularExpression::wildcardToRegularExpression((*incrit).toQString(), QRegularExpression::UnanchoredWildcardConversion);
QRegularExpression::UnanchoredWildcardConversion); if ((*objit).toQString().contains(regexp)) {
if ((*objit).toQString().contains(QRegularExpression(pattern))) {
increment = true; increment = true;
incrs_out.append((*objit)); incrs_out.append((*objit));
break; break;

View File

@ -1636,7 +1636,7 @@ bool QMakeEvaluator::isActiveConfig(const QStringRef &config, bool regex)
return m_hostBuild; return m_hostBuild;
if (regex && (config.contains(QLatin1Char('*')) || config.contains(QLatin1Char('?')))) { if (regex && (config.contains(QLatin1Char('*')) || config.contains(QLatin1Char('?')))) {
QRegularExpression re(QRegularExpression::wildcardToRegularExpression(config.toString())); auto re = QRegularExpression::fromWildcard(config.toString());
// mkspecs // mkspecs
if (re.match(m_qmakespecName).hasMatch()) if (re.match(m_qmakespecName).hasMatch())

View File

@ -2150,8 +2150,7 @@ bool QDir::match(const QStringList &filters, const QString &fileName)
{ {
for (QStringList::ConstIterator sit = filters.constBegin(); sit != filters.constEnd(); ++sit) { for (QStringList::ConstIterator sit = filters.constBegin(); sit != filters.constEnd(); ++sit) {
// Insensitive exact match // Insensitive exact match
QRegularExpression rx(QRegularExpression::wildcardToRegularExpression(*sit), auto rx = QRegularExpression::fromWildcard(*sit, Qt::CaseInsensitive);
QRegularExpression::CaseInsensitiveOption);
if (rx.match(fileName).hasMatch()) if (rx.match(fileName).hasMatch())
return true; return true;
} }

View File

@ -170,9 +170,9 @@ QDirIteratorPrivate::QDirIteratorPrivate(const QFileSystemEntry &entry, const QS
#if QT_CONFIG(regularexpression) #if QT_CONFIG(regularexpression)
nameRegExps.reserve(nameFilters.size()); nameRegExps.reserve(nameFilters.size());
for (const auto &filter : nameFilters) { for (const auto &filter : nameFilters) {
QString re = QRegularExpression::wildcardToRegularExpression(filter); auto re = QRegularExpression::fromWildcard(filter, (filters & QDir::CaseSensitive ?
nameRegExps.append( Qt::CaseSensitive : Qt::CaseInsensitive));
QRegularExpression(re, (filters & QDir::CaseSensitive) ? QRegularExpression::NoPatternOption : QRegularExpression::CaseInsensitiveOption)); nameRegExps.append(re);
} }
#endif #endif
QFileSystemMetaData metaData; QFileSystemMetaData metaData;

View File

@ -145,7 +145,7 @@ bool QMimeGlobPattern::matchFileName(const QString &inputFilename) const
// Other (quite rare) patterns, like "*.anim[1-9j]": use slow but correct method // Other (quite rare) patterns, like "*.anim[1-9j]": use slow but correct method
#if QT_CONFIG(regularexpression) #if QT_CONFIG(regularexpression)
QRegularExpression rx(QRegularExpression::wildcardToRegularExpression(m_pattern)); auto rx = QRegularExpression::fromWildcard(m_pattern);
return rx.match(filename).hasMatch(); return rx.match(filename).hasMatch();
#else #else
return false; return false;

View File

@ -1935,6 +1935,25 @@ QString QRegularExpression::wildcardToRegularExpression(QStringView pattern, Wil
return rx; 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 #if QT_STRINGVIEW_LEVEL < 2
/*! /*!
\fn QRegularExpression::anchoredPattern(const QString &expression) \fn QRegularExpression::anchoredPattern(const QString &expression)

View File

@ -165,6 +165,9 @@ public:
static QString wildcardToRegularExpression(QStringView str, WildcardConversionOptions options = DefaultWildcardConversion); static QString wildcardToRegularExpression(QStringView str, WildcardConversionOptions options = DefaultWildcardConversion);
static QString anchoredPattern(QStringView expression); static QString anchoredPattern(QStringView expression);
static QRegularExpression fromWildcard(QStringView str, Qt::CaseSensitivity cs = Qt::CaseInsensitive,
WildcardConversionOptions options = DefaultWildcardConversion);
bool operator==(const QRegularExpression &re) const; bool operator==(const QRegularExpression &re) const;
inline bool operator!=(const QRegularExpression &re) const { return !operator==(re); } inline bool operator!=(const QRegularExpression &re) const { return !operator==(re); }

View File

@ -110,8 +110,7 @@ static bool isHostExcluded(CFDictionaryRef dict, const QString &host)
return true; // excluded return true; // excluded
} else { } else {
// do wildcard matching // do wildcard matching
QRegularExpression rx(QRegularExpression::wildcardToRegularExpression(entry), auto rx = QRegularExpression::fromWildcard(entry, Qt::CaseInsensitive);
QRegularExpression::CaseInsensitiveOption);
if (rx.match(host).hasMatch()) if (rx.match(host).hasMatch())
return true; return true;
} }

View File

@ -212,8 +212,7 @@ static bool isBypassed(const QString &host, const QStringList &bypassList)
return true; // excluded return true; // excluded
} else { } else {
// do wildcard matching // do wildcard matching
QRegularExpression rx(QRegularExpression::wildcardToRegularExpression(entry), auto rx = QRegularExpression::fromWildcard(entry, Qt::CaseInsensitive);
QRegularExpression::CaseInsensitiveOption);
if (rx.match(host).hasMatch()) if (rx.match(host).hasMatch())
return true; return true;
} }

View File

@ -2148,12 +2148,10 @@ bool QFileSystemModelPrivate::passNameFilters(const QFileSystemNode *node) const
// Check the name regularexpression filters // Check the name regularexpression filters
if (!(node->isDir() && (filters & QDir::AllDirs))) { if (!(node->isDir() && (filters & QDir::AllDirs))) {
const QRegularExpression::PatternOptions options = auto cs = (filters & QDir::CaseSensitive) ? Qt::CaseSensitive : Qt::CaseInsensitive;
(filters & QDir::CaseSensitive) ? QRegularExpression::NoPatternOption
: QRegularExpression::CaseInsensitiveOption;
for (const auto &nameFilter : nameFilters) { for (const auto &nameFilter : nameFilters) {
QRegularExpression rx(QRegularExpression::wildcardToRegularExpression(nameFilter), options); auto rx = QRegularExpression::fromWildcard(nameFilter, cs);
QRegularExpressionMatch match = rx.match(node->fileName); QRegularExpressionMatch match = rx.match(node->fileName);
if (match.hasMatch()) if (match.hasMatch())
return true; return true;