summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@nokia.com>2012-05-12 03:33:50 +0200
committerQt by Nokia <qt-info@nokia.com>2012-05-16 04:25:14 +0200
commitb25b3a9f8bee10d490d3160d1fb28bf0013c8e0b (patch)
tree0dc6ece26193cbc111789f639e7f8ac78796f4be
parent1756a84756807a9849aa507e77845dfdf31b8020 (diff)
Avoid using iconv for text conversion where possible
Try to use a builtin codec as codecForLocale() if possible first. Fall back and instantiate the iconv codec only if that failed. In addition, make sure we initialize the locale correctly before we try to setup the codec. Change-Id: I86d635f9d11e8ff93093f162e79fb37f3d85731b Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r--src/corelib/codecs/qiconvcodec.cpp6
-rw-r--r--src/corelib/codecs/qtextcodec.cpp61
-rw-r--r--src/corelib/kernel/qcoreapplication.cpp18
-rw-r--r--src/corelib/kernel/qcoreapplication_p.h2
4 files changed, 44 insertions, 43 deletions
diff --git a/src/corelib/codecs/qiconvcodec.cpp b/src/corelib/codecs/qiconvcodec.cpp
index be22ff9bca..84a8e8d39d 100644
--- a/src/corelib/codecs/qiconvcodec.cpp
+++ b/src/corelib/codecs/qiconvcodec.cpp
@@ -88,8 +88,6 @@ static Ptr_iconv_close ptr_iconv_close = 0;
QT_BEGIN_NAMESPACE
-extern bool qt_locale_initialized;
-
QIconvCodec::QIconvCodec()
: utf16Codec(0)
{
@@ -190,7 +188,7 @@ QString QIconvCodec::convertToUnicode(const char* chars, int len, ConverterState
}
} else {
QThreadStorage<QIconvCodec::IconvState *> *ts = toUnicodeState();
- if (!qt_locale_initialized || !ts) {
+ if (!ts) {
// we're running after the Q_GLOBAL_STATIC has been deleted
// or before the QCoreApplication initialization
// bad programmer, no cookie for you
@@ -346,7 +344,7 @@ QByteArray QIconvCodec::convertFromUnicode(const QChar *uc, int len, ConverterSt
IconvState *temporaryState = 0;
QThreadStorage<QIconvCodec::IconvState *> *ts = fromUnicodeState();
- IconvState *&state = (qt_locale_initialized && ts) ? ts->localData() : temporaryState;
+ IconvState *&state = ts ? ts->localData() : temporaryState;
if (!state) {
iconv_t cd = QIconvCodec::createIconv_t(0, UTF16);
if (cd != reinterpret_cast<iconv_t>(-1)) {
diff --git a/src/corelib/codecs/qtextcodec.cpp b/src/corelib/codecs/qtextcodec.cpp
index 916aefab26..23cf95a774 100644
--- a/src/corelib/codecs/qtextcodec.cpp
+++ b/src/corelib/codecs/qtextcodec.cpp
@@ -49,6 +49,9 @@
#include "qfile.h"
#include "qstringlist.h"
#include "qvarlengtharray.h"
+#if !defined(QT_BOOTSTRAPPED)
+#include <private/qcoreapplication_p.h>
+#endif
#ifdef Q_OS_UNIX
# include "qiconvcodec_p.h"
@@ -82,8 +85,7 @@
#include <stdlib.h>
#include <ctype.h>
#include <locale.h>
-#if defined (_XOPEN_UNIX) && !defined(Q_OS_QNX) && \
- !defined(Q_OS_OSF) && !defined(Q_OS_LINUX_ANDROID)
+#if defined (_XOPEN_UNIX) && !defined(Q_OS_QNX) && !defined(Q_OS_OSF) && !defined(Q_OS_LINUX_ANDROID)
# include <langinfo.h>
#endif
@@ -477,14 +479,11 @@ static bool try_locale_list(const char * const locale[], const QByteArray &lang)
static const char * const probably_koi8_rlocales[] = {
"ru", "ru_SU", "ru_RU", "russian", 0 };
-static QTextCodec * ru_RU_hack(const char * i) {
+static QTextCodec * ru_RU_hack(const char * i)
+{
QTextCodec * ru_RU_codec = 0;
-#if !defined(QT_NO_SETLOCALE)
QByteArray origlocale(setlocale(LC_CTYPE, i));
-#else
- QByteArray origlocale(i);
-#endif
// unicode koi8r latin5 name
// 0x044E 0xC0 0xEE CYRILLIC SMALL LETTER YU
// 0x042E 0xE0 0xCE CYRILLIC CAPITAL LETTER YU
@@ -500,16 +499,11 @@ static QTextCodec * ru_RU_hack(const char * i) {
qWarning("QTextCodec: Using KOI8-R, probe failed (%02x %02x %s)",
koi8r, latin5, i);
}
-#if !defined(QT_NO_SETLOCALE)
setlocale(LC_CTYPE, origlocale.constData());
-#endif
return ru_RU_codec;
}
-#endif
-
-#if !defined(Q_OS_WIN32) && !defined(Q_OS_WINCE)
static QTextCodec *checkForCodec(const QByteArray &name) {
QTextCodec *c = QTextCodec::codecForName(name);
if (!c) {
@@ -531,16 +525,25 @@ static void setupLocaleMapper()
localeMapper = QTextCodec::codecForName("System");
#else
-#ifndef QT_NO_ICONV
- localeMapper = QTextCodec::codecForName("System");
+#if !defined(QT_BOOTSTRAPPED)
+ QCoreApplicationPrivate::initLocale();
#endif
-
-#if defined (_XOPEN_UNIX) && !defined(Q_OS_QNX) && \
- !defined(Q_OS_OSF) && !defined(Q_OS_LINUX_ANDROID)
+ // First try getting the codecs name from nl_langinfo and see
+ // if we have a builtin codec for it.
+ // Only fall back to using iconv if we can't find a builtin codec
+ // This is because the builtin utf8 codec is around 5 times faster
+ // then the using QIconvCodec
+
+#if defined (_XOPEN_UNIX) && !defined(Q_OS_QNX) && !defined(Q_OS_OSF) && !defined(Q_OS_LINUX_ANDROID)
+ char *charset = nl_langinfo(CODESET);
+ if (charset)
+ localeMapper = QTextCodec::codecForName(charset);
+#endif
+#if !defined(QT_NO_ICONV) && !defined(QT_BOOTSTRAPPED)
if (!localeMapper) {
- char *charset = nl_langinfo (CODESET);
- if (charset)
- localeMapper = QTextCodec::codecForName(charset);
+ // no builtin codec for the locale found, let's try using iconv
+ (void) new QIconvCodec();
+ localeMapper = QTextCodec::codecForName("System");
}
#endif
@@ -556,11 +559,7 @@ static void setupLocaleMapper()
// First part is getting that locale name. First try setlocale() which
// definitely knows it, but since we cannot fully trust it, get ready
// to fall back to environment variables.
-#if !defined(QT_NO_SETLOCALE)
const QByteArray ctype = setlocale(LC_CTYPE, 0);
-#else
- const QByteArray ctype;
-#endif
// Get the first nonempty value from $LC_ALL, $LC_CTYPE, and $LANG
// environment variables.
@@ -723,13 +722,6 @@ static void setup()
(void)new QLatin1Codec;
(void)new QUtf8Codec;
-#if !defined(Q_OS_INTEGRITY)
-#if defined(Q_OS_UNIX) && !defined(QT_NO_ICONV) && !defined(QT_BOOTSTRAPPED)
- // QIconvCodec depends on the UTF-16 codec, so it needs to be created last
- (void) new QIconvCodec();
-#endif
-#endif
-
if (!localeMapper)
setupLocaleMapper();
}
@@ -1105,9 +1097,10 @@ void QTextCodec::setCodecForLocale(QTextCodec *c)
Returns a pointer to the codec most suitable for this locale.
On Windows, the codec will be based on a system locale. On Unix
- systems, starting with Qt 4.2, the codec will be using the \e
- iconv library. Note that in both cases the codec's name will be
- "System".
+ systems, the codec will might fall back to using the \e iconv
+ library if no builtin codec for the locale can be found.
+
+ Note that in these cases the codec's name will be "System".
*/
QTextCodec* QTextCodec::codecForLocale()
diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp
index c4530712a2..ad8c35bbab 100644
--- a/src/corelib/kernel/qcoreapplication.cpp
+++ b/src/corelib/kernel/qcoreapplication.cpp
@@ -230,7 +230,7 @@ bool QCoreApplicationPrivate::is_app_running = false;
// app closing down if true
bool QCoreApplicationPrivate::is_app_closing = false;
// initialized in qcoreapplication and in qtextstream autotest when setlocale is called.
-Q_CORE_EXPORT bool qt_locale_initialized = false;
+static bool qt_locale_initialized = false;
Q_CORE_EXPORT uint qGlobalPostedEventsCount()
{
@@ -405,6 +405,17 @@ QString qAppName()
return QCoreApplication::instance()->d_func()->appName();
}
+void QCoreApplicationPrivate::initLocale()
+{
+ if (qt_locale_initialized)
+ return;
+ qt_locale_initialized = true;
+#ifdef Q_OS_UNIX
+ setlocale(LC_ALL, "");
+#endif
+}
+
+
/*!
\class QCoreApplication
\brief The QCoreApplication class provides an event loop for console Qt
@@ -543,10 +554,7 @@ void QCoreApplication::init()
{
Q_D(QCoreApplication);
-#ifdef Q_OS_UNIX
- setlocale(LC_ALL, ""); // use correct char set mapping
- qt_locale_initialized = true;
-#endif
+ QCoreApplicationPrivate::initLocale();
Q_ASSERT_X(!self, "QCoreApplication", "there should be only one application object");
QCoreApplication::self = this;
diff --git a/src/corelib/kernel/qcoreapplication_p.h b/src/corelib/kernel/qcoreapplication_p.h
index 112b313129..15aca398b7 100644
--- a/src/corelib/kernel/qcoreapplication_p.h
+++ b/src/corelib/kernel/qcoreapplication_p.h
@@ -89,6 +89,8 @@ public:
static QString macMenuBarName();
#endif
+ static void initLocale();
+
QAtomicInt quitLockRef;
void ref();
void deref();