author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 02 Feb 2010 00:43:10 +0200 | |
changeset 3 | 41300fa6a67c |
parent 0 | 1918ee327afb |
child 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 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 |
#include "qstringbuilder.h" |
|
43 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
44 |
QT_BEGIN_NAMESPACE |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
45 |
|
0 | 46 |
/*! |
47 |
\class QLatin1Literal |
|
48 |
\internal |
|
49 |
\reentrant |
|
50 |
\since 4.6 |
|
51 |
||
52 |
\brief The QLatin1Literal class provides a thin wrapper around string |
|
53 |
literals used in source code. |
|
54 |
||
55 |
\ingroup tools |
|
56 |
\ingroup shared |
|
57 |
\ingroup string-processing |
|
58 |
||
59 |
||
60 |
Unlike \c QLatin1String, a \c QLatin1Literal can retrieve its size |
|
61 |
without iterating over the literal. |
|
62 |
||
63 |
The main use of \c QLatin1Literal is in conjunction with \c QStringBuilder |
|
64 |
to reduce the number of reallocations needed to build up a string from |
|
65 |
smaller chunks. |
|
66 |
||
67 |
\sa QStringBuilder, QLatin1String, QString, QStringRef |
|
68 |
*/ |
|
69 |
||
70 |
/*! \fn int QLatin1Literal::size() const |
|
71 |
||
72 |
Returns the number of characters in the literal \e{excluding} the trailing |
|
73 |
NULL char. |
|
74 |
*/ |
|
75 |
||
76 |
/*! \fn QLatin1Literal::QLatin1Literal(const char str) |
|
77 |
||
78 |
Constructs a new literal from the string \a str. |
|
79 |
*/ |
|
80 |
||
81 |
/*! \fn const char *QLatin1Literal::data() const |
|
82 |
||
83 |
Returns a pointer to the first character of the string literal. |
|
84 |
The string literal is terminated by a NUL character. |
|
85 |
*/ |
|
86 |
||
87 |
/*! |
|
88 |
\class QStringBuilder |
|
89 |
\internal |
|
90 |
\reentrant |
|
91 |
\since 4.6 |
|
92 |
||
93 |
\brief The QStringBuilder class is a template class that provides a facility to build up QStrings from smaller chunks. |
|
94 |
||
95 |
\ingroup tools |
|
96 |
\ingroup shared |
|
97 |
\ingroup string-processing |
|
98 |
||
99 |
||
100 |
To build a QString by multiple concatenations, QString::operator+() |
|
101 |
is typically used. This causes \e{n - 1} reallocations when building |
|
102 |
a string from \e{n} chunks. |
|
103 |
||
104 |
QStringBuilder uses expression templates to collect the individual |
|
105 |
chunks, compute the total size, allocate the required amount of |
|
106 |
memory for the final QString object, and copy the chunks into the |
|
107 |
allocated memory. |
|
108 |
||
109 |
The QStringBuilder class is not to be used explicitly in user |
|
110 |
code. Instances of the class are created as return values of the |
|
111 |
operator%() function, acting on objects of type QString, |
|
112 |
QLatin1String, QLatin1Literal, QStringRef, QChar, QCharRef, |
|
113 |
QLatin1Char, and \c char. |
|
114 |
||
115 |
Concatenating strings with operator%() generally yields better |
|
116 |
performance then using \c QString::operator+() on the same chunks |
|
117 |
if there are three or more of them, and performs equally well in other |
|
118 |
cases. |
|
119 |
||
120 |
\sa QLatin1Literal, QString |
|
121 |
*/ |
|
122 |
||
123 |
/*! \fn QStringBuilder::QStringBuilder(const A &a, const B &b) |
|
124 |
Constructs a QStringBuilder from \a a and \a b. |
|
125 |
*/ |
|
126 |
||
127 |
/* \fn QStringBuilder::operator%(const A &a, const B &b) |
|
128 |
||
129 |
Returns a \c QStringBuilder object that is converted to a QString object |
|
130 |
when assigned to a variable of QString type or passed to a function that |
|
131 |
takes a QString parameter. |
|
132 |
||
133 |
This function is usable with arguments of type \c QString, |
|
134 |
\c QLatin1String, \c QLatin1Literal, \c QStringRef, |
|
135 |
\c QChar, \c QCharRef, \c QLatin1Char, and \c char. |
|
136 |
*/ |
|
137 |
||
138 |
/*! \fn QByteArray QStringBuilder::toLatin1() const |
|
139 |
Returns a Latin-1 representation of the string as a QByteArray. The |
|
140 |
returned byte array is undefined if the string contains non-Latin1 |
|
141 |
characters. |
|
142 |
*/ |
|
143 |
||
144 |
/*! \fn QStringBuilder::operator QString() const |
|
145 |
||
146 |
Converts the \c QLatin1Literal into a \c QString object. |
|
147 |
*/ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
148 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
149 |
/*! \internal */ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
150 |
void QAbstractConcatenable::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
|
151 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
152 |
#ifndef QT_NO_TEXTCODEC |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
153 |
if (QString::codecForCStrings) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
154 |
QString tmp = QString::fromAscii(a); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
155 |
memcpy(out, reinterpret_cast<const char *>(tmp.constData()), sizeof(QChar) * tmp.size()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
156 |
out += tmp.length(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
157 |
return; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
158 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
159 |
#endif |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
160 |
if (len == -1) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
161 |
while (*a) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
162 |
*out++ = QLatin1Char(*a++); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
163 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
164 |
for (int i = 0; i < len - 1; ++i) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
165 |
*out++ = QLatin1Char(a[i]); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
166 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
167 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
168 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
169 |
QT_END_NAMESPACE |