Simple optimisation in toLocal8Bit(): call codecForLocale once only

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>
bb10
Thiago Macieira 2012-08-15 18:15:06 +02:00 committed by Qt by Nokia
parent 12491f35bb
commit a3e563c091
1 changed files with 9 additions and 6 deletions

View 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();
}