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 ProStringList &exports = project->values("QMAKE_EXTRA_VARIABLES");
for (ProStringList::ConstIterator exp_it = exports.begin(); exp_it != exports.end(); ++exp_it) {
auto pattern = QRegularExpression::wildcardToRegularExpression((*exp_it).toQString());
QRegularExpression rx(pattern, QRegularExpression::CaseInsensitiveOption);
auto rx = QRegularExpression::fromWildcard((*exp_it).toQString(), Qt::CaseInsensitive);
for (ProValueMap::ConstIterator it = vars.begin(); it != vars.end(); ++it) {
if (rx.match(it.key().toQString()).hasMatch())
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) {
bool increment = false;
for (ProStringList::ConstIterator incrit = incrs.begin(); incrit != incrs.end(); ++incrit) {
auto pattern =
QRegularExpression::wildcardToRegularExpression((*incrit).toQString(),
QRegularExpression::UnanchoredWildcardConversion);
if ((*objit).toQString().contains(QRegularExpression(pattern))) {
auto regexp = QRegularExpression::fromWildcard((*incrit).toQString(), Qt::CaseSensitive,
QRegularExpression::UnanchoredWildcardConversion);
if ((*objit).toQString().contains(regexp)) {
increment = true;
incrs_out.append((*objit));
break;

View File

@ -1636,7 +1636,7 @@ bool QMakeEvaluator::isActiveConfig(const QStringRef &config, bool regex)
return m_hostBuild;
if (regex && (config.contains(QLatin1Char('*')) || config.contains(QLatin1Char('?')))) {
QRegularExpression re(QRegularExpression::wildcardToRegularExpression(config.toString()));
auto re = QRegularExpression::fromWildcard(config.toString());
// mkspecs
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) {
// Insensitive exact match
QRegularExpression rx(QRegularExpression::wildcardToRegularExpression(*sit),
QRegularExpression::CaseInsensitiveOption);
auto rx = QRegularExpression::fromWildcard(*sit, Qt::CaseInsensitive);
if (rx.match(fileName).hasMatch())
return true;
}

View File

@ -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;

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
#if QT_CONFIG(regularexpression)
QRegularExpression rx(QRegularExpression::wildcardToRegularExpression(m_pattern));
auto rx = QRegularExpression::fromWildcard(m_pattern);
return rx.match(filename).hasMatch();
#else
return false;

View File

@ -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)

View File

@ -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); }

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;