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 | #include "qstringbuilder.h"
|
---|
43 |
|
---|
44 | QT_BEGIN_NAMESPACE
|
---|
45 |
|
---|
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 | */
|
---|
148 |
|
---|
149 | /*! \internal */
|
---|
150 | void QAbstractConcatenable::convertFromAscii(const char *a, int len, QChar *&out)
|
---|
151 | {
|
---|
152 | #ifndef QT_NO_TEXTCODEC
|
---|
153 | if (QString::codecForCStrings) {
|
---|
154 | QString tmp = QString::fromAscii(a);
|
---|
155 | memcpy(out, reinterpret_cast<const char *>(tmp.constData()), sizeof(QChar) * tmp.size());
|
---|
156 | out += tmp.length();
|
---|
157 | return;
|
---|
158 | }
|
---|
159 | #endif
|
---|
160 | if (len == -1) {
|
---|
161 | while (*a)
|
---|
162 | *out++ = QLatin1Char(*a++);
|
---|
163 | } else {
|
---|
164 | for (int i = 0; i < len - 1; ++i)
|
---|
165 | *out++ = QLatin1Char(a[i]);
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | QT_END_NAMESPACE
|
---|