1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 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 |
|
---|
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 * const m_data;
|
---|
75 | };
|
---|
76 |
|
---|
77 | struct Q_CORE_EXPORT QAbstractConcatenable
|
---|
78 | {
|
---|
79 | protected:
|
---|
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 |
|
---|
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);
|
---|
105 |
|
---|
106 | QChar *d = s.data();
|
---|
107 | QConcatenable< QStringBuilder<A, B> >::appendTo(*this, d);
|
---|
108 | // this resize is necessary since we allocate a bit too much
|
---|
109 | // when dealing with variable sized 8-bit encodings
|
---|
110 | s.resize(d - s.data());
|
---|
111 | return s;
|
---|
112 | }
|
---|
113 | QByteArray toLatin1() const { return QString(*this).toLatin1(); }
|
---|
114 |
|
---|
115 | const A &a;
|
---|
116 | const B &b;
|
---|
117 | };
|
---|
118 |
|
---|
119 |
|
---|
120 | template <> struct QConcatenable<char> : private QAbstractConcatenable
|
---|
121 | {
|
---|
122 | typedef char type;
|
---|
123 | static int size(const char) { return 1; }
|
---|
124 | static inline void appendTo(const char c, QChar *&out)
|
---|
125 | {
|
---|
126 | QAbstractConcatenable::convertFromAscii(c, out);
|
---|
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);
|
---|
191 | out += n;
|
---|
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);
|
---|
203 | out += n;
|
---|
204 | }
|
---|
205 | };
|
---|
206 |
|
---|
207 | #ifndef QT_NO_CAST_FROM_ASCII
|
---|
208 | template <int N> struct QConcatenable<char[N]> : private QAbstractConcatenable
|
---|
209 | {
|
---|
210 | typedef char type[N];
|
---|
211 | static int size(const char[N])
|
---|
212 | {
|
---|
213 | return N - 1;
|
---|
214 | }
|
---|
215 | static inline void appendTo(const char a[N], QChar *&out)
|
---|
216 | {
|
---|
217 | QAbstractConcatenable::convertFromAscii(a, N, out);
|
---|
218 | }
|
---|
219 | };
|
---|
220 |
|
---|
221 | template <int N> struct QConcatenable<const char[N]> : private QAbstractConcatenable
|
---|
|
---|