Replace QRegExp with QRegularExpression

Reviewers: kbasan, msutcliffe

Reviewed By: kbasan, msutcliffe

Subscribers: jenkins

Differential Revision: https://dev.swift-project.org/D11
This commit is contained in:
Mathew Sutcliffe
2017-05-05 22:35:25 +01:00
parent 94fbfeefdb
commit d5ab73e1bc
17 changed files with 55 additions and 54 deletions

View File

@@ -49,7 +49,7 @@
#include <QJsonParseError> #include <QJsonParseError>
#include <QJsonValue> #include <QJsonValue>
#include <QList> #include <QList>
#include <QRegExp> #include <QRegularExpression>
#include <QTextCodec> #include <QTextCodec>
#include <QVector> #include <QVector>
#include <Qt> #include <Qt>
@@ -1062,7 +1062,7 @@ namespace BlackCore
{ {
// detect the stupid z1, z2, z3 placeholders // detect the stupid z1, z2, z3 placeholders
//! \fixme: Anything better as this stupid code here? //! \fixme: Anything better as this stupid code here?
const QString test = fixed.toLower().remove(QRegExp("[\\n\\t\\r]")); const QString test = fixed.toLower().remove(QRegularExpression("[\\n\\t\\r]"));
if (test == "z") return; if (test == "z") return;
if (test.startsWith("z") && test.length() == 2) return; // z1, z2, .. if (test.startsWith("z") && test.length() == 2) return; // z1, z2, ..
if (test.length() == 1) return; // sometimes just z if (test.length() == 1) return; // sometimes just z
@@ -1088,8 +1088,8 @@ namespace BlackCore
} }
auto cruiseAltString = cbvar_cast(cbvar)->fromFSD(fp->cruiseAltitude); auto cruiseAltString = cbvar_cast(cbvar)->fromFSD(fp->cruiseAltitude);
static const QRegExp withUnit("\\D+"); thread_local const QRegularExpression withUnit("\\D+");
if (!cruiseAltString.isEmpty() && withUnit.indexIn(cruiseAltString) < 0) if (!cruiseAltString.isEmpty() && !withUnit.match(cruiseAltString).hasMatch())
{ {
cruiseAltString += "ft"; cruiseAltString += "ft";
} }

View File

