1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtCore module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QSTRINGBUILDER_H
43 #define QSTRINGBUILDER_H
45 #include <QtCore/qstring.h>
46 #include <QtCore/qbytearray.h>
48 #if defined(Q_CC_GNU) && !defined(Q_CC_INTEL)
49 # if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ == 0)
50 # include <QtCore/qmap.h>
61 struct Q_CORE_EXPORT QAbstractConcatenable
64 static void convertFromAscii(const char *a, int len, QChar *&out);
65 static void convertToAscii(const QChar *a, int len, char *&out);
66 static inline void convertFromAscii(char a, QChar *&out)
68 #ifndef QT_NO_TEXTCODEC
69 if (QString::codecForCStrings)
70 *out++ = QChar::fromAscii(a);
73 *out++ = QLatin1Char(a);
76 static inline void convertToAscii(QChar a, char *&out)
78 #ifndef QT_NO_TEXTCODEC
79 if (QString::codecForCStrings)
80 *out++ = a.toAscii(); //###
83 convertToLatin1(a, out);
86 static inline void convertToLatin1(QChar a, char *&out)
88 *out++ = a.unicode() > 0xff ? '?' : char(a.unicode());
92 template <typename T> struct QConcatenable {};
94 template <typename A, typename B>
98 QStringBuilder(const A &a_, const B &b_) : a(a_), b(b_) {}
100 friend class QByteArray;
101 friend class QString;
102 template <typename T> T convertTo() const
104 const uint len = QConcatenable< QStringBuilder<A, B> >::size(*this);
105 T s(len, Qt::Uninitialized);
107 typename T::iterator d = s.data();
108 typename T::const_iterator const start = d;
109 QConcatenable< QStringBuilder<A, B> >::appendTo(*this, d);
111 if (!QConcatenable< QStringBuilder<A, B> >::ExactSize && int(len) != d - start) {
112 // this resize is necessary since we allocate a bit too much
113 // when dealing with variable sized 8-bit encodings
119 typedef QConcatenable<QStringBuilder<A, B> > Concatenable;
120 typedef typename Concatenable::ConvertTo ConvertTo;
122 operator ConvertTo() const { return convertTo<ConvertTo>(); }
124 QByteArray toLatin1() const { return convertTo<QString>().toLatin1(); }
125 int size() const { return Concatenable::size(*this); }
132 class QStringBuilder <QString, QString>
135 QStringBuilder(const QString &a_, const QString &b_) : a(a_), b(b_) {}
137 operator QString() const
138 { QString r(a); r += b; return r; }
139 QByteArray toLatin1() const { return QString(*this).toLatin1(); }
146 class QStringBuilder <QByteArray, QByteArray>
149 QStringBuilder(const QByteArray &a_, const QByteArray &b_) : a(a_), b(b_) {}
151 operator QByteArray() const
152 { QByteArray r(a); r += b; return r; }
159 template <> struct QConcatenable<char> : private QAbstractConcatenable
162 typedef QByteArray ConvertTo;
163 enum { ExactSize = true };
164 static int size(const char) { return 1; }
165 #ifndef QT_NO_CAST_FROM_ASCII
166 static inline QT_ASCII_CAST_WARN void appendTo(const char c, QChar *&out)
168 QAbstractConcatenable::convertFromAscii(c, out);
171 static inline void appendTo(const char c, char *&out)
175 template <> struct QConcatenable<QLatin1Char>
177 typedef QLatin1Char type;
178 typedef QString ConvertTo;
179 enum { ExactSize = true };
180 static int size(const QLatin1Char) { return 1; }
181 static inline void appendTo(const QLatin1Char c, QChar *&out)
183 static inline void appendTo(const QLatin1Char c, char *&out)
184 { *out++ = c.toLatin1(); }
187 template <> struct QConcatenable<QChar> : private QAbstractConcatenable
190 typedef QString ConvertTo;
191 enum { ExactSize = true };
192 static int size(const QChar) { return 1; }
193 static inline void appendTo(const QChar c, QChar *&out)
195 #ifndef QT_NO_CAST_TO_ASCII
196 static inline QT_ASCII_CAST_WARN void appendTo(const QChar c, char *&out)
197 { convertToAscii(c, out); }
201 template <> struct QConcatenable<QCharRef> : private QAbstractConcatenable
203 typedef QCharRef type;
204 typedef QString ConvertTo;
205 enum { ExactSize = true };
206 static int size(const QCharRef &) { return 1; }
207 static inline void appendTo(const QCharRef &c, QChar *&out)
208 { *out++ = QChar(c); }
209 #ifndef QT_NO_CAST_TO_ASCII
210 static inline QT_ASCII_CAST_WARN void appendTo(const QCharRef &c, char *&out)
211 { convertToAscii(c, out); }
215 template <> struct QConcatenable<QLatin1String>
217 typedef QLatin1String type;
218 typedef QString ConvertTo;
219 enum { ExactSize = true };
220 static int size(const QLatin1String &a) { return a.size(); }
221 static inline void appendTo(const QLatin1String &a, QChar *&out)
223 for (const char *s = a.data(); *s; )
224 *out++ = QLatin1Char(*s++);
226 static inline void appendTo(const QLatin1String &a, char *&out)
228 for (const char *s = a.data(); *s; )
233 template <> struct QConcatenable<QString> : private QAbstractConcatenable
235 typedef QString type;
236 typedef QString ConvertTo;
237 enum { ExactSize = true };
238 static int size(const QString &a) { return a.size(); }
239 static inline void appendTo(const QString &a, QChar *&out)
241 const int n = a.size();
242 memcpy(out, reinterpret_cast<const char*>(a.constData()), sizeof(QChar) * n);
245 #ifndef QT_NO_CAST_TO_ASCII
246 static inline QT_ASCII_CAST_WARN void appendTo(const QString &a, char *&out)
247 { convertToAscii(a.constData(), a.length(), out); }
251 template <int N> struct QConcatenable<QConstStringDataPtr<N> > : private QAbstractConcatenable
253 typedef QConstStringDataPtr<N> type;
254 typedef QString ConvertTo;
255 enum { ExactSize = true };
256 static int size(const type &) { return N; }
257 static inline void appendTo(const type &a, QChar *&out)
259 memcpy(out, reinterpret_cast<const char*>(a.ptr->data), sizeof(QChar) * N);
262 #ifndef QT_NO_CAST_TO_ASCII
263 static inline QT_ASCII_CAST_WARN void appendTo(const type &a, char *&out)
264 { convertToAscii(a.ptr->data, N, out); }
268 template <> struct QConcatenable<QStringRef> : private QAbstractConcatenable
270 typedef QStringRef type;
271 typedef QString ConvertTo;
272 enum { ExactSize = true };
273 static int size(const QStringRef &a) { return a.size(); }
274 static inline void appendTo(const QStringRef &a, QChar *&out)
276 const int n = a.size();
277 memcpy(out, reinterpret_cast<const char*>(a.constData()), sizeof(QChar) * n);
280 #ifndef QT_NO_CAST_TO_ASCII
281 static inline QT_ASCII_CAST_WARN void appendTo(const QStringRef &a, char *&out)
282 { convertToAscii(a.constData(), a.length(), out); }
287 template <int N> struct QConcatenable<char[N]> : private QAbstractConcatenable
289 typedef char type[N];
290 typedef QByteArray ConvertTo;
291 enum { ExactSize = false };
292 static int size(const char[N]) { return N - 1; }
293 #ifndef QT_NO_CAST_FROM_ASCII
294 static inline void QT_ASCII_CAST_WARN appendTo(const char a[N], QChar *&out)
296 QAbstractConcatenable::convertFromAscii(a, N - 1, out);
299 static inline void appendTo(const char a[N], char *&out)
306 template <int N> struct QConcatenable<const char[N]> : private QAbstractConcatenable
308 typedef const char type[N];
309 typedef QByteArray ConvertTo;
310 enum { ExactSize = false };
311 static int size(const char[N]) { return N - 1; }
312 #ifndef QT_NO_CAST_FROM_ASCII
313 static inline void QT_ASCII_CAST_WARN appendTo(const char a[N], QChar *&out)
315 QAbstractConcatenable::convertFromAscii(a, N - 1, out);
318 static inline void appendTo(const char a[N], char *&out)
325 template <> struct QConcatenable<const char *> : private QAbstractConcatenable
327 typedef char const *type;
328 typedef QByteArray ConvertTo;
329 enum { ExactSize = false };
330 static int size(const char *a) { return qstrlen(a); }
331 #ifndef QT_NO_CAST_FROM_ASCII
332 static inline void QT_ASCII_CAST_WARN appendTo(const char *a, QChar *&out)
333 { QAbstractConcatenable::convertFromAscii(a, -1, out); }
335 static inline void appendTo(const char *a, char *&out)
344 template <> struct QConcatenable<QByteArray> : private QAbstractConcatenable
346 typedef QByteArray type;
347 typedef QByteArray ConvertTo;
348 enum { ExactSize = false };
349 static int size(const QByteArray &ba) { return ba.size(); }
350 #ifndef QT_NO_CAST_FROM_ASCII
351 static inline QT_ASCII_CAST_WARN void appendTo(const QByteArray &ba, QChar *&out)
353 QAbstractConcatenable::convertFromAscii(ba.constData(), ba.size(), out);
356 static inline void appendTo(const QByteArray &ba, char *&out)
358 const char *a = ba.constData();
359 const char * const end = ba.end();
365 template <int N> struct QConcatenable<QConstByteArrayDataPtr<N> > : private QAbstractConcatenable
367 typedef QConstByteArrayDataPtr<N> type;
368 typedef QByteArray ConvertTo;
369 enum { ExactSize = false };
370 static int size(const type &) { return N; }
371 #ifndef QT_NO_CAST_FROM_ASCII
372 static inline QT_ASCII_CAST_WARN void appendTo(const type &a, QChar *&out)
374 QAbstractConcatenable::convertFromAscii(a.ptr->data, N, out);
377 static inline void appendTo(const type &ba, char *&out)
379 const char *a = ba.ptr->data;
385 namespace QtStringBuilder {
386 template <typename A, typename B> struct ConvertToTypeHelper
387 { typedef A ConvertTo; };
388 template <typename T> struct ConvertToTypeHelper<T, QString>
389 { typedef QString ConvertTo; };
392 template <typename A, typename B>
393 struct QConcatenable< QStringBuilder<A, B> >
395 typedef QStringBuilder<A, B> type;
396 typedef typename QtStringBuilder::ConvertToTypeHelper<typename QConcatenable<A>::ConvertTo, typename QConcatenable<B>::ConvertTo>::ConvertTo ConvertTo;
397 enum { ExactSize = QConcatenable<A>::ExactSize && QConcatenable<B>::ExactSize };
398 static int size(const type &p)
400 return QConcatenable<A>::size(p.a) + QConcatenable<B>::size(p.b);
402 template<typename T> static inline void appendTo(const type &p, T *&out)
404 QConcatenable<A>::appendTo(p.a, out);
405 QConcatenable<B>::appendTo(p.b, out);
409 template <typename A, typename B>
410 QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>
411 operator%(const A &a, const B &b)
413 return QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>(a, b);
416 // QT_USE_FAST_OPERATOR_PLUS was introduced in 4.7, QT_USE_QSTRINGBUILDER is to be used from 4.8 onwards
417 // QT_USE_FAST_OPERATOR_PLUS does not remove the normal operator+ for QByteArray
418 #if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER)
419 template <typename A, typename B>
420 QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>
421 operator+(const A &a, const B &b)
423 return QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>(a, b);
427 template <typename A, typename B>
428 QByteArray &operator+=(QByteArray &a, const QStringBuilder<A, B> &b)
430 #ifndef QT_NO_CAST_TO_ASCII
431 if (sizeof(typename QConcatenable< QStringBuilder<A, B> >::ConvertTo::value_type) == sizeof(QChar)) {
432 //it is not save to optimize as in utf8 it is not possible to compute the size
433 return a += QString(b);
436 int len = a.size() + QConcatenable< QStringBuilder<A, B> >::size(b);
438 char *it = a.data() + a.size();
439 QConcatenable< QStringBuilder<A, B> >::appendTo(b, it);
440 a.resize(len); //we need to resize after the appendTo for the case str+=foo+str
444 template <typename A, typename B>
445 QString &operator+=(QString &a, const QStringBuilder<A, B> &b)
447 int len = a.size() + QConcatenable< QStringBuilder<A, B> >::size(b);
449 QChar *it = a.data() + a.size();
450 QConcatenable< QStringBuilder<A, B> >::appendTo(b, it);
451 a.resize(it - a.constData()); //may be smaller than len if there was conversion from utf8
460 #endif // QSTRINGBUILDER_H