Simple optimisation in toLocal8Bit(): call codecForLocale once only
authorThiago Macieira <thiago.macieira@intel.com>
Wed, 15 Aug 2012 16:15:06 +0000 (18:15 +0200)
committerQt by Nokia <qt-info@nokia.com>
Wed, 22 Aug 2012 22:19:25 +0000 (00:19 +0200)
The function is only slow the first time we call it, but there's no
reason we can't do this simple optimisation anyway.

Change-Id: Icacbbeb340838b32f5278b76d1860ad22dc9f7b7
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
src/corelib/tools/qstring.cpp

index ff5e24d..6b47d6a 100644 (file)
@@ -3931,8 +3931,9 @@ QByteArray QString::toLatin1() const
 static QByteArray toLocal8Bit_helper(const QChar *data, int length)
 {
 #ifndef QT_NO_TEXTCODEC
-    if (QTextCodec::codecForLocale())
-        return QTextCodec::codecForLocale()->fromUnicode(data, length);
+    QTextCodec *localeCodec = QTextCodec::codecForLocale();
+    if (localeCodec)
+        return localeCodec->fromUnicode(data, length);
 #endif // QT_NO_TEXTCODEC
     return toLatin1_helper(data, length);
 }
@@ -3956,8 +3957,9 @@ static QByteArray toLocal8Bit_helper(const QChar *data, int length)
 QByteArray QString::toLocal8Bit() const
 {
 #ifndef QT_NO_TEXTCODEC
-    if (QTextCodec::codecForLocale())
-        return QTextCodec::codecForLocale()->fromUnicode(*this);
+    QTextCodec *localeCodec = QTextCodec::codecForLocale();
+    if (localeCodec)
+        return localeCodec->fromUnicode(*this);
 #endif // QT_NO_TEXTCODEC
     return toLatin1();
 }
@@ -9080,8 +9082,9 @@ QByteArray QStringRef::toLatin1() const
 QByteArray QStringRef::toLocal8Bit() const
 {
 #ifndef QT_NO_TEXTCODEC
-    if (QTextCodec::codecForLocale())
-        return QTextCodec::codecForLocale()->fromUnicode(unicode(), length());
+    QTextCodec *localeCodec = QTextCodec::codecForLocale();
+    if (localeCodec)
+        return localeCodec->fromUnicode(unicode(), length());
 #endif // QT_NO_TEXTCODEC
     return toLatin1();
 }