mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-01-23 21:14:33 +00:00
Use QRegularExpression instead of deprecated QRegExp
Now it follows closely the definition of wildcard for glob patterns. The backslash (\) character is not an escape char in this context. In order to match one of the special characters, place it in square brackets (for example, [?]).
This commit is contained in:
parent
ea1c4a8fc8
commit
61d2ff359b
@ -79,6 +79,7 @@
|
|||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QLocalServer>
|
#include <QLocalServer>
|
||||||
#include <QLocalSocket>
|
#include <QLocalSocket>
|
||||||
|
#include <QRegularExpression>
|
||||||
|
|
||||||
#include "base/utils/misc.h"
|
#include "base/utils/misc.h"
|
||||||
|
|
||||||
@ -108,7 +109,7 @@ QtLocalPeer::QtLocalPeer(QObject* parent, const QString &appId)
|
|||||||
#endif
|
#endif
|
||||||
prefix = id.section(QLatin1Char('/'), -1);
|
prefix = id.section(QLatin1Char('/'), -1);
|
||||||
}
|
}
|
||||||
prefix.remove(QRegExp("[^a-zA-Z]"));
|
prefix.remove(QRegularExpression("[^a-zA-Z]"));
|
||||||
prefix.truncate(6);
|
prefix.truncate(6);
|
||||||
|
|
||||||
QByteArray idc = id.toUtf8();
|
QByteArray idc = id.toUtf8();
|
||||||
|
@ -213,10 +213,8 @@ QRegularExpression AutoDownloadRule::cachedRegex(const QString &expression, cons
|
|||||||
QRegularExpression ®ex = m_dataPtr->cachedRegexes[expression];
|
QRegularExpression ®ex = m_dataPtr->cachedRegexes[expression];
|
||||||
if (regex.pattern().isEmpty())
|
if (regex.pattern().isEmpty())
|
||||||
{
|
{
|
||||||
regex = QRegularExpression
|
const QString pattern = (isRegex ? expression : Utils::String::wildcardToRegexPattern(expression));
|
||||||
{
|
regex = QRegularExpression {pattern, QRegularExpression::CaseInsensitiveOption};
|
||||||
(isRegex ? expression : Utils::String::wildcardToRegex(expression))
|
|
||||||
, QRegularExpression::CaseInsensitiveOption};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return regex;
|
return regex;
|
||||||
|
@ -34,7 +34,7 @@
|
|||||||
#include <QGlobalStatic>
|
#include <QGlobalStatic>
|
||||||
#include <QHash>
|
#include <QHash>
|
||||||
#include <QMetaObject>
|
#include <QMetaObject>
|
||||||
#include <QRegExp>
|
#include <QRegularExpression>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
#include <QXmlStreamEntityResolver>
|
#include <QXmlStreamEntityResolver>
|
||||||
@ -391,12 +391,13 @@ namespace
|
|||||||
int nmin = 8;
|
int nmin = 8;
|
||||||
int nsec = 9;
|
int nsec = 9;
|
||||||
// Also accept obsolete form "Weekday, DD-Mon-YY HH:MM:SS ±hhmm"
|
// Also accept obsolete form "Weekday, DD-Mon-YY HH:MM:SS ±hhmm"
|
||||||
QRegExp rx("^(?:([A-Z][a-z]+),\\s*)?(\\d{1,2})(\\s+|-)([^-\\s]+)(\\s+|-)(\\d{2,4})\\s+(\\d\\d):(\\d\\d)(?::(\\d\\d))?\\s+(\\S+)$");
|
QRegularExpression rx {"^(?:([A-Z][a-z]+),\\s*)?(\\d{1,2})(\\s+|-)([^-\\s]+)(\\s+|-)(\\d{2,4})\\s+(\\d\\d):(\\d\\d)(?::(\\d\\d))?\\s+(\\S+)$"};
|
||||||
|
QRegularExpressionMatch rxMatch;
|
||||||
QStringList parts;
|
QStringList parts;
|
||||||
if (!str.indexOf(rx))
|
if (str.indexOf(rx, 0, &rxMatch) == 0)
|
||||||
{
|
{
|
||||||
// Check that if date has '-' separators, both separators are '-'.
|
// Check that if date has '-' separators, both separators are '-'.
|
||||||
parts = rx.capturedTexts();
|
parts = rxMatch.capturedTexts();
|
||||||
const bool h1 = (parts[3] == QLatin1String("-"));
|
const bool h1 = (parts[3] == QLatin1String("-"));
|
||||||
const bool h2 = (parts[5] == QLatin1String("-"));
|
const bool h2 = (parts[5] == QLatin1String("-"));
|
||||||
if (h1 != h2)
|
if (h1 != h2)
|
||||||
@ -405,9 +406,10 @@ namespace
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Check for the obsolete form "Wdy Mon DD HH:MM:SS YYYY"
|
// Check for the obsolete form "Wdy Mon DD HH:MM:SS YYYY"
|
||||||
rx = QRegExp("^([A-Z][a-z]+)\\s+(\\S+)\\s+(\\d\\d)\\s+(\\d\\d):(\\d\\d):(\\d\\d)\\s+(\\d\\d\\d\\d)$");
|
rx = QRegularExpression {"^([A-Z][a-z]+)\\s+(\\S+)\\s+(\\d\\d)\\s+(\\d\\d):(\\d\\d):(\\d\\d)\\s+(\\d\\d\\d\\d)$"};
|
||||||
if (str.indexOf(rx))
|
if (str.indexOf(rx, 0, &rxMatch) != 0)
|
||||||
return QDateTime::currentDateTime();
|
return QDateTime::currentDateTime();
|
||||||
|
|
||||||
nyear = 7;
|
nyear = 7;
|
||||||
nmonth = 2;
|
nmonth = 2;
|
||||||
nday = 3;
|
nday = 3;
|
||||||
@ -415,7 +417,7 @@ namespace
|
|||||||
nhour = 4;
|
nhour = 4;
|
||||||
nmin = 5;
|
nmin = 5;
|
||||||
nsec = 6;
|
nsec = 6;
|
||||||
parts = rx.capturedTexts();
|
parts = rxMatch.capturedTexts();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ok[4];
|
bool ok[4];
|
||||||
@ -463,11 +465,11 @@ namespace
|
|||||||
bool negOffset = false;
|
bool negOffset = false;
|
||||||
if (parts.count() > 10)
|
if (parts.count() > 10)
|
||||||
{
|
{
|
||||||
rx = QRegExp("^([+-])(\\d\\d)(\\d\\d)$");
|
rx = QRegularExpression {"^([+-])(\\d\\d)(\\d\\d)$"};
|
||||||
if (!parts[10].indexOf(rx))
|
if (parts[10].indexOf(rx, 0, &rxMatch) == 0)
|
||||||
{
|
{
|
||||||
// It's a UTC offset ±hhmm
|
// It's a UTC offset ±hhmm
|
||||||
parts = rx.capturedTexts();
|
parts = rxMatch.capturedTexts();
|
||||||
offset = parts[2].toInt(&ok[0]) * 3600;
|
offset = parts[2].toInt(&ok[0]) * 3600;
|
||||||
const int offsetMin = parts[3].toInt(&ok[1]);
|
const int offsetMin = parts[3].toInt(&ok[1]);
|
||||||
if (!ok[0] || !ok[1] || offsetMin > 59)
|
if (!ok[0] || !ok[1] || offsetMin > 59)
|
||||||
|
@ -33,10 +33,15 @@
|
|||||||
|
|
||||||
#include <QCollator>
|
#include <QCollator>
|
||||||
#include <QLocale>
|
#include <QLocale>
|
||||||
#include <QRegExp>
|
|
||||||
#include <QtGlobal>
|
#include <QtGlobal>
|
||||||
#include <QVector>
|
#include <QVector>
|
||||||
|
|
||||||
|
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
|
||||||
|
#include <QRegularExpression>
|
||||||
|
#else
|
||||||
|
#include <QRegExp>
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(Q_OS_MACOS) || defined(__MINGW32__)
|
#if defined(Q_OS_MACOS) || defined(__MINGW32__)
|
||||||
#define QBT_USES_QTHREADSTORAGE
|
#define QBT_USES_QTHREADSTORAGE
|
||||||
#include <QThreadStorage>
|
#include <QThreadStorage>
|
||||||
@ -181,14 +186,21 @@ QString Utils::String::fromDouble(const double n, const int precision)
|
|||||||
return QLocale::system().toString(std::floor(n * prec) / prec, 'f', precision);
|
return QLocale::system().toString(std::floor(n * prec) / prec, 'f', precision);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
|
||||||
|
QString Utils::String::wildcardToRegexPattern(const QString &pattern)
|
||||||
|
{
|
||||||
|
return QRegularExpression::wildcardToRegularExpression(pattern, QRegularExpression::UnanchoredWildcardConversion);
|
||||||
|
}
|
||||||
|
#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
|
||||||
// copy the code from QRegExp::wc2rx().
|
// copy the code from QRegExp::wc2rx().
|
||||||
QString qt_regexp_toCanonical(const QString &pattern, QRegExp::PatternSyntax patternSyntax);
|
QString qt_regexp_toCanonical(const QString &pattern, QRegExp::PatternSyntax patternSyntax);
|
||||||
|
|
||||||
QString Utils::String::wildcardToRegex(const QString &pattern)
|
QString Utils::String::wildcardToRegexPattern(const QString &pattern)
|
||||||
{
|
{
|
||||||
return qt_regexp_toCanonical(pattern, QRegExp::Wildcard);
|
return qt_regexp_toCanonical(pattern, QRegExp::Wildcard);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
std::optional<bool> Utils::String::parseBool(const QString &string)
|
std::optional<bool> Utils::String::parseBool(const QString &string)
|
||||||
{
|
{
|
||||||
|
@ -50,7 +50,7 @@ namespace Utils::String
|
|||||||
return (naturalCompare(left, right, caseSensitivity) < 0);
|
return (naturalCompare(left, right, caseSensitivity) < 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString wildcardToRegex(const QString &pattern);
|
QString wildcardToRegexPattern(const QString &pattern);
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T unquote(const T &str, const QString "es = QChar('"'))
|
T unquote(const T &str, const QString "es = QChar('"'))
|
||||||
|
@ -825,7 +825,8 @@ void PropertiesWidget::filteredFilesChanged()
|
|||||||
|
|
||||||
void PropertiesWidget::filterText(const QString &filter)
|
void PropertiesWidget::filterText(const QString &filter)
|
||||||
{
|
{
|
||||||
m_propListModel->setFilterRegExp(QRegExp(filter, Qt::CaseInsensitive, QRegExp::WildcardUnix));
|
const QString pattern = Utils::String::wildcardToRegexPattern(filter);
|
||||||
|
m_propListModel->setFilterRegularExpression(QRegularExpression(pattern, QRegularExpression::CaseInsensitiveOption));
|
||||||
if (filter.isEmpty())
|
if (filter.isEmpty())
|
||||||
{
|
{
|
||||||
m_ui->filesList->collapseAll();
|
m_ui->filesList->collapseAll();
|
||||||
|
@ -719,10 +719,14 @@ void AutomatedRssDownloader::updateMustLineValidity()
|
|||||||
{
|
{
|
||||||
QStringList tokens;
|
QStringList tokens;
|
||||||
if (isRegex)
|
if (isRegex)
|
||||||
|
{
|
||||||
tokens << text;
|
tokens << text;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
for (const QString &token : asConst(text.split('|')))
|
for (const QString &token : asConst(text.split('|')))
|
||||||
tokens << Utils::String::wildcardToRegex(token);
|
tokens << Utils::String::wildcardToRegexPattern(token);
|
||||||
|
}
|
||||||
|
|
||||||
for (const QString &token : asConst(tokens))
|
for (const QString &token : asConst(tokens))
|
||||||
{
|
{
|
||||||
@ -762,10 +766,14 @@ void AutomatedRssDownloader::updateMustNotLineValidity()
|
|||||||
{
|
{
|
||||||
QStringList tokens;
|
QStringList tokens;
|
||||||
if (isRegex)
|
if (isRegex)
|
||||||
|
{
|
||||||
tokens << text;
|
tokens << text;
|
||||||
|
}
|
||||||
else
|
else
|
||||||
|
{
|
||||||
for (const QString &token : asConst(text.split('|')))
|
for (const QString &token : asConst(text.split('|')))
|
||||||
tokens << Utils::String::wildcardToRegex(token);
|
tokens << Utils::String::wildcardToRegexPattern(token);
|
||||||
|
}
|
||||||
|
|
||||||
for (const QString &token : asConst(tokens))
|
for (const QString &token : asConst(tokens))
|
||||||
{
|
{
|
||||||
|
@ -365,9 +365,9 @@ void SearchJobWidget::fillFilterComboBoxes()
|
|||||||
|
|
||||||
void SearchJobWidget::filterSearchResults(const QString &name)
|
void SearchJobWidget::filterSearchResults(const QString &name)
|
||||||
{
|
{
|
||||||
const QRegExp::PatternSyntax patternSyntax = Preferences::instance()->getRegexAsFilteringPatternForSearchJob()
|
const QString pattern = (Preferences::instance()->getRegexAsFilteringPatternForSearchJob()
|
||||||
? QRegExp::RegExp : QRegExp::WildcardUnix;
|
? name : Utils::String::wildcardToRegexPattern(name));
|
||||||
m_proxyModel->setFilterRegExp(QRegExp(name, Qt::CaseInsensitive, patternSyntax));
|
m_proxyModel->setFilterRegularExpression(QRegularExpression(pattern, QRegularExpression::CaseInsensitiveOption));
|
||||||
updateResultsCount();
|
updateResultsCount();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -129,7 +129,7 @@ bool TorrentContentFilterModel::hasFiltered(const QModelIndex &folder) const
|
|||||||
// this should be called only with folders
|
// this should be called only with folders
|
||||||
// check if the folder name itself matches the filter string
|
// check if the folder name itself matches the filter string
|
||||||
QString name = folder.data().toString();
|
QString name = folder.data().toString();
|
||||||
if (name.contains(filterRegExp()))
|
if (name.contains(filterRegularExpression()))
|
||||||
return true;
|
return true;
|
||||||
for (int child = 0; child < m_model->rowCount(folder); ++child)
|
for (int child = 0; child < m_model->rowCount(folder); ++child)
|
||||||
{
|
{
|
||||||
@ -141,7 +141,7 @@ bool TorrentContentFilterModel::hasFiltered(const QModelIndex &folder) const
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
name = childIndex.data().toString();
|
name = childIndex.data().toString();
|
||||||
if (name.contains(filterRegExp()))
|
if (name.contains(filterRegularExpression()))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,7 +36,6 @@
|
|||||||
#include <QHeaderView>
|
#include <QHeaderView>
|
||||||
#include <QMenu>
|
#include <QMenu>
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
#include <QRegExp>
|
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
#include <QSet>
|
#include <QSet>
|
||||||
#include <QShortcut>
|
#include <QShortcut>
|
||||||
@ -1131,9 +1130,9 @@ void TransferListWidget::applyTrackerFilter(const QSet<BitTorrent::TorrentID> &t
|
|||||||
|
|
||||||
void TransferListWidget::applyNameFilter(const QString &name)
|
void TransferListWidget::applyNameFilter(const QString &name)
|
||||||
{
|
{
|
||||||
const QRegExp::PatternSyntax patternSyntax = Preferences::instance()->getRegexAsFilteringPatternForTransferList()
|
const QString pattern = (Preferences::instance()->getRegexAsFilteringPatternForTransferList()
|
||||||
? QRegExp::RegExp : QRegExp::WildcardUnix;
|
? name : Utils::String::wildcardToRegexPattern(name));
|
||||||
m_sortFilterModel->setFilterRegExp(QRegExp(name, Qt::CaseInsensitive, patternSyntax));
|
m_sortFilterModel->setFilterRegularExpression(QRegularExpression(pattern, QRegularExpression::CaseInsensitiveOption));
|
||||||
}
|
}
|
||||||
|
|
||||||
void TransferListWidget::applyStatusFilter(int f)
|
void TransferListWidget::applyStatusFilter(int f)
|
||||||
|
@ -38,7 +38,7 @@
|
|||||||
#include <QMimeDatabase>
|
#include <QMimeDatabase>
|
||||||
#include <QMimeType>
|
#include <QMimeType>
|
||||||
#include <QNetworkCookie>
|
#include <QNetworkCookie>
|
||||||
#include <QRegExp>
|
#include <QRegularExpression>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
|
|
||||||
#include "base/algorithm.h"
|
#include "base/algorithm.h"
|
||||||
@ -693,7 +693,7 @@ bool WebApplication::validateHostHeader(const QStringList &domains) const
|
|||||||
// try matching host header with domain list
|
// try matching host header with domain list
|
||||||
for (const auto &domain : domains)
|
for (const auto &domain : domains)
|
||||||
{
|
{
|
||||||
QRegExp domainRegex(domain, Qt::CaseInsensitive, QRegExp::Wildcard);
|
const QRegularExpression domainRegex {Utils::String::wildcardToRegexPattern(domain), QRegularExpression::CaseInsensitiveOption};
|
||||||
if (requestHost.contains(domainRegex))
|
if (requestHost.contains(domainRegex))
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user