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); QMutexLocker locker(&loadingMutex);
QString result(msg); QString result(msg);
int replaceDiff = 0; // how much we have increased message size by replacing emoji with image tags for ( auto r = emoticonToPath.begin(); r != emoticonToPath.end(); ++r) {
int curCharLength; QRegularExpression exp;
QStringRef curChar; // the current UTF-32 which we are examining, which may look like a 2-length QString if (r.key().toUcs4().length() == 1) {
for (int strIndex = 0; strIndex < msg.length(); strIndex += curCharLength) { // UTF-8 emoji
if ((msg.length() - strIndex >= 2) && ((curChar = msg.midRef(strIndex, 2)).toUcs4().length() == 1)) { exp.setPattern(r.key());
// 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;
} }
else { else {
curChar = msg.midRef(strIndex, 1); // patterns like ":)" or ":smile:", don't match inside a word or else will hit punctuation and html tags
curCharLength = 1; exp.setPattern(QStringLiteral(R"((?<=^|\s))") + QRegularExpression::escape(r.key()) + QStringLiteral(R"((?=$|\s))"));
} }
if (emoticonToPath.find(curChar.toString()) != emoticonToPath.end()) { int replaceDiff = 0;
QString imgRichText = getAsRichText(curChar.toString()); QRegularExpressionMatchIterator iter = exp.globalMatch(result);
result.replace(strIndex + replaceDiff, curCharLength, imgRichText); while (iter.hasNext()) {
replaceDiff += imgRichText.length() - curCharLength; 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; return result;