summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qstring_compat.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_compat.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_compat.cpp')
-rw-r--r--src/corelib/tools/qstring_compat.cpp68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/corelib/tools/qstring_compat.cpp b/src/corelib/tools/qstring_compat.cpp
new file mode 100644
index 0000000000..34c37a9cb8
--- /dev/null
+++ b/src/corelib/tools/qstring_compat.cpp
@@ -0,0 +1,68 @@
+/****************************************************************************
+**
+** Copyright (C) 2013 Intel Corporation
+** Contact: http://www.qt-project.org/legal
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and Digia. For licensing terms and
+** conditions see http://qt.digia.com/licensing. For further information
+** use the contact form at http://qt.digia.com/contact-us.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Digia gives you certain additional
+** rights. These rights are described in the Digia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#if defined(QSTRING_H) && !defined(QT_BOOTSTRAPPED)
+# error "This file cannot be compiled with pre-compiled headers"
+// (unless it's configure.exe, which is bootstrapped)
+#endif
+#define QT_COMPILING_QSTRING_COMPAT_CPP
+
+#include "qstring.h"
+
+QT_BEGIN_NAMESPACE
+
+// all these implementations must be the same as the inline versions in qstring.h
+QByteArray QString::toLatin1() const
+{
+ return toLatin1_helper(*this);
+}
+
+QByteArray QString::toLocal8Bit() const
+{
+ return toLocal8Bit_helper(constData(), size());
+}
+
+QByteArray QString::toUtf8() const
+{
+ return toUtf8_helper(*this);
+}
+
+QT_END_NAMESPACE