Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qstringbuilder.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:critical reason:data-parser
4
6#include <private/qstringconverter_p.h>
7
9
10/*!
11 \class QStringBuilder
12 \inmodule QtCore
13 \internal
14 \reentrant
15 \since 4.6
16
17 \brief The QStringBuilder class is a template class that provides a facility to build up QStrings and QByteArrays from smaller chunks.
18
19 \ingroup tools
20 \ingroup shared
21 \ingroup string-processing
22
23
24 To build a QString by multiple concatenations, QString::operator+()
25 is typically used. This causes \e{n - 1} allocations when building
26 a string from \e{n} chunks. The same is true for QByteArray.
27
28 QStringBuilder uses expression templates to collect the individual
29 chunks, compute the total size, allocate the required amount of
30 memory for the final string object, and copy the chunks into the
31 allocated memory.
32
33 The QStringBuilder class is not to be used explicitly in user
34 code. Instances of the class are created as return values of the
35 operator%() function, acting on objects of the following types:
36
37 For building QStrings:
38
39 \list
40 \li QString, (since 5.10:) QStringView
41 \li QChar, QLatin1Char, (since 5.10:) \c char16_t,
42 \li QLatin1StringView,
43 \li (since 5.10:) \c{const char16_t[]} (\c{u"foo"}),
44 \li QByteArray, \c char, \c{const char[]}.
45 \endlist
46
47 The types in the last list point are only available when
48 \c QT_NO_CAST_FROM_ASCII is not defined.
49
50 For building QByteArrays:
51
52 \list
53 \li QByteArray, \c char, \c{const char[]}.
54 \endlist
55
56 Concatenating strings with operator%() generally yields better
57 performance than using \c QString::operator+() on the same chunks
58 if there are three or more of them, and performs equally well in other
59 cases.
60
61 \note Defining \c QT_USE_QSTRINGBUILDER at build time (this is the
62 default when building Qt libraries and tools), will make using \c {'+'}
63 when concatenating strings work the same way as \c operator%().
64
65 \sa QLatin1StringView, QString
66*/
67
68/*!
69 \internal
70 \fn template <typename A, typename B> QStringBuilder<A, B>::QStringBuilder(const A &a, const B &b)
71
72 Constructs a QStringBuilder from \a a and \a b.
73 */
74
75/*!
76 \internal
77 \fn template <typename A, typename B> QStringBuilder<A, B>::operator%(const A &a, const B &b)
78
79 Returns a \c QStringBuilder object that is converted to a QString object
80 when assigned to a variable of QString type or passed to a function that
81 takes a QString parameter.
82
83 This function is usable with arguments of any of the following types:
84 \list
85 \li \c QAnyStringView,
86 \li \c QString, \c QStringView
87 \li \c QByteArray, \c QByteArrayView, \c QLatin1StringView
88 \li \c QChar, \c QLatin1Char, \c char, (since 5.10:) \c char16_t
89 \li (since 5.10:) \c{const char16_t[]} (\c{u"foo"}),
90 \endlist
91*/
92
93/*!
94 \internal
95 \fn template <typename A, typename B> QByteArray QStringBuilder<A, B>::toLatin1() const
96
97 Returns a Latin-1 representation of the string as a QByteArray. It
98 is undefined behavior if the string contains non-Latin1 characters.
99 */
100
101/*!
102 \internal
103 \fn template <typename A, typename B> QByteArray QStringBuilder<A, B>::toUtf8() const
104
105 Returns a UTF-8 representation of the string as a QByteArray.
106 */
107
108/*!
109 \internal
110 Converts the UTF-8 string viewed by \a in to UTF-16 and writes the result
111 to the buffer starting at \a out.
112 */
113void QAbstractConcatenable::convertFromUtf8(QByteArrayView in, QChar *&out) noexcept
114{
115 out = QUtf8::convertToUnicode(out, in);
116}
117
118QT_END_NAMESPACE