From 498e3aa015e7706cd4f0d54a4976d5c39051c534 Mon Sep 17 00:00:00 2001 From: sithlord48 Date: Sun, 20 Oct 2024 23:54:50 -0400 Subject: [PATCH] refactor: use static regularexpressions and matches --- src/gui/src/MainWindow.cpp | 2 +- src/lib/gui/VersionChecker.cpp | 7 ++++--- src/lib/gui/validators/ComputerNameValidator.cpp | 6 ++---- src/lib/gui/validators/ComputerNameValidator.h | 4 ++++ 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/gui/src/MainWindow.cpp b/src/gui/src/MainWindow.cpp index 1cfbfec9c..ecf171147 100644 --- a/src/gui/src/MainWindow.cpp +++ b/src/gui/src/MainWindow.cpp @@ -691,7 +691,7 @@ void MainWindow::checkConnected(const QString &line) void MainWindow::checkFingerprint(const QString &line) { - QRegularExpression re(".*server fingerprint: ([A-F0-9:]+)"); + static const QRegularExpression re(".*server fingerprint: ([A-F0-9:]+)"); auto match = re.match(line); if (!match.hasMatch()) { return; diff --git a/src/lib/gui/VersionChecker.cpp b/src/lib/gui/VersionChecker.cpp index 157490a2b..ec1abdbd8 100644 --- a/src/lib/gui/VersionChecker.cpp +++ b/src/lib/gui/VersionChecker.cpp @@ -86,11 +86,12 @@ int VersionChecker::getStageVersion(QString stage) if (stage.isEmpty() || stage == stableName) { return stableValue; } else if (stage.toLower().startsWith(rcName)) { - QRegularExpression re("\\d*", QRegularExpression::CaseInsensitiveOption); - if (re.match(stage).hasMatch()) { + static QRegularExpression re("\\d*", QRegularExpression::CaseInsensitiveOption); + auto match = re.match(stage); + if (match.hasMatch()) { // return the rc value plus the rc number (e.g. 2 + 1) // this should be ok since stable is max int. - return rcValue + re.match(stage).captured(1).toInt(); + return rcValue + match.captured(1).toInt(); } } else if (stage == betaName) { return betaValue; diff --git a/src/lib/gui/validators/ComputerNameValidator.cpp b/src/lib/gui/validators/ComputerNameValidator.cpp index 3410c7672..b6bccff5e 100644 --- a/src/lib/gui/validators/ComputerNameValidator.cpp +++ b/src/lib/gui/validators/ComputerNameValidator.cpp @@ -27,10 +27,8 @@ ComputerNameValidator::ComputerNameValidator(const QString &message) : IStringVa bool ComputerNameValidator::validate(const QString &input) const { - const QRegularExpression re("^[\\w\\._-]{0,255}$", QRegularExpression::CaseInsensitiveOption); - auto match = re.match(input); - auto result = match.hasMatch(); - return result; + auto match = m_nameValidator.match(input); + return match.hasMatch(); } } // namespace validators diff --git a/src/lib/gui/validators/ComputerNameValidator.h b/src/lib/gui/validators/ComputerNameValidator.h index a8b66201b..5f30621e1 100644 --- a/src/lib/gui/validators/ComputerNameValidator.h +++ b/src/lib/gui/validators/ComputerNameValidator.h @@ -28,6 +28,10 @@ class ComputerNameValidator : public IStringValidator public: explicit ComputerNameValidator(const QString &message); bool validate(const QString &input) const override; + +private: + inline static const QRegularExpression m_nameValidator = + QRegularExpression(QStringLiteral("^[\\w\\._-]{0,255}$"), QRegularExpression::CaseInsensitiveOption); }; } // namespace validators