remove some redundant code

This commit is contained in:
Martin Rotter 2019-04-12 08:11:01 +02:00
parent 8a5c744210
commit f21f366850
7 changed files with 43 additions and 167 deletions

View File

@ -564,7 +564,6 @@ equals(USE_WEBENGINE, true) {
src/network-web/adblock/adblockurlinterceptor.h \
src/network-web/urlinterceptor.h \
src/network-web/networkurlinterceptor.h \
src/miscellaneous/simpleregexp.h \
src/gui/treewidget.h
SOURCES += src/network-web/adblock/adblockaddsubscriptiondialog.cpp \
@ -578,7 +577,6 @@ equals(USE_WEBENGINE, true) {
src/network-web/adblock/adblocktreewidget.cpp \
src/network-web/adblock/adblockurlinterceptor.cpp \
src/network-web/networkurlinterceptor.cpp \
src/miscellaneous/simpleregexp.cpp \
src/gui/treewidget.cpp
FORMS += src/network-web/adblock/adblockaddsubscriptiondialog.ui \

View File

@ -415,7 +415,9 @@ bool DatabaseFactory::sqliteUpdateDatabaseSchema(const QSqlDatabase& database, c
return true;
}
bool DatabaseFactory::mysqlUpdateDatabaseSchema(const QSqlDatabase& database, const QString& source_db_schema_version, const QString& db_name) {
bool DatabaseFactory::mysqlUpdateDatabaseSchema(const QSqlDatabase& database,
const QString& source_db_schema_version,
const QString& db_name) {
int working_version = QString(source_db_schema_version).remove('.').toInt();
const int current_version = QString(APP_DB_SCHEMA_VERSION).remove('.').toInt();
@ -709,8 +711,6 @@ QSqlDatabase DatabaseFactory::sqliteConnection(const QString& connection_name, D
database.setDatabaseName(QSL(":memory:"));
auto aaa = database.driverName();
if (!database.isOpen() && !database.open()) {
qFatal("In-memory SQLite database was NOT opened. Delivered error message: '%s'.",
qPrintable(database.lastError().text()));

View File

@ -36,30 +36,27 @@ QFile* Debugging::targetFileHandle() {
void Debugging::performLog(const char* message, QtMsgType type, const char* file, const char* function, int line) {
const char* type_string = typeToString(type);
std::time_t t = std::time(nullptr);
char mbstr[32];
std::strftime(mbstr, sizeof(mbstr), "%y/%d/%m %H:%M:%S", std::localtime(&t));
QString date_str = QDateTime::currentDateTimeUtc().toString(QSL("yyyy-MM-dd HH:mm:ss.zzz UTC"));
if (instance()->targetFile().isEmpty()) {
// Write to console.
if (file == nullptr || function == nullptr || line < 0) {
fprintf(stderr, "[%s] %s: %s (%s)\n", APP_LOW_NAME, type_string, message, mbstr);
fprintf(stderr, "[%s] %s: %s (%s)\n", APP_LOW_NAME, type_string, message, qPrintable(date_str));
}
else {
fprintf(stderr, "[%s] %s (%s)\n Type: %s\n File: %s (line %d)\n Function: %s\n\n",
APP_LOW_NAME, message, mbstr, type_string, file, line, function);
APP_LOW_NAME, message, qPrintable(date_str), type_string, file, line, function);
}
}
else {
if (file == 0 || function == 0 || line < 0) {
instance()->targetFileHandle()->write(QString("[%1] %2: %3 (%4)\n").arg(APP_LOW_NAME, type_string, message, mbstr).toUtf8());
if (file == nullptr || function == nullptr || line < 0) {
instance()->targetFileHandle()->write(QString("[%1] %2: %3 (%4)\n").arg(APP_LOW_NAME, type_string,
message, qPrintable(date_str)).toUtf8());
}
else {
instance()->targetFileHandle()->write(QString("[%1] %2 (%3)\n Type: %4\n File: %5 (line %6)\n Function: %7\n\n")
.arg(APP_LOW_NAME, message, mbstr, type_string,
.arg(APP_LOW_NAME, message, qPrintable(date_str), type_string,
file, QString::number(line), function).toUtf8());
}

View File

@ -1,75 +0,0 @@
// This file is part of RSS Guard.
//
// Copyright (C) 2011-2017 by Martin Rotter <rotter.martinos@gmail.com>
// Copyright (C) 2010-2014 by David Rosca <nowrep@gmail.com>
//
// RSS Guard is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// RSS Guard is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
#include "miscellaneous/simpleregexp.h"
SimpleRegExp::SimpleRegExp()
: QRegularExpression(QString(), QRegularExpression::DotMatchesEverythingOption), m_matchedLength(-1) {}
SimpleRegExp::SimpleRegExp(const QString& pattern, Qt::CaseSensitivity cs)
: QRegularExpression(pattern, QRegularExpression::DotMatchesEverythingOption), m_matchedLength(-1) {
if (cs == Qt::CaseInsensitive) {
setPatternOptions(patternOptions() | QRegularExpression::CaseInsensitiveOption);
}
}
SimpleRegExp::SimpleRegExp(const SimpleRegExp& re)
: QRegularExpression(re), m_matchedLength(-1) {}
void SimpleRegExp::setMinimal(bool minimal) {
QRegularExpression::PatternOptions opt;
if (minimal) {
opt = patternOptions() | QRegularExpression::InvertedGreedinessOption;
}
else {
opt = patternOptions() & ~QRegularExpression::InvertedGreedinessOption;
}
setPatternOptions(opt);
}
int SimpleRegExp::indexIn(const QString& str, int offset) const {
auto* that = const_cast<SimpleRegExp*>(this);
QRegularExpressionMatch m = match(str, offset);
if (!m.hasMatch()) {
that->m_matchedLength = -1;
that->m_capturedTexts.clear();
return -1;
}
else {
that->m_matchedLength = m.capturedLength();
that->m_capturedTexts = m.capturedTexts();
return m.capturedStart();
}
}
int SimpleRegExp::matchedLength() const {
return m_matchedLength;
}
QString SimpleRegExp::cap(int nth) const {
if (nth >= 0 && m_capturedTexts.size() > nth) {
return m_capturedTexts.at(nth);
}
else {
return QString();
}
}

View File

@ -1,42 +0,0 @@
// This file is part of RSS Guard.
//
// Copyright (C) 2011-2017 by Martin Rotter <rotter.martinos@gmail.com>
// Copyright (C) 2010-2014 by David Rosca <nowrep@gmail.com>
//
// RSS Guard is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// RSS Guard is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with RSS Guard. If not, see <http://www.gnu.org/licenses/>.
#ifndef SIMPLEREGEXP_H
#define SIMPLEREGEXP_H
#include <QRegularExpression>
#include <QStringList>
class SimpleRegExp : public QRegularExpression {
public:
explicit SimpleRegExp();
explicit SimpleRegExp(const QString& pattern, Qt::CaseSensitivity cs = Qt::CaseSensitive);
explicit SimpleRegExp(const SimpleRegExp& re);
void setMinimal(bool minimal);
int indexIn(const QString& str, int offset = 0) const;
int matchedLength() const;
QString cap(int nth = 0) const;
private:
QStringList m_capturedTexts;
int m_matchedLength;
};
#endif // SIMPLEREGEXP_H

View File

@ -49,9 +49,9 @@
#include "network-web/adblock/adblockrule.h"
#include "definitions/definitions.h"
#include "miscellaneous/simpleregexp.h"
#include "network-web/adblock/adblocksubscription.h"
#include <QRegularExpression>
#include <QString>
#include <QStringList>
#include <QUrl>
@ -81,14 +81,10 @@ static QString toSecondLevelDomain(const QUrl& url) {
AdBlockRule::AdBlockRule(const QString& filter, AdBlockSubscription* subscription)
: m_subscription(subscription), m_type(StringContainsMatchRule), m_caseSensitivity(Qt::CaseInsensitive),
m_isEnabled(true), m_isException(false), m_isInternalDisabled(false), m_regExp(0) {
m_isEnabled(true), m_isException(false), m_isInternalDisabled(false), matchers(QList<QStringMatcher>()) {
setFilter(filter);
}
AdBlockRule::~AdBlockRule() {
delete m_regExp;
}
AdBlockRule* AdBlockRule::copy() const {
AdBlockRule* rule = new AdBlockRule();
@ -104,12 +100,7 @@ AdBlockRule* AdBlockRule::copy() const {
rule->m_isInternalDisabled = m_isInternalDisabled;
rule->m_allowedDomains = m_allowedDomains;
rule->m_blockedDomains = m_blockedDomains;
if (m_regExp) {
rule->m_regExp = new RegExp;
rule->m_regExp->regExp = m_regExp->regExp;
rule->m_regExp->matchers = m_regExp->matchers;
}
rule->matchers = matchers;
return rule;
}
@ -168,7 +159,7 @@ void AdBlockRule::setEnabled(bool enabled) {
}
bool AdBlockRule::isSlow() const {
return m_regExp != 0;
return !m_regexPattern.isEmpty();
}
bool AdBlockRule::isInternalDisabled() const {
@ -460,9 +451,8 @@ void AdBlockRule::parseFilter() {
parsedLine = parsedLine.mid(1);
parsedLine = parsedLine.left(parsedLine.size() - 1);
m_type = RegExpMatchRule;
m_regExp = new RegExp;
m_regExp->regExp = SimpleRegExp(parsedLine, m_caseSensitivity);
m_regExp->matchers = createStringMatchers(parseRegExpFilter(parsedLine));
m_regexPattern = parsedLine;
matchers = createStringMatchers(parseRegExpFilter(parsedLine));
return;
}
@ -496,9 +486,8 @@ void AdBlockRule::parseFilter() {
// we must modify parsedLine to comply with SimpleRegExp.
if (parsedLine.contains(QL1C('*')) || parsedLine.contains(QL1C('^')) || parsedLine.contains(QL1C('|'))) {
m_type = RegExpMatchRule;
m_regExp = new RegExp;
m_regExp->regExp = SimpleRegExp(createRegExpFromFilter(parsedLine), m_caseSensitivity);
m_regExp->matchers = createStringMatchers(parseRegExpFilter(parsedLine));
m_regexPattern = createRegExpFromFilter(parsedLine);
matchers = createStringMatchers(parseRegExpFilter(parsedLine));
return;
}
@ -611,7 +600,7 @@ QString AdBlockRule::createRegExpFromFilter(const QString& filter) const {
break;
}
// fallthrough
[[fallthrough]];
default:
if (!wordCharacter(c)) {
@ -639,6 +628,23 @@ QList<QStringMatcher> AdBlockRule::createStringMatchers(const QStringList& filte
return matchers;
}
int AdBlockRule::regexMatched(const QString& str, int offset) const {
QRegularExpression exp(m_regexPattern);
if (m_caseSensitivity == Qt::CaseSensitivity::CaseInsensitive) {
exp.setPatternOptions(exp.patternOptions() | QRegularExpression::PatternOption::CaseInsensitiveOption);
}
QRegularExpressionMatch m = exp.match(str, offset);
if (!m.hasMatch()) {
return -1;
}
else {
return m.capturedStart();
}
}
bool AdBlockRule::stringMatch(const QString& domain, const QString& encodedUrl) const {
if (m_type == StringContainsMatchRule) {
return encodedUrl.contains(m_matchString, m_caseSensitivity);
@ -654,7 +660,7 @@ bool AdBlockRule::stringMatch(const QString& domain, const QString& encodedUrl)
return false;
}
else {
return (m_regExp->regExp.indexIn(encodedUrl) != -1);
return (regexMatched(encodedUrl) != -1);
}
}
@ -680,9 +686,7 @@ bool AdBlockRule::isMatchingDomain(const QString& domain, const QString& filter)
}
bool AdBlockRule::isMatchingRegExpStrings(const QString& url) const {
Q_ASSERT(m_regExp);
foreach (const QStringMatcher& matcher, m_regExp->matchers) {
for (const QStringMatcher& matcher : matchers) {
if (matcher.indexIn(url) == -1) {
return false;
}

View File

@ -52,8 +52,6 @@
#include <QStringList>
#include <QStringMatcher>
#include "miscellaneous/simpleregexp.h"
class QUrl;
class QWebEngineUrlRequestInfo;
class AdBlockSubscription;
@ -62,8 +60,8 @@ class AdBlockRule {
Q_DISABLE_COPY(AdBlockRule)
public:
explicit AdBlockRule(const QString& filter = QString(), AdBlockSubscription* subscription = 0);
virtual ~AdBlockRule();
explicit AdBlockRule(const QString& filter = QString(), AdBlockSubscription* subscription = nullptr);
virtual ~AdBlockRule() = default;
AdBlockRule* copy() const;
AdBlockSubscription* subscription() const;
@ -145,6 +143,7 @@ class AdBlockRule {
void parseDomains(const QString& domains, const QChar& separator);
bool filterIsOnlyDomain(const QString& filter) const;
bool filterIsOnlyEndsMatch(const QString& filter) const;
int regexMatched(const QString& str, int offset = 0) const;
QString createRegExpFromFilter(const QString& filter) const;
QList<QStringMatcher> createStringMatchers(const QStringList& filters) const;
@ -167,14 +166,9 @@ class AdBlockRule {
bool m_isInternalDisabled;
QStringList m_allowedDomains;
QStringList m_blockedDomains;
struct RegExp {
SimpleRegExp regExp;
QString m_regexPattern;
QList<QStringMatcher> matchers;
};
// Use dynamic allocation to save memory
RegExp* m_regExp;
QList<QStringMatcher> matchers;
friend class AdBlockMatcher;
friend class AdBlockSearchTree;