<commit_before>#include "common/LinkParser.hpp"

#include "debug/Log.hpp"

#include <QFile>
#include <QRegularExpression>
#include <QString>
#include <QTextStream>

#include <mutex>

namespace chatterino {

namespace {

std::once_flag regexInitializedFlag;
QRegularExpression *linkRegex = nullptr;

void initializeRegularExpressions()
{
    std::call_once(regexInitializedFlag, [] {
        QFile tldFile(":/tlds.txt");
        tldFile.open(QFile::ReadOnly);
        QTextStream t1(&tldFile);
        t1.setCodec("UTF-8");
        QString tldData = t1.readAll();
        tldData.replace("\n", "|");
        const QString urlRegExp =
            "^"
            // protocol identifier
            "(?:(?:https?|ftps?)://)?"
            // user:pass authentication
            "(?:\\S+(?::\\S*)?@)?"
            "(?:"
            // IP address dotted notation octets
            // excludes loopback network 0.0.0.0
            // excludes reserved space >= 172.16.31.10
            // excludes network & broacast addresses
            // (first & last IP address of each class)
            "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])"
            "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}"
            "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))"
            "|"
            // host name
            "(?:(?:[_a-z\\x{00a1}-\\x{ffff}0-9]-*)*[a-z\\x{00a1}-\\x{ffff}0-9]+)"
            // domain name
            "(?:\\.(?:[a-z\\x{00a1}-\\x{ffff}0-9]-*)*[a-z\\x{00a1}-\\x{ffff}0-9]+)*"
            // TLD identifier
            //"(?:\\.(?:[a-z\\x{00a1}-\\x{ffff}]{2,}))"
            "(?:[\\.](?:" +
            tldData +
            "))"
            "\\.?"
            ")"
            // port number
            "(?::\\d{2,5})?"
            // resource path
            "(?:[/?#]\\S*)?"
            "$";
        linkRegex = new QRegularExpression(urlRegExp, QRegularExpression::CaseInsensitiveOption);

        Log("fully initialized");
    });

    Log("call_once returned");
}

}  // namespace

LinkParser::LinkParser(const QString &unparsedString)
{
    initializeRegularExpressions();

    this->match_ = linkRegex->match(unparsedString);
}

}  // namespace chatterino<commit_msg>Reduce complexity of regular expression initialization<commit_after>#include "common/LinkParser.hpp"

#include <QFile>
#include <QRegularExpression>
#include <QString>
#include <QTextStream>

namespace chatterino {

LinkParser::LinkParser(const QString &unparsedString)
{
    static QRegularExpression linkRegex = [] {
        QFile tldFile(":/tlds.txt");
        tldFile.open(QFile::ReadOnly);

        QTextStream t1(&tldFile);
        t1.setCodec("UTF-8");

        // Read the TLDs in and replace the newlines with pipes
        QString tldData = t1.readAll().replace("\n", "|");

        const QString urlRegExp =
            "^"
            // protocol identifier
            "(?:(?:https?|ftps?)://)?"
            // user:pass authentication
            "(?:\\S+(?::\\S*)?@)?"
            "(?:"
            // IP address dotted notation octets
            // excludes loopback network 0.0.0.0
            // excludes reserved space >= 172.16.31.10
            // excludes network & broacast addresses
            // (first & last IP address of each class)
            "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])"
            "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}"
            "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))"
            "|"
            // host name
            "(?:(?:[_a-z\\x{00a1}-\\x{ffff}0-9]-*)*[a-z\\x{00a1}-\\x{ffff}0-9]+)"
            // domain name
            "(?:\\.(?:[a-z\\x{00a1}-\\x{ffff}0-9]-*)*[a-z\\x{00a1}-\\x{ffff}0-9]+)*"
            // TLD identifier
            //"(?:\\.(?:[a-z\\x{00a1}-\\x{ffff}]{2,}))"
            "(?:[\\.](?:" +
            tldData +
            "))"
            "\\.?"
            ")"
            // port number
            "(?::\\d{2,5})?"
            // resource path
            "(?:[/?#]\\S*)?"
            "$";

        return QRegularExpression(urlRegExp, QRegularExpression::CaseInsensitiveOption);
    }();

    this->match_ = linkRegex.match(unparsedString);
}

}  // namespace chatterino
{'fraction_non_alphanumeric': 0.22356058781016622, 'fraction_numerical': 0.03565405926282823, 'mean_word_length': 1.7569721115537849, 'pattern_counts': {'":': 2, '<': 12, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1043254', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1419, 'n_words': 204}