1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

fix(chatlog): Match multi-character emoticons again

Fixed after broken in https://github.com/qTox/qTox/pull/4940. Single-character UTF-8 emoji still work without whitespace on either side, but multi-character emoticon patterns like 😄 or :) do require surrounding whitespace, to avoid matching punctuation or HTML tags.
This commit is contained in:
Anthony Bilinski 2018-02-19 01:18:19 -08:00
parent 17cb76d87d
commit 9643e48ef1
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C

View File

@ -258,23 +258,25 @@ QString SmileyPack::smileyfied(const QString& msg)
{
QMutexLocker locker(&loadingMutex);
QString result(msg);
int replaceDiff = 0; // how much we have increased message size by replacing emoji with image tags
int curCharLength;
QStringRef curChar; // the current UTF-32 which we are examining, which may look like a 2-length QString
for (int strIndex = 0; strIndex < msg.length(); strIndex += curCharLength) {
if ((msg.length() - strIndex >= 2) && ((curChar = msg.midRef(strIndex, 2)).toUcs4().length() == 1)) {
// if converting two characters to UTF-32 gives us a single valid character, this is actually a single
// 4-byte character, with a QString length of 2
curCharLength = 2;
for ( auto r = emoticonToPath.begin(); r != emoticonToPath.end(); ++r) {
QRegularExpression exp;
if (r.key().toUcs4().length() == 1) {
// UTF-8 emoji
exp.setPattern(r.key());
}
else {
curChar = msg.midRef(strIndex, 1);
curCharLength = 1;
// patterns like ":)" or ":smile:", don't match inside a word or else will hit punctuation and html tags
exp.setPattern(QStringLiteral(R"((?<=^|\s))") + QRegularExpression::escape(r.key()) + QStringLiteral(R"((?=$|\s))"));
}
if (emoticonToPath.find(curChar.toString()) != emoticonToPath.end()) {
QString imgRichText = getAsRichText(curChar.toString());
result.replace(strIndex + replaceDiff, curCharLength, imgRichText);
replaceDiff += imgRichText.length() - curCharLength;
int replaceDiff = 0;
QRegularExpressionMatchIterator iter = exp.globalMatch(result);
while (iter.hasNext()) {
QRegularExpressionMatch match = iter.next();
int startPos = match.capturedStart();
int keyLength = r.key().length();
QString imgRichText = getAsRichText(r.key());
result.replace(startPos + replaceDiff, keyLength, imgRichText);
replaceDiff += imgRichText.length() - keyLength;
}
}
return result;