1/****************************************************************************
2**
3** Copyright (C) 2012 Giuseppe D'Angelo <dangelog@gmail.com>.
4** Copyright (C) 2012 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
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#ifndef QREGULAREXPRESSION_H
42#define QREGULAREXPRESSION_H
43
44#include <QtCore/qglobal.h>
45
46#include <QtCore/qstring.h>
47#include <QtCore/qstringlist.h>
48#include <QtCore/qshareddata.h>
49#include <QtCore/qvariant.h>
50
51QT_REQUIRE_CONFIG(regularexpression);
52
53QT_BEGIN_NAMESPACE
54
55class QStringView;
56
57class QRegularExpressionMatch;
58class QRegularExpressionMatchIterator;
59struct QRegularExpressionPrivate;
60class QRegularExpression;
61
62Q_CORE_EXPORT uint qHash(const QRegularExpression &key, uint seed = 0) Q_DECL_NOTHROW;
63
64class Q_CORE_EXPORT QRegularExpression
65{
66public:
67 enum PatternOption {
68 NoPatternOption = 0x0000,
69 CaseInsensitiveOption = 0x0001,
70 DotMatchesEverythingOption = 0x0002,
71 MultilineOption = 0x0004,
72 ExtendedPatternSyntaxOption = 0x0008,
73 InvertedGreedinessOption = 0x0010,
74 DontCaptureOption = 0x0020,
75 UseUnicodePropertiesOption = 0x0040,
76 OptimizeOnFirstUsageOption = 0x0080,
77 DontAutomaticallyOptimizeOption = 0x0100
78 };
79 Q_DECLARE_FLAGS(PatternOptions, PatternOption)
80
81 PatternOptions patternOptions() const;
82 void setPatternOptions(PatternOptions options);
83
84 QRegularExpression();
85 explicit QRegularExpression(const QString &pattern, PatternOptions options = NoPatternOption);
86 QRegularExpression(const QRegularExpression &re);
87 ~QRegularExpression();
88 QRegularExpression &operator=(const QRegularExpression &re);
89
90#ifdef Q_COMPILER_RVALUE_REFS
91 QRegularExpression &operator=(QRegularExpression &&re) Q_DECL_NOTHROW
92 { d.swap(re.d); return *this; }
93#endif
94
95 void swap(QRegularExpression &other) Q_DECL_NOTHROW { d.swap(other.d); }
96
97 QString pattern() const;
98 void setPattern(const QString &pattern);
99 void setWildcardPattern(const QString &pattern);
100
101 bool isValid() const;
102 int patternErrorOffset() const;
103 QString errorString() const;
104
105 int captureCount() const;
106 QStringList namedCaptureGroups() const;
107
108 enum MatchType {
109 NormalMatch = 0,
110 PartialPreferCompleteMatch,
111 PartialPreferFirstMatch,
112 NoMatch
113 };
114
115 enum MatchOption {
116 NoMatchOption = 0x0000,
117 AnchoredMatchOption = 0x0001,
118 DontCheckSubjectStringMatchOption = 0x0002
119 };
120 Q_DECLARE_FLAGS(MatchOptions, MatchOption)
121
122 QRegularExpressionMatch match(const QString &subject,
123 int offset = 0,
124 MatchType matchType = NormalMatch,
125 MatchOptions matchOptions = NoMatchOption) const;
126
127 QRegularExpressionMatch match(const QStringRef &subjectRef,
128 int offset = 0,
129 MatchType matchType = NormalMatch,
130 MatchOptions matchOptions = NoMatchOption) const;
131
132 QRegularExpressionMatchIterator globalMatch(const QString &subject,
133 int offset = 0,
134 MatchType matchType = NormalMatch,
135 MatchOptions matchOptions = NoMatchOption) const;
136
137 QRegularExpressionMatchIterator globalMatch(const QStringRef &subjectRef,
138 int offset = 0,
139 MatchType matchType = NormalMatch,
140 MatchOptions matchOptions = NoMatchOption) const;
141
142 void optimize() const;
143
144 static QString escape(const QString &str);
145
146 bool operator==(const QRegularExpression &re) const;
147 inline bool operator!=(const QRegularExpression &re) const { return !operator==(re); }
148
149private:
150 friend struct QRegularExpressionPrivate;
151 friend class QRegularExpressionMatch;
152 friend struct QRegularExpressionMatchPrivate;
153 friend class QRegularExpressionMatchIterator;
154 friend Q_CORE_EXPORT uint qHash(const QRegularExpression &key, uint seed) Q_DECL_NOTHROW;
155
156 QRegularExpression(QRegularExpressionPrivate &dd);
157 QExplicitlySharedDataPointer<QRegularExpressionPrivate> d;
158};
159
160Q_DECLARE_SHARED(QRegularExpression)
161Q_DECLARE_OPERATORS_FOR_FLAGS(QRegularExpression::PatternOptions)
162Q_DECLARE_OPERATORS_FOR_FLAGS(QRegularExpression::MatchOptions)
163
164#ifndef QT_NO_DATASTREAM
165Q_CORE_EXPORT QDataStream &operator<<(QDataStream &out, const QRegularExpression &re);
166Q_CORE_EXPORT QDataStream &operator>>(QDataStream &in, QRegularExpression &re);
167#endif
168
169#ifndef QT_NO_DEBUG_STREAM
170Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QRegularExpression &re);
171Q_CORE_EXPORT QDebug operator<<(QDebug debug, QRegularExpression::PatternOptions patternOptions);
172#endif
173
174struct QRegularExpressionMatchPrivate;
175
176class Q_CORE_EXPORT QRegularExpressionMatch
177{
178public:
179 QRegularExpressionMatch();
180 ~QRegularExpressionMatch();
181 QRegularExpressionMatch(const QRegularExpressionMatch &match);
182 QRegularExpressionMatch &operator=(const QRegularExpressionMatch &match);
183
184#ifdef Q_COMPILER_RVALUE_REFS
185 QRegularExpressionMatch &operator=(QRegularExpressionMatch &&match) Q_DECL_NOTHROW
186 { d.swap(match.d); return *this; }
187#endif
188 void swap(QRegularExpressionMatch &other) Q_DECL_NOTHROW { d.swap(other.d); }
189
190 QRegularExpression regularExpression() const;
191 QRegularExpression::MatchType matchType() const;
192 QRegularExpression::MatchOptions matchOptions() const;
193
194 bool hasMatch() const;
195 bool hasPartialMatch() const;
196
197 bool isValid() const;
198
199 int lastCapturedIndex() const;
200
201 QString captured(int nth = 0) const;
202 QStringRef capturedRef(int nth = 0) const;
203 QStringView capturedView(int nth = 0) const;
204
205#if QT_STRINGVIEW_LEVEL < 2
206 QString captured(const QString &name) const;
207 QStringRef capturedRef(const QString &name) const;
208#endif
209
210 QString captured(QStringView name) const;
211 QStringRef capturedRef(QStringView name) const;
212 QStringView capturedView(QStringView name) const;
213
214 QStringList capturedTexts() const;
215
216 int capturedStart(int nth = 0) const;
217 int capturedLength(int nth = 0) const;
218 int capturedEnd(int nth = 0) const;
219
220#if QT_STRINGVIEW_LEVEL < 2
221 int capturedStart(const QString &name) const;
222 int capturedLength(const QString &name) const;
223 int capturedEnd(const QString &name) const;
224#endif
225
226 int capturedStart(QStringView name) const;
227 int capturedLength(QStringView name) const;
228 int capturedEnd(QStringView name) const;
229
230private:
231 friend class QRegularExpression;
232 friend struct QRegularExpressionMatchPrivate;
233 friend class QRegularExpressionMatchIterator;
234
235 QRegularExpressionMatch(QRegularExpressionMatchPrivate &dd);
236 QSharedDataPointer<QRegularExpressionMatchPrivate> d;
237};
238
239Q_DECLARE_SHARED(QRegularExpressionMatch)
240
241#ifndef QT_NO_DEBUG_STREAM
242Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QRegularExpressionMatch &match);
243#endif
244
245struct QRegularExpressionMatchIteratorPrivate;
246
247class Q_CORE_EXPORT QRegularExpressionMatchIterator
248{
249public:
250 QRegularExpressionMatchIterator();
251 ~QRegularExpressionMatchIterator();
252 QRegularExpressionMatchIterator(const QRegularExpressionMatchIterator &iterator);
253 QRegularExpressionMatchIterator &operator=(const QRegularExpressionMatchIterator &iterator);
254#ifdef Q_COMPILER_RVALUE_REFS
255 QRegularExpressionMatchIterator &operator=(QRegularExpressionMatchIterator &&iterator) Q_DECL_NOTHROW
256 { d.swap(iterator.d); return *this; }
257#endif
258 void swap(QRegularExpressionMatchIterator &other) Q_DECL_NOTHROW { d.swap(other.d); }
259
260 bool isValid() const;
261
262 bool hasNext() const;
263 QRegularExpressionMatch next();
264 QRegularExpressionMatch peekNext() const;
265
266 QRegularExpression regularExpression() const;
267 QRegularExpression::MatchType matchType() const;
268 QRegularExpression::MatchOptions matchOptions() const;
269
270private:
271 friend class QRegularExpression;
272
273 QRegularExpressionMatchIterator(QRegularExpressionMatchIteratorPrivate &dd);
274 QSharedDataPointer<QRegularExpressionMatchIteratorPrivate> d;
275};
276
277Q_DECLARE_SHARED(QRegularExpressionMatchIterator)
278
279QT_END_NAMESPACE
280
281#endif // QREGULAREXPRESSION_H
282