Commit b11f6533 authored by Luis Javier Merino's avatar Luis Javier Merino Committed by Tomaz Canabrava
Browse files

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<uint>, and call QString::fromUcs4() once at the
end of the function.
parent 5067aeca
......@@ -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<uint> 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<uint> 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());
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment