1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Copyright (C) 2016 Intel Corporation.
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 <QtCore/qlist.h>
42
43#ifndef QSTRINGLIST_H
44#define QSTRINGLIST_H
45
46#include <QtCore/qalgorithms.h>
47#include <QtCore/qregexp.h>
48#include <QtCore/qstring.h>
49#include <QtCore/qstringmatcher.h>
50
51QT_BEGIN_NAMESPACE
52
53class QRegExp;
54class QRegularExpression;
55
56typedef QListIterator<QString> QStringListIterator;
57typedef QMutableListIterator<QString> QMutableStringListIterator;
58
59class QStringList;
60
61#ifdef Q_QDOC
62class QStringList : public QList<QString>
63#else
64template <> struct QListSpecialMethods<QString>
65#endif
66{
67#ifndef Q_QDOC
68protected:
69 ~QListSpecialMethods() {}
70#endif
71public:
72 inline void sort(Qt::CaseSensitivity cs = Qt::CaseSensitive);
73 inline int removeDuplicates();
74
75 inline QString join(const QString &sep) const;
76 inline QString join(QLatin1String sep) const;
77 inline QString join(QChar sep) const;
78
79 inline QStringList filter(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
80 inline QStringList &replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive);
81
82#ifndef QT_NO_REGEXP
83 inline QStringList filter(const QRegExp &rx) const;
84 inline QStringList &replaceInStrings(const QRegExp &rx, const QString &after);
85#endif
86
87#if QT_CONFIG(regularexpression)
88 inline QStringList filter(const QRegularExpression &re) const;
89 inline QStringList &replaceInStrings(const QRegularExpression &re, const QString &after);
90#endif // QT_CONFIG(regularexpression)
91
92#ifndef Q_QDOC
93private:
94 inline QStringList *self();
95 inline const QStringList *self() const;
96};
97
98// ### Qt6: check if there's a better way
99class QStringList : public QList<QString>
100{
101#endif
102public:
103 inline QStringList() Q_DECL_NOTHROW { }
104 inline explicit QStringList(const QString &i) { append(i); }
105 inline QStringList(const QList<QString> &l) : QList<QString>(l) { }
106#ifdef Q_COMPILER_RVALUE_REFS
107 inline QStringList(QList<QString> &&l) Q_DECL_NOTHROW : QList<QString>(std::move(l)) { }
108#endif
109#ifdef Q_COMPILER_INITIALIZER_LISTS
110 inline QStringList(std::initializer_list<QString> args) : QList<QString>(args) { }
111#endif
112
113 QStringList &operator=(const QList<QString> &other)
114 { QList<QString>::operator=(other); return *this; }
115#ifdef Q_COMPILER_RVALUE_REFS
116 QStringList &operator=(QList<QString> &&other) Q_DECL_NOTHROW
117 { QList<QString>::operator=(std::move(other)); return *this; }
118#endif
119
120#if QT_STRINGVIEW_LEVEL < 2
121 inline bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
122#endif
123 inline bool contains(QLatin1String str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
124 inline bool contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
125
126 inline QStringList operator+(const QStringList &other) const
127 { QStringList n = *this; n += other; return n; }
128 inline QStringList &operator<<(const QString &str)
129 { append(str); return *this; }
130 inline QStringList &operator<<(const QStringList &l)
131 { *this += l; return *this; }
132 inline QStringList &operator<<(const QList<QString> &l)
133 { *this += l; return *this; }
134
135 inline int indexOf(QStringView str, int from = 0) const;
136 inline int indexOf(QLatin1String str, int from = 0) const;
137
138 inline int lastIndexOf(QStringView str, int from = -1) const;
139 inline int lastIndexOf(QLatin1String str, int from = -1) const;
140
141#ifndef QT_NO_REGEXP
142 inline int indexOf(const QRegExp &rx, int from = 0) const;
143 inline int lastIndexOf(const QRegExp &rx, int from = -1) const;
144 inline int indexOf(QRegExp &rx, int from = 0) const;
145 inline int lastIndexOf(QRegExp &rx, int from = -1) const;
146#endif
147
148#if QT_CONFIG(regularexpression)
149 inline int indexOf(const QRegularExpression &re, int from = 0) const;
150 inline int lastIndexOf(const QRegularExpression &re, int from = -1) const;
151#endif // QT_CONFIG(regularexpression)
152
153 using QList<QString>::indexOf;
154 using QList<QString>::lastIndexOf;
155};
156
157Q_DECLARE_TYPEINFO(QStringList, Q_MOVABLE_TYPE);
158
159#ifndef Q_QDOC
160inline QStringList *QListSpecialMethods<QString>::self()
161{ return static_cast<QStringList *>(this); }
162inline const QStringList *QListSpecialMethods<QString>::self() const
163{ return static_cast<const QStringList *>(this); }
164
165namespace QtPrivate {
166 void Q_CORE_EXPORT QStringList_sort(QStringList *that, Qt::CaseSensitivity cs);
167 int Q_CORE_EXPORT QStringList_removeDuplicates(QStringList *that);
168 QString Q_CORE_EXPORT QStringList_join(const QStringList *that, const QChar *sep, int seplen);
169 Q_CORE_EXPORT QString QStringList_join(const QStringList &list, QLatin1String sep);
170 QStringList Q_CORE_EXPORT QStringList_filter(const QStringList *that, const QString &str,
171 Qt::CaseSensitivity cs);
172
173#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
174 bool Q_CORE_EXPORT QStringList_contains(const QStringList *that, const QString &str, Qt::CaseSensitivity cs);
175#endif
176 bool Q_CORE_EXPORT QStringList_contains(const QStringList *that, QStringView str, Qt::CaseSensitivity cs);
177 bool Q_CORE_EXPORT QStringList_contains(const QStringList *that, QLatin1String str, Qt::CaseSensitivity cs);
178 void Q_CORE_EXPORT QStringList_replaceInStrings(QStringList *that, const QString &before, const QString &after,
179 Qt::CaseSensitivity cs);
180
181#ifndef QT_NO_REGEXP
182 void Q_CORE_EXPORT QStringList_replaceInStrings(QStringList *that, const QRegExp &rx, const QString &after);
183 QStringList Q_CORE_EXPORT QStringList_filter(const QStringList *that, const QRegExp &re);
184 int Q_CORE_EXPORT QStringList_indexOf(const QStringList *that, const QRegExp &rx, int from);
185 int Q_CORE_EXPORT QStringList_lastIndexOf(const QStringList *that, const QRegExp &rx, int from);
186 int Q_CORE_EXPORT QStringList_indexOf(const QStringList *that, QRegExp &rx, int from);
187 int Q_CORE_EXPORT QStringList_lastIndexOf(const QStringList *that, QRegExp &rx, int from);
188#endif
189
190#if QT_CONFIG(regularexpression)
191 void Q_CORE_EXPORT QStringList_replaceInStrings(QStringList *that, const QRegularExpression &rx, const QString &after);
192 QStringList Q_CORE_EXPORT QStringList_filter(const QStringList *that, const QRegularExpression &re);
193 int Q_CORE_EXPORT QStringList_indexOf(const QStringList *that, const QRegularExpression &re, int from);
194 int Q_CORE_EXPORT QStringList_lastIndexOf(const QStringList *that, const QRegularExpression &re, int from);
195#endif // QT_CONFIG(regularexpression)
196}
197
198inline void QListSpecialMethods<QString>::sort(Qt::CaseSensitivity cs)
199{
200 QtPrivate::QStringList_sort(self(), cs);
201}
202
203inline int QListSpecialMethods<QString>::removeDuplicates()
204{
205 return QtPrivate::QStringList_removeDuplicates(self());
206}
207
208inline QString QListSpecialMethods<QString>::join(const QString &sep) const
209{
210 return QtPrivate::QStringList_join(self(), sep.constData(), sep.length());
211}
212
213QString QListSpecialMethods<QString>::join(QLatin1String sep) const
214{
215 return QtPrivate::QStringList_join(*self(), sep);
216}
217
218inline QString QListSpecialMethods<QString>::join(QChar sep) const
219{
220 return QtPrivate::QStringList_join(self(), &sep, 1);
221}
222
223inline QStringList QListSpecialMethods<QString>::filter(const QString &str, Qt::CaseSensitivity cs) const
224{
225 return QtPrivate::QStringList_filter(self(), str, cs);
226}
227
228#if QT_STRINGVIEW_LEVEL < 2
229inline bool QStringList::contains(const QString &str, Qt::CaseSensitivity cs) const
230{
231 return QtPrivate::QStringList_contains(this, str, cs);
232}
233#endif
234
235inline bool QStringList::contains(QLatin1String str, Qt::CaseSensitivity cs) const
236{
237 return QtPrivate::QStringList_contains(this, str, cs);
238}
239
240inline bool QStringList::contains(QStringView str, Qt::CaseSensitivity cs) const
241{
242 return QtPrivate::QStringList_contains(this, str, cs);
243}
244
245inline QStringList &QListSpecialMethods<QString>::replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs)
246{
247 QtPrivate::QStringList_replaceInStrings(self(), before, after, cs);
248 return *self();
249}
250
251inline QStringList operator+(const QList<QString> &one, const QStringList &other)
252{
253 QStringList n = one;
254 n += other;
255 return n;
256}
257
258inline int QStringList::indexOf(QStringView string, int from) const
259{
260 return QtPrivate::indexOf<QString, QStringView>(*this, string, from);
261}
262
263inline int QStringList::indexOf(QLatin1String string, int from) const
264{
265 return QtPrivate::indexOf<QString, QLatin1String>(*this, string, from);
266}
267
268inline int QStringList::lastIndexOf(QStringView string, int from) const
269{
270 return QtPrivate::lastIndexOf<QString, QStringView>(*this, string, from);
271}
272
273inline int QStringList::lastIndexOf(QLatin1String string, int from) const
274{
275 return QtPrivate::lastIndexOf<QString, QLatin1String>(*this, string, from);
276}
277
278#ifndef QT_NO_REGEXP
279inline QStringList &QListSpecialMethods<QString>::replaceInStrings(const QRegExp &rx, const QString &after)
280{
281 QtPrivate::QStringList_replaceInStrings(self(), rx, after);
282 return *self();
283}
284
285inline QStringList QListSpecialMethods<QString>::filter(const QRegExp &rx) const
286{
287 return QtPrivate::QStringList_filter(self(), rx);
288}
289
290inline int QStringList::indexOf(const QRegExp &rx, int from) const
291{
292 return QtPrivate::QStringList_indexOf(this, rx, from);
293}
294
295inline int QStringList::lastIndexOf(const QRegExp &rx, int from) const
296{
297 return QtPrivate::QStringList_lastIndexOf(this, rx, from);
298}
299
300inline int QStringList::indexOf(QRegExp &rx, int from) const
301{
302 return QtPrivate::QStringList_indexOf(this, rx, from);
303}
304
305inline int QStringList::lastIndexOf(QRegExp &rx, int from) const
306{
307 return QtPrivate::QStringList_lastIndexOf(this, rx, from);
308}
309#endif
310
311#if QT_CONFIG(regularexpression)
312inline QStringList &QListSpecialMethods<QString>::replaceInStrings(const QRegularExpression &rx, const QString &after)
313{
314 QtPrivate::QStringList_replaceInStrings(self(), rx, after);
315 return *self();
316}
317
318inline QStringList QListSpecialMethods<QString>::filter(const QRegularExpression &rx) const
319{
320 return QtPrivate::QStringList_filter(self(), rx);
321}
322
323inline int QStringList::indexOf(const QRegularExpression &rx, int from) const
324{
325 return QtPrivate::QStringList_indexOf(this, rx, from);
326}
327
328inline int QStringList::lastIndexOf(const QRegularExpression &rx, int from) const
329{
330 return QtPrivate::QStringList_lastIndexOf(this, rx, from);
331}
332#endif // QT_CONFIG(regularexpression)
333#endif // Q_QDOC
334
335QT_END_NAMESPACE
336
337#endif // QSTRINGLIST_H
338