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 | |
51 | QT_REQUIRE_CONFIG(regularexpression); |
52 | |
53 | QT_BEGIN_NAMESPACE |
54 | |
55 | class QStringView; |
56 | |
57 | class QRegularExpressionMatch; |
58 | class QRegularExpressionMatchIterator; |
59 | struct QRegularExpressionPrivate; |
60 | class QRegularExpression; |
61 | |
62 | Q_CORE_EXPORT uint qHash(const QRegularExpression &key, uint seed = 0) noexcept; |
63 | |
64 | class Q_CORE_EXPORT QRegularExpression |
65 | { |
66 | public: |
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 Q_DECL_ENUMERATOR_DEPRECATED_X("This option does not have any effect since Qt 5.12" ) = 0x0080, |
77 | DontAutomaticallyOptimizeOption Q_DECL_ENUMERATOR_DEPRECATED_X("This option does not have any effect since Qt 5.12" ) = 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 | QRegularExpression &operator=(QRegularExpression &&re) noexcept |
90 | { d.swap(re.d); return *this; } |
91 | |
92 | void swap(QRegularExpression &other) noexcept { d.swap(other.d); } |
93 | |
94 | QString pattern() const; |
95 | void setPattern(const QString &pattern); |
96 | |
97 | bool isValid() const; |
98 | int patternErrorOffset() const; |
99 | QString errorString() const; |
100 | |
101 | int captureCount() const; |
102 | QStringList namedCaptureGroups() const; |
103 | |
104 | enum MatchType { |
105 | NormalMatch = 0, |
106 | PartialPreferCompleteMatch, |
107 | PartialPreferFirstMatch, |
108 | NoMatch |
109 | }; |
110 | |
111 | enum MatchOption { |
112 | NoMatchOption = 0x0000, |
113 | AnchoredMatchOption = 0x0001, |
114 | DontCheckSubjectStringMatchOption = 0x0002 |
115 | }; |
116 | Q_DECLARE_FLAGS(MatchOptions, MatchOption) |
117 | |
118 | QRegularExpressionMatch match(const QString &subject, |
119 | int offset = 0, |
120 | MatchType matchType = NormalMatch, |
121 | MatchOptions matchOptions = NoMatchOption) const; |
122 | |
123 | QRegularExpressionMatch match(const QStringRef &subjectRef, |
124 | int offset = 0, |
125 | MatchType matchType = NormalMatch, |
126 | MatchOptions matchOptions = NoMatchOption) const; |
127 | |
128 | QRegularExpressionMatchIterator globalMatch(const QString &subject, |
129 | int offset = 0, |
130 | MatchType matchType = NormalMatch, |
131 | MatchOptions matchOptions = NoMatchOption) const; |
132 | |
133 | QRegularExpressionMatchIterator globalMatch(const QStringRef &subjectRef, |
134 | int offset = 0, |
135 | MatchType matchType = NormalMatch, |
136 | MatchOptions matchOptions = NoMatchOption) const; |
137 | |
138 | void optimize() const; |
139 | |
140 | static QString escape(const QString &str); |
141 | static QString wildcardToRegularExpression(const QString &str); |
142 | static inline QString anchoredPattern(const QString &expression) |
143 | { |
144 | return QLatin1String("\\A(?:" ) |
145 | + expression |
146 | + QLatin1String(")\\z" ); |
147 | } |
148 | |
149 | bool operator==(const QRegularExpression &re) const; |
150 | inline bool operator!=(const QRegularExpression &re) const { return !operator==(re); } |
151 | |
152 | private: |
153 | friend struct QRegularExpressionPrivate; |
154 | friend class QRegularExpressionMatch; |
155 | friend struct QRegularExpressionMatchPrivate; |
156 | friend class QRegularExpressionMatchIterator; |
157 | friend Q_CORE_EXPORT uint qHash(const QRegularExpression &key, uint seed) noexcept; |
158 | |
159 | QRegularExpression(QRegularExpressionPrivate &dd); |
160 | QExplicitlySharedDataPointer<QRegularExpressionPrivate> d; |
161 | }; |
162 | |
163 | Q_DECLARE_SHARED(QRegularExpression) |
164 | Q_DECLARE_OPERATORS_FOR_FLAGS(QRegularExpression::PatternOptions) |
165 | Q_DECLARE_OPERATORS_FOR_FLAGS(QRegularExpression::MatchOptions) |
166 | |
167 | #ifndef QT_NO_DATASTREAM |
168 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &out, const QRegularExpression &re); |
169 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &in, QRegularExpression &re); |
170 | #endif |
171 | |
172 | #ifndef QT_NO_DEBUG_STREAM |
173 | Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QRegularExpression &re); |
174 | Q_CORE_EXPORT QDebug operator<<(QDebug debug, QRegularExpression::PatternOptions patternOptions); |
175 | #endif |
176 | |
177 | struct QRegularExpressionMatchPrivate; |
178 | |
179 | class Q_CORE_EXPORT QRegularExpressionMatch |
180 | { |
181 | public: |
182 | QRegularExpressionMatch(); |
183 | ~QRegularExpressionMatch(); |
184 | QRegularExpressionMatch(const QRegularExpressionMatch &match); |
185 | QRegularExpressionMatch &operator=(const QRegularExpressionMatch &match); |
186 | QRegularExpressionMatch &operator=(QRegularExpressionMatch &&match) noexcept |
187 | { d.swap(match.d); return *this; } |
188 | void swap(QRegularExpressionMatch &other) noexcept { 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 | |
230 | private: |
231 | friend class QRegularExpression; |
232 | friend struct QRegularExpressionMatchPrivate; |
233 | friend class QRegularExpressionMatchIterator; |
234 | |
235 | QRegularExpressionMatch(QRegularExpressionMatchPrivate &dd); |
236 | QSharedDataPointer<QRegularExpressionMatchPrivate> d; |
237 | }; |
238 | |
239 | Q_DECLARE_SHARED(QRegularExpressionMatch) |
240 | |
241 | #ifndef QT_NO_DEBUG_STREAM |
242 | Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QRegularExpressionMatch &match); |
243 | #endif |
244 | |
245 | struct QRegularExpressionMatchIteratorPrivate; |
246 | |
247 | class Q_CORE_EXPORT QRegularExpressionMatchIterator |
248 | { |
249 | public: |
250 | QRegularExpressionMatchIterator(); |
251 | ~QRegularExpressionMatchIterator(); |
252 | QRegularExpressionMatchIterator(const QRegularExpressionMatchIterator &iterator); |
253 | QRegularExpressionMatchIterator &operator=(const QRegularExpressionMatchIterator &iterator); |
254 | QRegularExpressionMatchIterator &operator=(QRegularExpressionMatchIterator &&iterator) noexcept |
255 | { d.swap(iterator.d); return *this; } |
256 | void swap(QRegularExpressionMatchIterator &other) noexcept { d.swap(other.d); } |
257 | |
258 | bool isValid() const; |
259 | |
260 | bool hasNext() const; |
261 | QRegularExpressionMatch next(); |
262 | QRegularExpressionMatch peekNext() const; |
263 | |
264 | QRegularExpression regularExpression() const; |
265 | QRegularExpression::MatchType matchType() const; |
266 | QRegularExpression::MatchOptions matchOptions() const; |
267 | |
268 | private: |
269 | friend class QRegularExpression; |
270 | |
271 | QRegularExpressionMatchIterator(QRegularExpressionMatchIteratorPrivate &dd); |
272 | QSharedDataPointer<QRegularExpressionMatchIteratorPrivate> d; |
273 | }; |
274 | |
275 | Q_DECLARE_SHARED(QRegularExpressionMatchIterator) |
276 | |
277 | QT_END_NAMESPACE |
278 | |
279 | #endif // QREGULAREXPRESSION_H |
280 | |