mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
perf(smileys): create global regex object
This prevents recompiling the regex for every message
This commit is contained in:
parent
58f8a14a48
commit
0f90abebdd
|
@ -245,10 +245,38 @@ bool SmileyPack::load(const QString& filename)
|
||||||
emoticons.append(emoticonList);
|
emoticons.append(emoticonList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
constructRegex();
|
||||||
|
|
||||||
loadingMutex.unlock();
|
loadingMutex.unlock();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Creates the regex for replacing emoticons with the path to their pictures
|
||||||
|
*/
|
||||||
|
void SmileyPack::constructRegex()
|
||||||
|
{
|
||||||
|
QString allPattern = QStringLiteral("(");
|
||||||
|
|
||||||
|
// 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 % 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(emote) % QStringLiteral(R"((?=$|\s))");
|
||||||
|
}
|
||||||
|
allPattern = allPattern % QStringLiteral("|");
|
||||||
|
}
|
||||||
|
|
||||||
|
allPattern[allPattern.size() - 1] = QChar(')');
|
||||||
|
|
||||||
|
// compile and optimize regex
|
||||||
|
smilify.setPattern(allPattern);
|
||||||
|
smilify.optimize();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Replaces all found text emoticons to HTML reference with its according icon filename
|
* @brief Replaces all found text emoticons to HTML reference with its according icon filename
|
||||||
* @param msg Message where to search for emoticons
|
* @param msg Message where to search for emoticons
|
||||||
|
@ -258,26 +286,9 @@ QString SmileyPack::smileyfied(const QString& msg)
|
||||||
{
|
{
|
||||||
QMutexLocker locker(&loadingMutex);
|
QMutexLocker locker(&loadingMutex);
|
||||||
QString result(msg);
|
QString result(msg);
|
||||||
QRegularExpression exp;
|
|
||||||
QString allPattern = QStringLiteral("(");
|
|
||||||
|
|
||||||
|
|
||||||
for ( auto r = emoticonToPath.begin(); r != emoticonToPath.end(); ++r) {
|
|
||||||
if (r.key().toUcs4().length() == 1) {
|
|
||||||
// UTF-8 emoji
|
|
||||||
allPattern = allPattern % r.key();
|
|
||||||
} 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("|");
|
|
||||||
}
|
|
||||||
|
|
||||||
allPattern[allPattern.size() - 1] = QChar(')');
|
|
||||||
|
|
||||||
exp.setPattern(allPattern);
|
|
||||||
int replaceDiff = 0;
|
int replaceDiff = 0;
|
||||||
QRegularExpressionMatchIterator iter = exp.globalMatch(result);
|
QRegularExpressionMatchIterator iter = smilify.globalMatch(result);
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
QRegularExpressionMatch match = iter.next();
|
QRegularExpressionMatch match = iter.next();
|
||||||
int startPos = match.capturedStart();
|
int startPos = match.capturedStart();
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
#include <QIcon>
|
#include <QIcon>
|
||||||
#include <QMap>
|
#include <QMap>
|
||||||
#include <QMutex>
|
#include <QMutex>
|
||||||
|
#include <QRegularExpression>
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
@ -52,12 +53,14 @@ private:
|
||||||
~SmileyPack() override;
|
~SmileyPack() override;
|
||||||
|
|
||||||
bool load(const QString& filename);
|
bool load(const QString& filename);
|
||||||
|
void constructRegex();
|
||||||
|
|
||||||
mutable std::map<QString, std::shared_ptr<QIcon>> cachedIcon;
|
mutable std::map<QString, std::shared_ptr<QIcon>> cachedIcon;
|
||||||
QHash<QString, QString> emoticonToPath;
|
QHash<QString, QString> emoticonToPath;
|
||||||
QList<QStringList> emoticons;
|
QList<QStringList> emoticons;
|
||||||
QString path;
|
QString path;
|
||||||
QTimer* cleanupTimer;
|
QTimer* cleanupTimer;
|
||||||
|
QRegularExpression smilify;
|
||||||
mutable QMutex loadingMutex;
|
mutable QMutex loadingMutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user