1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qstringbuilder.h"
43 #include <QtCore/qtextcodec.h>
53 \brief The QStringBuilder class is a template class that provides a facility to build up QStrings from smaller chunks.
57 \ingroup string-processing
60 To build a QString by multiple concatenations, QString::operator+()
61 is typically used. This causes \e{n - 1} reallocations when building
62 a string from \e{n} chunks.
64 QStringBuilder uses expression templates to collect the individual
65 chunks, compute the total size, allocate the required amount of
66 memory for the final QString object, and copy the chunks into the
69 The QStringBuilder class is not to be used explicitly in user
70 code. Instances of the class are created as return values of the
71 operator%() function, acting on objects of type QString,
72 QLatin1String, QStringRef, QChar, QCharRef,
73 QLatin1Char, and \c char.
75 Concatenating strings with operator%() generally yields better
76 performance then using \c QString::operator+() on the same chunks
77 if there are three or more of them, and performs equally well in other
80 \sa QLatin1String, QString
83 /*! \fn QStringBuilder::QStringBuilder(const A &a, const B &b)
84 Constructs a QStringBuilder from \a a and \a b.
87 /* \fn QStringBuilder::operator%(const A &a, const B &b)
89 Returns a \c QStringBuilder object that is converted to a QString object
90 when assigned to a variable of QString type or passed to a function that
91 takes a QString parameter.
93 This function is usable with arguments of type \c QString,
94 \c QLatin1String, \c QStringRef,
95 \c QChar, \c QCharRef, \c QLatin1Char, and \c char.
98 /*! \fn QByteArray QStringBuilder::toLatin1() const
99 Returns a Latin-1 representation of the string as a QByteArray. The
100 returned byte array is undefined if the string contains non-Latin1
106 void QAbstractConcatenable::convertFromAscii(const char *a, int len, QChar *&out)
108 #ifndef QT_NO_TEXTCODEC
109 if (QString::codecForCStrings && len) {
110 QString tmp = QString::fromAscii(a, len > 0 ? len : -1);
111 memcpy(out, reinterpret_cast<const char *>(tmp.constData()), sizeof(QChar) * tmp.size());
120 *out++ = QLatin1Char(*a++);
122 for (int i = 0; i < len; ++i)
123 *out++ = QLatin1Char(a[i]);
128 void QAbstractConcatenable::convertToAscii(const QChar* a, int len, char*& out)
130 #ifndef QT_NO_TEXTCODEC
131 if (QString::codecForCStrings) {
132 QByteArray tmp = QString::codecForCStrings->fromUnicode(a, len);
133 memcpy(out, tmp.constData(), tmp.size());
140 convertToLatin1(*a++, out);
142 for (int i = 0; i < len; ++i)
143 convertToLatin1(a[i], out);