Loading...
Searching...
No Matches
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_view>
73#include <string>
74
75// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
76
77namespace Foam
78{
80/*---------------------------------------------------------------------------*\
81 Class regExpCxx Declaration
82\*---------------------------------------------------------------------------*/
83
84class regExpCxx
85{
86 // Data Types
87
88 //- Simple control types
89 enum ctrlType { EMPTY = 0, NORMAL = 1, NEGATED = 2 };
90
91
92 // Private Data
93
94 //- Regular expression (using char type)
95 std::regex re_;
96
97 //- Track if input pattern is non-empty, negated etc.
98 unsigned char ctrl_;
99
100
101 // Private Member Functions
102
103 //- Select grammar based on regExpCxx optimisationSwitch
104 // 0 = extended, 1 = ECMAScript
105 static inline std::regex::flag_type syntax();
106
107 //- Assign pattern
108 bool set_pattern(const char* pattern, size_t len, bool ignoreCase);
109
110
111public:
112
113 // Public Types
114
115 //- Type for matches
116 typedef std::smatch results_type;
117
118
119 // Static Member Data
120
121 //- The default grammar (extended | ECMAScript).
122 static int grammar;
123
125 // Static Member Functions
126
127 //- Test if character is a regex meta-character
128 // \return True if character is one of the following:
129 // - any character: '.' \n
130 // - quantifiers: '*', '+', '?' \n
131 // - grouping: '(', '|', ')' \n
132 // - range: '[', ']' \n
133 //
134 // \note Regex bounds '{', '}' are not considered
135 inline static bool is_meta(const char c) noexcept;
136
137 //- Test if string contains any (unquoted) meta-characters
138 inline static bool is_meta
139 (
140 const std::string& str,
141 const char quote = '\\'
142 );
143
144
145 // Public Classes
146
147 //- Functor wrapper for testing meta-characters
148 struct meta
149 {
150 //- Test if character is a regex meta-character
151 bool operator()(const char c) const noexcept
152 {
153 return is_meta(c);
154 }
155
156 //- Test string for meta-characters
157 bool operator()(const std::string& s, const char q = '\\') const
158 {
159 return is_meta(s, q);
160 }
161 };
162
163
164 // Constructors
166 //- Default construct
167 inline regExpCxx();
168
169 //- Copy construct
170 inline regExpCxx(const regExpCxx& rgx);
171
172 //- Move construct
173 inline regExpCxx(regExpCxx&& rgx) noexcept;
174
175 //- Construct from character array, optionally ignore case
176 inline explicit regExpCxx
177 (
178 const char* pattern,
179 const bool ignoreCase = false
180 );
181
182 //- Construct from string, optionally ignore case
183 inline explicit regExpCxx
184 (
185 const std::string& pattern,
186 const bool ignoreCase = false
187 );
188
189
190 //- Destructor
191 ~regExpCxx() = default;
192
193
194 // Member Functions
195
196 // Access
197
198 //- True if expression is empty
199 inline bool empty() const noexcept;
200
201 //- True if expression is non-empty
202 inline bool exists() const noexcept;
203
204 //- True if pattern matching is negated
205 inline bool negated() const noexcept;
206
207 //- Change pattern negation, return previous value
208 inline bool negate(bool on) noexcept;
209
210 //- The number of capture groups for a non-empty,
211 //- non-negated expressions
212 inline unsigned ngroups() const;
213
214 // \return True if the pattern was set with ignore-case.
215 inline bool nocase() const;
216
217
218 // Editing
219
220 //- Clear expression.
221 // \return True if expression had existed prior to the clear.
222 inline bool clear();
223
224 //- Swap contents
225 inline void swap(regExpCxx& rgx);
226
227 //- Compile pattern into a regular expression, optionally ignore case.
228 // \return True if the pattern was compiled
229 inline bool set(const char* pattern, bool ignoreCase=false);
230
231 //- Compile pattern into a regular expression, optionally ignore case.
232 // \return True if the pattern was compiled
233 inline bool set(const std::string& pattern, bool ignoreCase=false);
234
235
236 // Matching/Searching
237
238 //- Find position within the text.
239 // \return The index where it begins or std::string::npos if not found
240 //
241 // \note does not properly work with negated regex!
242 inline std::string::size_type find(const std::string& text) const;
243
244 //- True if the regex matches the entire text.
245 // The begin-of-line (^) and end-of-line ($) anchors are implicit
246 inline bool match(const std::string& text) const;
247
248 //- True if the regex matches the text, set the matches.
249 // The first group starts at index 1 (0 is the entire match).
250 // The begin-of-line (^) and end-of-line ($) anchors are implicit
251 //
252 // \note does not properly work with negated regex!
253 inline bool match(const std::string& text, results_type& matches) const;
254
255 //- Return true if the regex was found within the text
256 inline bool search(const std::string& text) const;
257
258
259 // Member Operators
260
261 //- Perform match on text
262 inline bool operator()(const std::string& text) const;
263
264 //- Copy assignment
265 inline void operator=(const regExpCxx& rgx);
266
267 //- Move assignment
268 inline void operator=(regExpCxx&& rgx);
269
270 //- Assign and compile pattern from a character array.
271 // Matching is case sensitive.
272 inline void operator=(const char* pattern);
273
274 //- Assign and compile pattern from string.
275 // Matching is case sensitive.
276 inline void operator=(const std::string& pattern);
277};
278
279
280// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
281
282} // End namespace Foam
283
284// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
285
286#include "regExpCxxI.H"
287
288#endif
289
290// ************************************************************************* //
graph_traits< Graph >::vertices_size_type size_type
Wrapper around C++11 regular expressions with some additional prefix-handling. The prefix-handling is...
Definition regExpCxx.H:80
bool nocase() const
Definition regExpCxxI.H:171
std::smatch results_type
Type for matches.
Definition regExpCxx.H:124
bool empty() const noexcept
True if expression is empty.
Definition regExpCxxI.H:126
regExpCxx()
Default construct.
Definition regExpCxxI.H:75
std::string::size_type find(const std::string &text) const
Find position within the text.
Definition regExpCxxI.H:226
bool set(const char *pattern, bool ignoreCase=false)
Compile pattern into a regular expression, optionally ignore case.
Definition regExpCxxI.H:202
bool exists() const noexcept
True if expression is non-empty.
Definition regExpCxxI.H:132
bool match(const std::string &text) const
True if the regex matches the entire text.
Definition regExpCxxI.H:282
bool search(const std::string &text) const
Return true if the regex was found within the text.
Definition regExpCxxI.H:268
~regExpCxx()=default
Destructor.
bool negated() const noexcept
True if pattern matching is negated.
Definition regExpCxxI.H:138
bool clear()
Clear expression.
Definition regExpCxxI.H:177
bool negate(bool on) noexcept
Change pattern negation, return previous value.
Definition regExpCxxI.H:144
static bool is_meta(const char c) noexcept
Test if character is a regex meta-character.
Definition regExpCxxI.H:35
void swap(regExpCxx &rgx)
Swap contents.
Definition regExpCxxI.H:191
static int grammar
The default grammar (extended | ECMAScript).
Definition regExpCxx.H:132
unsigned ngroups() const
The number of capture groups for a non-empty, non-negated expressions.
Definition regExpCxxI.H:164
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 scalarImpl.H:265
Functor wrapper for testing meta-characters.
Definition regExpCxx.H:166
bool operator()(const char c) const noexcept
Test if character is a regex meta-character.
Definition regExpCxx.H:170
bool operator()(const std::string &s, const char q='\\') const
Test string for meta-characters.
Definition regExpCxx.H:178