From 860094d4e2614c0926f2af0e3538412ece31f726 Mon Sep 17 00:00:00 2001 From: Benjamin Poulain Date: Fri, 25 Jun 2010 16:05:25 +0200 Subject: Avoid memory allocation when converting from Gbk to unicode. This change is the equivalent of 19e1b32bdeeeb5c7865038cab97b40dbac0e6c42 and 987458462994497f764baf253baca0faabdb63cc but for the Gbk case. It improve performance by avoiding the constructor of QChar and by allocating the memory once instead of doing it for each character. This also fix QTBUG-11704. Reviewed-by: Andreas Kling --- src/plugins/codecs/cn/qgb18030codec.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) (limited to 'src/plugins/codecs') diff --git a/src/plugins/codecs/cn/qgb18030codec.cpp b/src/plugins/codecs/cn/qgb18030codec.cpp index 3f2eec78c2..e10c8b1802 100644 --- a/src/plugins/codecs/cn/qgb18030codec.cpp +++ b/src/plugins/codecs/cn/qgb18030codec.cpp @@ -319,7 +319,7 @@ QString QGbkCodec::convertToUnicode(const char* chars, int len, ConverterState * { uchar buf[2]; int nbuf = 0; - QChar replacement = QChar::ReplacementCharacter; + ushort replacement = QChar::ReplacementCharacter; if (state) { if (state->flags & ConvertInvalidToNull) replacement = QChar::Null; @@ -330,6 +330,9 @@ QString QGbkCodec::convertToUnicode(const char* chars, int len, ConverterState * int invalid = 0; QString result; + result.resize(len); + int unicodeLen = 0; + ushort *const resultData = reinterpret_cast(result.data()); //qDebug("QGbkDecoder::toUnicode(const char* chars = \"%s\", int len = %d)", chars, len); for (int i=0; i(u)); + ++unicodeLen; } else { - result += replacement; + resultData[unicodeLen] = replacement; + ++unicodeLen; ++invalid; } nbuf = 0; } else { // Error - result += replacement; + resultData[unicodeLen] = replacement; + ++unicodeLen; ++invalid; nbuf = 0; } break; } } + result.resize(unicodeLen); if (state) { state->remainingChars = nbuf; -- cgit v1.2.3