wordReI.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) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2017-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27\*---------------------------------------------------------------------------*/
28
29// * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
30
31inline bool Foam::wordRe::valid(const char c)
32{
33 // Also accept '{' and '}' (for regex grouping?)
34 return (word::valid(c) || c == '{' || c == '}');
35}
36
37
38// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
39
41:
42 word(),
43 regexPtr_(nullptr)
44{}
45
46
47inline Foam::wordRe::wordRe(const wordRe& str)
48:
49 word(static_cast<const word&>(str))
50{
51 if (str.isPattern())
52 {
53 compile();
54 }
55}
56
57
59:
60 word(std::move(static_cast<word&>(str))),
61 regexPtr_(str.regexPtr_.release())
62{}
63
64
65inline Foam::wordRe::wordRe(const word& str)
66:
67 word(str),
68 regexPtr_(nullptr)
69{}
70
71
73:
74 word(std::move(str)),
75 regexPtr_(nullptr)
76{}
77
78
79inline Foam::wordRe::wordRe(const std::string& str, const compOption opt)
80:
81 word(str, false), // No stripping
82 regexPtr_(nullptr)
83{
84 if (opt != wordRe::LITERAL)
85 {
86 compile(opt);
87 }
88}
89
90
91inline Foam::wordRe::wordRe(const char* str, const compOption opt)
92:
93 word(str, false), // No stripping
94 regexPtr_(nullptr)
95{
96 if (opt != wordRe::LITERAL)
97 {
98 compile(opt);
99 }
100}
101
102
103// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
104
106{
107 return !bool(regexPtr_);
108}
109
110
112{
113 return bool(regexPtr_);
114}
115
116
117inline bool Foam::wordRe::compile(const compOption opt)
118{
119 if (opt != wordRe::LITERAL)
120 {
121 bool comp = false;
122
123 if (opt & wordRe::REGEX)
124 {
125 comp = true;
126 }
127 else if (opt & wordRe::DETECT)
128 {
129 comp = regExp::is_meta(*this) || !string::valid<word>(*this);
130 }
131 else if (opt & wordRe::ICASE)
132 {
133 comp = true;
134 }
135
136 if (comp)
137 {
138 if (!regexPtr_)
139 {
140 regexPtr_.reset(new Foam::regExp());
141 }
142
143 if (!regexPtr_->set(*this, (opt & wordRe::ICASE)))
144 {
145 // Compilation failed
146 regexPtr_.reset(nullptr);
147 }
148
149 return bool(regexPtr_);
150 }
151 }
152
153 // Fall-through behaviour - not a regex
154 regexPtr_.reset(nullptr);
155 return false;
156}
157
158
160{
161 if (!regexPtr_)
162 {
163 regexPtr_.reset(new Foam::regExp());
164 }
165
166 if (!regexPtr_->set(*this))
167 {
168 // Compilation failed
169 regexPtr_.reset(nullptr);
170 }
171
172 return bool(regexPtr_);
173}
174
175
177{
178 regexPtr_.reset(nullptr);
179}
180
181
182inline void Foam::wordRe::uncompile(bool adjust)
183{
184 // Only strip when debug is active (potentially costly operation)
185 if (adjust && isPattern() && word::debug)
186 {
187 string::stripInvalid<word>(*this);
188 }
189 regexPtr_.reset(nullptr);
190}
191
192
194{
195 word::clear();
196 regexPtr_.reset(nullptr);
197}
198
199
200inline bool Foam::wordRe::match(const std::string& text, bool literal) const
201{
202 if (!literal && regexPtr_)
203 {
204 return regexPtr_->match(text); // Match as regex
205 }
206
207 return !compare(text); // Compare as literal
208}
209
210
211inline void Foam::wordRe::set(const std::string& str, const compOption opt)
212{
213 assign(str);
214 compile(opt);
215}
216
217
218inline void Foam::wordRe::set(const char* str, const compOption opt)
219{
220 // No nullptr protection here
221 assign(str);
222 compile(opt);
223}
224
225
226inline void Foam::wordRe::swap(wordRe& str)
227{
228 if (this == &str)
229 {
230 return; // Self-swap is a no-op
231 }
232
233 word::swap(static_cast<word&>(str));
234 regexPtr_.swap(str.regexPtr_);
235}
236
237
238// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
239
240inline bool Foam::wordRe::operator()(const std::string& text) const
241{
242 return match(text);
243}
244
245
246inline void Foam::wordRe::operator=(const wordRe& str)
247{
248 if (this == &str)
249 {
250 return; // Self-assignment is a no-op
251 }
252
253 assign(str);
254 if (str.isPattern())
255 {
256 compile();
257 }
258 else
259 {
260 regexPtr_.reset(nullptr);
261 }
262}
263
264
265inline void Foam::wordRe::operator=(const word& str)
266{
267 assign(str);
268 regexPtr_.reset(nullptr);
269}
270
271
272inline void Foam::wordRe::operator=(const string& str)
273{
274 assign(str);
275 compile(wordRe::DETECT); // Auto-detect regex
276}
277
278
279inline void Foam::wordRe::operator=(const std::string& str)
280{
281 assign(str);
282 compile(wordRe::DETECT); // Auto-detect regex
283}
284
285
286inline void Foam::wordRe::operator=(const char* str)
287{
288 assign(str);
289 compile(wordRe::DETECT); // Auto-detect regex
290}
291
292
294{
295 if (this == &str)
296 {
297 return; // Self-assignment is a no-op
298 }
299
300 clear();
301 swap(str);
302}
303
304
305// ************************************************************************* //
bool valid() const
True if all internal ids are non-negative.
Ostream & operator()() const
Output stream (master only).
Definition: ensightCaseI.H:74
Wrapper around C++11 regular expressions with some additional prefix-handling. The prefix-handling is...
Definition: regExpCxx.H:83
static bool is_meta(const char c) noexcept
Test if character is a regex meta-character.
Definition: regExpCxxI.H:42
bool set() const
Are all the vector set.
Definition: triadI.H:76
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:83
bool isLiteral() const noexcept
The wordRe is a literal string, not a pattern.
Definition: wordReI.H:105
void operator=(const wordRe &str)
Copy assignment, retaining type (literal or regex)
Definition: wordReI.H:246
bool compile()
Compile as regular expression (if possible)
Definition: wordReI.H:159
wordRe()
Default construct, empty literal.
Definition: wordReI.H:40
compOption
Enumeration with compile options.
Definition: wordRe.H:103
@ LITERAL
String literal.
Definition: wordRe.H:104
@ DETECT
Detect if the string contains meta-characters.
Definition: wordRe.H:108
@ REGEX
Regular expression.
Definition: wordRe.H:105
@ ICASE
Ignore case in regular expression.
Definition: wordRe.H:106
bool isPattern() const noexcept
The wordRe is a pattern, not a literal string.
Definition: wordReI.H:111
void clear()
Clear string and regular expression.
Definition: wordReI.H:193
void uncompile()
Mark as literal string, remove any regular expression.
Definition: wordReI.H:176
bool match(const std::string &text, bool literal=false) const
Smart match as regular expression or as a string.
Definition: wordReI.H:200
void swap(wordRe &str)
Swap contents. Self-swapping is a no-op.
Definition: wordReI.H:226
A class for handling words, derived from Foam::string.
Definition: word.H:68
static int debug
Debugging.
Definition: word.H:77
bool
Definition: EEqn.H:20
patchWriters clear()
const direction noexcept
Definition: Scalar.H:223