mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-22 11:00:48 +03:00
Reduce allocations when escaping emoji
This commit is contained in:
parent
c47ae99805
commit
98e0b95635
1 changed files with 15 additions and 6 deletions
|
@ -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;
|
||||||
|
|
Loading…
Reference in a new issue