@@ -35,7 +35,6 @@
#include <QMetaObject> #include <QMetaObject>
#include <QNetworkReply> #include <QNetworkReply>
#include <QReadLocker> #include <QReadLocker>
#include <QRegExp>
#include <QRegularExpression> #include <QRegularExpression>
#include <QScopedPointer> #include <QScopedPointer>
#include <QScopedPointerDeleteLater> #include <QScopedPointerDeleteLater>
@@ -317,7 +316,7 @@ namespace BlackCore
{ {
// http://uk.flightaware.com/about/faq_aircraft_flight_plan_suffix.rvt // http://uk.flightaware.com/about/faq_aircraft_flight_plan_suffix.rvt
// we expect something like H/B772/F B773 B773/F // we expect something like H/B772/F B773 B773/F
static const QRegularExpression reg("/."); thread_local const QRegularExpression reg("/.");
aircraftIcaoCode = aircraftIcaoCode.replace(reg, "").trimmed().toUpper(); aircraftIcaoCode = aircraftIcaoCode.replace(reg, "").trimmed().toUpper();
if (CAircraftIcaoCode::isValidDesignator(aircraftIcaoCode)) if (CAircraftIcaoCode::isValidDesignator(aircraftIcaoCode))
{ {

View File

@@ -20,7 +20,6 @@
#include <QList> #include <QList>
#include <QMetaObject> #include <QMetaObject>
#include <QNetworkReply> #include <QNetworkReply>
#include <QRegExp>
#include <QScopedPointer> #include <QScopedPointer>
#include <QScopedPointerDeleteLater> #include <QScopedPointerDeleteLater>
#include <QString> #include <QString>

View File

@@ -37,7 +37,7 @@
#include <QPlainTextEdit> #include <QPlainTextEdit>
#include <QPushButton> #include <QPushButton>
#include <QRadioButton> #include <QRadioButton>
#include <QRegExp> #include <QRegularExpression>
#include <QTabBar> #include <QTabBar>
#include <QCompleter> #include <QCompleter>
#include <Qt> #include <Qt>
@@ -270,9 +270,9 @@ namespace BlackGui
flightPlan.setTakeoffTimePlanned(v); flightPlan.setTakeoffTimePlanned(v);
} }
static const QRegExp withUnit("\\D+"); thread_local const QRegularExpression withUnit("\\D+");
v = ui->le_CrusingAltitude->text().trimmed(); v = ui->le_CrusingAltitude->text().trimmed();
if (!v.isEmpty() && withUnit.indexIn(v) < 0) if (!v.isEmpty() && !withUnit.match(v).hasMatch())
{ {
v += "ft"; v += "ft";
ui->le_CrusingAltitude->setText(v); ui->le_CrusingAltitude->setText(v);

View File

@@ -13,6 +13,8 @@
#include "blackmisc/logmessage.h" #include "blackmisc/logmessage.h"
#include "blackconfig/buildconfig.h" #include "blackconfig/buildconfig.h"
#include <QRegularExpression>
using namespace BlackMisc; using namespace BlackMisc;
using namespace BlackMisc::Simulation; using namespace BlackMisc::Simulation;
using namespace BlackMisc::Simulation::FsCommon; using namespace BlackMisc::Simulation::FsCommon;
@@ -176,7 +178,7 @@ namespace BlackGui
const QString raw = rawString.trimmed(); const QString raw = rawString.trimmed();
if (raw.isEmpty()) { return QStringList(); } if (raw.isEmpty()) { return QStringList(); }
QStringList dirs; QStringList dirs;
const QStringList rawLines = raw.split(QRegExp("\n|\r\n|\r")); const QStringList rawLines = raw.split(QRegularExpression("\n|\r\n|\r"));
for (const QString &l : rawLines) for (const QString &l : rawLines)
{ {
const QString normalized = CFileUtils::normalizeFilePathToQtStandard(l); const QString normalized = CFileUtils::normalizeFilePathToQtStandard(l);

View File

@@ -229,10 +229,10 @@ namespace BlackGui
void CWeatherComponent::setupInputValidators() void CWeatherComponent::setupInputValidators()
{ {
QRegExp reIcaoOrLatitude("^[a-zA-Z]{4}|-?\\d{1,2}[,.]?\\d+$", Qt::CaseInsensitive); QRegularExpression reIcaoOrLatitude("^[a-zA-Z]{4}|-?\\d{1,2}[,.]?\\d+$", QRegularExpression::CaseInsensitiveOption);
ui->le_LatOrIcao->setValidator(new QRegExpValidator(reIcaoOrLatitude, this)); ui->le_LatOrIcao->setValidator(new QRegularExpressionValidator(reIcaoOrLatitude, this));
QRegExp reLongitude("^-?\\d{1,2}[,.]?\\d+$", Qt::CaseInsensitive); QRegularExpression reLongitude("^-?\\d{1,2}[,.]?\\d+$", QRegularExpression::CaseInsensitiveOption);
ui->le_Lon->setValidator(new QRegExpValidator(reLongitude, this)); ui->le_Lon->setValidator(new QRegularExpressionValidator(reLongitude, this));
} }
void CWeatherComponent::setupCompleter() void CWeatherComponent::setupCompleter()

View File

@@ -39,7 +39,7 @@
#include <QMenu> #include <QMenu>
#include <QMessageBox> #include <QMessageBox>
#include <QProcess> #include <QProcess>
#include <QRegExp> #include <QRegularExpression>
#include <QSplashScreen> #include <QSplashScreen>
#include <QStyleFactory> #include <QStyleFactory>
#include <QStringList> #include <QStringList>
@@ -244,7 +244,7 @@ namespace BlackGui
html += "</td></tr>\n"; html += "</td></tr>\n";
} }
html += "<tr><td>"; html += "<tr><td>";
static const QRegExp reg("[ ]{2,}"); thread_local const QRegularExpression reg("[ ]{2,}");
html += lt.replace(reg, "</td><td>"); html += lt.replace(reg, "</td><td>");
pendingTr = true; pendingTr = true;
} }

View File

@@ -21,7 +21,7 @@
#include <QFlags> #include <QFlags>
#include <QFont> #include <QFont>
#include <QIODevice> #include <QIODevice>
#include <QRegExp> #include <QRegularExpression>
#include <QStyleOption> #include <QStyleOption>
#include <QStylePainter> #include <QStylePainter>
#include <QTextStream> #include <QTextStream>
@@ -101,9 +101,8 @@ namespace BlackGui
{ {
const QString s = this->style(fileNameFonts()).toLower(); const QString s = this->style(fileNameFonts()).toLower();
if (!s.contains("color:")) return "red"; if (!s.contains("color:")) return "red";
QRegExp rx("color:\\s*(#*\\w+);"); thread_local const QRegularExpression rx("color:\\s*(#*\\w+);");
rx.indexIn(s); const QString c = rx.match(s).captured(1);
const QString c = rx.cap(1);
return c.isEmpty() ? "red" : c; return c.isEmpty() ? "red" : c;
} }

View File

@@ -10,7 +10,6 @@
#include "blackmisc/aviation/callsign.h" #include "blackmisc/aviation/callsign.h"
#include "blackmisc/compare.h" #include "blackmisc/compare.h"
#include <QRegExp>
#include <QRegularExpression> #include <QRegularExpression>
#include <QRegularExpressionMatch> #include <QRegularExpressionMatch>
#include <QStringList> #include <QStringList>
@@ -126,10 +125,10 @@ namespace BlackMisc
if (this->m_callsign.length() < 3) { return ""; } if (this->m_callsign.length() < 3) { return ""; }
if (this->isAtcCallsign()) { return ""; } if (this->isAtcCallsign()) { return ""; }
static const QRegExp regExp("^[A-Z]{3,}"); thread_local const QRegularExpression regExp("^[A-Z]{3,}");
const int pos = regExp.indexIn(this->m_callsign); QRegularExpressionMatch match = regExp.match(this->m_callsign);
if (pos < 0) { return ""; } if (!match.hasMatch()) { return QString(); }
const QString airline = regExp.cap(0); const QString airline = match.captured(0);
if (airline.length() == 3) { return airline; } // we allow 3 letters if (airline.length() == 3) { return airline; } // we allow 3 letters
if (airline.length() == 4 && airline.startsWith('V')) { return airline; } // we allow virtual 4 letter codes, e.g. VDLD if (airline.length() == 4 && airline.startsWith('V')) { return airline; } // we allow virtual 4 letter codes, e.g. VDLD
return ""; // invalid return ""; // invalid

View File

@@ -11,7 +11,7 @@
#include "blackmisc/propertyindex.h" #include "blackmisc/propertyindex.h"
#include "blackmisc/variant.h" #include "blackmisc/variant.h"
#include <QRegExp> #include <QRegularExpression>
#include <Qt> #include <Qt>
#include <QtDebug> #include <QtDebug>
@@ -131,8 +131,8 @@ namespace BlackMisc
qint32 tc = transponderCode.toInt(&number); qint32 tc = transponderCode.toInt(&number);
if (!number) return false; if (!number) return false;
if (tc < 0 || tc > 7777) return false; if (tc < 0 || tc > 7777) return false;
QRegExp rx("[0-7]{1,4}"); thread_local const QRegularExpression rx("^[0-7]{1,4}$");
return rx.exactMatch(transponderCode); return rx.match(transponderCode).hasMatch();
} }
bool CTransponder::isValidTransponderCode(qint32 transponderCode) bool CTransponder::isValidTransponderCode(qint32 transponderCode)

