summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qstring.cpp
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2013-10-09 12:14:01 -0700
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-12-13 09:29:58 +0100
commitb0afad8f0b6a3be7ab3a23e063b0201cd68ada95 (patch)
treef51e7623e623e24c859bcb889885bda078247aeb /src/corelib/tools/qstring.cpp
parentc0b899b3df328f9957bea404f351b492ecfefe31 (diff)
Implement support for ref-qualified QString::toLatin1 & friends
This is the first step in implementing an in-place conversion of QString to QByteArray. This requires ref-qualifiers in member functions so we know that we have an rvalue QString. Converting from UTF-16 to Latin1 always requires half the memory. For conversion from UTF-16 to UTF-8, the typical string will also need the same memory or less: characters from U+0000 to U+007F consume one fewer byte; characters from U+0080 to U+07FF and from U+10000 to U+1FFFFF occupy the same space in UTF-8 and UTF-16; it's only the ones from U+0800 to U+FFFF that consume more space in the UTF-8 string. For the locale's 8-bit codec, we can't be sure and the code (currently) needs to go through QTextCodec anyway. This requires a #define set before #include'ing "qstring.h". However, since qstring.h is included by the QtCore PCH, we need an extra qmake compiler without the PCH flags to compile this .cpp. After this change, the distribution of calls in QtCore, Network, Gui, and Widgets is as follows: const & && toUtf8 31 (74%) 11 (26%) toLatin1 79 (77%) 24 (23%) toLocal8Bit 26 (16%) 138 (84%) Change-Id: Idd96f9ddb51b989bc59f6da50054dd10c953dd4f Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'src/corelib/tools/qstring.cpp')
-rw-r--r--src/corelib/tools/qstring.cpp52
1 files changed, 29 insertions, 23 deletions
diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp
index 77d002e4d5..a94b18b5d3 100644
--- a/src/corelib/tools/qstring.cpp
+++ b/src/corelib/tools/qstring.cpp
@@ -4006,7 +4006,22 @@ static QByteArray toLatin1_helper(const QChar *data, int length)
return ba;
}
+QByteArray QString::toLatin1_helper(const QString &string)
+{
+ if (Q_UNLIKELY(string.isNull()))
+ return QByteArray();
+
+ return toLatin1_helper(string.constData(), string.length());
+}
+
+QByteArray QString::toLatin1_helper(const QChar *data, int length)
+{
+ return QT_PREPEND_NAMESPACE(toLatin1_helper)(data, length);
+}
+
/*!
+ \fn QByteArray QString::toLatin1() const
+
Returns a Latin-1 representation of the string as a QByteArray.
The returned byte array is undefined if the string contains non-Latin1
@@ -4015,10 +4030,6 @@ static QByteArray toLatin1_helper(const QChar *data, int length)
\sa fromLatin1(), toUtf8(), toLocal8Bit(), QTextCodec
*/
-QByteArray QString::toLatin1() const
-{
- return toLatin1_helper(unicode(), length());
-}
/*!
\fn QByteArray QString::toAscii() const
@@ -4033,19 +4044,9 @@ QByteArray QString::toLatin1() const
\sa fromAscii(), toLatin1(), toUtf8(), toLocal8Bit(), QTextCodec
*/
-#if !defined(Q_OS_MAC) && defined(Q_OS_UNIX) && !defined(QT_USE_ICU)
-static QByteArray toLocal8Bit_helper(const QChar *data, int length)
-{
-#ifndef QT_NO_TEXTCODEC
- QTextCodec *localeCodec = QTextCodec::codecForLocale();
- if (localeCodec)
- return localeCodec->fromUnicode(data, length);
-#endif // QT_NO_TEXTCODEC
- return toLatin1_helper(data, length);
-}
-#endif
-
/*!
+ \fn QByteArray QString::toLocal8Bit() const
+
Returns the local 8-bit representation of the string as a
QByteArray. The returned byte array is undefined if the string
contains characters not supported by the local 8-bit encoding.
@@ -4060,17 +4061,21 @@ static QByteArray toLocal8Bit_helper(const QChar *data, int length)
\sa fromLocal8Bit(), toLatin1(), toUtf8(), QTextCodec
*/
-QByteArray QString::toLocal8Bit() const
+
+QByteArray QString::toLocal8Bit_helper(const QChar *data, int size)
{
#ifndef QT_NO_TEXTCODEC
QTextCodec *localeCodec = QTextCodec::codecForLocale();
if (localeCodec)
- return localeCodec->fromUnicode(*this);
+ return localeCodec->fromUnicode(data, size);
#endif // QT_NO_TEXTCODEC
- return toLatin1();
+ return toLatin1_helper(data, size);
}
+
/*!
+ \fn QByteArray QString::toUtf8() const
+
Returns a UTF-8 representation of the string as a QByteArray.
UTF-8 is a Unicode codec and can represent all characters in a Unicode
@@ -4086,12 +4091,13 @@ QByteArray QString::toLocal8Bit() const
\sa fromUtf8(), toLatin1(), toLocal8Bit(), QTextCodec
*/
-QByteArray QString::toUtf8() const
+
+QByteArray QString::toUtf8_helper(const QString &str)
{
- if (isNull())
+ if (str.isNull())
return QByteArray();
- return QUtf8::convertFromUnicode(constData(), length(), 0);
+ return QUtf8::convertFromUnicode(str.constData(), str.length(), 0);
}
/*!
@@ -9292,7 +9298,7 @@ static inline bool qt_ends_with(const QChar *haystack, int haystackLen,
*/
QByteArray QStringRef::toLatin1() const
{
- return toLatin1_helper(unicode(), length());
+ return QString::toLatin1_helper(unicode(), length());
}
/*!