|
|
|
@ -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(); |
|
|
|
|