Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/tools/qstringbuilder.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
9 | ** Commercial License Usage | - | ||||||||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||
16 | ** | - | ||||||||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
29 | ** | - | ||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||
31 | ** | - | ||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||
33 | - | |||||||||||||
34 | #include "qstringbuilder.h" | - | ||||||||||||
35 | #include <QtCore/qtextcodec.h> | - | ||||||||||||
36 | - | |||||||||||||
37 | QT_BEGIN_NAMESPACE | - | ||||||||||||
38 | - | |||||||||||||
39 | /*! | - | ||||||||||||
40 | \class QStringBuilder | - | ||||||||||||
41 | \inmodule QtCore | - | ||||||||||||
42 | \internal | - | ||||||||||||
43 | \reentrant | - | ||||||||||||
44 | \since 4.6 | - | ||||||||||||
45 | - | |||||||||||||
46 | \brief The QStringBuilder class is a template class that provides a facility to build up QStrings and QByteArrays from smaller chunks. | - | ||||||||||||
47 | - | |||||||||||||
48 | \ingroup tools | - | ||||||||||||
49 | \ingroup shared | - | ||||||||||||
50 | \ingroup string-processing | - | ||||||||||||
51 | - | |||||||||||||
52 | - | |||||||||||||
53 | To build a QString by multiple concatenations, QString::operator+() | - | ||||||||||||
54 | is typically used. This causes \e{n - 1} allocations when building | - | ||||||||||||
55 | a string from \e{n} chunks. The same is true for QByteArray. | - | ||||||||||||
56 | - | |||||||||||||
57 | QStringBuilder uses expression templates to collect the individual | - | ||||||||||||
58 | chunks, compute the total size, allocate the required amount of | - | ||||||||||||
59 | memory for the final string object, and copy the chunks into the | - | ||||||||||||
60 | allocated memory. | - | ||||||||||||
61 | - | |||||||||||||
62 | The QStringBuilder class is not to be used explicitly in user | - | ||||||||||||
63 | code. Instances of the class are created as return values of the | - | ||||||||||||
64 | operator%() function, acting on objects of the following types: | - | ||||||||||||
65 | - | |||||||||||||
66 | For building QStrings: | - | ||||||||||||
67 | - | |||||||||||||
68 | \li QString, QStringRef, | - | ||||||||||||
69 | \li QChar, QCharRef, QLatin1Char, | - | ||||||||||||
70 | \li QLatin1String, | - | ||||||||||||
71 | \li QByteArray, \c char, \c{const char[]}. | - | ||||||||||||
72 | - | |||||||||||||
73 | The types in the last list point are only available when | - | ||||||||||||
74 | QT_NO_CAST_FROM_ASCII is not defined. | - | ||||||||||||
75 | - | |||||||||||||
76 | For building QByteArrays: | - | ||||||||||||
77 | - | |||||||||||||
78 | \li QByteArray, \c char, \c{const char[]}. | - | ||||||||||||
79 | - | |||||||||||||
80 | Concatenating strings with operator%() generally yields better | - | ||||||||||||
81 | performance than using \c QString::operator+() on the same chunks | - | ||||||||||||
82 | if there are three or more of them, and performs equally well in other | - | ||||||||||||
83 | cases. | - | ||||||||||||
84 | - | |||||||||||||
85 | \sa QLatin1String, QString | - | ||||||||||||
86 | */ | - | ||||||||||||
87 | - | |||||||||||||
88 | /*! \fn QStringBuilder::QStringBuilder(const A &a, const B &b) | - | ||||||||||||
89 | Constructs a QStringBuilder from \a a and \a b. | - | ||||||||||||
90 | */ | - | ||||||||||||
91 | - | |||||||||||||
92 | /* \fn QStringBuilder::operator%(const A &a, const B &b) | - | ||||||||||||
93 | - | |||||||||||||
94 | Returns a \c QStringBuilder object that is converted to a QString object | - | ||||||||||||
95 | when assigned to a variable of QString type or passed to a function that | - | ||||||||||||
96 | takes a QString parameter. | - | ||||||||||||
97 | - | |||||||||||||
98 | This function is usable with arguments of type \c QString, | - | ||||||||||||
99 | \c QLatin1String, \c QStringRef, | - | ||||||||||||
100 | \c QChar, \c QCharRef, \c QLatin1Char, and \c char. | - | ||||||||||||
101 | */ | - | ||||||||||||
102 | - | |||||||||||||
103 | /* \fn QByteArray QStringBuilder::toLatin1() const | - | ||||||||||||
104 | Returns a Latin-1 representation of the string as a QByteArray. The | - | ||||||||||||
105 | returned byte array is undefined if the string contains non-Latin1 | - | ||||||||||||
106 | characters. | - | ||||||||||||
107 | */ | - | ||||||||||||
108 | /* \fn QByteArray QStringBuilder::toUtf8() const | - | ||||||||||||
109 | Returns a UTF-8 representation of the string as a QByteArray. | - | ||||||||||||
110 | */ | - | ||||||||||||
111 | - | |||||||||||||
112 | - | |||||||||||||
113 | /*! | - | ||||||||||||
114 | \internal | - | ||||||||||||
115 | */ | - | ||||||||||||
116 | void QAbstractConcatenable::convertFromAscii(const char *a, int len, QChar *&out) | - | ||||||||||||
117 | { | - | ||||||||||||
118 | if (len == -1) {
| 8-274 | ||||||||||||
119 | if (!a)
| 4 | ||||||||||||
120 | return; executed 4 times by 2 tests: return; Executed by:
| 4 | ||||||||||||
121 | while (*a && uchar(*a) < 0x80U)
| 1-4 | ||||||||||||
122 | *out++ = QLatin1Char(*a++); executed 3 times by 1 test: *out++ = QLatin1Char(*a++); Executed by:
| 3 | ||||||||||||
123 | if (!*a)
| 1-3 | ||||||||||||
124 | return; executed 3 times by 1 test: return; Executed by:
| 3 | ||||||||||||
125 | } else { executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
126 | int i; | - | ||||||||||||
127 | for (i = 0; i < len && uchar(a[i]) < 0x80U; ++i)
| 16-1388 | ||||||||||||
128 | *out++ = QLatin1Char(a[i]); executed 1372 times by 4 tests: *out++ = QLatin1Char(a[i]); Executed by:
| 1372 | ||||||||||||
129 | if (i == len)
| 16-258 | ||||||||||||
130 | return; executed 258 times by 4 tests: return; Executed by:
| 258 | ||||||||||||
131 | a += i; | - | ||||||||||||
132 | len -= i; | - | ||||||||||||
133 | } executed 16 times by 3 tests: end of block Executed by:
| 16 | ||||||||||||
134 | - | |||||||||||||
135 | // we need to complement with UTF-8 appending | - | ||||||||||||
136 | QString tmp = QString::fromUtf8(a, len); | - | ||||||||||||
137 | memcpy(out, reinterpret_cast<const char *>(tmp.constData()), sizeof(QChar) * tmp.size()); | - | ||||||||||||
138 | out += tmp.size(); | - | ||||||||||||
139 | } executed 17 times by 3 tests: end of block Executed by:
| 17 | ||||||||||||
140 | - | |||||||||||||
141 | QT_END_NAMESPACE | - | ||||||||||||
Source code | Switch to Preprocessed file |