|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtCore module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #ifndef QSTRINGBUILDER_H |
|
43 #define QSTRINGBUILDER_H |
|
44 |
|
45 #include <QtCore/qstring.h> |
|
46 |
|
47 #if defined(Q_CC_GNU) && !defined(Q_CC_INTEL) |
|
48 # if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ == 0) |
|
49 # include <QtCore/qmap.h> |
|
50 # endif |
|
51 #endif |
|
52 |
|
53 #include <string.h> |
|
54 |
|
55 QT_BEGIN_HEADER |
|
56 |
|
57 QT_BEGIN_NAMESPACE |
|
58 |
|
59 QT_MODULE(Core) |
|
60 |
|
61 // ### Qt 5: merge with QLatin1String |
|
62 class QLatin1Literal |
|
63 { |
|
64 public: |
|
65 int size() const { return m_size; } |
|
66 const char *data() const { return m_data; } |
|
67 |
|
68 template <int N> |
|
69 QLatin1Literal(const char (&str)[N]) |
|
70 : m_size(N - 1), m_data(str) {} |
|
71 |
|
72 private: |
|
73 const int m_size; |
|
74 const char *m_data; |
|
75 }; |
|
76 |
|
77 |
|
78 template <typename T> struct QConcatenable {}; |
|
79 |
|
80 template <typename A, typename B> |
|
81 class QStringBuilder |
|
82 { |
|
83 public: |
|
84 QStringBuilder(const A &a_, const B &b_) : a(a_), b(b_) {} |
|
85 |
|
86 operator QString() const |
|
87 { |
|
88 QString s(QConcatenable< QStringBuilder<A, B> >::size(*this), |
|
89 Qt::Uninitialized); |
|
90 |
|
91 QChar *d = s.data(); |
|
92 QConcatenable< QStringBuilder<A, B> >::appendTo(*this, d); |
|
93 return s; |
|
94 } |
|
95 QByteArray toLatin1() const { return QString(*this).toLatin1(); } |
|
96 |
|
97 const A &a; |
|
98 const B &b; |
|
99 }; |
|
100 |
|
101 |
|
102 template <> struct QConcatenable<char> |
|
103 { |
|
104 typedef char type; |
|
105 static int size(const char) { return 1; } |
|
106 static inline void appendTo(const char c, QChar *&out) |
|
107 { |
|
108 *out++ = QLatin1Char(c); |
|
109 } |
|
110 }; |
|
111 |
|
112 template <> struct QConcatenable<QLatin1Char> |
|
113 { |
|
114 typedef QLatin1Char type; |
|
115 static int size(const QLatin1Char) { return 1; } |
|
116 static inline void appendTo(const QLatin1Char c, QChar *&out) |
|
117 { |
|
118 *out++ = c; |
|
119 } |
|
120 }; |
|
121 |
|
122 template <> struct QConcatenable<QChar> |
|
123 { |
|
124 typedef QChar type; |
|
125 static int size(const QChar) { return 1; } |
|
126 static inline void appendTo(const QChar c, QChar *&out) |
|
127 { |
|
128 *out++ = c; |
|
129 } |
|
130 }; |
|
131 |
|
132 template <> struct QConcatenable<QCharRef> |
|
133 { |
|
134 typedef QCharRef type; |
|
135 static int size(const QCharRef &) { return 1; } |
|
136 static inline void appendTo(const QCharRef &c, QChar *&out) |
|
137 { |
|
138 *out++ = QChar(c); |
|
139 } |
|
140 }; |
|
141 |
|
142 template <> struct QConcatenable<QLatin1String> |
|
143 { |
|
144 typedef QLatin1String type; |
|
145 static int size(const QLatin1String &a) { return qstrlen(a.latin1()); } |
|
146 static inline void appendTo(const QLatin1String &a, QChar *&out) |
|
147 { |
|
148 for (const char *s = a.latin1(); *s; ) |
|
149 *out++ = QLatin1Char(*s++); |
|
150 } |
|
151 |
|
152 }; |
|
153 |
|
154 template <> struct QConcatenable<QLatin1Literal> |
|
155 { |
|
156 typedef QLatin1Literal type; |
|
157 static int size(const QLatin1Literal &a) { return a.size(); } |
|
158 static inline void appendTo(const QLatin1Literal &a, QChar *&out) |
|
159 { |
|
160 for (const char *s = a.data(); *s; ) |
|
161 *out++ = QLatin1Char(*s++); |
|
162 } |
|
163 }; |
|
164 |
|
165 template <> struct QConcatenable<QString> |
|
166 { |
|
167 typedef QString type; |
|
168 static int size(const QString &a) { return a.size(); } |
|
169 static inline void appendTo(const QString &a, QChar *&out) |
|
170 { |
|
171 const int n = a.size(); |
|
172 memcpy(out, reinterpret_cast<const char*>(a.constData()), sizeof(QChar) * n); |
|
173 out += n; |
|
174 } |
|
175 }; |
|
176 |
|
177 template <> struct QConcatenable<QStringRef> |
|
178 { |
|
179 typedef QStringRef type; |
|
180 static int size(const QStringRef &a) { return a.size(); } |
|
181 static inline void appendTo(QStringRef a, QChar *&out) |
|
182 { |
|
183 const int n = a.size(); |
|
184 memcpy(out, reinterpret_cast<const char*>(a.constData()), sizeof(QChar) * n); |
|
185 out += n; |
|
186 } |
|
187 }; |
|
188 |
|
189 #ifndef QT_NO_CAST_FROM_ASCII |
|
190 template <int N> struct QConcatenable<char[N]> |
|
191 { |
|
192 typedef char type[N]; |
|
193 static int size(const char[N]) { return N - 1; } |
|
194 static inline void appendTo(const char a[N], QChar *&out) |
|
195 { |
|
196 for (int i = 0; i < N - 1; ++i) |
|
197 *out++ = QLatin1Char(a[i]); |
|
198 } |
|
199 }; |
|
200 |
|
201 template <int N> struct QConcatenable<const char[N]> |
|
202 { |
|
203 typedef const char type[N]; |
|
204 static int size(const char[N]) { return N - 1; } |
|
205 static inline void appendTo(const char a[N], QChar *&out) |
|
206 { |
|
207 for (int i = 0; i < N - 1; ++i) |
|
208 *out++ = QLatin1Char(a[i]); |
|
209 } |
|
210 }; |
|
211 |
|
212 template <> struct QConcatenable<const char *> |
|
213 { |
|
214 typedef char const *type; |
|
215 static int size(const char *a) { return qstrlen(a); } |
|
216 static inline void appendTo(const char *a, QChar *&out) |
|
217 { |
|
218 while (*a) |
|
219 *out++ = QLatin1Char(*a++); |
|
220 } |
|
221 }; |
|
222 |
|
223 template <> struct QConcatenable<QByteArray> |
|
224 { |
|
225 typedef QByteArray type; |
|
226 static int size(const QByteArray &ba) { return qstrnlen(ba.constData(), ba.size()); } |
|
227 static inline void appendTo(const QByteArray &ba, QChar *&out) |
|
228 { |
|
229 const char *data = ba.constData(); |
|
230 while (*data) |
|
231 *out++ = QLatin1Char(*data++); |
|
232 } |
|
233 }; |
|
234 #endif |
|
235 |
|
236 template <typename A, typename B> |
|
237 struct QConcatenable< QStringBuilder<A, B> > |
|
238 { |
|
239 typedef QStringBuilder<A, B> type; |
|
240 static int size(const type &p) |
|
241 { |
|
242 return QConcatenable<A>::size(p.a) + QConcatenable<B>::size(p.b); |
|
243 } |
|
244 static inline void appendTo(const QStringBuilder<A, B> &p, QChar *&out) |
|
245 { |
|
246 QConcatenable<A>::appendTo(p.a, out); |
|
247 QConcatenable<B>::appendTo(p.b, out); |
|
248 } |
|
249 }; |
|
250 |
|
251 template <typename A, typename B> |
|
252 QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type> |
|
253 operator%(const A &a, const B &b) |
|
254 { |
|
255 return QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>(a, b); |
|
256 } |
|
257 |
|
258 #ifdef QT_USE_FAST_OPERATOR_PLUS |
|
259 template <typename A, typename B> |
|
260 QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type> |
|
261 operator+(const A &a, const B &b) |
|
262 { |
|
263 return QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>(a, b); |
|
264 } |
|
265 #endif |
|
266 |
|
267 QT_END_NAMESPACE |
|
268 |
|
269 QT_END_HEADER |
|
270 |
|
271 #endif // QSTRINGBUILDER_H |