Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
perf(smileys): create global regex object
Browse files Browse the repository at this point in the history
This prevents recompiling the regex for every message
  • Loading branch information
sudden6 committed Jan 16, 2019
1 parent 58f8a14 commit 0f90abe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/persistence/smileypack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,39 +245,50 @@ bool SmileyPack::load(const QString& filename)
emoticons.append(emoticonList);
}

constructRegex();

loadingMutex.unlock();
return true;
}

/**
* @brief Replaces all found text emoticons to HTML reference with its according icon filename
* @param msg Message where to search for emoticons
* @return Formatted copy of message
* @brief Creates the regex for replacing emoticons with the path to their pictures
*/
QString SmileyPack::smileyfied(const QString& msg)
void SmileyPack::constructRegex()
{
QMutexLocker locker(&loadingMutex);
QString result(msg);
QRegularExpression exp;
QString allPattern = QStringLiteral("(");


for ( auto r = emoticonToPath.begin(); r != emoticonToPath.end(); ++r) {
if (r.key().toUcs4().length() == 1) {
// construct one big regex that matches on every emoticon
for (const QString& emote : emoticonToPath.keys()) {
if (emote.toUcs4().length() == 1) {
// UTF-8 emoji
allPattern = allPattern % r.key();
allPattern = allPattern % emote;
} else {
// patterns like ":)" or ":smile:", don't match inside a word or else will hit punctuation and html tags
allPattern = allPattern % QStringLiteral(R"((?<=^|\s))") % QRegularExpression::escape(r.key()) % QStringLiteral(R"((?=$|\s))");
allPattern = allPattern % QStringLiteral(R"((?<=^|\s))") % QRegularExpression::escape(emote) % QStringLiteral(R"((?=$|\s))");
}
allPattern = allPattern % QStringLiteral("|");
}

allPattern[allPattern.size() - 1] = QChar(')');

exp.setPattern(allPattern);
// compile and optimize regex
smilify.setPattern(allPattern);
smilify.optimize();
}

/**
* @brief Replaces all found text emoticons to HTML reference with its according icon filename
* @param msg Message where to search for emoticons
* @return Formatted copy of message
*/
QString SmileyPack::smileyfied(const QString& msg)
{
QMutexLocker locker(&loadingMutex);
QString result(msg);

int replaceDiff = 0;
QRegularExpressionMatchIterator iter = exp.globalMatch(result);
QRegularExpressionMatchIterator iter = smilify.globalMatch(result);
while (iter.hasNext()) {
QRegularExpressionMatch match = iter.next();
int startPos = match.capturedStart();
Expand Down
3 changes: 3 additions & 0 deletions src/persistence/smileypack.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <QIcon>
#include <QMap>
#include <QMutex>
#include <QRegularExpression>

#include <memory>

Expand Down Expand Up @@ -52,12 +53,14 @@ private slots:
~SmileyPack() override;

bool load(const QString& filename);
void constructRegex();

mutable std::map<QString, std::shared_ptr<QIcon>> cachedIcon;
QHash<QString, QString> emoticonToPath;
QList<QStringList> emoticons;
QString path;
QTimer* cleanupTimer;
QRegularExpression smilify;
mutable QMutex loadingMutex;
};

Expand Down

0 comments on commit 0f90abe

Please sign in to comment.