mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-07-10 23:42:46 -07:00
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.
This commit is contained in:
parent
acdd08e9a2
commit
b84333f8a1
1 changed files with 12 additions and 5 deletions
|
@ -32,11 +32,10 @@
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
#include <QLocale>
|
#include <QLocale>
|
||||||
|
#include <QRegularExpression>
|
||||||
#include <QVector>
|
#include <QVector>
|
||||||
|
|
||||||
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
|
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||||
#include <QRegularExpression>
|
|
||||||
#else
|
|
||||||
#include <QRegExp>
|
#include <QRegExp>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -56,7 +55,11 @@ QString Utils::String::fromDouble(const double n, const int precision)
|
||||||
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
|
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
|
||||||
QString Utils::String::wildcardToRegexPattern(const QString &pattern)
|
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
|
#else
|
||||||
// This is marked as internal in QRegExp.cpp, but is exported. The alternative would be to
|
// 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)
|
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
|
#endif
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue