Reduce allocations when escaping emoji

This commit is contained in:
Nicolas Werner 2021-03-14 14:04:30 +01:00
parent c47ae99805
commit 98e0b95635
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9

View file

@ -13,6 +13,7 @@
#include <QProcessEnvironment> #include <QProcessEnvironment>
#include <QScreen> #include <QScreen>
#include <QSettings> #include <QSettings>
#include <QStringBuilder>
#include <QTextDocument> #include <QTextDocument>
#include <QXmlStreamReader> #include <QXmlStreamReader>
@ -66,7 +67,8 @@ utils::codepointIsEmoji(uint code)
QString QString
utils::replaceEmoji(const QString &body) utils::replaceEmoji(const QString &body)
{ {
QString fmtBody = ""; QString fmtBody;
fmtBody.reserve(body.size());
QVector<uint> utf32_string = body.toUcs4(); QVector<uint> utf32_string = body.toUcs4();
@ -74,21 +76,28 @@ utils::replaceEmoji(const QString &body)
for (auto &code : utf32_string) { for (auto &code : utf32_string) {
if (utils::codepointIsEmoji(code)) { if (utils::codepointIsEmoji(code)) {
if (!insideFontBlock) { if (!insideFontBlock) {
fmtBody += QString("<font face=\"" + fmtBody += QStringLiteral("<font face=\"") %
UserSettings::instance()->emojiFont() + "\">"); UserSettings::instance()->emojiFont() %
QStringLiteral("\">");
insideFontBlock = true; insideFontBlock = true;
} }
} else { } else {
if (insideFontBlock) { if (insideFontBlock) {
fmtBody += "</font>"; fmtBody += QStringLiteral("</font>");
insideFontBlock = false; insideFontBlock = false;
} }
} }
fmtBody += QString::fromUcs4(&code, 1); if (QChar::requiresSurrogates(code)) {
QChar emoji[] = {static_cast<ushort>(QChar::highSurrogate(code)),
static_cast<ushort>(QChar::lowSurrogate(code))};
fmtBody.append(emoji, 2);
} else {
fmtBody.append(QChar(static_cast<ushort>(code)));
}
} }
if (insideFontBlock) { if (insideFontBlock) {
fmtBody += "</font>"; fmtBody += QStringLiteral("</font>");
} }
return fmtBody; return fmtBody;