From e6b9a07163fee6721478ab982edb6211498a816c Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Fri, 22 Dec 2023 03:50:04 +0400 Subject: [PATCH] Instantiate QRegularExpression instances statically --- Telegram/SourceFiles/boxes/connection_box.cpp | 18 +++++++++------ Telegram/SourceFiles/core/launcher.cpp | 8 +++++-- Telegram/SourceFiles/core/update_checker.cpp | 5 +++-- Telegram/SourceFiles/intro/intro_phone.cpp | 3 ++- Telegram/SourceFiles/mtproto/mtp_instance.cpp | 9 +++++--- .../mtproto/mtproto_dc_options.cpp | 3 ++- .../mtproto/mtproto_proxy_data.cpp | 7 +++--- .../SourceFiles/mtproto/mtproto_response.cpp | 4 ++-- .../mtproto/special_config_request.cpp | 3 ++- .../passport/passport_panel_controller.cpp | 22 ++++++++++--------- .../passport/ui/passport_details_row.cpp | 8 ++++--- .../payments/smartglocal/smartglocal_card.cpp | 3 ++- .../payments/stripe/stripe_card_validator.cpp | 6 +++-- .../payments/ui/payments_edit_card.cpp | 3 ++- .../payments/ui/payments_field.cpp | 10 ++++----- .../statistics_data_deserialize.cpp | 3 ++- Telegram/SourceFiles/storage/localstorage.cpp | 3 ++- .../ui/boxes/country_select_box.cpp | 3 ++- 18 files changed, 74 insertions(+), 47 deletions(-) diff --git a/Telegram/SourceFiles/boxes/connection_box.cpp b/Telegram/SourceFiles/boxes/connection_box.cpp index 144582a533..3bdaa932d9 100644 --- a/Telegram/SourceFiles/boxes/connection_box.cpp +++ b/Telegram/SourceFiles/boxes/connection_box.cpp @@ -113,7 +113,8 @@ Base64UrlInput::Base64UrlInput( rpl::producer placeholder, const QString &val) : MaskedInputField(parent, st, std::move(placeholder), val) { - if (!QRegularExpression("^[a-zA-Z0-9_\\-]+$").match(val).hasMatch()) { + static const auto RegExp = QRegularExpression("^[a-zA-Z0-9_\\-]+$"); + if (!RegExp.match(val).hasMatch()) { setText(QString()); } } @@ -831,8 +832,9 @@ void ProxyBox::prepare() { connect(_host.data(), &HostInput::changed, [=] { Ui::PostponeCall(_host, [=] { const auto host = _host->getLastText().trimmed(); - static const auto mask = u"^\\d+\\.\\d+\\.\\d+\\.\\d+:(\\d*)$"_q; - const auto match = QRegularExpression(mask).match(host); + static const auto mask = QRegularExpression( + u"^\\d+\\.\\d+\\.\\d+\\.\\d+:(\\d*)$"_q); + const auto match = mask.match(host); if (_host->cursorPosition() == host.size() && match.hasMatch()) { const auto port = match.captured(1); @@ -1107,6 +1109,10 @@ void ProxiesBoxController::ShowApplyConfirmation( proxy.password = fields.value(u"secret"_q); } if (proxy) { + static const auto UrlStartRegExp = QRegularExpression( + "^https://", + QRegularExpression::CaseInsensitiveOption); + static const auto UrlEndRegExp = QRegularExpression("/$"); const auto displayed = "https://" + server + "/"; const auto parsed = QUrl::fromUserInput(displayed); const auto displayUrl = !UrlClickHandler::IsSuspicious(displayed) @@ -1117,11 +1123,9 @@ void ProxiesBoxController::ShowApplyConfirmation( const auto displayServer = QString( displayUrl ).replace( - QRegularExpression( - "^https://", - QRegularExpression::CaseInsensitiveOption), + UrlStartRegExp, QString() - ).replace(QRegularExpression("/$"), QString()); + ).replace(UrlEndRegExp, QString()); const auto text = tr::lng_sure_enable_socks( tr::now, lt_server, diff --git a/Telegram/SourceFiles/core/launcher.cpp b/Telegram/SourceFiles/core/launcher.cpp index b171b070e1..64fe1f639f 100644 --- a/Telegram/SourceFiles/core/launcher.cpp +++ b/Telegram/SourceFiles/core/launcher.cpp @@ -542,9 +542,13 @@ void Launcher::processArguments() { } } + static const auto RegExp = QRegularExpression("[^a-z0-9\\-_]"); gDebugMode = parseResult.contains("-debug"); - gKeyFile = parseResult.value("-key", {}).join(QString()).toLower(); - gKeyFile = gKeyFile.replace(QRegularExpression("[^a-z0-9\\-_]"), {}); + gKeyFile = parseResult + .value("-key", {}) + .join(QString()) + .toLower() + .replace(RegExp, {}); gLaunchMode = parseResult.contains("-autostart") ? LaunchModeAutoStart : parseResult.contains("-fixprevious") ? LaunchModeFixPrevious : parseResult.contains("-cleanup") ? LaunchModeCleanup diff --git a/Telegram/SourceFiles/core/update_checker.cpp b/Telegram/SourceFiles/core/update_checker.cpp index 43cbbdaeb1..fe95f60dbc 100644 --- a/Telegram/SourceFiles/core/update_checker.cpp +++ b/Telegram/SourceFiles/core/update_checker.cpp @@ -241,7 +241,7 @@ QString FindUpdateFile() { } const auto list = updates.entryInfoList(QDir::Files); for (const auto &info : list) { - if (QRegularExpression( + static const auto RegExp = QRegularExpression( "^(" "tupdate|" "tx64upd|" @@ -250,7 +250,8 @@ QString FindUpdateFile() { "tlinuxupd|" ")\\d+(_[a-z\\d]+)?$", QRegularExpression::CaseInsensitiveOption - ).match(info.fileName()).hasMatch()) { + ); + if (RegExp.match(info.fileName()).hasMatch()) { return info.absoluteFilePath(); } } diff --git a/Telegram/SourceFiles/intro/intro_phone.cpp b/Telegram/SourceFiles/intro/intro_phone.cpp index bf8a355bcd..7ac4209562 100644 --- a/Telegram/SourceFiles/intro/intro_phone.cpp +++ b/Telegram/SourceFiles/intro/intro_phone.cpp @@ -169,7 +169,8 @@ void PhoneWidget::submit() { // Check if such account is authorized already. const auto digitsOnly = [](QString value) { - return value.replace(QRegularExpression("[^0-9]"), QString()); + static const auto RegExp = QRegularExpression("[^0-9]"); + return value.replace(RegExp, QString()); }; const auto phoneDigits = digitsOnly(phone); for (const auto &[index, existing] : Core::App().domain().accounts()) { diff --git a/Telegram/SourceFiles/mtproto/mtp_instance.cpp b/Telegram/SourceFiles/mtproto/mtp_instance.cpp index e5b5cc8ec8..7a9908d992 100644 --- a/Telegram/SourceFiles/mtproto/mtp_instance.cpp +++ b/Telegram/SourceFiles/mtproto/mtp_instance.cpp @@ -1354,8 +1354,11 @@ bool Instance::Private::onErrorDefault( const auto &type = error.type(); const auto code = error.code(); auto badGuestDc = (code == 400) && (type == u"FILE_ID_INVALID"_q); + static const auto MigrateRegExp = QRegularExpression("^(FILE|PHONE|NETWORK|USER)_MIGRATE_(\\d+)$"); + static const auto FloodWaitRegExp = QRegularExpression("^FLOOD_WAIT_(\\d+)$"); + static const auto SlowmodeWaitRegExp = QRegularExpression("^SLOWMODE_WAIT_(\\d+)$"); QRegularExpressionMatch m1, m2; - if ((m1 = QRegularExpression("^(FILE|PHONE|NETWORK|USER)_MIGRATE_(\\d+)$").match(type)).hasMatch()) { + if ((m1 = MigrateRegExp.match(type)).hasMatch()) { if (!requestId) return false; auto dcWithShift = ShiftedDcId(0); @@ -1458,8 +1461,8 @@ bool Instance::Private::onErrorDefault( return true; } else if (code < 0 || code >= 500 - || (m1 = QRegularExpression("^FLOOD_WAIT_(\\d+)$").match(type)).hasMatch() - || ((m2 = QRegularExpression("^SLOWMODE_WAIT_(\\d+)$").match(type)).hasMatch() + || (m1 = FloodWaitRegExp.match(type)).hasMatch() + || ((m2 = SlowmodeWaitRegExp.match(type)).hasMatch() && m2.captured(1).toInt() < 3)) { if (!requestId) return false; diff --git a/Telegram/SourceFiles/mtproto/mtproto_dc_options.cpp b/Telegram/SourceFiles/mtproto/mtproto_dc_options.cpp index c659f8509a..048845a8f2 100644 --- a/Telegram/SourceFiles/mtproto/mtproto_dc_options.cpp +++ b/Telegram/SourceFiles/mtproto/mtproto_dc_options.cpp @@ -757,8 +757,9 @@ bool DcOptions::loadFromFile(const QString &path) { stream.setCodec("UTF-8"); #endif // Qt < 6.0.0 while (!stream.atEnd()) { + static const auto RegExp = QRegularExpression(R"(\s)"); auto line = stream.readLine(); - auto components = line.split(QRegularExpression(R"(\s)"), Qt::SkipEmptyParts); + auto components = line.split(RegExp, Qt::SkipEmptyParts); if (components.isEmpty() || components[0].startsWith('#')) { continue; } diff --git a/Telegram/SourceFiles/mtproto/mtproto_proxy_data.cpp b/Telegram/SourceFiles/mtproto/mtproto_proxy_data.cpp index 260fe0b12e..8ed267514c 100644 --- a/Telegram/SourceFiles/mtproto/mtproto_proxy_data.cpp +++ b/Telegram/SourceFiles/mtproto/mtproto_proxy_data.cpp @@ -157,11 +157,12 @@ bool ProxyData::supportsCalls() const { } bool ProxyData::tryCustomResolve() const { + static const auto RegExp = QRegularExpression( + QStringLiteral("^\\d+\\.\\d+\\.\\d+\\.\\d+$") + ); return (type == Type::Socks5 || type == Type::Mtproto) && !qthelp::is_ipv6(host) - && !QRegularExpression( - QStringLiteral("^\\d+\\.\\d+\\.\\d+\\.\\d+$") - ).match(host).hasMatch(); + && !RegExp.match(host).hasMatch(); } bytes::vector ProxyData::secretFromMtprotoPassword() const { diff --git a/Telegram/SourceFiles/mtproto/mtproto_response.cpp b/Telegram/SourceFiles/mtproto/mtproto_response.cpp index 0c0a716040..7729044334 100644 --- a/Telegram/SourceFiles/mtproto/mtproto_response.cpp +++ b/Telegram/SourceFiles/mtproto/mtproto_response.cpp @@ -25,11 +25,11 @@ namespace { Error::Error(const MTPrpcError &error) : _code(error.c_rpc_error().verror_code().v) { QString text = qs(error.c_rpc_error().verror_message()); - const auto expression = QRegularExpression( + static const auto Expression = QRegularExpression( "^([A-Z0-9_]+)(: .*)?$", (QRegularExpression::DotMatchesEverythingOption | QRegularExpression::MultilineOption)); - const auto match = expression.match(text); + const auto match = Expression.match(text); if (match.hasMatch()) { _type = match.captured(1); _description = match.captured(2).mid(2); diff --git a/Telegram/SourceFiles/mtproto/special_config_request.cpp b/Telegram/SourceFiles/mtproto/special_config_request.cpp index d0ca191879..9736518110 100644 --- a/Telegram/SourceFiles/mtproto/special_config_request.cpp +++ b/Telegram/SourceFiles/mtproto/special_config_request.cpp @@ -62,8 +62,9 @@ QString InstanceId() { } bool CheckPhoneByPrefixesRules(const QString &phone, const QString &rules) { + static const auto RegExp = QRegularExpression("[^0-9]"); const auto check = QString(phone).replace( - QRegularExpression("[^0-9]"), + RegExp, QString()); auto result = false; for (const auto &prefix : rules.split(',')) { diff --git a/Telegram/SourceFiles/passport/passport_panel_controller.cpp b/Telegram/SourceFiles/passport/passport_panel_controller.cpp index ddfd24ce42..14ee9bd789 100644 --- a/Telegram/SourceFiles/passport/passport_panel_controller.cpp +++ b/Telegram/SourceFiles/passport/passport_panel_controller.cpp @@ -152,11 +152,12 @@ EditDocumentScheme GetDocumentScheme( }; using Result = std::optional; const auto NameValidate = [](const QString &value) -> Result { + static const auto RegExp = QRegularExpression( + "^[a-zA-Z0-9\\.,/&\\-' ]+$" + ); if (value.isEmpty() || value.size() > kMaxNameSize) { return QString(); - } else if (!QRegularExpression( - "^[a-zA-Z0-9\\.,/&\\-' ]+$" - ).match(value).hasMatch()) { + } else if (!RegExp.match(value).hasMatch()) { return tr::lng_passport_bad_name(tr::now); } return std::nullopt; @@ -167,14 +168,16 @@ EditDocumentScheme GetDocumentScheme( const auto StreetValidate = LimitedValidate(kMaxStreetSize); const auto CityValidate = LimitedValidate(kMaxCitySize, kMinCitySize); const auto PostcodeValidate = FromBoolean([](const QString &value) { - return QRegularExpression( + static const auto RegExp = QRegularExpression( QString("^[a-zA-Z0-9\\-]{2,%1}$").arg(kMaxPostcodeSize) - ).match(value).hasMatch(); + ); + return RegExp.match(value).hasMatch(); }); const auto DateValidateBoolean = [](const QString &value) { - return QRegularExpression( + static const auto RegExp = QRegularExpression( "^\\d{2}\\.\\d{2}\\.\\d{4}$" - ).match(value).hasMatch(); + ); + return RegExp.match(value).hasMatch(); }; const auto DateValidate = FromBoolean(DateValidateBoolean); const auto DateOrEmptyValidate = FromBoolean([=](const QString &value) { @@ -479,9 +482,8 @@ EditContactScheme GetContactScheme(Scope::Type type) { result.newHeader = tr::lng_passport_new_phone(tr::now); result.aboutNew = tr::lng_passport_new_phone_code(tr::now); result.validate = [](const QString &value) { - return QRegularExpression( - "^\\d{2,12}$" - ).match(value).hasMatch(); + static const auto RegExp = QRegularExpression("^\\d{2,12}$"); + return RegExp.match(value).hasMatch(); }; result.format = [](const QString &value) { return Ui::FormatPhone(value); diff --git a/Telegram/SourceFiles/passport/ui/passport_details_row.cpp b/Telegram/SourceFiles/passport/ui/passport_details_row.cpp index 2fc7c3698a..b50ff5164b 100644 --- a/Telegram/SourceFiles/passport/ui/passport_details_row.cpp +++ b/Telegram/SourceFiles/passport/ui/passport_details_row.cpp @@ -51,7 +51,8 @@ PostcodeInput::PostcodeInput( rpl::producer placeholder, const QString &val) : MaskedInputField(parent, st, std::move(placeholder), val) { - if (!QRegularExpression("^[a-zA-Z0-9\\-]+$").match(val).hasMatch()) { + static const auto RegExp = QRegularExpression("^[a-zA-Z0-9\\-]+$"); + if (!RegExp.match(val).hasMatch()) { setText(QString()); } } @@ -414,8 +415,9 @@ void CountryRow::chooseCountry() { } QDate ValidateDate(const QString &value) { - const auto match = QRegularExpression( - "^([0-9]{2})\\.([0-9]{2})\\.([0-9]{4})$").match(value); + static const auto RegExp = QRegularExpression( + "^([0-9]{2})\\.([0-9]{2})\\.([0-9]{4})$"); + const auto match = RegExp.match(value); if (!match.hasMatch()) { return QDate(); } diff --git a/Telegram/SourceFiles/payments/smartglocal/smartglocal_card.cpp b/Telegram/SourceFiles/payments/smartglocal/smartglocal_card.cpp index 0423216ec6..a08b0980da 100644 --- a/Telegram/SourceFiles/payments/smartglocal/smartglocal_card.cpp +++ b/Telegram/SourceFiles/payments/smartglocal/smartglocal_card.cpp @@ -54,8 +54,9 @@ bool Card::empty() const { } QString Last4(const Card &card) { + static const auto RegExp = QRegularExpression("[^\\d]\\d*(\\d{4})$"); const auto masked = card.maskedNumber(); - const auto m = QRegularExpression("[^\\d]\\d*(\\d{4})$").match(masked); + const auto m = RegExp.match(masked); return m.hasMatch() ? m.captured(1) : QString(); } diff --git a/Telegram/SourceFiles/payments/stripe/stripe_card_validator.cpp b/Telegram/SourceFiles/payments/stripe/stripe_card_validator.cpp index e6542adf0f..99a037e22e 100644 --- a/Telegram/SourceFiles/payments/stripe/stripe_card_validator.cpp +++ b/Telegram/SourceFiles/payments/stripe/stripe_card_validator.cpp @@ -90,11 +90,13 @@ struct BinRange { } [[nodiscard]] bool IsNumeric(const QString &value) { - return QRegularExpression("^[0-9]*$").match(value).hasMatch(); + static const auto RegExp = QRegularExpression("^[0-9]*$"); + return RegExp.match(value).hasMatch(); } [[nodiscard]] QString RemoveWhitespaces(QString value) { - return value.replace(QRegularExpression("\\s"), QString()); + static const auto RegExp = QRegularExpression("\\s"); + return value.replace(RegExp, QString()); } [[nodiscard]] std::vector BinRangesForNumber( diff --git a/Telegram/SourceFiles/payments/ui/payments_edit_card.cpp b/Telegram/SourceFiles/payments/ui/payments_edit_card.cpp index 34efdc5ccc..ec18ef6d27 100644 --- a/Telegram/SourceFiles/payments/ui/payments_edit_card.cpp +++ b/Telegram/SourceFiles/payments/ui/payments_edit_card.cpp @@ -39,7 +39,8 @@ struct SimpleFieldState { } [[nodiscard]] QString RemoveNonNumbers(QString value) { - return value.replace(QRegularExpression("[^0-9]"), QString()); + static const auto RegExp = QRegularExpression("[^0-9]"); + return value.replace(RegExp, QString()); } [[nodiscard]] SimpleFieldState NumbersOnlyState(SimpleFieldState state) { diff --git a/Telegram/SourceFiles/payments/ui/payments_field.cpp b/Telegram/SourceFiles/payments/ui/payments_field.cpp index 73c87dff0b..e182714e30 100644 --- a/Telegram/SourceFiles/payments/ui/payments_field.cpp +++ b/Telegram/SourceFiles/payments/ui/payments_field.cpp @@ -36,7 +36,8 @@ struct SimpleFieldState { } [[nodiscard]] QString RemoveNonNumbers(QString value) { - return value.replace(QRegularExpression("[^0-9]"), QString()); + static const auto RegExp = QRegularExpression("[^0-9]"); + return value.replace(RegExp, QString()); } [[nodiscard]] SimpleFieldState CleanMoneyState( @@ -216,6 +217,7 @@ struct SimpleFieldState { const FieldConfig &config, const QString &parsed, const QString &countryIso2) { + static const auto RegExp = QRegularExpression("[^0-9]\\."); if (config.type == FieldType::Country) { return countryIso2; } else if (config.type == FieldType::Money) { @@ -227,16 +229,14 @@ struct SimpleFieldState { QChar(','), QChar('.') ).replace( - QRegularExpression("[^0-9\\.]"), + RegExp, QString() ).toDouble(); return QString::number( int64(base::SafeRound(real * std::pow(10., rule.exponent)))); } else if (config.type == FieldType::CardNumber || config.type == FieldType::CardCVC) { - return QString(parsed).replace( - QRegularExpression("[^0-9\\.]"), - QString()); + return QString(parsed).replace(RegExp, QString()); } return parsed; } diff --git a/Telegram/SourceFiles/statistics/statistics_data_deserialize.cpp b/Telegram/SourceFiles/statistics/statistics_data_deserialize.cpp index 9c3c90274d..355c500c16 100644 --- a/Telegram/SourceFiles/statistics/statistics_data_deserialize.cpp +++ b/Telegram/SourceFiles/statistics/statistics_data_deserialize.cpp @@ -139,7 +139,8 @@ Data::StatisticalChart StatisticalChartFromJSON(const QByteArray &json) { for (auto &line : result.lines) { const auto colorIt = colors.constFind(line.idString); if (colorIt != colors.constEnd() && (*colorIt).isString()) { - const auto match = QRegularExpression(colorPattern).match( + static const auto RegExp = QRegularExpression(u"(.*)(#.*)"_q); + const auto match = RegExp.match( colorIt->toString()); if (match.hasMatch()) { line.colorKey = match.captured(1); diff --git a/Telegram/SourceFiles/storage/localstorage.cpp b/Telegram/SourceFiles/storage/localstorage.cpp index 2d1c5e9535..2958024b5e 100644 --- a/Telegram/SourceFiles/storage/localstorage.cpp +++ b/Telegram/SourceFiles/storage/localstorage.cpp @@ -579,8 +579,9 @@ void writeAutoupdatePrefix(const QString &prefix) { QString readAutoupdatePrefix() { Expects(!Core::UpdaterDisabled()); + static const auto RegExp = QRegularExpression("/+$"); auto result = readAutoupdatePrefixRaw(); - return result.replace(QRegularExpression("/+$"), QString()); + return result.replace(RegExp, QString()); } void writeBackground(const Data::WallPaper &paper, const QImage &image) { diff --git a/Telegram/SourceFiles/ui/boxes/country_select_box.cpp b/Telegram/SourceFiles/ui/boxes/country_select_box.cpp index 0d21dacdb5..bb7cc70bf5 100644 --- a/Telegram/SourceFiles/ui/boxes/country_select_box.cpp +++ b/Telegram/SourceFiles/ui/boxes/country_select_box.cpp @@ -234,13 +234,14 @@ void CountrySelectBox::Inner::init() { } auto index = 0; for (const auto &info : _list) { + static const auto RegExp = QRegularExpression("[\\s\\-]"); auto full = info.country + ' ' + (!info.alternativeName.isEmpty() ? info.alternativeName : QString()); const auto namesList = std::move(full).toLower().split( - QRegularExpression("[\\s\\-]"), + RegExp, Qt::SkipEmptyParts); auto &names = _namesList.emplace_back(); names.reserve(namesList.size());