1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Copyright (C) 2014 by Southwest Research Institute (R) |
5 | ** Contact: https://www.qt.io/licensing/ |
6 | ** |
7 | ** This file is part of the QtCore module of the Qt Toolkit. |
8 | ** |
9 | ** $QT_BEGIN_LICENSE:LGPL$ |
10 | ** Commercial License Usage |
11 | ** Licensees holding valid commercial Qt licenses may use this file in |
12 | ** accordance with the commercial license agreement provided with the |
13 | ** Software or, alternatively, in accordance with the terms contained in |
14 | ** a written agreement between you and The Qt Company. For licensing terms |
15 | ** and conditions see https://www.qt.io/terms-conditions. For further |
16 | ** information use the contact form at https://www.qt.io/contact-us. |
17 | ** |
18 | ** GNU Lesser General Public License Usage |
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
20 | ** General Public License version 3 as published by the Free Software |
21 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
22 | ** packaging of this file. Please review the following information to |
23 | ** ensure the GNU Lesser General Public License version 3 requirements |
24 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
25 | ** |
26 | ** GNU General Public License Usage |
27 | ** Alternatively, this file may be used under the terms of the GNU |
28 | ** General Public License version 2.0 or (at your option) the GNU General |
29 | ** Public license version 3 or any later version approved by the KDE Free |
30 | ** Qt Foundation. The licenses are as published by the Free Software |
31 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
32 | ** included in the packaging of this file. Please review the following |
33 | ** information to ensure the GNU General Public License requirements will |
34 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
35 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
36 | ** |
37 | ** $QT_END_LICENSE$ |
38 | ** |
39 | ****************************************************************************/ |
40 | |
41 | #include <qbytearraylist.h> |
42 | |
43 | QT_BEGIN_NAMESPACE |
44 | |
45 | /*! \typedef QByteArrayListIterator |
46 | \relates QByteArrayList |
47 | |
48 | The QByteArrayListIterator type definition provides a Java-style const |
49 | iterator for QByteArrayList. |
50 | |
51 | QByteArrayList provides both \l{Java-style iterators} and |
52 | \l{STL-style iterators}. The Java-style const iterator is simply |
53 | a type definition for QListIterator<QByteArray>. |
54 | |
55 | \sa QMutableByteArrayListIterator, QByteArrayList::const_iterator |
56 | */ |
57 | |
58 | /*! \typedef QMutableByteArrayListIterator |
59 | \relates QByteArrayList |
60 | |
61 | The QByteArrayListIterator type definition provides a Java-style |
62 | non-const iterator for QByteArrayList. |
63 | |
64 | QByteArrayList provides both \l{Java-style iterators} and |
65 | \l{STL-style iterators}. The Java-style non-const iterator is |
66 | simply a type definition for QMutableListIterator<QByteArray>. |
67 | |
68 | \sa QByteArrayListIterator, QByteArrayList::iterator |
69 | */ |
70 | |
71 | /*! |
72 | \class QByteArrayList |
73 | \inmodule QtCore |
74 | \since 5.4 |
75 | \brief The QByteArrayList class provides a list of byte arrays. |
76 | |
77 | \ingroup tools |
78 | \ingroup shared |
79 | \ingroup string-processing |
80 | |
81 | \reentrant |
82 | |
83 | QByteArrayList is actually just a QList<QByteArray>. It is documented as a |
84 | full class just for simplicity of documenting the member methods that exist |
85 | only in QList<QByteArray>. |
86 | |
87 | All of QList's functionality also applies to QByteArrayList. For example, you |
88 | can use isEmpty() to test whether the list is empty, and you can call |
89 | functions like append(), prepend(), insert(), replace(), removeAll(), |
90 | removeAt(), removeFirst(), removeLast(), and removeOne() to modify a |
91 | QByteArrayList. In addition, QByteArrayList provides several join() |
92 | methods for concatenating the list into a single QByteArray. |
93 | |
94 | The purpose of QByteArrayList is quite different from that of QStringList. |
95 | Whereas QStringList has many methods for manipulation of elements within |
96 | the list, QByteArrayList does not. |
97 | Normally, QStringList should be used whenever working with a list of printable |
98 | strings. QByteArrayList should be used to handle and efficiently join large blobs |
99 | of binary data, as when sequentially receiving serialized data through a |
100 | QIODevice. |
101 | |
102 | \sa QByteArray, QStringList |
103 | */ |
104 | |
105 | /*! |
106 | \fn QByteArray QByteArrayList::join() const |
107 | |
108 | Joins all the byte arrays into a single byte array. |
109 | */ |
110 | |
111 | /*! |
112 | \fn QByteArray QByteArrayList::join(const QByteArray &separator) const |
113 | |
114 | Joins all the byte arrays into a single byte array with each |
115 | element separated by the given \a separator. |
116 | */ |
117 | |
118 | /*! |
119 | \fn QByteArray QByteArrayList::join(char separator) const |
120 | |
121 | Joins all the byte arrays into a single byte array with each |
122 | element separated by the given \a separator. |
123 | */ |
124 | |
125 | static int QByteArrayList_joinedSize(const QByteArrayList *that, int seplen) |
126 | { |
127 | int totalLength = 0; |
128 | const int size = that->size(); |
129 | |
130 | for (int i = 0; i < size; ++i) |
131 | totalLength += that->at(i).size(); |
132 | |
133 | if (size > 0) |
134 | totalLength += seplen * (size - 1); |
135 | |
136 | return totalLength; |
137 | } |
138 | |
139 | QByteArray QtPrivate::QByteArrayList_join(const QByteArrayList *that, const char *sep, int seplen) |
140 | { |
141 | QByteArray res; |
142 | if (const int joinedSize = QByteArrayList_joinedSize(that, seplen)) |
143 | res.reserve(asize: joinedSize); // don't call reserve(0) - it allocates one byte for the NUL |
144 | const int size = that->size(); |
145 | for (int i = 0; i < size; ++i) { |
146 | if (i) |
147 | res.append(s: sep, len: seplen); |
148 | res += that->at(i); |
149 | } |
150 | return res; |
151 | } |
152 | |
153 | /*! |
154 | \fn int QByteArrayList::indexOf(const char *needle, int from) const |
155 | |
156 | Returns the index position of the first occurrence of \a needle in |
157 | the list, searching forward from index position \a from. Returns |
158 | -1 if no item matched. |
159 | |
160 | \a needle must be NUL-terminated. |
161 | |
162 | This overload doesn't require creating a QByteArray, thus saving a |
163 | memory allocation and some CPU time. |
164 | |
165 | \since 5.13 |
166 | \overload |
167 | */ |
168 | |
169 | int QtPrivate::QByteArrayList_indexOf(const QByteArrayList *that, const char *needle, int from) |
170 | { |
171 | const auto it = std::find_if(first: that->begin() + from, last: that->end(), pred: [needle](const QByteArray &item) { return item == needle; }); |
172 | return it == that->end() ? -1 : int(std::distance(first: that->begin(), last: it)); |
173 | } |
174 | |
175 | QT_END_NAMESPACE |
176 | |