| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // Copyright (C) 2014 by Southwest Research Institute (R) |
| 3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 4 | |
| 5 | #include <qbytearraylist.h> |
| 6 | |
| 7 | QT_BEGIN_NAMESPACE |
| 8 | |
| 9 | /*! \typedef QByteArrayListIterator |
| 10 | \relates QByteArrayList |
| 11 | |
| 12 | The QByteArrayListIterator type definition provides a Java-style const |
| 13 | iterator for QByteArrayList. |
| 14 | |
| 15 | QByteArrayList provides both \l{Java-style iterators} and |
| 16 | \l{STL-style iterators}. The Java-style const iterator is simply |
| 17 | a type definition for QListIterator<QByteArray>. |
| 18 | |
| 19 | \sa QMutableByteArrayListIterator, QByteArrayList::const_iterator |
| 20 | */ |
| 21 | |
| 22 | /*! \typedef QMutableByteArrayListIterator |
| 23 | \relates QByteArrayList |
| 24 | |
| 25 | The QByteArrayListIterator type definition provides a Java-style |
| 26 | non-const iterator for QByteArrayList. |
| 27 | |
| 28 | QByteArrayList provides both \l{Java-style iterators} and |
| 29 | \l{STL-style iterators}. The Java-style non-const iterator is |
| 30 | simply a type definition for QMutableListIterator<QByteArray>. |
| 31 | |
| 32 | \sa QByteArrayListIterator, QByteArrayList::iterator |
| 33 | */ |
| 34 | |
| 35 | /*! |
| 36 | \class QByteArrayList |
| 37 | \inmodule QtCore |
| 38 | \since 5.4 |
| 39 | \brief The QByteArrayList class provides a list of byte arrays. |
| 40 | |
| 41 | \ingroup tools |
| 42 | \ingroup shared |
| 43 | \ingroup string-processing |
| 44 | |
| 45 | \reentrant |
| 46 | |
| 47 | QByteArrayList is actually just a QList<QByteArray>. It is documented as a |
| 48 | full class just for simplicity of documenting the member methods that exist |
| 49 | only in QList<QByteArray>. |
| 50 | |
| 51 | All of QList's functionality also applies to QByteArrayList. For example, you |
| 52 | can use isEmpty() to test whether the list is empty, and you can call |
| 53 | functions like append(), prepend(), insert(), replace(), removeAll(), |
| 54 | removeAt(), removeFirst(), removeLast(), and removeOne() to modify a |
| 55 | QByteArrayList. In addition, QByteArrayList provides several join() |
| 56 | methods for concatenating the list into a single QByteArray. |
| 57 | |
| 58 | The purpose of QByteArrayList is quite different from that of QStringList. |
| 59 | Whereas QStringList has many methods for manipulation of elements within |
| 60 | the list, QByteArrayList does not. |
| 61 | Normally, QStringList should be used whenever working with a list of printable |
| 62 | strings. QByteArrayList should be used to handle and efficiently join large blobs |
| 63 | of binary data, as when sequentially receiving serialized data through a |
| 64 | QIODevice. |
| 65 | |
| 66 | \sa QByteArray, QStringList |
| 67 | */ |
| 68 | |
| 69 | /*! |
| 70 | \fn QByteArray QByteArrayList::join(const QByteArray &separator) const |
| 71 | |
| 72 | Joins all the byte arrays into a single byte array with each |
| 73 | element separated by the given \a separator. |
| 74 | */ |
| 75 | |
| 76 | /*! |
| 77 | \fn QByteArray QByteArrayList::join(QByteArrayView separator) const |
| 78 | \since 6.3 |
| 79 | |
| 80 | Joins all the byte arrays into a single byte array with each |
| 81 | element separated by the given \a separator, if any. |
| 82 | */ |
| 83 | |
| 84 | /*! |
| 85 | \fn QByteArray QByteArrayList::join(char separator) const |
| 86 | |
| 87 | Joins all the byte arrays into a single byte array with each |
| 88 | element separated by the given \a separator. |
| 89 | */ |
| 90 | |
| 91 | static qsizetype QByteArrayList_joinedSize(const QByteArrayList *that, qsizetype seplen) |
| 92 | { |
| 93 | qsizetype totalLength = 0; |
| 94 | const qsizetype size = that->size(); |
| 95 | |
| 96 | for (qsizetype i = 0; i < size; ++i) |
| 97 | totalLength += that->at(i).size(); |
| 98 | |
| 99 | if (size > 0) |
| 100 | totalLength += seplen * (size - 1); |
| 101 | |
| 102 | return totalLength; |
| 103 | } |
| 104 | |
| 105 | QByteArray QtPrivate::QByteArrayList_join(const QByteArrayList *that, const char *sep, qsizetype seplen) |
| 106 | { |
| 107 | QByteArray res; |
| 108 | if (const qsizetype joinedSize = QByteArrayList_joinedSize(that, seplen)) |
| 109 | res.reserve(asize: joinedSize); // don't call reserve(0) - it allocates one byte for the NUL |
| 110 | const qsizetype size = that->size(); |
| 111 | for (qsizetype i = 0; i < size; ++i) { |
| 112 | if (i) |
| 113 | res.append(s: sep, len: seplen); |
| 114 | res += that->at(i); |
| 115 | } |
| 116 | return res; |
| 117 | } |
| 118 | |
| 119 | QT_END_NAMESPACE |
| 120 | |