regExpCxx.H
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2017-2021 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26Class
27 Foam::regExpCxx
28
29Description
30 Wrapper around C++11 regular expressions
31 with some additional prefix-handling. The prefix-handling is loosely
32 oriented on PCRE regular expressions and provides a simple means of
33 tuning the expressions.
34
35 The prefixes are detected as \c (?...) at the beginning of
36 the regular expression. Any unknown/unsupported prefixes are silently
37 ignored.
38
39 - "(?!i)" :
40 one or more embedded pattern-match modifiers for the entire pattern.
41 - the \c 'i' indicates ignore-case
42 - the \c '!' (exclamation) indicates negated (inverted) matching
43 .
44
45Note
46 Uses either POSIX extended regular expressions or
47 <a href=
48 "http://www.cplusplus.com/reference/regex/ECMAScript"
49 >modified ECMAScript regular expression grammar</a>
50
51 Since ECMAScript grammar may not work correctly on all installations,
52 the current default is to use extended regular expressions.
53
54 The C++11 regular expressions may be broken on some compilers.
55 For example, gcc 4.8 is known to fail.
56 For these systems the POSIX implementation or alternative must be used.
57
58Warning
59 This class should not be used directly.
60 Use the Foam::regExp typedef instead.
61
62SourceFiles
63 regExpCxx.C
64 regExpCxxI.H
65
66\*---------------------------------------------------------------------------*/
67
68#ifndef Foam_regExpCxx_H
69#define Foam_regExpCxx_H
70
71#include <regex>
72#include <string>
73
74// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
75
76namespace Foam
77{
78
79/*---------------------------------------------------------------------------*\
80 Class regExpCxx Declaration
81\*---------------------------------------------------------------------------*/
83class regExpCxx
84{
85 // Data Types
86
87 //- Simple control types
88 enum ctrlType { EMPTY = 0, NORMAL = 1, NEGATED = 2 };
89
90
91 // Private Data
92
93 //- Regular expression (using char type)
94 std::regex re_;
95
96 //- Track if input pattern is non-empty, negated etc.
97 unsigned char ctrl_;
98
99
100 // Private Member Functions
101
102 //- Select grammar based on regExpCxx optimisationSwitch
103 // 0 = extended, 1 = ECMAScript
104 static inline std::regex::flag_type syntax();
105
106 //- Assign pattern
107 bool set_pattern(const char* pattern, size_t len, bool ignoreCase);
108
109
110public:
111
112 // Public Types
113
114 //- Type for matches
115 typedef std::smatch results_type;
116
117
118 // Static Member Data
119
120 //- The default grammar (extended | ECMAScript).
121 static int grammar;
122
123
124 // Static Member Functions
125
126 //- Test if character is a regex meta-character
127 // \return True if character is one of the following:
128 // - any character: '.' \n
129 // - quantifiers: '*', '+', '?' \n
130 // - grouping: '(', '|', ')' \n
131 // - range: '[', ']' \n
132 //
133 // \note Regex bounds '{', '}' are not considered
134 inline static bool is_meta(const char c) noexcept;
135
136 //- Test if string contains any (unquoted) meta-characters
137 inline static bool is_meta
138 (
139 const std::string& str,
140 const char quote = '\\'
141 );
142
143
144 // Public Classes
145
146 //- Functor wrapper for testing meta-characters
147 struct meta
148 {
149 //- Test if character is a regex meta-character
150 bool operator()(const char c) const noexcept
151 {
152 return is_meta(c);
153 }
154
155 //- Test string for meta-characters
156 bool operator()(const std::string& s, const char q = '\\') const
157 {
158 return is_meta(s, q);
159 }
160 };
161
162
163 // Constructors
164
165 //- Default construct
166 inline regExpCxx();
167
168 //- Copy construct
169 inline regExpCxx(const regExpCxx& rgx);
170
171 //- Move construct
172 inline regExpCxx(regExpCxx&& rgx) noexcept;
173
174 //- Construct from character array, optionally ignore case
175 inline explicit regExpCxx
176 (
177 const char* pattern,
178 const bool ignoreCase = false
179 );
180
181 //- Construct from string, optionally ignore case
182 inline explicit regExpCxx
183 (
184 const std::string& pattern,
185 const bool ignoreCase = false
186 );
187
188
189 //- Destructor
190 ~regExpCxx() = default;
191
192
193 // Member Functions
194
195 // Access
196
197 //- True if expression is empty
198 inline bool empty() const noexcept;
199
200 //- True if expression is non-empty
201 inline bool exists() const noexcept;
202
203 //- True if pattern matching is negated
204 inline bool negated() const noexcept;
205
206 //- Change pattern negation, return previous value
207 inline bool negate(bool on) noexcept;
208
209 //- The number of capture groups for a non-empty,
210 //- non-negated expressions
211 inline unsigned ngroups() const;
212
213 // \return True if the pattern was set with ignore-case.
214 inline bool nocase() const;
215
216
217 // Editing
218
219 //- Clear expression.
220 // \return True if expression had existed prior to the clear.
221 inline bool clear();
222
223 //- Swap contents
224 inline void swap(regExpCxx& rgx);
225
226 //- Compile pattern into a regular expression, optionally ignore case.
227 // \return True if the pattern was compiled
228 inline bool set(const char* pattern, bool ignoreCase=false);
229
230 //- Compile pattern into a regular expression, optionally ignore case.
231 // \return True if the pattern was compiled
232 inline bool set(const std::string& pattern, bool ignoreCase=false);
233
234
235 // Matching/Searching
236
237 //- Find position within the text.
238 // \return The index where it begins or string::npos if not found
239 //
240 // \note does not properly work with negated regex!
241 inline std::string::size_type find(const std::string& text) const;
242
243 //- True if the regex matches the entire text.
244 // The begin-of-line (^) and end-of-line ($) anchors are implicit
245 inline bool match(const std::string& text) const;
246
247 //- True if the regex matches the text, set the matches.
248 // The first group starts at index 1 (0 is the entire match).
249 // The begin-of-line (^) and end-of-line ($) anchors are implicit
250 //
251 // \note does not properly work with negated regex!
252 inline bool match(const std::string& text, results_type& matches) const;
253
254 //- Return true if the regex was found within the text
255 inline bool search(const std::string& text) const;
256
257
258 // Member Operators
259
260 //- Perform match on text
261 inline bool operator()(const std::string& text) const;
262
263 //- Copy assignment
264 inline void operator=(const regExpCxx& rgx);
265
266 //- Move assignment
267 inline void operator=(regExpCxx&& rgx);
268
269 //- Assign and compile pattern from a character array.
270 // Matching is case sensitive.
271 inline void operator=(const char* pattern);
272
273 //- Assign and compile pattern from string.
274 // Matching is case sensitive.
275 inline void operator=(const std::string& pattern);
276};
277
278
279// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
280
281} // End namespace Foam
282
283// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
284
285#include "regExpCxxI.H"
286
287#endif
288
289// ************************************************************************* //
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:75
Wrapper around C++11 regular expressions with some additional prefix-handling. The prefix-handling is...
Definition: regExpCxx.H:83
bool nocase() const
Definition: regExpCxxI.H:178
std::smatch results_type
Type for matches.
Definition: regExpCxx.H:114
bool empty() const noexcept
True if expression is empty.
Definition: regExpCxxI.H:133
regExpCxx()
Default construct.
Definition: regExpCxxI.H:82
std::string::size_type find(const std::string &text) const
Find position within the text.
Definition: regExpCxxI.H:233
bool set(const char *pattern, bool ignoreCase=false)
Compile pattern into a regular expression, optionally ignore case.
Definition: regExpCxxI.H:209
bool exists() const noexcept
True if expression is non-empty.
Definition: regExpCxxI.H:139
bool match(const std::string &text) const
True if the regex matches the entire text.
Definition: regExpCxxI.H:289
bool search(const std::string &text) const
Return true if the regex was found within the text.
Definition: regExpCxxI.H:275
~regExpCxx()=default
Destructor.
bool negated() const noexcept
True if pattern matching is negated.
Definition: regExpCxxI.H:145
bool clear()
Clear expression.
Definition: regExpCxxI.H:184
bool negate(bool on) noexcept
Change pattern negation, return previous value.
Definition: regExpCxxI.H:151
static bool is_meta(const char c) noexcept
Test if character is a regex meta-character.
Definition: regExpCxxI.H:42
void swap(regExpCxx &rgx)
Swap contents.
Definition: regExpCxxI.H:198
static int grammar
The default grammar (extended | ECMAScript).
Definition: regExpCxx.H:120
unsigned ngroups() const
Definition: regExpCxxI.H:171
gmvFile<< "tracers "<< particles.size()<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().x()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().y()<< " ";}gmvFile<< nl;for(const passiveParticle &p :particles){ gmvFile<< p.position().z()<< " ";}gmvFile<< nl;forAll(lagrangianScalarNames, i){ word name=lagrangianScalarNames[i];IOField< scalar > s(IOobject(name, runTime.timeName(), cloud::prefix, mesh, IOobject::MUST_READ, IOobject::NO_WRITE))
Namespace for OpenFOAM.
const direction noexcept
Definition: Scalar.H:223
Functor wrapper for testing meta-characters.
Definition: regExpCxx.H:147
bool operator()(const char c) const noexcept
Test if character is a regex meta-character.
Definition: regExpCxx.H:149
bool operator()(const std::string &s, const char q='\\') const
Test string for meta-characters.
Definition: regExpCxx.H:155