summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-03-31 14:12:50 +0200
committerLars Knoll <lars.knoll@qt.io>2020-04-15 14:38:05 +0200
commitcb1000ea02a42de0f79ff8db97219a9923d3efdf (patch)
treea8fe635c88a2870ec12aa7ef9aba576a863ce6fd
parent4b37abc0c9c1a0e1bdb182e725e31daaf610dc7a (diff)
Add WildcardConversionOptions to QRegularExpression
There are cases, where the conversion from a wildcard pattern to a regular expression should not lead to an anchored pattern. Allow this, but adding an optional second argument to wildcardToRegularExpression, that allows tuning the conversion. Change-Id: Ida7a32d65ee49bf58d5f8d9906c0a0cd8954a02a Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r--src/corelib/text/qregularexpression.cpp29
-rw-r--r--src/corelib/text/qregularexpression.h12
2 files changed, 33 insertions, 8 deletions
diff --git a/src/corelib/text/qregularexpression.cpp b/src/corelib/text/qregularexpression.cpp
index 77aad5e294..cff4c1870c 100644
--- a/src/corelib/text/qregularexpression.cpp
+++ b/src/corelib/text/qregularexpression.cpp
@@ -1901,12 +1901,27 @@ QString QRegularExpression::escape(QStringView str)
#if QT_STRINGVIEW_LEVEL < 2
/*!
\since 5.12
- \fn QString QRegularExpression::wildcardToRegularExpression(const QString &pattern)
+ \fn QString QRegularExpression::wildcardToRegularExpression(const QString &pattern, WildcardConversionType type)
\overload
*/
#endif // QT_STRINGVIEW_LEVEL < 2
/*!
+ \since 6.0
+ \enum QRegularExpression::WildcardConversionOption
+
+ The WildcardConversionOption enum defines modifiers to the way a wildcard glob
+ pattern gets converted to a regular expression pattern.
+
+ \value DefaultWildcardConversion
+ No conversion options are set.
+
+ \value UnanchoredWildcardConversion
+ The conversion will not anchor the pattern. This allows for partial string matches of
+ wildcard expressions.
+*/
+
+/*!
\since 5.15
Returns a regular expression representation of the given glob \a pattern.
@@ -1916,9 +1931,10 @@ QString QRegularExpression::escape(QStringView str)
\snippet code/src_corelib_tools_qregularexpression.cpp 31
- The returned regular expression is already fully anchored. In other
+ By default, the returned regular expression is fully anchored. In other
words, there is no need of calling anchoredPattern() again on the
- result.
+ result. To get an a regular expression that is not anchored, pass
+ UnanchoredWildcardConversion as the conversion \a option.
\warning Unlike QRegExp, this implementation follows closely the definition
of wildcard for glob patterns:
@@ -1956,7 +1972,7 @@ QString QRegularExpression::escape(QStringView str)
\sa escape()
*/
-QString QRegularExpression::wildcardToRegularExpression(QStringView pattern)
+QString QRegularExpression::wildcardToRegularExpression(QStringView pattern, WildcardConversionOptions options)
{
const int wclen = pattern.length();
QString rx;
@@ -2031,7 +2047,10 @@ QString QRegularExpression::wildcardToRegularExpression(QStringView pattern)
}
}
- return anchoredPattern(rx);
+ if (!(options & UnanchoredWildcardConversion))
+ rx = anchoredPattern(rx);
+
+ return rx;
}
#if QT_STRINGVIEW_LEVEL < 2
diff --git a/src/corelib/text/qregularexpression.h b/src/corelib/text/qregularexpression.h
index aa6c48bf54..cfb7f34d9d 100644
--- a/src/corelib/text/qregularexpression.h
+++ b/src/corelib/text/qregularexpression.h
@@ -138,15 +138,21 @@ public:
void optimize() const;
+ enum WildcardConversionOption {
+ DefaultWildcardConversion = 0x0,
+ UnanchoredWildcardConversion = 0x1
+ };
+ Q_DECLARE_FLAGS(WildcardConversionOptions, WildcardConversionOption)
+
#if QT_STRINGVIEW_LEVEL < 2
static QString escape(const QString &str)
{
return escape(qToStringViewIgnoringNull(str));
}
- static QString wildcardToRegularExpression(const QString &str)
+ static QString wildcardToRegularExpression(const QString &str, WildcardConversionOptions options = DefaultWildcardConversion)
{
- return wildcardToRegularExpression(qToStringViewIgnoringNull(str));
+ return wildcardToRegularExpression(qToStringViewIgnoringNull(str), options);
}
static inline QString anchoredPattern(const QString &expression)
@@ -156,7 +162,7 @@ public:
#endif
static QString escape(QStringView str);
- static QString wildcardToRegularExpression(QStringView str);
+ static QString wildcardToRegularExpression(QStringView str, WildcardConversionOptions options = DefaultWildcardConversion);
static QString anchoredPattern(QStringView expression);
bool operator==(const QRegularExpression &re) const;