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>
This commit is contained in:
parent
4b37abc0c9
commit
cb1000ea02
@ -1901,11 +1901,26 @@ QString QRegularExpression::escape(QStringView str)
|
|||||||
#if QT_STRINGVIEW_LEVEL < 2
|
#if QT_STRINGVIEW_LEVEL < 2
|
||||||
/*!
|
/*!
|
||||||
\since 5.12
|
\since 5.12
|
||||||
\fn QString QRegularExpression::wildcardToRegularExpression(const QString &pattern)
|
\fn QString QRegularExpression::wildcardToRegularExpression(const QString &pattern, WildcardConversionType type)
|
||||||
\overload
|
\overload
|
||||||
*/
|
*/
|
||||||
#endif // QT_STRINGVIEW_LEVEL < 2
|
#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
|
\since 5.15
|
||||||
|
|
||||||
@ -1916,9 +1931,10 @@ QString QRegularExpression::escape(QStringView str)
|
|||||||
|
|
||||||
\snippet code/src_corelib_tools_qregularexpression.cpp 31
|
\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
|
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
|
\warning Unlike QRegExp, this implementation follows closely the definition
|
||||||
of wildcard for glob patterns:
|
of wildcard for glob patterns:
|
||||||
@ -1956,7 +1972,7 @@ QString QRegularExpression::escape(QStringView str)
|
|||||||
|
|
||||||
\sa escape()
|
\sa escape()
|
||||||
*/
|
*/
|
||||||
QString QRegularExpression::wildcardToRegularExpression(QStringView pattern)
|
QString QRegularExpression::wildcardToRegularExpression(QStringView pattern, WildcardConversionOptions options)
|
||||||
{
|
{
|
||||||
const int wclen = pattern.length();
|
const int wclen = pattern.length();
|
||||||
QString rx;
|
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
|
#if QT_STRINGVIEW_LEVEL < 2
|
||||||
|
@ -138,15 +138,21 @@ public:
|
|||||||
|
|
||||||
void optimize() const;
|
void optimize() const;
|
||||||
|
|
||||||
|
enum WildcardConversionOption {
|
||||||
|
DefaultWildcardConversion = 0x0,
|
||||||
|
UnanchoredWildcardConversion = 0x1
|
||||||
|
};
|
||||||
|
Q_DECLARE_FLAGS(WildcardConversionOptions, WildcardConversionOption)
|
||||||
|
|
||||||
#if QT_STRINGVIEW_LEVEL < 2
|
#if QT_STRINGVIEW_LEVEL < 2
|
||||||
static QString escape(const QString &str)
|
static QString escape(const QString &str)
|
||||||
{
|
{
|
||||||
return escape(qToStringViewIgnoringNull(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)
|
static inline QString anchoredPattern(const QString &expression)
|
||||||
@ -156,7 +162,7 @@ public:
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
static QString escape(QStringView str);
|
static QString escape(QStringView str);
|
||||||
static QString wildcardToRegularExpression(QStringView str);
|
static QString wildcardToRegularExpression(QStringView str, WildcardConversionOptions options = DefaultWildcardConversion);
|
||||||
static QString anchoredPattern(QStringView expression);
|
static QString anchoredPattern(QStringView expression);
|
||||||
|
|
||||||
bool operator==(const QRegularExpression &re) const;
|
bool operator==(const QRegularExpression &re) const;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user