Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/corelib/tools/qstringlist.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||
5 | ** | - | ||||||||||||
6 | ** This file is part of the QtCore module of the Qt Toolkit. | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||
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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||
15 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||
25 | ** | - | ||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||
29 | ** | - | ||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||
31 | ** | - | ||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||
33 | - | |||||||||||||
34 | #include <qstringlist.h> | - | ||||||||||||
35 | #include <qset.h> | - | ||||||||||||
36 | #include <qregularexpression.h> | - | ||||||||||||
37 | - | |||||||||||||
38 | #include <algorithm> | - | ||||||||||||
39 | - | |||||||||||||
40 | QT_BEGIN_NAMESPACE | - | ||||||||||||
41 | - | |||||||||||||
42 | /*! \typedef QStringListIterator | - | ||||||||||||
43 | \relates QStringList | - | ||||||||||||
44 | - | |||||||||||||
45 | The QStringListIterator type definition provides a Java-style const | - | ||||||||||||
46 | iterator for QStringList. | - | ||||||||||||
47 | - | |||||||||||||
48 | QStringList provides both \l{Java-style iterators} and | - | ||||||||||||
49 | \l{STL-style iterators}. The Java-style const iterator is simply | - | ||||||||||||
50 | a type definition for QListIterator<QString>. | - | ||||||||||||
51 | - | |||||||||||||
52 | \sa QMutableStringListIterator, QStringList::const_iterator | - | ||||||||||||
53 | */ | - | ||||||||||||
54 | - | |||||||||||||
55 | /*! \typedef QMutableStringListIterator | - | ||||||||||||
56 | \relates QStringList | - | ||||||||||||
57 | - | |||||||||||||
58 | The QStringListIterator type definition provides a Java-style | - | ||||||||||||
59 | non-const iterator for QStringList. | - | ||||||||||||
60 | - | |||||||||||||
61 | QStringList provides both \l{Java-style iterators} and | - | ||||||||||||
62 | \l{STL-style iterators}. The Java-style non-const iterator is | - | ||||||||||||
63 | simply a type definition for QMutableListIterator<QString>. | - | ||||||||||||
64 | - | |||||||||||||
65 | \sa QStringListIterator, QStringList::iterator | - | ||||||||||||
66 | */ | - | ||||||||||||
67 | - | |||||||||||||
68 | /*! | - | ||||||||||||
69 | \class QStringList | - | ||||||||||||
70 | \inmodule QtCore | - | ||||||||||||
71 | \brief The QStringList class provides a list of strings. | - | ||||||||||||
72 | - | |||||||||||||
73 | \ingroup tools | - | ||||||||||||
74 | \ingroup shared | - | ||||||||||||
75 | \ingroup string-processing | - | ||||||||||||
76 | - | |||||||||||||
77 | \reentrant | - | ||||||||||||
78 | - | |||||||||||||
79 | QStringList inherits from QList<QString>. Like QList, QStringList is | - | ||||||||||||
80 | \l{implicitly shared}. It provides fast index-based access as well as fast | - | ||||||||||||
81 | insertions and removals. Passing string lists as value parameters is both | - | ||||||||||||
82 | fast and safe. | - | ||||||||||||
83 | - | |||||||||||||
84 | All of QList's functionality also applies to QStringList. For example, you | - | ||||||||||||
85 | can use isEmpty() to test whether the list is empty, and you can call | - | ||||||||||||
86 | functions like append(), prepend(), insert(), replace(), removeAll(), | - | ||||||||||||
87 | removeAt(), removeFirst(), removeLast(), and removeOne() to modify a | - | ||||||||||||
88 | QStringList. In addition, QStringList provides a few convenience | - | ||||||||||||
89 | functions that make handling lists of strings easier: | - | ||||||||||||
90 | - | |||||||||||||
91 | \tableofcontents | - | ||||||||||||
92 | - | |||||||||||||
93 | \section1 Adding Strings | - | ||||||||||||
94 | - | |||||||||||||
95 | Strings can be added to a list using the \l | - | ||||||||||||
96 | {QList::append()}{append()}, \l | - | ||||||||||||
97 | {QList::operator+=()}{operator+=()} and \l | - | ||||||||||||
98 | {QStringList::operator<<()}{operator<<()} functions. For example: | - | ||||||||||||
99 | - | |||||||||||||
100 | \snippet qstringlist/main.cpp 0 | - | ||||||||||||
101 | - | |||||||||||||
102 | \section1 Iterating Over the Strings | - | ||||||||||||
103 | - | |||||||||||||
104 | To iterate over a list, you can either use index positions or | - | ||||||||||||
105 | QList's Java-style and STL-style iterator types: | - | ||||||||||||
106 | - | |||||||||||||
107 | Indexing: | - | ||||||||||||
108 | - | |||||||||||||
109 | \snippet qstringlist/main.cpp 1 | - | ||||||||||||
110 | - | |||||||||||||
111 | Java-style iterator: | - | ||||||||||||
112 | - | |||||||||||||
113 | \snippet qstringlist/main.cpp 2 | - | ||||||||||||
114 | - | |||||||||||||
115 | STL-style iterator: | - | ||||||||||||
116 | - | |||||||||||||
117 | \snippet qstringlist/main.cpp 3 | - | ||||||||||||
118 | - | |||||||||||||
119 | The QStringListIterator class is simply a type definition for | - | ||||||||||||
120 | QListIterator<QString>. QStringList also provide the | - | ||||||||||||
121 | QMutableStringListIterator class which is a type definition for | - | ||||||||||||
122 | QMutableListIterator<QString>. | - | ||||||||||||
123 | - | |||||||||||||
124 | \section1 Manipulating the Strings | - | ||||||||||||
125 | - | |||||||||||||
126 | QStringList provides several functions allowing you to manipulate | - | ||||||||||||
127 | the contents of a list. You can concatenate all the strings in a | - | ||||||||||||
128 | string list into a single string (with an optional separator) | - | ||||||||||||
129 | using the join() function. For example: | - | ||||||||||||
130 | - | |||||||||||||
131 | \snippet qstringlist/main.cpp 4 | - | ||||||||||||
132 | - | |||||||||||||
133 | The argument to join can be a single character or a string. | - | ||||||||||||
134 | - | |||||||||||||
135 | To break up a string into a string list, use the QString::split() | - | ||||||||||||
136 | function: | - | ||||||||||||
137 | - | |||||||||||||
138 | \snippet qstringlist/main.cpp 6 | - | ||||||||||||
139 | - | |||||||||||||
140 | The argument to split can be a single character, a string, or a | - | ||||||||||||
141 | QRegExp. | - | ||||||||||||
142 | - | |||||||||||||
143 | In addition, the \l {QStringList::operator+()}{operator+()} | - | ||||||||||||
144 | function allows you to concatenate two string lists into one. To | - | ||||||||||||
145 | sort a string list, use the sort() function. | - | ||||||||||||
146 | - | |||||||||||||
147 | QString list also provides the filter() function which lets you | - | ||||||||||||
148 | to extract a new list which contains only those strings which | - | ||||||||||||
149 | contain a particular substring (or match a particular regular | - | ||||||||||||
150 | expression): | - | ||||||||||||
151 | - | |||||||||||||
152 | \snippet qstringlist/main.cpp 7 | - | ||||||||||||
153 | - | |||||||||||||
154 | The contains() function tells you whether the list contains a | - | ||||||||||||
155 | given string, while the indexOf() function returns the index of | - | ||||||||||||
156 | the first occurrence of the given string. The lastIndexOf() | - | ||||||||||||
157 | function on the other hand, returns the index of the last | - | ||||||||||||
158 | occurrence of the string. | - | ||||||||||||
159 | - | |||||||||||||
160 | Finally, the replaceInStrings() function calls QString::replace() | - | ||||||||||||
161 | on each string in the string list in turn. For example: | - | ||||||||||||
162 | - | |||||||||||||
163 | \snippet qstringlist/main.cpp 8 | - | ||||||||||||
164 | - | |||||||||||||
165 | \sa QString | - | ||||||||||||
166 | */ | - | ||||||||||||
167 | - | |||||||||||||
168 | /*! | - | ||||||||||||
169 | \fn QStringList::QStringList() | - | ||||||||||||
170 | - | |||||||||||||
171 | Constructs an empty string list. | - | ||||||||||||
172 | */ | - | ||||||||||||
173 | - | |||||||||||||
174 | /*! | - | ||||||||||||
175 | \fn QStringList::QStringList(const QString &str) | - | ||||||||||||
176 | - | |||||||||||||
177 | Constructs a string list that contains the given string, \a | - | ||||||||||||
178 | str. Longer lists are easily created like this: | - | ||||||||||||
179 | - | |||||||||||||
180 | \snippet qstringlist/main.cpp 9 | - | ||||||||||||
181 | - | |||||||||||||
182 | \sa append() | - | ||||||||||||
183 | */ | - | ||||||||||||
184 | - | |||||||||||||
185 | /*! | - | ||||||||||||
186 | \fn QStringList::QStringList(const QList<QString> &other) | - | ||||||||||||
187 | - | |||||||||||||
188 | Constructs a copy of \a other. | - | ||||||||||||
189 | - | |||||||||||||
190 | This operation takes \l{constant time}, because QStringList is | - | ||||||||||||
191 | \l{implicitly shared}. This makes returning a QStringList from a | - | ||||||||||||
192 | function very fast. If a shared instance is modified, it will be | - | ||||||||||||
193 | copied (copy-on-write), and that takes \l{linear time}. | - | ||||||||||||
194 | - | |||||||||||||
195 | \sa operator=() | - | ||||||||||||
196 | */ | - | ||||||||||||
197 | - | |||||||||||||
198 | /*! | - | ||||||||||||
199 | \fn QStringList::QStringList(QList<QString> &&other) | - | ||||||||||||
200 | \overload | - | ||||||||||||
201 | \since 5.4 | - | ||||||||||||
202 | - | |||||||||||||
203 | Move-constructs from QList<QString>. | - | ||||||||||||
204 | - | |||||||||||||
205 | After a successful construction, \a other will be empty. | - | ||||||||||||
206 | */ | - | ||||||||||||
207 | - | |||||||||||||
208 | /*! | - | ||||||||||||
209 | \fn QStringList &QStringList::operator=(const QList<QString> &other) | - | ||||||||||||
210 | \since 5.4 | - | ||||||||||||
211 | - | |||||||||||||
212 | Copy assignment operator from QList<QString>. Assigns the \a other | - | ||||||||||||
213 | list of strings to this string list. | - | ||||||||||||
214 | - | |||||||||||||
215 | After the operation, \a other and \c *this will be equal. | - | ||||||||||||
216 | */ | - | ||||||||||||
217 | - | |||||||||||||
218 | /*! | - | ||||||||||||
219 | \fn QStringList &QStringList::operator=(QList<QString> &&other) | - | ||||||||||||
220 | \overload | - | ||||||||||||
221 | \since 5.4 | - | ||||||||||||
222 | - | |||||||||||||
223 | Move assignment operator from QList<QString>. Moves the \a other | - | ||||||||||||
224 | list of strings to this string list. | - | ||||||||||||
225 | - | |||||||||||||
226 | After the operation, \a other will be empty. | - | ||||||||||||
227 | */ | - | ||||||||||||
228 | - | |||||||||||||
229 | /*! | - | ||||||||||||
230 | \fn void QStringList::sort(Qt::CaseSensitivity cs) | - | ||||||||||||
231 | - | |||||||||||||
232 | Sorts the list of strings in ascending order. | - | ||||||||||||
233 | If \a cs is \l Qt::CaseSensitive (the default), the string comparison | - | ||||||||||||
234 | is case sensitive; otherwise the comparison is case insensitive. | - | ||||||||||||
235 | - | |||||||||||||
236 | Sorting is performed using the STL's std::sort() algorithm, | - | ||||||||||||
237 | which averages \l{linear-logarithmic time}, i.e. O(\e{n} log \e{n}). | - | ||||||||||||
238 | - | |||||||||||||
239 | If you want to sort your strings in an arbitrary order, consider | - | ||||||||||||
240 | using the QMap class. For example, you could use a QMap<QString, | - | ||||||||||||
241 | QString> to create a case-insensitive ordering (e.g. with the keys | - | ||||||||||||
242 | being lower-case versions of the strings, and the values being the | - | ||||||||||||
243 | strings), or a QMap<int, QString> to sort the strings by some | - | ||||||||||||
244 | integer index. | - | ||||||||||||
245 | */ | - | ||||||||||||
246 | - | |||||||||||||
247 | namespace { | - | ||||||||||||
248 | struct CaseInsensitiveLessThan { | - | ||||||||||||
249 | typedef bool result_type; | - | ||||||||||||
250 | result_type operator()(const QString &s1, const QString &s2) const | - | ||||||||||||
251 | { | - | ||||||||||||
252 | return s1.compare(s2, Qt::CaseInsensitive) < 0; executed 15 times by 1 test: return s1.compare(s2, Qt::CaseInsensitive) < 0; Executed by:
| 15 | ||||||||||||
253 | } | - | ||||||||||||
254 | }; | - | ||||||||||||
255 | } | - | ||||||||||||
256 | - | |||||||||||||
257 | void QtPrivate::QStringList_sort(QStringList *that, Qt::CaseSensitivity cs) | - | ||||||||||||
258 | { | - | ||||||||||||
259 | if (cs == Qt::CaseSensitive)
| 1-723 | ||||||||||||
260 | std::sort(that->begin(), that->end()); executed 723 times by 10 tests: std::sort(that->begin(), that->end()); Executed by:
| 723 | ||||||||||||
261 | else | - | ||||||||||||
262 | std::sort(that->begin(), that->end(), CaseInsensitiveLessThan()); executed 1 time by 1 test: std::sort(that->begin(), that->end(), CaseInsensitiveLessThan()); Executed by:
| 1 | ||||||||||||
263 | } | - | ||||||||||||
264 | - | |||||||||||||
265 | - | |||||||||||||
266 | /*! | - | ||||||||||||
267 | \fn QStringList QStringList::filter(const QString &str, Qt::CaseSensitivity cs) const | - | ||||||||||||
268 | - | |||||||||||||
269 | Returns a list of all the strings containing the substring \a str. | - | ||||||||||||
270 | - | |||||||||||||
271 | If \a cs is \l Qt::CaseSensitive (the default), the string | - | ||||||||||||
272 | comparison is case sensitive; otherwise the comparison is case | - | ||||||||||||
273 | insensitive. | - | ||||||||||||
274 | - | |||||||||||||
275 | \snippet qstringlist/main.cpp 5 | - | ||||||||||||
276 | \snippet qstringlist/main.cpp 10 | - | ||||||||||||
277 | - | |||||||||||||
278 | This is equivalent to | - | ||||||||||||
279 | - | |||||||||||||
280 | \snippet qstringlist/main.cpp 11 | - | ||||||||||||
281 | \snippet qstringlist/main.cpp 12 | - | ||||||||||||
282 | - | |||||||||||||
283 | \sa contains() | - | ||||||||||||
284 | */ | - | ||||||||||||
285 | QStringList QtPrivate::QStringList_filter(const QStringList *that, const QString &str, | - | ||||||||||||
286 | Qt::CaseSensitivity cs) | - | ||||||||||||
287 | { | - | ||||||||||||
288 | QStringMatcher matcher(str, cs); | - | ||||||||||||
289 | QStringList res; | - | ||||||||||||
290 | for (int i = 0; i < that->size(); ++i)
| 137-787 | ||||||||||||
291 | if (matcher.indexIn(that->at(i)) != -1)
| 158-629 | ||||||||||||
292 | res << that->at(i); executed 158 times by 9 tests: res << that->at(i); Executed by:
| 158 | ||||||||||||
293 | return res; executed 137 times by 9 tests: return res; Executed by:
| 137 | ||||||||||||
294 | } | - | ||||||||||||
295 | - | |||||||||||||
296 | - | |||||||||||||
297 | /*! | - | ||||||||||||
298 | \fn bool QStringList::contains(const QString &str, Qt::CaseSensitivity cs) const | - | ||||||||||||
299 | - | |||||||||||||
300 | Returns \c true if the list contains the string \a str; otherwise | - | ||||||||||||
301 | returns \c false. The search is case insensitive if \a cs is | - | ||||||||||||
302 | Qt::CaseInsensitive; the search is case sensitive by default. | - | ||||||||||||
303 | - | |||||||||||||
304 | \sa indexOf(), lastIndexOf(), QString::contains() | - | ||||||||||||
305 | */ | - | ||||||||||||
306 | bool QtPrivate::QStringList_contains(const QStringList *that, const QString &str, | - | ||||||||||||
307 | Qt::CaseSensitivity cs) | - | ||||||||||||
308 | { | - | ||||||||||||
309 | for (int i = 0; i < that->size(); ++i) {
| 230342-341047 | ||||||||||||
310 | const QString & string = that->at(i); | - | ||||||||||||
311 | if (string.length() == str.length() && str.compare(string, cs) == 0)
| 34550-150735 | ||||||||||||
312 | return true; executed 45057 times by 205 tests: return true; Executed by:
| 45057 | ||||||||||||
313 | } executed 185285 times by 228 tests: end of block Executed by:
| 185285 | ||||||||||||
314 | return false; executed 341047 times by 263 tests: return false; Executed by:
| 341047 | ||||||||||||
315 | } | - | ||||||||||||
316 | - | |||||||||||||
317 | #ifndef QT_NO_REGEXP | - | ||||||||||||
318 | /*! | - | ||||||||||||
319 | \fn QStringList QStringList::filter(const QRegExp &rx) const | - | ||||||||||||
320 | - | |||||||||||||
321 | \overload | - | ||||||||||||
322 | - | |||||||||||||
323 | Returns a list of all the strings that match the regular | - | ||||||||||||
324 | expression \a rx. | - | ||||||||||||
325 | */ | - | ||||||||||||
326 | QStringList QtPrivate::QStringList_filter(const QStringList *that, const QRegExp &rx) | - | ||||||||||||
327 | { | - | ||||||||||||
328 | QStringList res; | - | ||||||||||||
329 | for (int i = 0; i < that->size(); ++i)
| 18-56 | ||||||||||||
330 | if (that->at(i).contains(rx))
| 3-53 | ||||||||||||
331 | res << that->at(i); executed 3 times by 2 tests: res << that->at(i); Executed by:
| 3 | ||||||||||||
332 | return res; executed 18 times by 3 tests: return res; Executed by:
| 18 | ||||||||||||
333 | } | - | ||||||||||||
334 | #endif | - | ||||||||||||
335 | - | |||||||||||||
336 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||
337 | #ifndef QT_NO_REGULAREXPRESSION | - | ||||||||||||
338 | /*! | - | ||||||||||||
339 | \fn QStringList QStringList::filter(const QRegularExpression &re) const | - | ||||||||||||
340 | \overload | - | ||||||||||||
341 | \since 5.0 | - | ||||||||||||
342 | - | |||||||||||||
343 | Returns a list of all the strings that match the regular | - | ||||||||||||
344 | expression \a re. | - | ||||||||||||
345 | */ | - | ||||||||||||
346 | QStringList QtPrivate::QStringList_filter(const QStringList *that, const QRegularExpression &re) | - | ||||||||||||
347 | { | - | ||||||||||||
348 | QStringList res; | - | ||||||||||||
349 | for (int i = 0; i < that->size(); ++i) {
| 1-3 | ||||||||||||
350 | if (that->at(i).contains(re))
| 1-2 | ||||||||||||
351 | res << that->at(i); executed 2 times by 1 test: res << that->at(i); Executed by:
| 2 | ||||||||||||
352 | } executed 3 times by 1 test: end of block Executed by:
| 3 | ||||||||||||
353 | return res; executed 1 time by 1 test: return res; Executed by:
| 1 | ||||||||||||
354 | } | - | ||||||||||||
355 | #endif // QT_NO_REGULAREXPRESSION | - | ||||||||||||
356 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||
357 | - | |||||||||||||
358 | /*! | - | ||||||||||||
359 | \fn QStringList &QStringList::replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs) | - | ||||||||||||
360 | - | |||||||||||||
361 | Returns a string list where every string has had the \a before | - | ||||||||||||
362 | text replaced with the \a after text wherever the \a before text | - | ||||||||||||
363 | is found. The \a before text is matched case-sensitively or not | - | ||||||||||||
364 | depending on the \a cs flag. | - | ||||||||||||
365 | - | |||||||||||||
366 | For example: | - | ||||||||||||
367 | - | |||||||||||||
368 | \snippet qstringlist/main.cpp 5 | - | ||||||||||||
369 | \snippet qstringlist/main.cpp 13 | - | ||||||||||||
370 | - | |||||||||||||
371 | \sa QString::replace() | - | ||||||||||||
372 | */ | - | ||||||||||||
373 | void QtPrivate::QStringList_replaceInStrings(QStringList *that, const QString &before, | - | ||||||||||||
374 | const QString &after, Qt::CaseSensitivity cs) | - | ||||||||||||
375 | { | - | ||||||||||||
376 | for (int i = 0; i < that->size(); ++i)
| 3-18 | ||||||||||||
377 | (*that)[i].replace(before, after, cs); executed 18 times by 2 tests: (*that)[i].replace(before, after, cs); Executed by:
| 18 | ||||||||||||
378 | } executed 3 times by 2 tests: end of block Executed by:
| 3 | ||||||||||||
379 | - | |||||||||||||
380 | - | |||||||||||||
381 | #ifndef QT_NO_REGEXP | - | ||||||||||||
382 | /*! | - | ||||||||||||
383 | \fn QStringList &QStringList::replaceInStrings(const QRegExp &rx, const QString &after) | - | ||||||||||||
384 | \overload | - | ||||||||||||
385 | - | |||||||||||||
386 | Replaces every occurrence of the regexp \a rx, in each of the | - | ||||||||||||
387 | string lists's strings, with \a after. Returns a reference to the | - | ||||||||||||
388 | string list. | - | ||||||||||||
389 | - | |||||||||||||
390 | For example: | - | ||||||||||||
391 | - | |||||||||||||
392 | \snippet qstringlist/main.cpp 5 | - | ||||||||||||
393 | \snippet qstringlist/main.cpp 14 | - | ||||||||||||
394 | - | |||||||||||||
395 | For regular expressions that contain \l{capturing parentheses}, | - | ||||||||||||
396 | occurrences of \b{\\1}, \b{\\2}, ..., in \a after are | - | ||||||||||||
397 | replaced with \a{rx}.cap(1), \a{rx}.cap(2), ... | - | ||||||||||||
398 | - | |||||||||||||
399 | For example: | - | ||||||||||||
400 | - | |||||||||||||
401 | \snippet qstringlist/main.cpp 5 | - | ||||||||||||
402 | \snippet qstringlist/main.cpp 15 | - | ||||||||||||
403 | */ | - | ||||||||||||
404 | void QtPrivate::QStringList_replaceInStrings(QStringList *that, const QRegExp &rx, const QString &after) | - | ||||||||||||
405 | { | - | ||||||||||||
406 | for (int i = 0; i < that->size(); ++i)
| 2-6 | ||||||||||||
407 | (*that)[i].replace(rx, after); executed 6 times by 1 test: (*that)[i].replace(rx, after); Executed by:
| 6 | ||||||||||||
408 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
409 | #endif | - | ||||||||||||
410 | - | |||||||||||||
411 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||
412 | #ifndef QT_NO_REGULAREXPRESSION | - | ||||||||||||
413 | /*! | - | ||||||||||||
414 | \fn QStringList &QStringList::replaceInStrings(const QRegularExpression &re, const QString &after) | - | ||||||||||||
415 | \overload | - | ||||||||||||
416 | \since 5.0 | - | ||||||||||||
417 | - | |||||||||||||
418 | Replaces every occurrence of the regular expression \a re, in each of the | - | ||||||||||||
419 | string lists's strings, with \a after. Returns a reference to the string | - | ||||||||||||
420 | list. | - | ||||||||||||
421 | - | |||||||||||||
422 | For example: | - | ||||||||||||
423 | - | |||||||||||||
424 | \snippet qstringlist/main.cpp 5 | - | ||||||||||||
425 | \snippet qstringlist/main.cpp 16 | - | ||||||||||||
426 | - | |||||||||||||
427 | For regular expressions that contain capturing groups, | - | ||||||||||||
428 | occurrences of \b{\\1}, \b{\\2}, ..., in \a after are | - | ||||||||||||
429 | replaced with the string captured by the corresponding capturing group. | - | ||||||||||||
430 | - | |||||||||||||
431 | For example: | - | ||||||||||||
432 | - | |||||||||||||
433 | \snippet qstringlist/main.cpp 5 | - | ||||||||||||
434 | \snippet qstringlist/main.cpp 17 | - | ||||||||||||
435 | */ | - | ||||||||||||
436 | void QtPrivate::QStringList_replaceInStrings(QStringList *that, const QRegularExpression &re, const QString &after) | - | ||||||||||||
437 | { | - | ||||||||||||
438 | for (int i = 0; i < that->size(); ++i)
| 2-6 | ||||||||||||
439 | (*that)[i].replace(re, after); executed 6 times by 1 test: (*that)[i].replace(re, after); Executed by:
| 6 | ||||||||||||
440 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||
441 | #endif // QT_NO_REGULAREXPRESSION | - | ||||||||||||
442 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||
443 | - | |||||||||||||
444 | /*! | - | ||||||||||||
445 | \fn QString QStringList::join(const QString &separator) const | - | ||||||||||||
446 | - | |||||||||||||
447 | Joins all the string list's strings into a single string with each | - | ||||||||||||
448 | element separated by the given \a separator (which can be an | - | ||||||||||||
449 | empty string). | - | ||||||||||||
450 | - | |||||||||||||
451 | \sa QString::split() | - | ||||||||||||
452 | */ | - | ||||||||||||
453 | - | |||||||||||||
454 | /*! | - | ||||||||||||
455 | \fn QString QStringList::join(QChar separator) const | - | ||||||||||||
456 | \since 5.0 | - | ||||||||||||
457 | \overload join() | - | ||||||||||||
458 | */ | - | ||||||||||||
459 | QString QtPrivate::QStringList_join(const QStringList *that, const QChar *sep, int seplen) | - | ||||||||||||
460 | { | - | ||||||||||||
461 | int totalLength = 0; | - | ||||||||||||
462 | const int size = that->size(); | - | ||||||||||||
463 | - | |||||||||||||
464 | for (int i = 0; i < size; ++i)
| 46113-108934 | ||||||||||||
465 | totalLength += that->at(i).size(); executed 108934 times by 139 tests: totalLength += that->at(i).size(); Executed by:
| 108934 | ||||||||||||
466 | - | |||||||||||||
467 | if(size > 0)
| 669-45444 | ||||||||||||
468 | totalLength += seplen * (size - 1); executed 45444 times by 139 tests: totalLength += seplen * (size - 1); Executed by:
| 45444 | ||||||||||||
469 | - | |||||||||||||
470 | QString res; | - | ||||||||||||
471 | if (totalLength == 0)
| 807-45306 | ||||||||||||
472 | return res; executed 807 times by 11 tests: return res; Executed by:
| 807 | ||||||||||||
473 | res.reserve(totalLength); | - | ||||||||||||
474 | for (int i = 0; i < size; ++i) {
| 45306-108796 | ||||||||||||
475 | if (i)
| 45306-63490 | ||||||||||||
476 | res.append(sep, seplen); executed 63490 times by 26 tests: res.append(sep, seplen); Executed by:
| 63490 | ||||||||||||
477 | res += that->at(i); | - | ||||||||||||
478 | } executed 108796 times by 139 tests: end of block Executed by:
| 108796 | ||||||||||||
479 | return res; executed 45306 times by 139 tests: return res; Executed by:
| 45306 | ||||||||||||
480 | } | - | ||||||||||||
481 | - | |||||||||||||
482 | /*! | - | ||||||||||||
483 | \fn QStringList QStringList::operator+(const QStringList &other) const | - | ||||||||||||
484 | - | |||||||||||||
485 | Returns a string list that is the concatenation of this string | - | ||||||||||||
486 | list with the \a other string list. | - | ||||||||||||
487 | - | |||||||||||||
488 | \sa append() | - | ||||||||||||
489 | */ | - | ||||||||||||
490 | - | |||||||||||||
491 | /*! | - | ||||||||||||
492 | \fn QStringList &QStringList::operator<<(const QString &str) | - | ||||||||||||
493 | - | |||||||||||||
494 | Appends the given string, \a str, to this string list and returns | - | ||||||||||||
495 | a reference to the string list. | - | ||||||||||||
496 | - | |||||||||||||
497 | \sa append() | - | ||||||||||||
498 | */ | - | ||||||||||||
499 | - | |||||||||||||
500 | /*! | - | ||||||||||||
501 | \fn QStringList &QStringList::operator<<(const QStringList &other) | - | ||||||||||||
502 | - | |||||||||||||
503 | \overload | - | ||||||||||||
504 | - | |||||||||||||
505 | Appends the \a other string list to the string list and returns a reference to | - | ||||||||||||
506 | the latter string list. | - | ||||||||||||
507 | */ | - | ||||||||||||
508 | - | |||||||||||||
509 | /*! | - | ||||||||||||
510 | \fn QStringList &QStringList::operator<<(const QList<QString> &other) | - | ||||||||||||
511 | \since 5.4 | - | ||||||||||||
512 | - | |||||||||||||
513 | \overload | - | ||||||||||||
514 | - | |||||||||||||
515 | Appends the \a other string list to the string list and returns a reference to | - | ||||||||||||
516 | the latter string list. | - | ||||||||||||
517 | */ | - | ||||||||||||
518 | - | |||||||||||||
519 | #ifndef QT_NO_REGEXP | - | ||||||||||||
520 | static int indexOfMutating(const QStringList *that, QRegExp &rx, int from) | - | ||||||||||||
521 | { | - | ||||||||||||
522 | if (from < 0)
| 3-8 | ||||||||||||
523 | from = qMax(from + that->size(), 0); executed 3 times by 1 test: from = qMax(from + that->size(), 0); Executed by:
| 3 | ||||||||||||
524 | for (int i = from; i < that->size(); ++i) {
| 6-61 | ||||||||||||
525 | if (rx.exactMatch(that->at(i)))
| 5-56 | ||||||||||||
526 | return i; executed 5 times by 2 tests: return i; Executed by:
| 5 | ||||||||||||
527 | } executed 56 times by 2 tests: end of block Executed by:
| 56 | ||||||||||||
528 | return -1; executed 6 times by 2 tests: return -1; Executed by:
| 6 | ||||||||||||
529 | } | - | ||||||||||||
530 | - | |||||||||||||
531 | static int lastIndexOfMutating(const QStringList *that, QRegExp &rx, int from) | - | ||||||||||||
532 | { | - | ||||||||||||
533 | if (from < 0)
| 3-6 | ||||||||||||
534 | from += that->size(); executed 6 times by 1 test: from += that->size(); Executed by:
| 6 | ||||||||||||
535 | else if (from >= that->size())
| 1-2 | ||||||||||||
536 | from = that->size() - 1; executed 1 time by 1 test: from = that->size() - 1; Executed by:
| 1 | ||||||||||||
537 | for (int i = from; i >= 0; --i) {
| 3-17 | ||||||||||||
538 | if (rx.exactMatch(that->at(i)))
| 6-11 | ||||||||||||
539 | return i; executed 6 times by 1 test: return i; Executed by:
| 6 | ||||||||||||
540 | } executed 11 times by 1 test: end of block Executed by:
| 11 | ||||||||||||
541 | return -1; executed 3 times by 1 test: return -1; Executed by:
| 3 | ||||||||||||
542 | } | - | ||||||||||||
543 | - | |||||||||||||
544 | /*! | - | ||||||||||||
545 | \fn int QStringList::indexOf(const QRegExp &rx, int from) const | - | ||||||||||||
546 | - | |||||||||||||
547 | Returns the index position of the first exact match of \a rx in | - | ||||||||||||
548 | the list, searching forward from index position \a from. Returns | - | ||||||||||||
549 | -1 if no item matched. | - | ||||||||||||
550 | - | |||||||||||||
551 | By default, this function is case sensitive. | - | ||||||||||||
552 | - | |||||||||||||
553 | \sa lastIndexOf(), contains(), QRegExp::exactMatch() | - | ||||||||||||
554 | */ | - | ||||||||||||
555 | int QtPrivate::QStringList_indexOf(const QStringList *that, const QRegExp &rx, int from) | - | ||||||||||||
556 | { | - | ||||||||||||
557 | QRegExp rx2(rx); | - | ||||||||||||
558 | return indexOfMutating(that, rx2, from); executed 2 times by 1 test: return indexOfMutating(that, rx2, from); Executed by:
| 2 | ||||||||||||
559 | } | - | ||||||||||||
560 | - | |||||||||||||
561 | /*! | - | ||||||||||||
562 | \fn int QStringList::indexOf(QRegExp &rx, int from) const | - | ||||||||||||
563 | \overload indexOf() | - | ||||||||||||
564 | \since 4.5 | - | ||||||||||||
565 | - | |||||||||||||
566 | Returns the index position of the first exact match of \a rx in | - | ||||||||||||
567 | the list, searching forward from index position \a from. Returns | - | ||||||||||||
568 | -1 if no item matched. | - | ||||||||||||
569 | - | |||||||||||||
570 | By default, this function is case sensitive. | - | ||||||||||||
571 | - | |||||||||||||
572 | If an item matched, the \a rx regular expression will contain the | - | ||||||||||||
573 | matched objects (see QRegExp::matchedLength, QRegExp::cap). | - | ||||||||||||
574 | - | |||||||||||||
575 | \sa lastIndexOf(), contains(), QRegExp::exactMatch() | - | ||||||||||||
576 | */ | - | ||||||||||||
577 | int QtPrivate::QStringList_indexOf(const QStringList *that, QRegExp &rx, int from) | - | ||||||||||||
578 | { | - | ||||||||||||
579 | return indexOfMutating(that, rx, from); executed 9 times by 2 tests: return indexOfMutating(that, rx, from); Executed by:
| 9 | ||||||||||||
580 | } | - | ||||||||||||
581 | - | |||||||||||||
582 | /*! | - | ||||||||||||
583 | \fn int QStringList::lastIndexOf(const QRegExp &rx, int from) const | - | ||||||||||||
584 | - | |||||||||||||
585 | Returns the index position of the last exact match of \a rx in | - | ||||||||||||
586 | the list, searching backward from index position \a from. If \a | - | ||||||||||||
587 | from is -1 (the default), the search starts at the last item. | - | ||||||||||||
588 | Returns -1 if no item matched. | - | ||||||||||||
589 | - | |||||||||||||
590 | By default, this function is case sensitive. | - | ||||||||||||
591 | - | |||||||||||||
592 | \sa indexOf(), contains(), QRegExp::exactMatch() | - | ||||||||||||
593 | */ | - | ||||||||||||
594 | int QtPrivate::QStringList_lastIndexOf(const QStringList *that, const QRegExp &rx, int from) | - | ||||||||||||
595 | { | - | ||||||||||||
596 | QRegExp rx2(rx); | - | ||||||||||||
597 | return lastIndexOfMutating(that, rx2, from); executed 2 times by 1 test: return lastIndexOfMutating(that, rx2, from); Executed by:
| 2 | ||||||||||||
598 | } | - | ||||||||||||
599 | - | |||||||||||||
600 | /*! | - | ||||||||||||
601 | \fn int QStringList::lastIndexOf(QRegExp &rx, int from) const | - | ||||||||||||
602 | \overload lastIndexOf() | - | ||||||||||||
603 | \since 4.5 | - | ||||||||||||
604 | - | |||||||||||||
605 | Returns the index position of the last exact match of \a rx in | - | ||||||||||||
606 | the list, searching backward from index position \a from. If \a | - | ||||||||||||
607 | from is -1 (the default), the search starts at the last item. | - | ||||||||||||
608 | Returns -1 if no item matched. | - | ||||||||||||
609 | - | |||||||||||||
610 | By default, this function is case sensitive. | - | ||||||||||||
611 | - | |||||||||||||
612 | If an item matched, the \a rx regular expression will contain the | - | ||||||||||||
613 | matched objects (see QRegExp::matchedLength, QRegExp::cap). | - | ||||||||||||
614 | - | |||||||||||||
615 | \sa indexOf(), contains(), QRegExp::exactMatch() | - | ||||||||||||
616 | */ | - | ||||||||||||
617 | int QtPrivate::QStringList_lastIndexOf(const QStringList *that, QRegExp &rx, int from) | - | ||||||||||||
618 | { | - | ||||||||||||
619 | return lastIndexOfMutating(that, rx, from); executed 7 times by 1 test: return lastIndexOfMutating(that, rx, from); Executed by:
| 7 | ||||||||||||
620 | } | - | ||||||||||||
621 | #endif | - | ||||||||||||
622 | - | |||||||||||||
623 | #ifndef QT_BOOTSTRAPPED | - | ||||||||||||
624 | #ifndef QT_NO_REGULAREXPRESSION | - | ||||||||||||
625 | /*! | - | ||||||||||||
626 | \fn int QStringList::indexOf(const QRegularExpression &re, int from) const | - | ||||||||||||
627 | \overload | - | ||||||||||||
628 | \since 5.0 | - | ||||||||||||
629 | - | |||||||||||||
630 | Returns the index position of the first match of \a re in | - | ||||||||||||
631 | the list, searching forward from index position \a from. Returns | - | ||||||||||||
632 | -1 if no item matched. | - | ||||||||||||
633 | - | |||||||||||||
634 | \sa lastIndexOf() | - | ||||||||||||
635 | */ | - | ||||||||||||
636 | int QtPrivate::QStringList_indexOf(const QStringList *that, const QRegularExpression &re, int from) | - | ||||||||||||
637 | { | - | ||||||||||||
638 | if (from < 0)
| 3-6 | ||||||||||||
639 | from = qMax(from + that->size(), 0); executed 3 times by 1 test: from = qMax(from + that->size(), 0); Executed by:
| 3 | ||||||||||||
640 | - | |||||||||||||
641 | QString exactPattern = QLatin1String("\\A(?:") + re.pattern() + QLatin1String(")\\z"); | - | ||||||||||||
642 | QRegularExpression exactRe(exactPattern, re.patternOptions()); | - | ||||||||||||
643 | - | |||||||||||||
644 | for (int i = from; i < that->size(); ++i) {
| 5-16 | ||||||||||||
645 | QRegularExpressionMatch m = exactRe.match(that->at(i)); | - | ||||||||||||
646 | if (m.hasMatch())
| 4-12 | ||||||||||||
647 | return i; executed 4 times by 1 test: return i; Executed by:
| 4 | ||||||||||||
648 | } executed 12 times by 1 test: end of block Executed by:
| 12 | ||||||||||||
649 | return -1; executed 5 times by 1 test: return -1; Executed by:
| 5 | ||||||||||||
650 | } | - | ||||||||||||
651 | - | |||||||||||||
652 | /*! | - | ||||||||||||
653 | \fn int QStringList::lastIndexOf(const QRegularExpression &re, int from) const | - | ||||||||||||
654 | \overload | - | ||||||||||||
655 | \since 5.0 | - | ||||||||||||
656 | - | |||||||||||||
657 | Returns the index position of the last exact match of \a re in | - | ||||||||||||
658 | the list, searching backward from index position \a from. If \a | - | ||||||||||||
659 | from is -1 (the default), the search starts at the last item. | - | ||||||||||||
660 | Returns -1 if no item matched. | - | ||||||||||||
661 | - | |||||||||||||
662 | \sa indexOf() | - | ||||||||||||
663 | */ | - | ||||||||||||
664 | int QtPrivate::QStringList_lastIndexOf(const QStringList *that, const QRegularExpression &re, int from) | - | ||||||||||||
665 | { | - | ||||||||||||
666 | if (from < 0)
| 3-6 | ||||||||||||
667 | from += that->size(); executed 6 times by 1 test: from += that->size(); Executed by:
| 6 | ||||||||||||
668 | else if (from >= that->size())
| 1-2 | ||||||||||||
669 | from = that->size() - 1; executed 1 time by 1 test: from = that->size() - 1; Executed by:
| 1 | ||||||||||||
670 | - | |||||||||||||
671 | QString exactPattern = QLatin1String("\\A(?:") + re.pattern() + QLatin1String(")\\z"); | - | ||||||||||||
672 | QRegularExpression exactRe(exactPattern, re.patternOptions()); | - | ||||||||||||
673 | - | |||||||||||||
674 | for (int i = from; i >= 0; --i) {
| 3-17 | ||||||||||||
675 | QRegularExpressionMatch m = exactRe.match(that->at(i)); | - | ||||||||||||
676 | if (m.hasMatch())
| 6-11 | ||||||||||||
677 | return i; executed 6 times by 1 test: return i; Executed by:
| 6 | ||||||||||||
678 | } executed 11 times by 1 test: end of block Executed by:
| 11 | ||||||||||||
679 | return -1; executed 3 times by 1 test: return -1; Executed by:
| 3 | ||||||||||||
680 | } | - | ||||||||||||
681 | #endif // QT_NO_REGULAREXPRESSION | - | ||||||||||||
682 | #endif // QT_BOOTSTRAPPED | - | ||||||||||||
683 | - | |||||||||||||
684 | /*! | - | ||||||||||||
685 | \fn int QStringList::removeDuplicates() | - | ||||||||||||
686 | - | |||||||||||||
687 | \since 4.5 | - | ||||||||||||
688 | - | |||||||||||||
689 | This function removes duplicate entries from a list. | - | ||||||||||||
690 | The entries do not have to be sorted. They will retain their | - | ||||||||||||
691 | original order. | - | ||||||||||||
692 | - | |||||||||||||
693 | Returns the number of removed entries. | - | ||||||||||||
694 | */ | - | ||||||||||||
695 | int QtPrivate::QStringList_removeDuplicates(QStringList *that) | - | ||||||||||||
696 | { | - | ||||||||||||
697 | int n = that->size(); | - | ||||||||||||
698 | int j = 0; | - | ||||||||||||
699 | QSet<QString> seen; | - | ||||||||||||
700 | seen.reserve(n); | - | ||||||||||||
701 | int setSize = 0; | - | ||||||||||||
702 | for (int i = 0; i < n; ++i) {
| 149-159 | ||||||||||||
703 | const QString &s = that->at(i); | - | ||||||||||||
704 | seen.insert(s); | - | ||||||||||||
705 | if (setSize == seen.size()) // unchanged size => was already seen
| 3-156 | ||||||||||||
706 | continue; executed 3 times by 2 tests: continue; Executed by:
| 3 | ||||||||||||
707 | ++setSize; | - | ||||||||||||
708 | if (j != i)
| 1-155 | ||||||||||||
709 | that->swap(i, j); executed 1 time by 1 test: that->swap(i, j); Executed by:
| 1 | ||||||||||||
710 | ++j; | - | ||||||||||||
711 | } executed 156 times by 4 tests: end of block Executed by:
| 156 | ||||||||||||
712 | if (n != j)
| 3-146 | ||||||||||||
713 | that->erase(that->begin() + j, that->end()); executed 3 times by 2 tests: that->erase(that->begin() + j, that->end()); Executed by:
| 3 | ||||||||||||
714 | return n - j; executed 149 times by 5 tests: return n - j; Executed by:
| 149 | ||||||||||||
715 | } | - | ||||||||||||
716 | - | |||||||||||||
717 | /*! \fn QStringList::QStringList(std::initializer_list<QString> args) | - | ||||||||||||
718 | \since 4.8 | - | ||||||||||||
719 | - | |||||||||||||
720 | Construct a list from a std::initializer_list given by \a args. | - | ||||||||||||
721 | - | |||||||||||||
722 | This constructor is only enabled if the compiler supports C++11 initializer | - | ||||||||||||
723 | lists. | - | ||||||||||||
724 | */ | - | ||||||||||||
725 | - | |||||||||||||
726 | - | |||||||||||||
727 | QT_END_NAMESPACE | - | ||||||||||||
Source code | Switch to Preprocessed file |