From b84333f8a1d3568b3cae00f7077ad738ed79aa41 Mon Sep 17 00:00:00 2001 From: vlakoff Date: Wed, 18 May 2022 07:33:14 +0200 Subject: [PATCH] Consider brackets within wildcard as regular characters In glob patterns, square brackets have a special meaning, that may be unexpected by the users. Thus we escape these brackets, so that the only remaining special characters are the * and ? wildcards. PR #16965. --- src/base/utils/string.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/base/utils/string.cpp b/src/base/utils/string.cpp index 01e7d3fb8..f7c0049ca 100644 --- a/src/base/utils/string.cpp +++ b/src/base/utils/string.cpp @@ -32,11 +32,10 @@ #include #include +#include #include -#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) -#include -#else +#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) #include #endif @@ -56,7 +55,11 @@ QString Utils::String::fromDouble(const double n, const int precision) #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) QString Utils::String::wildcardToRegexPattern(const QString &pattern) { - return QRegularExpression::wildcardToRegularExpression(pattern, QRegularExpression::UnanchoredWildcardConversion); + // replace [ and ] with [[] and []], respectively + QString escapedPattern = pattern; + escapedPattern.replace(QRegularExpression(u"\\[|\\]"_qs), u"[\\0]"_qs); + + return QRegularExpression::wildcardToRegularExpression(escapedPattern, QRegularExpression::UnanchoredWildcardConversion); } #else // This is marked as internal in QRegExp.cpp, but is exported. The alternative would be to @@ -65,7 +68,11 @@ QString qt_regexp_toCanonical(const QString &pattern, QRegExp::PatternSyntax pat QString Utils::String::wildcardToRegexPattern(const QString &pattern) { - return qt_regexp_toCanonical(pattern, QRegExp::Wildcard); + // replace [ and ] with [[] and []], respectively + QString escapedPattern = pattern; + escapedPattern.replace(QRegularExpression(u"\\[|\\]"_qs), u"[\\0]"_qs); + + return qt_regexp_toCanonical(escapedPattern, QRegExp::Wildcard); } #endif