View File

@@ -12,6 +12,7 @@
#include <QJsonObject> #include <QJsonObject>
#include <QJsonValue> #include <QJsonValue>
#include <QRegularExpression>
#include <Qt> #include <Qt>
namespace BlackMisc namespace BlackMisc
@@ -33,9 +34,9 @@ namespace BlackMisc
{ {
if (countryName.isEmpty()) { return CCountry(); } if (countryName.isEmpty()) { return CCountry(); }
static const QRegExp reg("^[a-z]+", Qt::CaseInsensitive); thread_local const QRegularExpression reg("^[a-z]+", QRegularExpression::CaseInsensitiveOption);
int pos = reg.indexIn(countryName); QRegularExpressionMatch match = reg.match(countryName);
const QString cn(pos >= 0 ? reg.cap(0) : countryName); const QString cn(match.hasMatch() ? match.captured(0) : countryName);
CCountryList countries = this->findBy([&](const CCountry & country) CCountryList countries = this->findBy([&](const CCountry & country)
{ {
return country.matchesCountryName(cn); return country.matchesCountryName(cn);

View File

@@ -20,7 +20,6 @@
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
#include <QJsonValue> #include <QJsonValue>
#include <QRegExp>
#include <QTimeZone> #include <QTimeZone>
using namespace BlackMisc; using namespace BlackMisc;

View File

@@ -8,6 +8,7 @@
*/ */
#include "distribution.h" #include "distribution.h"
#include <QRegularExpression>
#include <QStringBuilder> #include <QStringBuilder>
using namespace BlackConfig; using namespace BlackConfig;
@@ -230,23 +231,25 @@ namespace BlackMisc
if (filename.isEmpty()) { return QVersionNumber(); } if (filename.isEmpty()) { return QVersionNumber(); }
// swift-installer-linux-64-0.7.3_2017-03-25_11-24-50.run // swift-installer-linux-64-0.7.3_2017-03-25_11-24-50.run
static const QRegExp firstSegments("\\d+\\.\\d+\\.\\d+"); thread_local const QRegularExpression firstSegments("\\d+\\.\\d+\\.\\d+");
if (firstSegments.indexIn(filename) < 0) QRegularExpressionMatch firstSegmentsMatch = firstSegments.match(filename);
if (!firstSegmentsMatch.hasMatch())
{ {
return QVersionNumber(); // no version, invalid return QVersionNumber(); // no version, invalid
} }
QString v = firstSegments.cap(0); // first 3 segments, like 0.9.3 QString v = firstSegmentsMatch.captured(0); // first 3 segments, like 0.9.3
if (!v.endsWith('.')) { v += '.'; } if (!v.endsWith('.')) { v += '.'; }
static const QRegExp ts1("\\d{4}.?\\d{2}.?\\d{2}.?\\d{2}.?\\d{2}.?\\d{2}"); thread_local const QRegularExpression ts1("\\d{4}.?\\d{2}.?\\d{2}.?\\d{2}.?\\d{2}.?\\d{2}");
if (ts1.indexIn(filename) < 0) QRegularExpressionMatch ts1Match = ts1.match(filename);
if (!ts1Match.hasMatch())
{ {
// version without timestamp // version without timestamp
v += "0"; v += "0";
return QVersionNumber::fromString(v); return QVersionNumber::fromString(v);
} }
const QString versionTimestampString = BlackMisc::digitOnlyString(ts1.cap(0)); const QString versionTimestampString = BlackMisc::digitOnlyString(ts1Match.captured(0));
const QDateTime versionTimestamp = QDateTime::fromString(versionTimestampString, "yyyyMMddHHmmss"); const QDateTime versionTimestamp = QDateTime::fromString(versionTimestampString, "yyyyMMddHHmmss");
const QString lastSegment = QString::number(CBuildConfig::buildTimestampAsVersionSegment(versionTimestamp)); const QString lastSegment = QString::number(CBuildConfig::buildTimestampAsVersionSegment(versionTimestamp));

View File

@@ -12,7 +12,7 @@
#include "blackmisc/geo/latitude.h" #include "blackmisc/geo/latitude.h"
#include "blackmisc/geo/longitude.h" #include "blackmisc/geo/longitude.h"
#include <QRegExp> #include <QRegularExpression>
#include <Qt> #include <Qt>
#include <QtGlobal> #include <QtGlobal>
#include <QtMath> #include <QtMath>
@@ -72,7 +72,7 @@ namespace BlackMisc
{ {
// http://www.regular-expressions.info/floatingpoint.html // http://www.regular-expressions.info/floatingpoint.html
const QString wgs = wgsCoordinate.simplified().trimmed(); const QString wgs = wgsCoordinate.simplified().trimmed();
QRegExp rx("([-+]?[0-9]*\\.?[0-9]+)"); thread_local const QRegularExpression rx("([-+]?[0-9]*\\.?[0-9]+)");
qint32 deg = 0; qint32 deg = 0;
qint32 min = 0; qint32 min = 0;
double sec = 0.0; double sec = 0.0;
@@ -80,10 +80,11 @@ namespace BlackMisc
int fragmentLength = 0; int fragmentLength = 0;
int c = 0; int c = 0;
int pos = 0; int pos = 0;
while ((pos = rx.indexIn(wgs, pos)) != -1) QRegularExpressionMatch match = rx.match(wgs, pos);
while (match.hasMatch())
{ {
QString cap = rx.cap(1); QString cap = match.captured(1);
pos += rx.matchedLength(); pos += match.capturedLength(1);
switch (c++) switch (c++)
{ {
case 0: case 0:
@@ -102,6 +103,7 @@ namespace BlackMisc
default: default:
break; break;
} }
match = rx.match(wgs, pos);
} }
if (fragmentLength > 0) if (fragmentLength > 0)
{ {

View File

@@ -346,7 +346,7 @@ namespace BlackMisc
{ {
if (errorMessage.isEmpty()) { return errorMessage; } if (errorMessage.isEmpty()) { return errorMessage; }
QString phpError(errorMessage); QString phpError(errorMessage);
static const QRegularExpression regEx("<[^>]*>"); thread_local const QRegularExpression regEx("<[^>]*>");
return phpError.remove(regEx); return phpError.remove(regEx);
} }

View File

@@ -14,7 +14,7 @@
#include "blackmisc/pq/constants.h" #include "blackmisc/pq/constants.h"
#include "blackmisc/pq/physicalquantity.h" #include "blackmisc/pq/physicalquantity.h"
#include <QRegExp> #include <QRegularExpression>
#include <Qt> #include <Qt>
#include <QtGlobal> #include <QtGlobal>
@@ -176,7 +176,7 @@ namespace BlackMisc
if (this->isEmpty()) return invalid; if (this->isEmpty()) return invalid;
if (this->isPrivateMessage()) return invalid; if (this->isPrivateMessage()) return invalid;
if (this->m_message.length() > 15 || this->m_message.length() < 10) return invalid; // SELCAL AB-CD -> 12, I allow some more characters as I do not know wheter in real life it exactly matches if (this->m_message.length() > 15 || this->m_message.length() < 10) return invalid; // SELCAL AB-CD -> 12, I allow some more characters as I do not know wheter in real life it exactly matches
QString candidate = this->m_message.toUpper().remove(QRegExp("[^A-Z]")); // SELCALABCD QString candidate = this->m_message.toUpper().remove(QRegularExpression("[^A-Z]")); // SELCALABCD
if (candidate.length() != 10) return invalid; if (candidate.length() != 10) return invalid;
if (!candidate.startsWith("SELCAL")) return invalid; if (!candidate.startsWith("SELCAL")) return invalid;
return candidate.right(4).toUpper(); return candidate.right(4).toUpper();

View File

@@ -21,7 +21,7 @@
#include "blackmisc/pq/units.h" #include "blackmisc/pq/units.h"
#include <QLocale> #include <QLocale>
#include <QRegExp> #include <QRegularExpression>
#include <QThreadStorage> #include <QThreadStorage>
#include <QtDebug> #include <QtDebug>
@@ -45,12 +45,10 @@ namespace BlackMisc
// check // check
if (vs.isEmpty()) { return v; } if (vs.isEmpty()) { return v; }
static QThreadStorage<QRegExp> tsRegex; thread_local const QRegularExpression regex("([-+]?[0-9]*[\\.,]?[0-9]+)\\s*(\\D*)$");
if (! tsRegex.hasLocalData()) { tsRegex.setLocalData(QRegExp("([-+]?[0-9]*[\\.,]?[0-9]+)\\s*(\\D*)$")); } auto match = regex.match(value);
const auto &regex = tsRegex.localData(); if (!match.hasMatch()) { return v; } // not a valid number
QString unit = match.captured(2).trimmed();
if (regex.indexIn(value) < 0) { return v; } // not a valid number
QString unit = regex.cap(2).trimmed();
QString number = QString(value).replace(unit, ""); QString number = QString(value).replace(unit, "");
unit = unit.trimmed(); // trim after replace, not before unit = unit.trimmed(); // trim after replace, not before