From 98e0b95635fdf48f79064766ffca330d7bbd390b Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Sun, 14 Mar 2021 14:04:30 +0100 Subject: [PATCH] Reduce allocations when escaping emoji --- src/Utils.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Utils.cpp b/src/Utils.cpp index 7106d865..5c03c52f 100644 --- a/src/Utils.cpp +++ b/src/Utils.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -66,7 +67,8 @@ utils::codepointIsEmoji(uint code) QString utils::replaceEmoji(const QString &body) { - QString fmtBody = ""; + QString fmtBody; + fmtBody.reserve(body.size()); QVector 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("emojiFont() + "\">"); + fmtBody += QStringLiteral("emojiFont() % + QStringLiteral("\">"); insideFontBlock = true; } } else { if (insideFontBlock) { - fmtBody += ""; + fmtBody += QStringLiteral(""); insideFontBlock = false; } } - fmtBody += QString::fromUcs4(&code, 1); + if (QChar::requiresSurrogates(code)) { + QChar emoji[] = {static_cast(QChar::highSurrogate(code)), + static_cast(QChar::lowSurrogate(code))}; + fmtBody.append(emoji, 2); + } else { + fmtBody.append(QChar(static_cast(code))); + } } if (insideFontBlock) { - fmtBody += ""; + fmtBody += QStringLiteral(""); } return fmtBody;