Commit a001d349 authored by Ahmad Samir's avatar Ahmad Samir Committed by Volker Krause
Browse files

StringUtil: Port QRegExp to QRegularExpression

stringutil unit test still passes.
parent 999893c4
Pipeline #30790 passed with stage
in 82 minutes and 24 seconds
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
#include <KIdentityManagement/IdentityManager> #include <KIdentityManagement/IdentityManager>
#include <KIdentityManagement/Identity> #include <KIdentityManagement/Identity>
#include <QHostInfo> #include <QHostInfo>
#include <QRegExp>
#include <QRegularExpression> #include <QRegularExpression>
#include <QStringList> #include <QStringList>
#include <QUrlQuery> #include <QUrlQuery>
...@@ -723,15 +722,16 @@ QString replacePrefixes(const QString &str, const QStringList &prefixRegExps, bo ...@@ -723,15 +722,16 @@ QString replacePrefixes(const QString &str, const QStringList &prefixRegExps, bo
// construct a big regexp that // construct a big regexp that
// 1. is anchored to the beginning of str (sans whitespace) // 1. is anchored to the beginning of str (sans whitespace)
// 2. matches at least one of the part regexps in prefixRegExps // 2. matches at least one of the part regexps in prefixRegExps
QString bigRegExp = QStringLiteral("^(?:\\s+|(?:%1))+\\s*") const QString bigRegExp = QStringLiteral("^(?:\\s+|(?:%1))+\\s*")
.arg(prefixRegExps.join(QStringLiteral(")|(?:"))); .arg(prefixRegExps.join(QStringLiteral(")|(?:")));
QRegExp rx(bigRegExp, Qt::CaseInsensitive); const QRegularExpression rx(bigRegExp, QRegularExpression::CaseInsensitiveOption);
if (rx.isValid()) { if (rx.isValid()) {
QString tmp = str; QString tmp = str;
if (rx.indexIn(tmp) == 0) { const QRegularExpressionMatch match = rx.match(tmp);
if (match.hasMatch()) {
recognized = true; recognized = true;
if (replace) { if (replace) {
return tmp.replace(0, rx.matchedLength(), newPrefix + QLatin1Char(' ')); return tmp.replace(0, match.capturedLength(0), newPrefix + QLatin1Char(' '));
} }
} }
} else { } else {
...@@ -762,20 +762,18 @@ QString stripOffPrefixes(const QString &subject) ...@@ -762,20 +762,18 @@ QString stripOffPrefixes(const QString &subject)
// 2. matches at least one of the part regexps in prefixRegExps // 2. matches at least one of the part regexps in prefixRegExps
const QString bigRegExp = QStringLiteral("^(?:\\s+|(?:%1))+\\s*").arg(prefixRegExps.join(QStringLiteral(")|(?:"))); const QString bigRegExp = QStringLiteral("^(?:\\s+|(?:%1))+\\s*").arg(prefixRegExps.join(QStringLiteral(")|(?:")));
static QString regExpPattern; static QRegularExpression regex;
static QRegExp regExp;
regExp.setCaseSensitivity(Qt::CaseInsensitive); regex.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
if (regExpPattern != bigRegExp) { if (regex.pattern() != bigRegExp) {
// the prefixes have changed, so update the regexp // the prefixes have changed, so update the regexp
regExpPattern = bigRegExp; regex.setPattern(bigRegExp);
regExp.setPattern(regExpPattern);
} }
if (regExp.isValid()) { if (regex.isValid()) {
QString tmp = subject; QRegularExpressionMatch match = regex.match(subject);
if (regExp.indexIn(tmp) == 0) { if (match.hasMatch()) {
return tmp.remove(0, regExp.matchedLength()); return subject.mid(match.capturedEnd(0));
} }
} else { } else {
qCWarning(MESSAGECORE_LOG) << "bigRegExp = \"" qCWarning(MESSAGECORE_LOG) << "bigRegExp = \""
......
Markdown is supported
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