Merge remote-tracking branch 'origin/master' into api_changes
[profile/ivi/qtbase.git] / src / corelib / tools / qregularexpression.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Giuseppe D'Angelo <dangelog@gmail.com>.
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QREGULAREXPRESSION_H
43 #define QREGULAREXPRESSION_H
44
45 #ifndef QT_NO_REGEXP
46
47 #include <QtCore/qstring.h>
48 #include <QtCore/qshareddata.h>
49 #include <QtCore/qvariant.h>
50
51 QT_BEGIN_HEADER
52
53 QT_BEGIN_NAMESPACE
54
55 class QRegularExpressionMatch;
56 class QRegularExpressionMatchIterator;
57 struct QRegularExpressionPrivate;
58
59 class Q_CORE_EXPORT QRegularExpression
60 {
61 public:
62     enum PatternOption {
63         NoPatternOption                = 0x0000,
64         CaseInsensitiveOption          = 0x0001,
65         DotMatchesEverythingOption     = 0x0002,
66         MultilineOption                = 0x0004,
67         ExtendedPatternSyntaxOption    = 0x0008,
68         InvertedGreedinessOption       = 0x0010,
69         DontCaptureOption              = 0x0020,
70         UseUnicodePropertiesOption     = 0x0040
71     };
72     Q_DECLARE_FLAGS(PatternOptions, PatternOption)
73
74     PatternOptions patternOptions() const;
75     void setPatternOptions(PatternOptions options);
76
77     QRegularExpression();
78     explicit QRegularExpression(const QString &pattern, PatternOptions options = NoPatternOption);
79     QRegularExpression(const QRegularExpression &re);
80     ~QRegularExpression();
81     QRegularExpression &operator=(const QRegularExpression &re);
82
83 #ifdef Q_COMPILER_RVALUE_REFS
84     inline QRegularExpression &operator=(QRegularExpression &&re)
85     { d.swap(re.d); return *this; }
86 #endif
87
88     inline void swap(QRegularExpression &re) { d.swap(re.d); }
89
90     QString pattern() const;
91     void setPattern(const QString &pattern);
92
93     bool isValid() const;
94     int patternErrorOffset() const;
95     QString errorString() const;
96
97     int captureCount() const;
98
99     enum MatchType {
100         NormalMatch = 0,
101         PartialPreferCompleteMatch,
102         PartialPreferFirstMatch
103     };
104
105     enum MatchOption {
106         NoMatchOption              = 0x0000,
107         AnchoredMatchOption        = 0x0001
108     };
109     Q_DECLARE_FLAGS(MatchOptions, MatchOption)
110
111     QRegularExpressionMatch match(const QString &subject,
112                                   int offset                = 0,
113                                   MatchType matchType       = NormalMatch,
114                                   MatchOptions matchOptions = NoMatchOption) const;
115
116     QRegularExpressionMatchIterator globalMatch(const QString &subject,
117                                                 int offset                = 0,
118                                                 MatchType matchType       = NormalMatch,
119                                                 MatchOptions matchOptions = NoMatchOption) const;
120
121     static QString escape(const QString &str);
122
123     bool operator==(const QRegularExpression &re) const;
124     inline bool operator!=(const QRegularExpression &re) const { return !operator==(re); }
125
126 private:
127     friend struct QRegularExpressionPrivate;
128     friend class QRegularExpressionMatch;
129     friend struct QRegularExpressionMatchPrivate;
130     friend class QRegularExpressionMatchIterator;
131
132     QRegularExpression(QRegularExpressionPrivate &dd);
133     QExplicitlySharedDataPointer<QRegularExpressionPrivate> d;
134 };
135
136 Q_DECLARE_OPERATORS_FOR_FLAGS(QRegularExpression::PatternOptions)
137 Q_DECLARE_OPERATORS_FOR_FLAGS(QRegularExpression::MatchOptions)
138 Q_DECLARE_TYPEINFO(QRegularExpression, Q_MOVABLE_TYPE);
139
140 #ifndef QT_NO_DATASTREAM
141 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &out, const QRegularExpression &re);
142 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &in, QRegularExpression &re);
143 #endif
144
145 #ifndef QT_NO_DEBUG_STREAM
146 Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QRegularExpression &re);
147 Q_CORE_EXPORT QDebug operator<<(QDebug debug, QRegularExpression::PatternOptions patternOptions);
148 #endif
149
150 struct QRegularExpressionMatchPrivate;
151
152 class Q_CORE_EXPORT QRegularExpressionMatch
153 {
154 public:
155     ~QRegularExpressionMatch();
156     QRegularExpressionMatch(const QRegularExpressionMatch &match);
157     QRegularExpressionMatch &operator=(const QRegularExpressionMatch &match);
158
159 #ifdef Q_COMPILER_RVALUE_REFS
160     inline QRegularExpressionMatch &operator=(QRegularExpressionMatch &&match)
161     { d.swap(match.d); return *this; }
162 #endif
163     inline void swap(QRegularExpressionMatch &match) { d.swap(match.d); }
164
165     QRegularExpression regularExpression() const;
166     QRegularExpression::MatchType matchType() const;
167     QRegularExpression::MatchOptions matchOptions() const;
168
169     bool hasMatch() const;
170     bool hasPartialMatch() const;
171
172     bool isValid() const;
173
174     int lastCapturedIndex() const;
175
176     QString captured(int nth = 0) const;
177     QStringRef capturedRef(int nth = 0) const;
178
179     QString captured(const QString &name) const;
180     QStringRef capturedRef(const QString &name) const;
181
182     QStringList capturedTexts() const;
183
184     int capturedStart(int nth = 0) const;
185     int capturedLength(int nth = 0) const;
186     int capturedEnd(int nth = 0) const;
187
188     int capturedStart(const QString &name) const;
189     int capturedLength(const QString &name) const;
190     int capturedEnd(const QString &name) const;
191
192 private:
193     friend class QRegularExpression;
194     friend struct QRegularExpressionMatchPrivate;
195     friend class QRegularExpressionMatchIterator;
196
197     QRegularExpressionMatch(QRegularExpressionMatchPrivate &dd);
198     QSharedDataPointer<QRegularExpressionMatchPrivate> d;
199 };
200
201 Q_DECLARE_TYPEINFO(QRegularExpressionMatch, Q_MOVABLE_TYPE);
202
203 #ifndef QT_NO_DEBUG_STREAM
204 Q_CORE_EXPORT QDebug operator<<(QDebug debug, const QRegularExpressionMatch &match);
205 #endif
206
207 struct QRegularExpressionMatchIteratorPrivate;
208
209 class Q_CORE_EXPORT QRegularExpressionMatchIterator
210 {
211 public:
212     ~QRegularExpressionMatchIterator();
213     QRegularExpressionMatchIterator(const QRegularExpressionMatchIterator &iterator);
214     QRegularExpressionMatchIterator &operator=(const QRegularExpressionMatchIterator &iterator);
215 #ifdef Q_COMPILER_RVALUE_REFS
216     inline QRegularExpressionMatchIterator &operator=(QRegularExpressionMatchIterator &&iterator)
217     { d.swap(iterator.d); return *this; }
218 #endif
219     void swap(QRegularExpressionMatchIterator &iterator) { d.swap(iterator.d); }
220
221     bool isValid() const;
222
223     bool hasNext() const;
224     QRegularExpressionMatch next();
225     QRegularExpressionMatch peekNext() const;
226
227     QRegularExpression regularExpression() const;
228     QRegularExpression::MatchType matchType() const;
229     QRegularExpression::MatchOptions matchOptions() const;
230
231 private:
232     friend class QRegularExpression;
233
234     QRegularExpressionMatchIterator(QRegularExpressionMatchIteratorPrivate &dd);
235     QSharedDataPointer<QRegularExpressionMatchIteratorPrivate> d;
236 };
237
238 Q_DECLARE_TYPEINFO(QRegularExpressionMatchIterator, Q_MOVABLE_TYPE);
239
240 QT_END_NAMESPACE
241
242 QT_END_HEADER
243
244 #endif // QT_NO_REGEXP
245
246 #endif // QREGULAREXPRESSION_H