From e848459ccbd42fba6990cb50f83b2875debae895 Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Thu, 11 May 2017 18:25:55 +0100 Subject: [PATCH] Avoiding QRegularExpression for simple stuff [follow-up D11] Summary: Fixing a snag I noticed while reviewing D11. There is no need to use a regex for such a simple thing as removing `[^A-Z]`. Some other minor optimizations in the same function. Reviewers: rwinklmeier, kbasan Reviewed By: rwinklmeier Subscribers: jenkins Tags: #swift_pilot_client Differential Revision: https://dev.swift-project.org/D20 --- src/blackmisc/network/textmessage.cpp | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/blackmisc/network/textmessage.cpp b/src/blackmisc/network/textmessage.cpp index 701bf9112..650d39a2a 100644 --- a/src/blackmisc/network/textmessage.cpp +++ b/src/blackmisc/network/textmessage.cpp @@ -13,8 +13,8 @@ #include "blackmisc/aviation/selcal.h" #include "blackmisc/pq/constants.h" #include "blackmisc/pq/physicalquantity.h" +#include "blackmisc/stringutils.h" -#include #include #include @@ -170,16 +170,13 @@ namespace BlackMisc // on their primary frequency, formatted like so: // SELCAL AB-CD - const QString invalid; - - // some first level checks, before really parsing the message - if (this->isEmpty()) 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 - QString candidate = this->m_message.toUpper().remove(QRegularExpression("[^A-Z]")); // SELCALABCD - if (candidate.length() != 10) return invalid; - if (!candidate.startsWith("SELCAL")) return invalid; - return candidate.right(4).toUpper(); + if (this->isEmpty()) return {}; + if (this->isPrivateMessage()) return {}; + if (!this->m_message.startsWith(QLatin1String("SELCAL"), Qt::CaseInsensitive)) return {}; + if (this->m_message.length() > 15 || this->m_message.length() < 10) return {}; // SELCAL AB-CD -> 12, I allow some more characters as I do not know wheter in real life it exactly matches + QString candidate = removeChars(this->m_message, [](QChar c) { return !c.isLetter(); }); // SELCALABCD + if (candidate.length() != 10) return {}; + return std::move(candidate).right(4).toUpper(); } CIcon CTextMessage::toIcon() const