Browse Source

perf(smileys): create global regex object

This prevents recompiling the regex for every message
reviewable/pr5493/r2
sudden6 7 years ago
parent
commit
0f90abebdd
No known key found for this signature in database
GPG Key ID: 279509B499E032B9
  1. 39
      src/persistence/smileypack.cpp
  2. 3
      src/persistence/smileypack.h

39
src/persistence/smileypack.cpp

@ -245,39 +245,50 @@ bool SmileyPack::load(const QString& filename) @@ -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();

3
src/persistence/smileypack.h

@ -23,6 +23,7 @@ @@ -23,6 +23,7 @@
#include <QIcon>
#include <QMap>
#include <QMutex>
#include <QRegularExpression>
#include <memory>
@ -52,12 +53,14 @@ private: @@ -52,12 +53,14 @@ private:
~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;
};

Loading…
Cancel
Save