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 <QScreen>
#include <QSettings>
#include <QStringBuilder>
#include <QTextDocument>
#include <QXmlStreamReader>
@ -66,7 +67,8 @@ utils::codepointIsEmoji(uint code)
QString
utils::replaceEmoji(const QString &body)
{
QString fmtBody = "";
QString fmtBody;
fmtBody.reserve(body.size());
QVector<uint> utf32_string = body.toUcs4();
@ -74,21 +76,28 @@ utils::replaceEmoji(const QString &body)
for (auto &code : utf32_string) {
if (utils::codepointIsEmoji(code)) {
if (!insideFontBlock) {
fmtBody += QString("<font face=\"" +
UserSettings::instance()->emojiFont() + "\">");
fmtBody += QStringLiteral("<font face=\"") %
UserSettings::instance()->emojiFont() %
QStringLiteral("\">");
insideFontBlock = true;
}
} else {
if (insideFontBlock) {
fmtBody += "</font>";
fmtBody += QStringLiteral("</font>");
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) {
fmtBody += "</font>";
fmtBody += QStringLiteral("</font>");
}
return fmtBody;