author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Wed, 21 Apr 2010 11:15:19 +0300 | |
branch | RCL_3 |
changeset 11 | 25a739ee40f4 |
parent 7 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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> |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
69 |
QLatin1Literal(const char (&str)[N]) |
0 | 70 |
: m_size(N - 1), m_data(str) {} |
71 |
||
72 |
private: |
|
73 |
const int m_size; |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
74 |
const char * const m_data; |
0 | 75 |
}; |
76 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
77 |
struct Q_CORE_EXPORT QAbstractConcatenable |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
78 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
79 |
protected: |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
80 |
static void convertFromAscii(const char *a, int len, QChar *&out); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
81 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
82 |
static inline void convertFromAscii(char a, QChar *&out) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
83 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
84 |
#ifndef QT_NO_TEXTCODEC |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
85 |
if (QString::codecForCStrings) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
86 |
*out++ = QChar::fromAscii(a); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
87 |
else |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
88 |
#endif |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
89 |
*out++ = QLatin1Char(a); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
90 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
91 |
}; |
0 | 92 |
|
93 |
template <typename T> struct QConcatenable {}; |
|
94 |
||
95 |
template <typename A, typename B> |
|
96 |
class QStringBuilder |
|
97 |
{ |
|
98 |
public: |
|
99 |
QStringBuilder(const A &a_, const B &b_) : a(a_), b(b_) {} |
|
100 |
||
101 |
operator QString() const |
|
102 |
{ |
|
103 |
QString s(QConcatenable< QStringBuilder<A, B> >::size(*this), |
|
104 |
Qt::Uninitialized); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
105 |
|
0 | 106 |
QChar *d = s.data(); |
107 |
QConcatenable< QStringBuilder<A, B> >::appendTo(*this, d); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
108 |
// this resize is necessary since we allocate a bit too much |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
109 |
// when dealing with variable sized 8-bit encodings |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
110 |
s.resize(d - s.data()); |
0 | 111 |
return s; |
112 |
} |
|
113 |
QByteArray toLatin1() const { return QString(*this).toLatin1(); } |
|
114 |
||
115 |
const A &a; |
|
116 |
const B &b; |
|
117 |
}; |
|
118 |
||
119 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
120 |
template <> struct QConcatenable<char> : private QAbstractConcatenable |
0 | 121 |
{ |
122 |
typedef char type; |
|
123 |
static int size(const char) { return 1; } |
|
124 |
static inline void appendTo(const char c, QChar *&out) |
|
125 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
126 |
QAbstractConcatenable::convertFromAscii(c, out); |
0 | 127 |
} |
128 |
}; |
|
129 |
||
130 |
template <> struct QConcatenable<QLatin1Char> |
|
131 |
{ |
|
132 |
typedef QLatin1Char type; |
|
133 |
static int size(const QLatin1Char) { return 1; } |
|
134 |
static inline void appendTo(const QLatin1Char c, QChar *&out) |
|
135 |
{ |
|
136 |
*out++ = c; |
|
137 |
} |
|
138 |
}; |
|
139 |
||
140 |
template <> struct QConcatenable<QChar> |
|
141 |
{ |
|
142 |
typedef QChar type; |
|
143 |
static int size(const QChar) { return 1; } |
|
144 |
static inline void appendTo(const QChar c, QChar *&out) |
|
145 |
{ |
|
146 |
*out++ = c; |
|
147 |
} |
|
148 |
}; |
|
149 |
||
150 |
template <> struct QConcatenable<QCharRef> |
|
151 |
{ |
|
152 |
typedef QCharRef type; |
|
153 |
static int size(const QCharRef &) { return 1; } |
|
154 |
static inline void appendTo(const QCharRef &c, QChar *&out) |
|
155 |
{ |
|
156 |
*out++ = QChar(c); |
|
157 |
} |
|
158 |
}; |
|
159 |
||
160 |
template <> struct QConcatenable<QLatin1String> |
|
161 |
{ |
|
162 |
typedef QLatin1String type; |
|
163 |
static int size(const QLatin1String &a) { return qstrlen(a.latin1()); } |
|
164 |
static inline void appendTo(const QLatin1String &a, QChar *&out) |
|
165 |
{ |
|
166 |
for (const char *s = a.latin1(); *s; ) |
|
167 |
*out++ = QLatin1Char(*s++); |
|
168 |
} |
|
169 |
||
170 |
}; |
|
171 |
||
172 |
template <> struct QConcatenable<QLatin1Literal> |
|
173 |
{ |
|
174 |
typedef QLatin1Literal type; |
|
175 |
static int size(const QLatin1Literal &a) { return a.size(); } |
|
176 |
static inline void appendTo(const QLatin1Literal &a, QChar *&out) |
|
177 |
{ |
|
178 |
for (const char *s = a.data(); *s; ) |
|
179 |
*out++ = QLatin1Char(*s++); |
|
180 |
} |
|
181 |
}; |
|
182 |
||
183 |
template <> struct QConcatenable<QString> |
|
184 |
{ |
|
185 |
typedef QString type; |
|
186 |
static int size(const QString &a) { return a.size(); } |
|
187 |
static inline void appendTo(const QString &a, QChar *&out) |
|
188 |
{ |
|
189 |
const int n = a.size(); |
|
190 |
memcpy(out, reinterpret_cast<const char*>(a.constData()), sizeof(QChar) * n); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
191 |
out += n; |
0 | 192 |
} |
193 |
}; |
|
194 |
||
195 |
template <> struct QConcatenable<QStringRef> |
|
196 |
{ |
|
197 |
typedef QStringRef type; |
|
198 |
static int size(const QStringRef &a) { return a.size(); } |
|
199 |
static inline void appendTo(QStringRef a, QChar *&out) |
|
200 |
{ |
|
201 |
const int n = a.size(); |
|
202 |
memcpy(out, reinterpret_cast<const char*>(a.constData()), sizeof(QChar) * n); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
203 |
out += n; |
0 | 204 |
} |
205 |
}; |
|
206 |
||
207 |
#ifndef QT_NO_CAST_FROM_ASCII |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
208 |
template <int N> struct QConcatenable<char[N]> : private QAbstractConcatenable |
0 | 209 |
{ |
210 |
typedef char type[N]; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
211 |
static int size(const char[N]) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
212 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
213 |
return N - 1; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
214 |
} |
0 | 215 |
static inline void appendTo(const char a[N], QChar *&out) |
216 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
217 |
QAbstractConcatenable::convertFromAscii(a, N, out); |
0 | 218 |
} |
219 |
}; |
|
220 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
221 |
template <int N> struct QConcatenable<const char[N]> : private QAbstractConcatenable |
0 | 222 |
{ |
223 |
typedef const char type[N]; |
|
224 |
static int size(const char[N]) { return N - 1; } |
|
225 |
static inline void appendTo(const char a[N], QChar *&out) |
|
226 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
227 |
QAbstractConcatenable::convertFromAscii(a, N, out); |
0 | 228 |
} |
229 |
}; |
|
230 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
231 |
template <> struct QConcatenable<const char *> : private QAbstractConcatenable |
0 | 232 |
{ |
233 |
typedef char const *type; |
|
234 |
static int size(const char *a) { return qstrlen(a); } |
|
235 |
static inline void appendTo(const char *a, QChar *&out) |
|
236 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
237 |
QAbstractConcatenable::convertFromAscii(a, -1, out); |
0 | 238 |
} |
239 |
}; |
|
240 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
241 |
template <> struct QConcatenable<QByteArray> : private QAbstractConcatenable |
0 | 242 |
{ |
243 |
typedef QByteArray type; |
|
244 |
static int size(const QByteArray &ba) { return qstrnlen(ba.constData(), ba.size()); } |
|
245 |
static inline void appendTo(const QByteArray &ba, QChar *&out) |
|
246 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
247 |
QAbstractConcatenable::convertFromAscii(ba.constData(), -1, out); |
0 | 248 |
} |
249 |
}; |
|
250 |
#endif |
|
251 |
||
252 |
template <typename A, typename B> |
|
253 |
struct QConcatenable< QStringBuilder<A, B> > |
|
254 |
{ |
|
255 |
typedef QStringBuilder<A, B> type; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
256 |
static int size(const type &p) |
0 | 257 |
{ |
258 |
return QConcatenable<A>::size(p.a) + QConcatenable<B>::size(p.b); |
|
259 |
} |
|
260 |
static inline void appendTo(const QStringBuilder<A, B> &p, QChar *&out) |
|
261 |
{ |
|
262 |
QConcatenable<A>::appendTo(p.a, out); |
|
263 |
QConcatenable<B>::appendTo(p.b, out); |
|
264 |
} |
|
265 |
}; |
|
266 |
||
267 |
template <typename A, typename B> |
|
268 |
QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type> |
|
269 |
operator%(const A &a, const B &b) |
|
270 |
{ |
|
271 |
return QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>(a, b); |
|
272 |
} |
|
273 |
||
274 |
#ifdef QT_USE_FAST_OPERATOR_PLUS |
|
275 |
template <typename A, typename B> |
|
276 |
QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type> |
|
277 |
operator+(const A &a, const B &b) |
|
278 |
{ |
|
279 |
return QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>(a, b); |
|
280 |
} |
|
281 |
#endif |
|
282 |
||
283 |
QT_END_NAMESPACE |
|
284 |
||
285 |
QT_END_HEADER |
|
286 |
||
287 |
#endif // QSTRINGBUILDER_H |