source: trunk/src/corelib/tools/qstringbuilder.h@ 986

Last change on this file since 986 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 9.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
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
55QT_BEGIN_HEADER
56
57QT_BEGIN_NAMESPACE
58
59QT_MODULE(Core)
60
61// ### Qt 5: merge with QLatin1String
62class QLatin1Literal
63{
64public:
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
72private:
73 const int m_size;
74 const char * const m_data;
75};
76
77struct Q_CORE_EXPORT QAbstractConcatenable
78{
79protected:
80 static void convertFromAscii(const char *a, int len, QChar *&out);
81
82 static inline void convertFromAscii(char a, QChar *&out)
83 {
84#ifndef QT_NO_TEXTCODEC
85 if (QString::codecForCStrings)
86 *out++ = QChar::fromAscii(a);
87 else
88#endif
89 *out++ = QLatin1Char(a);
90 }
91};
92
93template <typename T> struct QConcatenable {};
94
95template <typename A, typename B>
96class QStringBuilder
97{
98public:
99 QStringBuilder(const A &a_, const B &b_) : a(a_), b(b_) {}
100
101 operator QString() const
102 {
103 const uint size = QConcatenable< QStringBuilder<A, B> >::size(*this);
104 QString s(size, Qt::Uninitialized);
105
106 QChar *d = s.data();
107 const QChar * const start = d;
108 QConcatenable< QStringBuilder<A, B> >::appendTo(*this, d);
109
110 if (!QConcatenable< QStringBuilder<A, B> >::ExactSize && int(size) != d - start) {
111 // this resize is necessary since we allocate a bit too much
112 // when dealing with variable sized 8-bit encodings
113 s.resize(d - start);
114 }
115 return s;
116 }
117 QByteArray toLatin1() const { return QString(*this).toLatin1(); }
118
119 const A &a;
120 const B &b;
121};
122
123template <>
124class QStringBuilder <QString, QString>
125{
126 public:
127 QStringBuilder(const QString &a_, const QString &b_) : a(a_), b(b_) {}
128
129 operator QString() const
130 { QString r(a); r += b; return r; }
131 QByteArray toLatin1() const { return QString(*this).toLatin1(); }
132
133 const QString &a;
134 const QString &b;
135};
136
137template <> struct QConcatenable<char> : private QAbstractConcatenable
138{
139 typedef char type;
140 enum { ExactSize = true };
141 static int size(const char) { return 1; }
142 static inline void appendTo(const char c, QChar *&out)
143 {
144 QAbstractConcatenable::convertFromAscii(c, out);
145 }
146};
147
148template <> struct QConcatenable<QLatin1Char>
149{
150 typedef QLatin1Char type;
151 enum { ExactSize = true };
152 static int size(const QLatin1Char) { return 1; }
153 static inline void appendTo(const QLatin1Char c, QChar *&out)
154 {
155 *out++ = c;
156 }
157};
158
159template <> struct QConcatenable<QChar>
160{
161 typedef QChar type;
162 enum { ExactSize = true };
163 static int size(const QChar) { return 1; }
164 static inline void appendTo(const QChar c, QChar *&out)
165 {
166 *out++ = c;
167 }
168};
169
170template <> struct QConcatenable<QCharRef>
171{
172 typedef QCharRef type;
173 enum { ExactSize = true };
174 static int size(const QCharRef &) { return 1; }
175 static inline void appendTo(const QCharRef &c, QChar *&out)
176 {
177 *out++ = QChar(c);
178 }
179};
180
181template <> struct QConcatenable<QLatin1String>
182{
183 typedef QLatin1String type;
184 enum { ExactSize = true };
185 static int size(const QLatin1String &a) { return qstrlen(a.latin1()); }
186 static inline void appendTo(const QLatin1String &a, QChar *&out)
187 {
188 for (const char *s = a.latin1(); *s; )
189 *out++ = QLatin1Char(*s++);
190 }
191
192};
193
194template <> struct QConcatenable<QLatin1Literal>
195{
196 typedef QLatin1Literal type;
197 enum { ExactSize = true };
198 static int size(const QLatin1Literal &a) { return a.size(); }
199 static inline void appendTo(const QLatin1Literal &a, QChar *&out)
200 {
201 for (const char *s = a.data(); *s; )
202 *out++ = QLatin1Char(*s++);
203 }
204};
205
206template <> struct QConcatenable<QString>
207{
208 typedef QString type;
209 enum { ExactSize = true };
210 static int size(const QString &a) { return a.size(); }
211 static inline void appendTo(const QString &a, QChar *&out)
212 {
213 const int n = a.size();
214 memcpy(out, reinterpret_cast<const char*>(a.constData()), sizeof(QChar) * n);
215 out += n;
216 }
217};
218
219template <> struct QConcatenable<QStringRef>
220{
221 typedef QStringRef type;
222 enum { ExactSize = true };
223 static int size(const QStringRef &a) { return a.size(); }
224 static inline void appendTo(QStringRef a, QChar *&out)
225 {
226 const int n = a.size();
227 memcpy(out, reinterpret_cast<const char*>(a.constData()), sizeof(QChar) * n);
228 out += n;
229 }
230};
231
232#ifndef QT_NO_CAST_FROM_ASCII
233template <int N> struct QConcatenable<char[N]> : private QAbstractConcatenable
234{
235 typedef char type[N];
236 enum { ExactSize = false };
237 static int size(const char[N])
238 {
239 return N - 1;
240 }
241 static inline void appendTo(const char a[N], QChar *&out)
242 {
243 QAbstractConcatenable::convertFromAscii(a, N, out);
244 }
245};
246
247template <int N> struct QConcatenable<const char[N]> : private QAbstractConcatenable
248{
249 typedef const char type[N];
250 enum { ExactSize = false };
251 static int size(const char[N]) { return N - 1; }
252 static inline void appendTo(const char a[N], QChar *&out)
253 {
254 QAbstractConcatenable::convertFromAscii(a, N, out);
255 }
256};
257
258template <> struct QConcatenable<const char *> : private QAbstractConcatenable
259{
260 typedef char const *type;
261 enum { ExactSize = false };
262 static int size(const char *a) { return qstrlen(a); }
263 static inline void appendTo(const char *a, QChar *&out)
264 {
265 QAbstractConcatenable::convertFromAscii(a, -1, out);
266 }
267};
268
269template <> struct QConcatenable<QByteArray> : private QAbstractConcatenable
270{
271 typedef QByteArray type;
272 enum { ExactSize = false };
273 static int size(const QByteArray &ba) { return qstrnlen(ba.constData(), ba.size()); }
274 static inline void appendTo(const QByteArray &ba, QChar *&out)
275 {
276 QAbstractConcatenable::convertFromAscii(ba.constData(), -1, out);
277 }
278};
279#endif
280
281template <typename A, typename B>
282struct QConcatenable< QStringBuilder<A, B> >
283{
284 typedef QStringBuilder<A, B> type;
285 enum { ExactSize = QConcatenable<A>::ExactSize && QConcatenable<B>::ExactSize };
286 static int size(const type &p)
287 {
288 return QConcatenable<A>::size(p.a) + QConcatenable<B>::size(p.b);
289 }
290 static inline void appendTo(const QStringBuilder<A, B> &p, QChar *&out)
291 {
292 QConcatenable<A>::appendTo(p.a, out);
293 QConcatenable<B>::appendTo(p.b, out);
294 }
295};
296
297template <typename A, typename B>
298QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>
299operator%(const A &a, const B &b)
300{
301 return QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>(a, b);
302}
303
304#ifdef QT_USE_FAST_OPERATOR_PLUS
305template <typename A, typename B>
306QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>
307operator+(const A &a, const B &b)
308{
309 return QStringBuilder<typename QConcatenable<A>::type, typename QConcatenable<B>::type>(a, b);
310}
311#endif
312
313QT_END_NAMESPACE
314
315QT_END_HEADER
316
317#endif // QSTRINGBUILDER_H
Note: See TracBrowser for help on using the repository browser.