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 #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 *out++ = QLatin1Char(a);
71 static inline void convertToAscii(QChar a, char *&out)
73 convertToLatin1(a, out);
76 static inline void convertToLatin1(QChar a, char *&out)
78 *out++ = a.unicode() > 0xff ? '?' : char(a.unicode());
82 template <typename T> struct QConcatenable {};
84 template <typename A, typename B>
88 QStringBuilder(const A &a_, const B &b_) : a(a_), b(b_) {}
90 friend class QByteArray;
92 template <typename T> T convertTo() const
94 const uint len = QConcatenable< QStringBuilder<A, B> >::size(*this);
95 T s(len, Qt::Uninitialized);
97 typename T::iterator d = s.data();
98 typename T::const_iterator const start = d;
99 QConcatenable< QStringBuilder<A, B> >::appendTo(*this, d);
101 if (!QConcatenable< QStringBuilder<A, B> >::ExactSize && int(len) != d - start) {
102 // this resize is necessary since we allocate a bit too much
103 // when dealing with variable sized 8-bit encodings
109 typedef QConcatenable<QStringBuilder<A, B> > Concatenable;
110 typedef typename Concatenable::ConvertTo ConvertTo;
112 operator ConvertTo() const { return convertTo<ConvertTo>(); }
114 QByteArray toLatin1() const { return convertTo<QString>().toLatin1(); }
115 int size() const { return Concatenable::size(*this); }
122 class QStringBuilder <QString, QString>
125 QStringBuilder(const QString &a_, const QString &b_) : a(a_), b(b_) {}
127 operator QString() const
128 { QString r(a); r += b; return r; }
129 QByteArray toLatin1() const { return QString(*this).toLatin1(); }
136 class QStringBuilder <QByteArray, QByteArray>
139 QStringBuilder(const QByteArray &a_, const QByteArray &b_) : a(a_), b(b_) {}
141 operator QByteArray() const
142 { QByteArray r(a); r += b; return r; }
149 template <> struct QConcatenable<char> : private QAbstractConcatenable
152 typedef QByteArray ConvertTo;
153 enum { ExactSize = true };
154 static int size(const char) { return 1; }
155 #ifndef QT_NO_CAST_FROM_ASCII
156 static inline QT_ASCII_CAST_WARN void appendTo(const char c, QChar *&out)
158 QAbstractConcatenable::convertFromAscii(c, out);
161 static inline void appendTo(const char c, char *&out)
165 template <> struct QConcatenable<QLatin1Char>
167 typedef QLatin1Char type;
168 typedef QString ConvertTo;
169 enum { ExactSize = true };
170 static int size(const QLatin1Char) { return 1; }
171 static inline void appendTo(const QLatin1Char c, QChar *&out)
173 static inline void appendTo(const QLatin1Char c, char *&out)
174 { *out++ = c.toLatin1(); }
177 template <> struct QConcatenable<QChar> : private QAbstractConcatenable
180 typedef QString ConvertTo;
181 enum { ExactSize = true };
182 static int size(const QChar) { return 1; }
183 static inline void appendTo(const QChar c, QChar *&out)
185 #ifndef QT_NO_CAST_TO_ASCII
186 static inline QT_ASCII_CAST_WARN void appendTo(const QChar c, char *&out)
187 { convertToAscii(c, out); }
191 template <> struct QConcatenable<QCharRef> : private QAbstractConcatenable
193 typedef QCharRef type;
194 typedef QString ConvertTo;
195 enum { ExactSize = true };
196 static int size(const QCharRef &) { return 1; }
197 static inline void appendTo(const QCharRef &c, QChar *&out)
198 { *out++ = QChar(c); }
199 #ifndef QT_NO_CAST_TO_ASCII
200 static inline QT_ASCII_CAST_WARN void appendTo(const QCharRef &c, char *&out)
201 { convertToAscii(c, out); }
205 template <> struct QConcatenable<QLatin1String>
207 typedef QLatin1String type;
208 typedef QString ConvertTo;
209 enum { ExactSize = true };
210 static int size(const QLatin1String &a) { return a.size(); }
211 static inline void appendTo(const QLatin1String &a, QChar *&out)
213 for (const char *s = a.data(); *s; )
214 *out++ = QLatin1Char(*s++);
216 static inline void appendTo(const QLatin1String &a, char *&out)
218 for (const char *s = a.data(); *s; )
223 template <> struct QConcatenable<QString> : private QAbstractConcatenable
225 typedef QString type;
226 typedef QString ConvertTo;
227 enum { ExactSize = true };
228 static int size(const QString &a) { return a.size(); }
229 static inline void appendTo(const QString &a, QChar *&out)
231 const int n = a.size();
232 memcpy(out, reinterpret_cast<const char*>(a.constData()), sizeof(QChar) * n);
235 #ifndef QT_NO_CAST_TO_ASCII
236 static inline QT_ASCII_CAST_WARN void appendTo(const QString &a, char *&out)
237 { convertToAscii(a.constData(), a.length(), out); }
241 template <int N> struct QConcatenable<QConstStringDataPtr<N> > : private QAbstractConcatenable
243 typedef QConstStringDataPtr<N> type;
244 typedef QString ConvertTo;
245 enum { ExactSize = true };
246 static int size(const type &) { return N; }
247 static inline void appendTo(const type &a, QChar *&out)
249 memcpy(out, reinterpret_cast<const char*>(a.ptr->data), sizeof(QChar) * N);
252 #ifndef QT_NO_CAST_TO_ASCII
253 static inline QT_ASCII_CAST_WARN void appendTo(const type &a, char *&out)
254 { convertToAscii(a.ptr->data, N, out); }
258 template <> struct QConcatenable<QStringRef> : private QAbstractConcatenable
260 typedef QStringRef type;
261 typedef QString ConvertTo;
262 enum { ExactSize = true };
263 static int size(const QStringRef &a) { return a.size(); }
264 static inline void appendTo(const QStringRef &a, QChar *&out)
266 const int n = a.size();
267 memcpy(out, reinterpret_cast<const char*>(a.constData()), sizeof(QChar) * n);
270 #ifndef QT_NO_CAST_TO_ASCII
271 static inline QT_ASCII_CAST_WARN void appendTo(const QStringRef &a, char *&out)
272 { convertToAscii(a.constData(), a.length(), out); }
277 template <int N> struct QConcatenable<char[N]> : private QAbstractConcatenable
279 typedef char type[N];
280 typedef QByteArray ConvertTo;
281 enum { ExactSize = false };
282 static int size(const char[N]) { return N - 1; }
283 #ifndef QT_NO_CAST_FROM_ASCII
284 static inline void QT_ASCII_CAST_WARN appendTo(const char a[N], QChar *&out)
286 QAbstractConcatenable::convertFromAscii(a, N - 1, out);
289 static inline void appendTo(const char a[N], char *&out)
296 template <int N> struct QConcatenable<const char[N]> : private QAbstractConcatenable
298 typedef const char type[N];
299 typedef QByteArray ConvertTo;
300 enum { ExactSize = false };
301 static int size(const char[N]) { return N - 1; }
302 #ifndef QT_NO_CAST_FROM_ASCII
303 static inline void QT_ASCII_CAST_WARN appendTo(const char a[N], QChar *&out)
305 QAbstractConcatenable::convertFromAscii(a, N - 1, out);
308 static inline void appendTo(const char a[N], char *&out)
315 template <> struct QConcatenable<const char *> : private QAbstractConcatenable
317 typedef char const *type;
318 typedef QByteArray ConvertTo;
319 enum { ExactSize = false };
320 static int size(const char *a) { return qstrlen(a); }
321 #ifndef QT_NO_CAST_FROM_ASCII
322 static inline void QT_ASCII_CAST_WARN appendTo(const char *a, QChar *&out)
323 { QAbstractConcatenable::convertFromAscii(a, -1, out); }
325 static inline void appendTo(const char *a, char *&out)
334 template <> struct QConcatenable<QByteArray> : private QAbstractConcatenable
336 typedef QByteArray type;
337 typedef QByteArray ConvertTo;
338 enum { ExactSize = false };
339 static int size(const QByteArray &ba) { return ba.size(); }
340 #ifndef QT_NO_CAST_FROM_ASCII
341 static inline QT_ASCII_CAST_WARN void appendTo(const QByteArray &ba, QChar *&out)
343 QAbstractConcatenable::convertFromAscii(ba.constData(), ba.size(), out);
346 static inline void appendTo(const QByteArray &ba, char *&out)
348 const char *a = ba.constData();
349 const char * const end = ba.end();
355 template <int N> struct QConcatenable<QConstByteArrayDataPtr<N> > : private QAbstractConcatenable
357 typedef QConstByteArrayDataPtr<N> type;
358 typedef QByteArray ConvertTo;
359 enum { ExactSize = false };
360 static int size(const type &) { return N; }
361 #ifndef QT_NO_CAST_FROM_ASCII
362 static inline QT_ASCII_CAST_WARN void appendTo(const type &a, QChar *&out)
364 QAbstractConcatenable::convertFromAscii(a.ptr->data, N, out);
367 static inline void appendTo(const type &ba, char *&out)
369 const char *a = ba.ptr->data;
375 namespace QtStringBuilder {
376 template <typename A, typename B> struct ConvertToTypeHelper
377 { typedef A ConvertTo; };
378 template <typename T> struct ConvertToTypeHelper<T, QString>
379 { typedef QString ConvertTo; };
382 template <typename A, typename B>
383 struct QConcatenable< QStringBuilder<A, B> >
385 typedef QStringBuilder<A, B> type;
386 typedef typename QtStringBuilder::ConvertToTypeHelper<typename QConcatenable<A>::ConvertTo, typename QConcatenable<B>::ConvertTo>::ConvertTo ConvertTo;
387 enum { ExactSize = QConcatenable<A>::ExactSize && QConcatenable<B>::ExactSize };
388 static int size(const type &p)
390 return QConcatenable<A>::size(p.a) + QConcatenable<B>::size(p.b);
392 template<typename T> static inline void appendTo(const type &p, T *&out)
394 QConcatenable<A>::appendTo(p.a, out);
395 QConcatenable<B>::appendTo(p.b, out);
399 template <typename A, typename B>
400 QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>
401 operator%(const A &a, const B &b)
403 return QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>(a, b);
406 // QT_USE_FAST_OPERATOR_PLUS was introduced in 4.7, QT_USE_QSTRINGBUILDER is to be used from 4.8 onwards
407 // QT_USE_FAST_OPERATOR_PLUS does not remove the normal operator+ for QByteArray
408 #if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER)
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);
417 template <typename A, typename B>
418 QByteArray &operator+=(QByteArray &a, const QStringBuilder<A, B> &b)
420 #ifndef QT_NO_CAST_TO_ASCII
421 if (sizeof(typename QConcatenable< QStringBuilder<A, B> >::ConvertTo::value_type) == sizeof(QChar)) {
422 //it is not save to optimize as in utf8 it is not possible to compute the size
423 return a += QString(b);
426 int len = a.size() + QConcatenable< QStringBuilder<A, B> >::size(b);
428 char *it = a.data() + a.size();
429 QConcatenable< QStringBuilder<A, B> >::appendTo(b, it);
430 a.resize(len); //we need to resize after the appendTo for the case str+=foo+str
434 template <typename A, typename B>
435 QString &operator+=(QString &a, const QStringBuilder<A, B> &b)
437 int len = a.size() + QConcatenable< QStringBuilder<A, B> >::size(b);
439 QChar *it = a.data() + a.size();
440 QConcatenable< QStringBuilder<A, B> >::appendTo(b, it);
441 a.resize(it - a.constData()); //may be smaller than len if there was conversion from utf8
450 #endif // QSTRINGBUILDER_H