From b11f65332b07a8a32e41c8258abd3d0012dc812b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20Javier=20Merino=20Mor=C3=A1n?= Date: Sun, 5 Dec 2021 14:12:33 +0100 Subject: [PATCH] Don't create a QString for every char in decodeLine PlainTextDecoder::decodeLine() was creating a new QString via QString::fromUcs4() for every character. Instead, accumulate the characters in a QVector, and call QString::fromUcs4() once at the end of the function. --- src/decoders/PlainTextDecoder.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/decoders/PlainTextDecoder.cpp b/src/decoders/PlainTextDecoder.cpp index 9fcfc027..1c5a6dc0 100644 --- a/src/decoders/PlainTextDecoder.cpp +++ b/src/decoders/PlainTextDecoder.cpp @@ -69,12 +69,6 @@ void PlainTextDecoder::decodeLine(const Character *const characters, int count, // TODO should we ignore or respect the LINE_WRAPPED line property? - // note: we build up a QString and send it to the text stream rather writing into the text - // stream a character at a time because it is more efficient. - //(since QTextStream always deals with QStrings internally anyway) - QString plainText; - plainText.reserve(count); - // If we should remove leading whitespace find the first non-space character int start = 0; if (!_includeLeadingWhitespace) { @@ -109,14 +103,19 @@ void PlainTextDecoder::decodeLine(const Character *const characters, int count, } } + // note: we build up a QVector and send it to the text stream transformed into a QString + // rather than writing into the text stream a character at a time because it is more efficient. + //(since QTextStream always deals with QStrings internally anyway) + QVector characterBuffer; + characterBuffer.reserve(count); + for (int i = start; i < outputCount;) { if ((characters[i].rendition & RE_EXTENDED_CHAR) != 0) { ushort extendedCharLength = 0; const uint *chars = ExtendedCharTable::instance.lookupExtendedChar(characters[i].character, extendedCharLength); if (chars != nullptr) { - const QString s = QString::fromUcs4(chars, extendedCharLength); - plainText.append(s); - i += qMax(1, Character::stringWidth(s)); + characterBuffer.insert(characterBuffer.end(), extendedCharLength, *chars); + i += qMax(1, Character::stringWidth(chars, extendedCharLength)); } else { ++i; } @@ -129,12 +128,12 @@ void PlainTextDecoder::decodeLine(const Character *const characters, int count, // lost in some situation. One typical example is copying the result // of `dialog --infobox "qwe" 10 10` . if (characters[i].isRealCharacter || i <= realCharacterGuard) { - plainText.append(QString::fromUcs4(&characters[i].character, 1)); + characterBuffer.append(characters[i].character); i += qMax(1, characters[i].width()); } else { ++i; // should we 'break' directly here? } } } - *_output << plainText; + *_output << QString::fromUcs4(characterBuffer.data(), characterBuffer.size()); }