1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
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>
62 struct Q_CORE_EXPORT QAbstractConcatenable
65 static void convertFromAscii(const char *a, int len, QChar *&out);
66 static void convertToAscii(const QChar *a, int len, char *&out);
67 static inline void convertFromAscii(char a, QChar *&out)
69 #ifndef QT_NO_TEXTCODEC
70 if (QString::codecForCStrings)
71 *out++ = QChar::fromAscii(a);
74 *out++ = QLatin1Char(a);
77 static inline void convertToAscii(QChar a, char *&out)
79 #ifndef QT_NO_TEXTCODEC
80 if (QString::codecForCStrings)
81 *out++ = a.toAscii(); //###
84 convertToLatin1(a, out);
87 static inline void convertToLatin1(QChar a, char *&out)
89 *out++ = a.unicode() > 0xff ? '?' : char(a.unicode());
93 template <typename T> struct QConcatenable {};
95 template <typename A, typename B>
99 QStringBuilder(const A &a_, const B &b_) : a(a_), b(b_) {}
101 friend class QByteArray;
102 friend class QString;
103 template <typename T> T convertTo() const
105 const uint len = QConcatenable< QStringBuilder<A, B> >::size(*this);
106 T s(len, Qt::Uninitialized);
108 typename T::iterator d = s.data();
109 typename T::const_iterator const start = d;
110 QConcatenable< QStringBuilder<A, B> >::appendTo(*this, d);
112 if (!QConcatenable< QStringBuilder<A, B> >::ExactSize && int(len) != d - start) {
113 // this resize is necessary since we allocate a bit too much
114 // when dealing with variable sized 8-bit encodings
120 typedef QConcatenable<QStringBuilder<A, B> > Concatenable;
121 typedef typename Concatenable::ConvertTo ConvertTo;
123 operator ConvertTo() const { return convertTo<ConvertTo>(); }
125 QByteArray toLatin1() const { return convertTo<QString>().toLatin1(); }
126 int size() const { return Concatenable::size(*this); }
133 class QStringBuilder <QString, QString>
136 QStringBuilder(const QString &a_, const QString &b_) : a(a_), b(b_) {}
138 operator QString() const
139 { QString r(a); r += b; return r; }
140 QByteArray toLatin1() const { return QString(*this).toLatin1(); }
147 class QStringBuilder <QByteArray, QByteArray>
150 QStringBuilder(const QByteArray &a_, const QByteArray &b_) : a(a_), b(b_) {}
152 operator QByteArray() const
153 { QByteArray r(a); r += b; return r; }
160 template <> struct QConcatenable<char> : private QAbstractConcatenable
163 typedef QByteArray ConvertTo;
164 enum { ExactSize = true };
165 static int size(const char) { return 1; }
166 #ifndef QT_NO_CAST_FROM_ASCII
167 static inline QT_ASCII_CAST_WARN void appendTo(const char c, QChar *&out)
169 QAbstractConcatenable::convertFromAscii(c, out);
172 static inline void appendTo(const char c, char *&out)
176 template <> struct QConcatenable<QLatin1Char>
178 typedef QLatin1Char type;
179 typedef QString ConvertTo;
180 enum { ExactSize = true };
181 static int size(const QLatin1Char) { return 1; }
182 static inline void appendTo(const QLatin1Char c, QChar *&out)
184 static inline void appendTo(const QLatin1Char c, char *&out)
185 { *out++ = c.toLatin1(); }
188 template <> struct QConcatenable<QChar> : private QAbstractConcatenable
191 typedef QString ConvertTo;
192 enum { ExactSize = true };
193 static int size(const QChar) { return 1; }
194 static inline void appendTo(const QChar c, QChar *&out)
196 #ifndef QT_NO_CAST_TO_ASCII
197 static inline QT_ASCII_CAST_WARN void appendTo(const QChar c, char *&out)
198 { convertToAscii(c, out); }
202 template <> struct QConcatenable<QCharRef> : private QAbstractConcatenable
204 typedef QCharRef type;
205 typedef QString ConvertTo;
206 enum { ExactSize = true };
207 static int size(const QCharRef &) { return 1; }
208 static inline void appendTo(const QCharRef &c, QChar *&out)
209 { *out++ = QChar(c); }
210 #ifndef QT_NO_CAST_TO_ASCII
211 static inline QT_ASCII_CAST_WARN void appendTo(const QCharRef &c, char *&out)
212 { convertToAscii(c, out); }
216 template <> struct QConcatenable<QLatin1String>
218 typedef QLatin1String type;
219 typedef QString ConvertTo;
220 enum { ExactSize = true };
221 static int size(const QLatin1String &a) { return a.size(); }
222 static inline void appendTo(const QLatin1String &a, QChar *&out)
224 for (const char *s = a.data(); *s; )
225 *out++ = QLatin1Char(*s++);
227 static inline void appendTo(const QLatin1String &a, char *&out)
229 for (const char *s = a.data(); *s; )
234 template <> struct QConcatenable<QString> : private QAbstractConcatenable
236 typedef QString type;
237 typedef QString ConvertTo;
238 enum { ExactSize = true };
239 static int size(const QString &a) { return a.size(); }
240 static inline void appendTo(const QString &a, QChar *&out)
242 const int n = a.size();
243 memcpy(out, reinterpret_cast<const char*>(a.constData()), sizeof(QChar) * n);
246 #ifndef QT_NO_CAST_TO_ASCII
247 static inline QT_ASCII_CAST_WARN void appendTo(const QString &a, char *&out)
248 { convertToAscii(a.constData(), a.length(), out); }
252 template <> struct QConcatenable<QStringRef> : private QAbstractConcatenable
254 typedef QStringRef type;
255 typedef QString ConvertTo;
256 enum { ExactSize = true };
257 static int size(const QStringRef &a) { return a.size(); }
258 static inline void appendTo(const QStringRef &a, QChar *&out)
260 const int n = a.size();
261 memcpy(out, reinterpret_cast<const char*>(a.constData()), sizeof(QChar) * n);
264 #ifndef QT_NO_CAST_TO_ASCII
265 static inline QT_ASCII_CAST_WARN void appendTo(const QStringRef &a, char *&out)
266 { convertToAscii(a.constData(), a.length(), out); }
271 template <int N> struct QConcatenable<char[N]> : private QAbstractConcatenable
273 typedef char type[N];
274 typedef QByteArray ConvertTo;
275 enum { ExactSize = false };
276 static int size(const char[N]) { return N - 1; }
277 #ifndef QT_NO_CAST_FROM_ASCII
278 static inline void QT_ASCII_CAST_WARN appendTo(const char a[N], QChar *&out)
280 QAbstractConcatenable::convertFromAscii(a, N, out);
283 static inline void appendTo(const char a[N], char *&out)
290 template <int N> struct QConcatenable<const char[N]> : private QAbstractConcatenable
292 typedef const char type[N];
293 typedef QByteArray ConvertTo;
294 enum { ExactSize = false };
295 static int size(const char[N]) { return N - 1; }
296 #ifndef QT_NO_CAST_FROM_ASCII
297 static inline void QT_ASCII_CAST_WARN appendTo(const char a[N], QChar *&out)
299 QAbstractConcatenable::convertFromAscii(a, N, out);
302 static inline void appendTo(const char a[N], char *&out)
309 template <> struct QConcatenable<const char *> : private QAbstractConcatenable
311 typedef char const *type;
312 typedef QByteArray ConvertTo;
313 enum { ExactSize = false };
314 static int size(const char *a) { return qstrlen(a); }
315 #ifndef QT_NO_CAST_FROM_ASCII
316 static inline void QT_ASCII_CAST_WARN appendTo(const char *a, QChar *&out)
317 { QAbstractConcatenable::convertFromAscii(a, -1, out); }
319 static inline void appendTo(const char *a, char *&out)
328 template <> struct QConcatenable<QByteArray> : private QAbstractConcatenable
330 typedef QByteArray type;
331 typedef QByteArray ConvertTo;
332 enum { ExactSize = false };
333 static int size(const QByteArray &ba) { return ba.size(); }
334 #ifndef QT_NO_CAST_FROM_ASCII
335 static inline void appendTo(const QByteArray &ba, QChar *&out)
337 // adding 1 because convertFromAscii expects the size including the null-termination
338 QAbstractConcatenable::convertFromAscii(ba.constData(), ba.size() + 1, out);
341 static inline void appendTo(const QByteArray &ba, char *&out)
343 const char *a = ba.constData();
344 const char * const end = ba.end();
350 namespace QtStringBuilder {
351 template <typename A, typename B> struct ConvertToTypeHelper
352 { typedef A ConvertTo; };
353 template <typename T> struct ConvertToTypeHelper<T, QString>
354 { typedef QString ConvertTo; };
357 template <typename A, typename B>
358 struct QConcatenable< QStringBuilder<A, B> >
360 typedef QStringBuilder<A, B> type;
361 typedef typename QtStringBuilder::ConvertToTypeHelper<typename QConcatenable<A>::ConvertTo, typename QConcatenable<B>::ConvertTo>::ConvertTo ConvertTo;
362 enum { ExactSize = QConcatenable<A>::ExactSize && QConcatenable<B>::ExactSize };
363 static int size(const type &p)
365 return QConcatenable<A>::size(p.a) + QConcatenable<B>::size(p.b);
367 template<typename T> static inline void appendTo(const type &p, T *&out)
369 QConcatenable<A>::appendTo(p.a, out);
370 QConcatenable<B>::appendTo(p.b, out);
374 template <typename A, typename B>
375 QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>
376 operator%(const A &a, const B &b)
378 return QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>(a, b);
381 // QT_USE_FAST_OPERATOR_PLUS was introduced in 4.7, QT_USE_QSTRINGBUILDER is to be used from 4.8 onwards
382 // QT_USE_FAST_OPERATOR_PLUS does not remove the normal operator+ for QByteArray
383 #if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER)
384 template <typename A, typename B>
385 QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>
386 operator+(const A &a, const B &b)
388 return QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>(a, b);
392 template <typename A, typename B>
393 QByteArray &operator+=(QByteArray &a, const QStringBuilder<A, B> &b)
395 #ifndef QT_NO_CAST_TO_ASCII
396 if (sizeof(typename QConcatenable< QStringBuilder<A, B> >::ConvertTo::value_type) == sizeof(QChar)) {
397 //it is not save to optimize as in utf8 it is not possible to compute the size
398 return a += QString(b);
401 int len = a.size() + QConcatenable< QStringBuilder<A, B> >::size(b);
403 char *it = a.data() + a.size();
404 QConcatenable< QStringBuilder<A, B> >::appendTo(b, it);
405 a.resize(len); //we need to resize after the appendTo for the case str+=foo+str
409 template <typename A, typename B>
410 QString &operator+=(QString &a, const QStringBuilder<A, B> &b)
412 int len = a.size() + QConcatenable< QStringBuilder<A, B> >::size(b);
414 QChar *it = a.data() + a.size();
415 QConcatenable< QStringBuilder<A, B> >::appendTo(b, it);
416 a.resize(it - a.constData()); //may be smaller than len if there was conversion from utf8
425 #endif // QSTRINGBUILDER_H