Commit db6faabd authored by Laurent Montel's avatar Laurent Montel
Browse files

Port some code to QRegularExpression

parent d9bd248d
Pipeline #245098 passed with stage
in 32 minutes
......@@ -8,7 +8,7 @@
#include "richtextcomposerng.h"
#include <KIdentityManagement/Signature>
#include <QRegExp>
#include <QTextBlock>
using namespace MessageComposer;
......
......@@ -59,7 +59,7 @@ void AntiSpamConfig::readConfig()
} else if (qstricmp(type.data(), "adjusted") == 0) {
typeE = SpamAgentAdjustedFloat;
}
mAgents.append(SpamAgent(name, typeE, header, cheader, QRegExp(score), QRegExp(threshold), QRegExp(confidence)));
mAgents.append(SpamAgent(name, typeE, header, cheader, QRegularExpression(score), QRegularExpression(threshold), QRegularExpression(confidence)));
}
}
}
......
......@@ -10,8 +10,7 @@
#pragma once
#include "messageviewer_export.h"
#include <QRegExp>
#include <QRegularExpression>
#include <QVector>
class QString;
......@@ -39,9 +38,9 @@ public:
SpamAgentTypes type,
const QByteArray &field,
const QByteArray &cfield,
const QRegExp &score,
const QRegExp &threshold,
const QRegExp &confidence)
const QRegularExpression &score,
const QRegularExpression &threshold,
const QRegularExpression &confidence)
: mName(name)
, mType(type)
, mField(field)
......@@ -72,17 +71,17 @@ public:
return mConfidenceField;
}
Q_REQUIRED_RESULT QRegExp scorePattern() const
Q_REQUIRED_RESULT QRegularExpression scorePattern() const
{
return mScore;
}
Q_REQUIRED_RESULT QRegExp thresholdPattern() const
Q_REQUIRED_RESULT QRegularExpression thresholdPattern() const
{
return mThreshold;
}
Q_REQUIRED_RESULT QRegExp confidencePattern() const
Q_REQUIRED_RESULT QRegularExpression confidencePattern() const
{
return mConfidence;
}
......@@ -92,9 +91,9 @@ private:
SpamAgentTypes mType;
QByteArray mField;
QByteArray mConfidenceField;
QRegExp mScore;
QRegExp mThreshold;
QRegExp mConfidence;
QRegularExpression mScore;
QRegularExpression mThreshold;
QRegularExpression mConfidence;
};
using SpamAgents = QVector<SpamAgent>;
......
......@@ -15,6 +15,8 @@
#include <KMime/Headers>
#include <KMime/KMimeMessage>
#include <QRegularExpression>
using namespace MessageViewer;
// static
......@@ -50,9 +52,9 @@ SpamScores SpamHeaderAnalyzer::getSpamScores(KMime::Message *message)
if ((*it).scoreType() != SpamAgentBool) {
// Can we extract the score?
QRegExp scorePattern = (*it).scorePattern();
if (scorePattern.indexIn(mField) != -1) {
scoreString = scorePattern.cap(1);
QRegularExpression scorePattern = (*it).scorePattern();
if (scorePattern.match(mField).hasMatch()) {
scoreString = scorePattern.match(mField).captured(1);
scoreValid = true;
}
} else {
......@@ -70,7 +72,7 @@ SpamScores SpamHeaderAnalyzer::getSpamScores(KMime::Message *message)
break;
case SpamAgentBool:
if ((*it).scorePattern().indexIn(mField) == -1) {
if ((*it).scorePattern().match(mField).hasMatch()) {
score = 0.0;
} else {
score = 100.0;
......@@ -105,9 +107,9 @@ SpamScores SpamHeaderAnalyzer::getSpamScores(KMime::Message *message)
// Find the threshold value.
QString thresholdString;
const QRegExp thresholdPattern = (*it).thresholdPattern();
if (thresholdPattern.indexIn(mField) != -1) {
thresholdString = thresholdPattern.cap(1);
const QRegularExpression thresholdPattern = (*it).thresholdPattern();
if (thresholdPattern.match(mField).hasMatch()) {
thresholdString = thresholdPattern.match(mField).captured(1);
} else {
spamError = couldNotFindTheThresholdField;
qCDebug(MESSAGEVIEWER_LOG) << "Threshold could not be extracted from header '" << mField << "'";
......@@ -147,9 +149,9 @@ SpamScores SpamHeaderAnalyzer::getSpamScores(KMime::Message *message)
mCField = cHeader->asUnicodeString();
if (!mCField.isEmpty()) {
// Can we extract the confidence?
QRegExp cScorePattern = (*it).confidencePattern();
if (cScorePattern.indexIn(mCField) != -1) {
confidenceString = cScorePattern.cap(1);
QRegularExpression cScorePattern = (*it).confidencePattern();
if (cScorePattern.match(mCField).hasMatch()) {
confidenceString = cScorePattern.match(mCField).captured(1);
}
confidence = confidenceString.toFloat(&confidenceValid);
if (!confidenceValid) {
......
......@@ -4,9 +4,9 @@
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "util.h"
#include <QFile>
#include <QProcess>
#include <QRegularExpression>
#include <QTest>
using namespace MessageViewer;
KMime::Message::Ptr Test::readAndParseMail(const QString &mailFile)
......@@ -33,11 +33,11 @@ void Test::compareFile(const QString &outFile, const QString &referenceFile)
QVERIFY(f.open(QIODevice::ReadOnly));
QString content = QString::fromUtf8(f.readAll());
f.close();
content.replace(QRegExp(QStringLiteral("[\t ]+")), QStringLiteral(" "));
content.replace(QRegExp(QStringLiteral("[\t ]*\n+[\t ]*")), QStringLiteral("\n"));
content.replace(QRegExp(QStringLiteral("([\n\t ])\\1+")), QStringLiteral("\\1"));
content.replace(QRegExp(QStringLiteral(">\n+[\t ]*")), QStringLiteral(">"));
content.replace(QRegExp(QStringLiteral("[\t ]*\n+[\t ]*<")), QStringLiteral("<"));
content.replace(QRegularExpression(QStringLiteral("[\t ]+")), QStringLiteral(" "));
content.replace(QRegularExpression(QStringLiteral("[\t ]*\n+[\t ]*")), QStringLiteral("\n"));
content.replace(QRegularExpression(QStringLiteral("([\n\t ])\\1+")), QStringLiteral("\\1"));
content.replace(QRegularExpression(QStringLiteral(">\n+[\t ]*")), QStringLiteral(">"));
content.replace(QRegularExpression(QStringLiteral("[\t ]*\n+[\t ]*<")), QStringLiteral("<"));
content.replace(QLatin1String("&nbsp;"), QLatin1String("NBSP_ENTITY_PLACEHOLDER")); // xmlling chokes on &nbsp;
QVERIFY(f.open(QIODevice::WriteOnly | QIODevice::Truncate));
f.write(content.toUtf8());
......@@ -56,8 +56,8 @@ void Test::compareFile(const QString &outFile, const QString &referenceFile)
QVERIFY(f.open(QIODevice::ReadOnly));
QString content = QString::fromUtf8(f.readAll());
f.close();
content.replace(QRegExp(QStringLiteral("\"file:[^\"]*[/(?:%2F)]([^\"/(?:%2F)]*)\"")), QStringLiteral("\"file:\\1\""));
content.replace(QRegExp(QStringLiteral("(file:///tmp/messageviewer)(_[^\"]+)(\\.index\\.[^\"]*)")), QStringLiteral("\\1\\3"));
content.replace(QRegularExpression(QStringLiteral("\"file:[^\"]*[/(?:%2F)]([^\"/(?:%2F)]*)\"")), QStringLiteral("\"file:\\1\""));
content.replace(QRegularExpression(QStringLiteral("(file:///tmp/messageviewer)(_[^\"]+)(\\.index\\.[^\"]*)")), QStringLiteral("\\1\\3"));
content.replace(QLatin1String("NBSP_ENTITY_PLACEHOLDER"), QLatin1String("&nbsp;")); // undo above transformation for xmllint
QVERIFY(f.open(QIODevice::WriteOnly | QIODevice::Truncate));
f.write(content.toUtf8());
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment