From d95f9e778b5c38ef98182d1749ae50e2ae822471 Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Fri, 22 Dec 2023 03:42:31 +0400 Subject: [PATCH] Instantiate QRegularExpression instances statically --- ui/basic_click_handlers.cpp | 6 ++++-- ui/text/text_entity.cpp | 19 ++++++++++--------- ui/text/text_entity.h | 2 +- ui/widgets/fields/time_part_input.cpp | 3 ++- ui/widgets/time_input.cpp | 5 +++-- 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/ui/basic_click_handlers.cpp b/ui/basic_click_handlers.cpp index 91e52e7..32a23a7 100644 --- a/ui/basic_click_handlers.cpp +++ b/ui/basic_click_handlers.cpp @@ -64,9 +64,11 @@ QString UrlClickHandler::EncodeForOpening(const QString &originalUrl) { ? QString::fromUtf8(good.toEncoded()) : originalUrl; + static const auto RegExp = QRegularExpression( + QStringLiteral("^[a-zA-Z]+:")); + if (!result.isEmpty() - && !QRegularExpression( - QStringLiteral("^[a-zA-Z]+:")).match(result).hasMatch()) { + && !RegExp.match(result).hasMatch()) { // No protocol. return QStringLiteral("https://") + result; } diff --git a/ui/text/text_entity.cpp b/ui/text/text_entity.cpp index 383b3d0..65c9dee 100644 --- a/ui/text/text_entity.cpp +++ b/ui/text/text_entity.cpp @@ -1236,8 +1236,9 @@ const QRegularExpression &RegExpBotCommand() { return result; } -QRegularExpression RegExpDigitsExclude() { - return QRegularExpression("[^\\d]"); +const QRegularExpression &RegExpDigitsExclude() { + static const auto result = QRegularExpression("[^\\d]"); + return result; } QString MarkdownBoldGoodBefore() { @@ -2225,9 +2226,10 @@ TextWithTags::Tags ConvertEntitiesToTextTags( }; switch (entity.type()) { case EntityType::MentionName: { - const auto match = QRegularExpression( + static const auto RegExp = QRegularExpression( "^(\\d+\\.\\d+:\\d+)$" - ).match(entity.data()); + ); + const auto match = RegExp.match(entity.data()); if (match.hasMatch()) { push(kMentionTagStart + entity.data()); } @@ -2240,9 +2242,8 @@ TextWithTags::Tags ConvertEntitiesToTextTags( } } break; case EntityType::CustomEmoji: { - const auto match = QRegularExpression( - "^(\\d+)$" - ).match(entity.data()); + static const auto RegExp = QRegularExpression("^(\\d+)$"); + const auto match = RegExp.match(entity.data()); if (match.hasMatch()) { push(Ui::InputField::CustomEmojiLink(entity.data())); } @@ -2261,8 +2262,8 @@ TextWithTags::Tags ConvertEntitiesToTextTags( case EntityType::Code: push(Ui::InputField::kTagCode); break; case EntityType::Pre: { if (!entity.data().isEmpty()) { - const auto language = QRegularExpression("^[a-z0-9\\-]+$"); - if (language.match(entity.data()).hasMatch()) { + static const auto Language = QRegularExpression("^[a-z0-9\\-]+$"); + if (Language.match(entity.data()).hasMatch()) { push(Ui::InputField::kTagPre + entity.data()); break; } diff --git a/ui/text/text_entity.h b/ui/text/text_entity.h index 2aa8385..10bddc2 100644 --- a/ui/text/text_entity.h +++ b/ui/text/text_entity.h @@ -293,7 +293,7 @@ const QRegularExpression &RegExpHashtag(); const QRegularExpression &RegExpHashtagExclude(); const QRegularExpression &RegExpMention(); const QRegularExpression &RegExpBotCommand(); -QRegularExpression RegExpDigitsExclude(); +const QRegularExpression &RegExpDigitsExclude(); QString MarkdownBoldGoodBefore(); QString MarkdownBoldBadAfter(); QString MarkdownItalicGoodBefore(); diff --git a/ui/widgets/fields/time_part_input.cpp b/ui/widgets/fields/time_part_input.cpp index 3767574..5ca80a8 100644 --- a/ui/widgets/fields/time_part_input.cpp +++ b/ui/widgets/fields/time_part_input.cpp @@ -14,12 +14,13 @@ namespace Ui { std::optional TimePart::number() { + static const auto RegExp = QRegularExpression("^\\d+$"); const auto text = getLastText(); auto view = QStringView(text); while (view.size() > 1 && view.at(0) == '0') { view = base::StringViewMid(view, 1); } - return QRegularExpression("^\\d+$").match(view).hasMatch() + return RegExp.match(view).hasMatch() ? std::make_optional(view.toInt()) : std::nullopt; } diff --git a/ui/widgets/time_input.cpp b/ui/widgets/time_input.cpp index 1317f3a..66041e6 100644 --- a/ui/widgets/time_input.cpp +++ b/ui/widgets/time_input.cpp @@ -17,8 +17,9 @@ namespace Ui { namespace { QTime ValidateTime(const QString &value) { - const auto match = QRegularExpression( - "^(\\d{1,2})\\:(\\d\\d)$").match(value); + static const auto RegExp = QRegularExpression( + "^(\\d{1,2})\\:(\\d\\d)$"); + const auto match = RegExp.match(value); if (!match.hasMatch()) { return QTime(); }