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