| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 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 https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://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 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qstringbuilder.h" |
| 41 | #include <private/qutfcodec_p.h> |
| 42 | |
| 43 | QT_BEGIN_NAMESPACE |
| 44 | |
| 45 | /*! |
| 46 | \class QStringBuilder |
| 47 | \inmodule QtCore |
| 48 | \internal |
| 49 | \reentrant |
| 50 | \since 4.6 |
| 51 | |
| 52 | \brief The QStringBuilder class is a template class that provides a facility to build up QStrings and QByteArrays from smaller chunks. |
| 53 | |
| 54 | \ingroup tools |
| 55 | \ingroup shared |
| 56 | \ingroup string-processing |
| 57 | |
| 58 | |
| 59 | To build a QString by multiple concatenations, QString::operator+() |
| 60 | is typically used. This causes \e{n - 1} allocations when building |
| 61 | a string from \e{n} chunks. The same is true for QByteArray. |
| 62 | |
| 63 | QStringBuilder uses expression templates to collect the individual |
| 64 | chunks, compute the total size, allocate the required amount of |
| 65 | memory for the final string object, and copy the chunks into the |
| 66 | allocated memory. |
| 67 | |
| 68 | The QStringBuilder class is not to be used explicitly in user |
| 69 | code. Instances of the class are created as return values of the |
| 70 | operator%() function, acting on objects of the following types: |
| 71 | |
| 72 | For building QStrings: |
| 73 | |
| 74 | \list |
| 75 | \li QString, QStringRef, (since 5.10:) QStringView |
| 76 | \li QChar, QCharRef, QLatin1Char, (since 5.10:) \c char16_t, |
| 77 | \li QLatin1String, |
| 78 | \li (since 5.10:) \c{const char16_t[]} (\c{u"foo"}), |
| 79 | \li QByteArray, \c char, \c{const char[]}. |
| 80 | \endlist |
| 81 | |
| 82 | The types in the last list point are only available when |
| 83 | \c QT_NO_CAST_FROM_ASCII is not defined. |
| 84 | |
| 85 | For building QByteArrays: |
| 86 | |
| 87 | \list |
| 88 | \li QByteArray, \c char, \c{const char[]}. |
| 89 | \endlist |
| 90 | |
| 91 | Concatenating strings with operator%() generally yields better |
| 92 | performance than using \c QString::operator+() on the same chunks |
| 93 | if there are three or more of them, and performs equally well in other |
| 94 | cases. |
| 95 | |
| 96 | \sa QLatin1String, QString |
| 97 | */ |
| 98 | |
| 99 | /*! \fn template <typename A, typename B> QStringBuilder<A, B>::QStringBuilder(const A &a, const B &b) |
| 100 | Constructs a QStringBuilder from \a a and \a b. |
| 101 | */ |
| 102 | |
| 103 | /* \fn template <typename A, typename B> QStringBuilder<A, B>::operator%(const A &a, const B &b) |
| 104 | |
| 105 | Returns a \c QStringBuilder object that is converted to a QString object |
| 106 | when assigned to a variable of QString type or passed to a function that |
| 107 | takes a QString parameter. |
| 108 | |
| 109 | This function is usable with arguments of type \c QString, |
| 110 | \c QLatin1String, \c QStringRef, |
| 111 | \c QChar, \c QCharRef, \c QLatin1Char, and \c char. |
| 112 | */ |
| 113 | |
| 114 | /* \fn template <typename A, typename B> QByteArray QStringBuilder<A, B>::toLatin1() const |
| 115 | Returns a Latin-1 representation of the string as a QByteArray. The |
| 116 | returned byte array is undefined if the string contains non-Latin1 |
| 117 | characters. |
| 118 | */ |
| 119 | /* \fn template <typename A, typename B> QByteArray QStringBuilder<A, B>::toUtf8() const |
| 120 | Returns a UTF-8 representation of the string as a QByteArray. |
| 121 | */ |
| 122 | |
| 123 | |
| 124 | /*! |
| 125 | \internal |
| 126 | */ |
| 127 | void QAbstractConcatenable::convertFromAscii(const char *a, int len, QChar *&out) noexcept |
| 128 | { |
| 129 | if (Q_UNLIKELY(len == -1)) { |
| 130 | if (!a) |
| 131 | return; |
| 132 | len = int(strlen(s: a)); |
| 133 | } |
| 134 | out = QUtf8::convertToUnicode(out, a, len); |
| 135 | } |
| 136 | |
| 137 | QT_END_NAMESPACE |
| 138 | |