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

source code of qtbase/src/corelib/text/qstringbuilder.cpp