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#ifndef QT_NO_REGEXP
136 inline int indexOf(const QRegExp &rx, int from = 0) const;
137 inline int lastIndexOf(const QRegExp &rx, int from = -1) const;
138 inline int indexOf(QRegExp &rx, int from = 0) const;
139 inline int lastIndexOf(QRegExp &rx, int from = -1) const;
140#endif
141
142#if QT_CONFIG(regularexpression)
143 inline int indexOf(const QRegularExpression &re, int from = 0) const;
144 inline int lastIndexOf(const QRegularExpression &re, int from = -1) const;
145#endif // QT_CONFIG(regularexpression)
146
147 using QList<QString>::indexOf;
148 using QList<QString>::lastIndexOf;
149};
150
151Q_DECLARE_TYPEINFO(QStringList, Q_MOVABLE_TYPE);
152
153#ifndef Q_QDOC
154inline QStringList *QListSpecialMethods<QString>::self()
155{ return static_cast<QStringList *>(this); }
156inline const QStringList *QListSpecialMethods<QString>::self() const
157{ return static_cast<const QStringList *>(this); }
158
159namespace QtPrivate {
160 void Q_CORE_EXPORT QStringList_sort(QStringList *that, Qt::CaseSensitivity cs);
161 int Q_CORE_EXPORT QStringList_removeDuplicates(QStringList *that);
162 QString Q_CORE_EXPORT QStringList_join(const QStringList *that, const QChar *sep, int seplen);
163 Q_CORE_EXPORT QString QStringList_join(const QStringList &list, QLatin1String sep);
164 QStringList Q_CORE_EXPORT QStringList_filter(const QStringList *that, const QString &str,
165 Qt::CaseSensitivity cs);
166
167#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
168 bool Q_CORE_EXPORT QStringList_contains(const QStringList *that, const QString &str, Qt::CaseSensitivity cs);
169#endif
170 bool Q_CORE_EXPORT QStringList_contains(const QStringList *that, QStringView str, Qt::CaseSensitivity cs);
171 bool Q_CORE_EXPORT QStringList_contains(const QStringList *that, QLatin1String str, Qt::CaseSensitivity cs);
172 void Q_CORE_EXPORT QStringList_replaceInStrings(QStringList *that, const QString &before, const QString &after,
173 Qt::CaseSensitivity cs);
174
175#ifndef QT_NO_REGEXP
176 void Q_CORE_EXPORT QStringList_replaceInStrings(QStringList *that, const QRegExp &rx, const QString &after);
177 QStringList Q_CORE_EXPORT QStringList_filter(const QStringList *that, const QRegExp &re);
178 int Q_CORE_EXPORT QStringList_indexOf(const QStringList *that, const QRegExp &rx, int from);
179 int Q_CORE_EXPORT QStringList_lastIndexOf(const QStringList *that, const QRegExp &rx, int from);
180 int Q_CORE_EXPORT QStringList_indexOf(const QStringList *that, QRegExp &rx, int from);
181 int Q_CORE_EXPORT QStringList_lastIndexOf(const QStringList *that, QRegExp &rx, int from);
182#endif
183
184#if QT_CONFIG(regularexpression)
185 void Q_CORE_EXPORT QStringList_replaceInStrings(QStringList *that, const QRegularExpression &rx, const QString &after);
186 QStringList Q_CORE_EXPORT QStringList_filter(const QStringList *that, const QRegularExpression &re);
187 int Q_CORE_EXPORT QStringList_indexOf(const QStringList *that, const QRegularExpression &re, int from);
188 int Q_CORE_EXPORT QStringList_lastIndexOf(const QStringList *that, const QRegularExpression &re, int from);
189#endif // QT_CONFIG(regularexpression)
190}
191
192inline void QListSpecialMethods<QString>::sort(Qt::CaseSensitivity cs)
193{
194 QtPrivate::QStringList_sort(self(), cs);
195}
196
197inline int QListSpecialMethods<QString>::removeDuplicates()
198{
199 return QtPrivate::QStringList_removeDuplicates(self());
200}
201
202inline QString QListSpecialMethods<QString>::join(const QString &sep) const
203{
204 return QtPrivate::QStringList_join(self(), sep.constData(), sep.length());
205}
206
207QString QListSpecialMethods<QString>::join(QLatin1String sep) const
208{
209 return QtPrivate::QStringList_join(*self(), sep);
210}
211
212inline QString QListSpecialMethods<QString>::join(QChar sep) const
213{
214 return QtPrivate::QStringList_join(self(), &sep, 1);
215}
216
217inline QStringList QListSpecialMethods<QString>::filter(const QString &str, Qt::CaseSensitivity cs) const
218{
219 return QtPrivate::QStringList_filter(self(), str, cs);
220}
221
222#if QT_STRINGVIEW_LEVEL < 2
223inline bool QStringList::contains(const QString &str, Qt::CaseSensitivity cs) const
224{
225 return QtPrivate::QStringList_contains(this, str, cs);
226}
227#endif
228
229inline bool QStringList::contains(QLatin1String str, Qt::CaseSensitivity cs) const
230{
231 return QtPrivate::QStringList_contains(this, str, cs);
232}
233
234inline bool QStringList::contains(QStringView str, Qt::CaseSensitivity cs) const
235{
236 return QtPrivate::QStringList_contains(this, str, cs);
237}
238
239inline QStringList &QListSpecialMethods<QString>::replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs)
240{
241 QtPrivate::QStringList_replaceInStrings(self(), before, after, cs);
242 return *self();
243}
244
245inline QStringList operator+(const QList<QString> &one, const QStringList &other)
246{
247 QStringList n = one;
248 n += other;
249 return n;
250}
251
252#ifndef QT_NO_REGEXP
253inline QStringList &QListSpecialMethods<QString>::replaceInStrings(const QRegExp &rx, const QString &after)
254{
255 QtPrivate::QStringList_replaceInStrings(self(), rx, after);
256 return *self();
257}
258
259inline QStringList QListSpecialMethods<QString>::filter(const QRegExp &rx) const
260{
261 return QtPrivate::QStringList_filter(self(), rx);
262}
263
264inline int QStringList::indexOf(const QRegExp &rx, int from) const
265{
266 return QtPrivate::QStringList_indexOf(this, rx, from);
267}
268
269inline int QStringList::lastIndexOf(const QRegExp &rx, int from) const
270{
271 return QtPrivate::QStringList_lastIndexOf(this, rx, from);
272}
273
274inline int QStringList::indexOf(QRegExp &rx, int from) const
275{
276 return QtPrivate::QStringList_indexOf(this, rx, from);
277}
278
279inline int QStringList::lastIndexOf(QRegExp &rx, int from) const
280{
281 return QtPrivate::QStringList_lastIndexOf(this, rx, from);
282}
283#endif
284
285#if QT_CONFIG(regularexpression)
286inline QStringList &QListSpecialMethods<QString>::replaceInStrings(const QRegularExpression &rx, const QString &after)
287{
288 QtPrivate::QStringList_replaceInStrings(self(), rx, after);
289 return *self();
290}
291
292inline QStringList QListSpecialMethods<QString>::filter(const QRegularExpression &rx) const
293{
294 return QtPrivate::QStringList_filter(self(), rx);
295}
296
297inline int QStringList::indexOf(const QRegularExpression &rx, int from) const
298{
299 return QtPrivate::QStringList_indexOf(this, rx, from);
300}
301
302inline int QStringList::lastIndexOf(const QRegularExpression &rx, int from) const
303{
304 return QtPrivate::QStringList_lastIndexOf(this, rx, from);
305}
306#endif // QT_CONFIG(regularexpression)
307#endif // Q_QDOC
308
309QT_END_NAMESPACE
310
311#endif // QSTRINGLIST_H
312