From 524be95e8b6485c24f311adb0e740dc2ae519a56 Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Thu, 28 Jun 2018 00:48:25 +0200 Subject: [PATCH] Reduce complexity of regular expression initialization --- src/common/LinkParser.cpp | 36 ++++++++++-------------------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/src/common/LinkParser.cpp b/src/common/LinkParser.cpp index 1d1e39702..1c081d8d9 100644 --- a/src/common/LinkParser.cpp +++ b/src/common/LinkParser.cpp @@ -1,30 +1,24 @@ #include "common/LinkParser.hpp" -#include "debug/Log.hpp" - #include #include #include #include -#include - namespace chatterino { -namespace { - -std::once_flag regexInitializedFlag; -QRegularExpression *linkRegex = nullptr; - -void initializeRegularExpressions() +LinkParser::LinkParser(const QString &unparsedString) { - std::call_once(regexInitializedFlag, [] { + static QRegularExpression linkRegex = [] { QFile tldFile(":/tlds.txt"); tldFile.open(QFile::ReadOnly); + QTextStream t1(&tldFile); t1.setCodec("UTF-8"); - QString tldData = t1.readAll(); - tldData.replace("\n", "|"); + + // Read the TLDs in and replace the newlines with pipes + QString tldData = t1.readAll().replace("\n", "|"); + const QString urlRegExp = "^" // protocol identifier @@ -57,21 +51,11 @@ void initializeRegularExpressions() // resource path "(?:[/?#]\\S*)?" "$"; - linkRegex = new QRegularExpression(urlRegExp, QRegularExpression::CaseInsensitiveOption); - Log("fully initialized"); - }); + return QRegularExpression(urlRegExp, QRegularExpression::CaseInsensitiveOption); + }(); - Log("call_once returned"); -} - -} // namespace - -LinkParser::LinkParser(const QString &unparsedString) -{ - initializeRegularExpressions(); - - this->match_ = linkRegex->match(unparsedString); + this->match_ = linkRegex.match(unparsedString); } } // namespace chatterino