Avoid unneccessary duplication of string data.
authorAndrew den Exter <andrew.den-exter@nokia.com>
Wed, 22 Feb 2012 08:13:29 +0000 (18:13 +1000)
committerQt by Nokia <qt-info@nokia.com>
Wed, 29 Feb 2012 04:21:36 +0000 (05:21 +0100)
Check for the existence of new line characters before trying to replace
them.  There's some redundancy if the characters are found but for
single line strings we avoid the detach in replace.

Change-Id: I48ccc614601a6f356b3d2e68f617e112c100bbdd
Reviewed-by: Yann Bodson <yann.bodson@nokia.com>
src/quick/items/qquicktext.cpp

index 28491d8..fd8ffe5 100644 (file)
@@ -305,9 +305,17 @@ void QQuickTextPrivate::updateLayout()
                 formatModifiesFontSize = fontSizeModified;
             } else {
                 layout.clearAdditionalFormats();
-                multilengthEos = text.indexOf(QLatin1Char('\x9c'));
-                QString tmp = multilengthEos != -1 ? text.mid(0, multilengthEos) : text;
-                tmp.replace(QLatin1Char('\n'), QChar::LineSeparator);
+                QString tmp = text;
+                multilengthEos = tmp.indexOf(QLatin1Char('\x9c'));
+                if (multilengthEos != -1) {
+                    tmp = tmp.mid(0, multilengthEos);
+                    tmp.replace(QLatin1Char('\n'), QChar::LineSeparator);
+                } else if (tmp.contains(QLatin1Char('\n'))) {
+                    // Replace always does a detach.  Checking for the new line character first
+                    // means iterating over those items again if found but prevents a realloc
+                    // otherwise.
+                    tmp.replace(QLatin1Char('\n'), QChar::LineSeparator);
+                }
                 layout.setText(tmp);
             }
             textHasChanged = false;