Skip to content
This repository has been archived by the owner on Feb 12, 2023. It is now read-only.

Commit

Permalink
fix(chatlog): parse multi-length emoji properly
Browse files Browse the repository at this point in the history
Before, when multi-length emoji were next to any other character, they would not be transformed into images. With this change, emoji are replaced with images no matter where they are in the string. Parsing is now done by checking to see if two-character blocks are valid as a single UTF-32 character, or if they are truly two distinct characters. Because we no longer need white space before emoji, I also removed our addition of spaces on either side of an emoji when a user sends them.
  • Loading branch information
anthonybilinski committed Feb 5, 2018
1 parent 0dea039 commit 5df63f9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
30 changes: 17 additions & 13 deletions src/persistence/smileypack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,25 @@ QString SmileyPack::smileyfied(const QString& msg)
{
QMutexLocker locker(&loadingMutex);
QString result(msg);
QRegularExpression exp("\\S+");
QRegularExpressionMatchIterator iter = exp.globalMatch(result);
int replaceDiff = 0;
while (iter.hasNext()) {
QRegularExpressionMatch match = iter.next();
QString key = match.captured();
int startPos = match.capturedStart();
int keyLength = key.length();
if (emoticonToPath.find(key) != emoticonToPath.end()) {
QString imgRichText = getAsRichText(key);
result.replace(startPos + replaceDiff, keyLength, imgRichText);
replaceDiff += imgRichText.length() - keyLength;
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;
}
else {
curChar = msg.midRef(strIndex, 1);
curCharLength = 1;
}
if (emoticonToPath.find(curChar.toString()) != emoticonToPath.end()) {
QString imgRichText = getAsRichText(curChar.toString());
result.replace(strIndex + replaceDiff, curCharLength, imgRichText);
replaceDiff += imgRichText.length() - curCharLength;
}
}

return result;
}

Expand Down
2 changes: 1 addition & 1 deletion src/widget/emoticonswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void EmoticonsWidget::onSmileyClicked()
if (sender) {
QString sequence =
sender->property("sequence").toString().replace("&lt;", "<").replace("&gt;", ">");
emit insertEmoticon(' ' + sequence + ' ');
emit insertEmoticon(sequence);
}
}

Expand Down

0 comments on commit 5df63f9

Please sign in to comment.