From b0afad8f0b6a3be7ab3a23e063b0201cd68ada95 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Wed, 9 Oct 2013 12:14:01 -0700 Subject: 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 --- src/corelib/tools/qstring_compat.cpp | 68 ++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/corelib/tools/qstring_compat.cpp (limited to 'src/corelib/tools/qstring_compat.cpp') 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 -- cgit v1.2.3