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-2019 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
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 
31 inline bool Foam::wordRe::meta(char c)
32 {
33  return regExp::meta(c);
34 }
35 
36 
37 inline bool Foam::wordRe::valid(char c)
38 {
39  return keyType::valid(c);
40 }
41 
42 
43 inline bool Foam::wordRe::isPattern(const std::string& str)
44 {
45  return string::meta<regExp>(str);
46 }
47 
48 
49 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
50 
52 :
53  word(),
54  re_()
55 {}
56 
57 
58 inline Foam::wordRe::wordRe(const wordRe& str)
59 :
60  word(str, false),
61  re_()
62 {
63  if (str.isPattern())
64  {
65  compile();
66  }
67 }
68 
69 
71 :
72  word(std::move(static_cast<word&>(str))),
73  re_(std::move(str.re_))
74 {}
75 
76 
77 inline Foam::wordRe::wordRe(const keyType& str)
78 :
79  word(str, false),
80  re_()
81 {
82  if (str.isPattern())
83  {
84  compile();
85  }
86 }
87 
88 
89 inline Foam::wordRe::wordRe(const char* str)
90 :
91  word(str, false),
92  re_()
93 {}
94 
95 
96 inline Foam::wordRe::wordRe(const std::string& str)
97 :
98  word(str, false),
99  re_()
100 {}
101 
102 
103 inline Foam::wordRe::wordRe(const string& str)
104 :
105  word(str, false),
106  re_()
107 {}
108 
109 
110 inline Foam::wordRe::wordRe(const word& str)
111 :
112  word(str, false),
113  re_()
114 {}
115 
116 
117 inline Foam::wordRe::wordRe(const keyType& str, const compOption opt)
118 :
119  word(str, false),
120  re_()
121 {
122  if (str.isPattern())
123  {
124  compile(opt);
125  }
126 }
127 
128 
129 inline Foam::wordRe::wordRe(const char* str, const compOption opt)
130 :
131  wordRe(str)
132 {
133  compile(opt);
134 }
135 
136 
137 inline Foam::wordRe::wordRe(const std::string& str, const compOption opt)
138 :
139  wordRe(str)
140 {
141  compile(opt);
142 }
143 
144 
145 inline Foam::wordRe::wordRe(const string& str, const compOption opt)
146 :
147  wordRe(str)
148 {
149  compile(opt);
150 }
151 
152 
153 inline Foam::wordRe::wordRe(const word& str, const compOption opt)
154 :
155  wordRe(str)
156 {
157  compile(opt);
158 }
159 
160 
161 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
162 
163 inline bool Foam::wordRe::isLiteral() const
164 {
165  return !re_.exists();
166 }
167 
168 
169 inline bool Foam::wordRe::isPattern() const
170 {
171  return re_.exists();
172 }
173 
174 
175 inline bool Foam::wordRe::compile(const compOption opt)
176 {
177  if (opt)
178  {
179  bool comp = false;
180 
181  if (opt & wordRe::REGEX)
182  {
183  comp = true;
184  }
185  else if (opt & wordRe::DETECT)
186  {
187  comp = string::meta<regExp>(*this) || !string::valid<word>(*this);
188  }
189  else if (opt & wordRe::ICASE)
190  {
191  comp = true;
192  }
193 
194  if (comp)
195  {
196  return re_.set(*this, (opt & wordRe::ICASE));
197  }
198  }
199 
200  // Fall-through behaviour - not a regex
201  re_.clear();
202  return false;
203 }
204 
205 
207 {
208  return re_.set(*this);
209 }
210 
211 
212 inline void Foam::wordRe::uncompile(bool adjust)
213 {
214  // Only strip when debug is active (potentially costly operation)
215  if (re_.clear() && adjust && word::debug)
216  {
217  string::stripInvalid<word>(*this);
218  }
219 }
220 
221 
222 inline void Foam::wordRe::clear()
223 {
224  word::clear();
225  re_.clear();
226 }
227 
228 
229 inline bool Foam::wordRe::match(const std::string& text, bool literal) const
230 {
231  if (!literal && re_.exists())
232  {
233  return re_.match(text); // Match as regex
234  }
235 
236  return !compare(text); // Compare as literal
237 }
238 
239 
241 {
242  return string::quotemeta<regExp>(*this);
243 }
244 
245 
246 inline void Foam::wordRe::set(const std::string& str, const compOption opt)
247 {
248  assign(str);
249  compile(opt);
250 }
251 
252 
253 inline void Foam::wordRe::set(const char* str, const compOption opt)
254 {
255  assign(str);
256  compile(opt);
257 }
258 
259 
260 inline void Foam::wordRe::swap(wordRe& str)
261 {
262  if (this == &str)
263  {
264  return; // Self-swap is a no-op
265  }
266 
267  word::swap(static_cast<word&>(str));
268  re_.swap(str.re_);
269 }
270 
271 
272 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
273 
274 inline bool Foam::wordRe::operator()(const std::string& text) const
275 {
276  return match(text);
277 }
278 
279 
280 inline void Foam::wordRe::operator=(const wordRe& str)
281 {
282  if (this == &str)
283  {
284  return; // Self-assignment is a no-op
285  }
286 
287  assign(str);
288  if (str.isPattern())
289  {
290  compile();
291  }
292  else
293  {
294  re_.clear();
295  }
296 }
297 
298 
299 inline void Foam::wordRe::operator=(const word& str)
300 {
301  assign(str);
302  re_.clear();
303 }
304 
305 
306 inline void Foam::wordRe::operator=(const keyType& str)
307 {
308  assign(str);
309  if (str.isPattern())
310  {
311  compile();
312  }
313  else
314  {
315  re_.clear();
316  }
317 }
318 
319 
320 inline void Foam::wordRe::operator=(const string& str)
321 {
322  assign(str);
323  compile(wordRe::DETECT); // Auto-detect regex
324 }
325 
326 
327 inline void Foam::wordRe::operator=(const std::string& str)
328 {
329  assign(str);
330  compile(wordRe::DETECT); // Auto-detect regex
331 }
332 
333 
334 inline void Foam::wordRe::operator=(const char* str)
335 {
336  assign(str);
337  compile(wordRe::DETECT); // Auto-detect regex
338 }
339 
340 
341 inline void Foam::wordRe::operator=(wordRe&& str)
342 {
343  if (this == &str)
344  {
345  return; // Self-assignment is a no-op
346  }
347 
348  clear();
349  swap(str);
350 }
351 
352 
353 // ************************************************************************* //
Foam::keyType::valid
static bool valid(char c)
Is this character valid for a keyType?
Definition: keyTypeI.H:33
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::wordRe::valid
static bool valid(char c)
Is this character valid for a wordRe?
Definition: wordReI.H:37
Foam::wordRe::operator=
void operator=(const wordRe &str)
Copy assignment, retaining type (literal or regex)
Definition: wordReI.H:280
Foam::keyType::isPattern
bool isPattern() const
The keyType is treated as a pattern, not as literal string.
Definition: keyTypeI.H:128
Foam::wordRe::REGEX
Regular expression.
Definition: wordRe.H:97
Foam::wordRe::DETECT
Detect if the string contains meta-characters.
Definition: wordRe.H:100
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:72
Foam::wordRe::compile
bool compile()
Compile the regular expression.
Definition: wordReI.H:206
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:60
Foam::wordRe::wordRe
wordRe()
Construct null.
Definition: wordReI.H:51
Foam::wordRe::isPattern
static bool isPattern(const std::string &str)
Test string for regular expression meta characters.
Definition: wordReI.H:43
Foam::wordRe::set
void set(const std::string &str, const compOption opt=DETECT)
Copy string, auto-test for regular expression or other options.
Definition: wordReI.H:246
Foam::wordRe::quotemeta
string quotemeta() const
Return a string with quoted meta-characters.
Definition: wordReI.H:240
Foam::wordRe::ICASE
Ignore case in regular expression.
Definition: wordRe.H:98
Foam::word::debug
static int debug
Debugging.
Definition: word.H:74
Foam::wordRe::clear
void clear()
Clear string and regular expression.
Definition: wordReI.H:222
Foam::stringOps::match
bool match(const UList< wordRe > &patterns, const std::string &text)
Return true if text matches one of the regular expressions.
Definition: stringOps.H:75
Foam::wordRe::isLiteral
bool isLiteral() const
The wordRe is treated as literal string, not as pattern.
Definition: wordReI.H:163
clear
patchWriters clear()
Foam::regExpCxx::meta
static bool meta(const char c)
Test if character appears to be a regular expression meta-character.
Definition: regExpCxxI.H:42
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::string::swap
void swap(std::string &str)
Swap contents. Self-swapping is a no-op.
Definition: stringI.H:266
Foam::FieldOps::assign
void assign(Field< Tout > &result, const Field< T1 > &a, const UnaryOp &op)
Populate a field as the result of a unary operation on an input.
Definition: FieldOps.C:35
Foam::wordRe::meta
static bool meta(char c)
Is this a meta character?
Definition: wordReI.H:31
Foam::wordRe::uncompile
void uncompile(bool adjust=false)
Make wordRe a literal again, instead of a regular expression.
Definition: wordReI.H:212
Foam::wordRe::operator()
bool operator()(const std::string &text) const
Perform smart match on text, as per match()
Definition: wordReI.H:274
Foam::wordRe::match
bool match(const std::string &text, bool literal=false) const
Smart match as regular expression or as a string.
Definition: wordReI.H:229
Foam::wordRe::compOption
compOption
Enumeration with compile options.
Definition: wordRe.H:94
Foam::wordRe::isPattern
bool isPattern() const
The wordRe is treated as a pattern, not as literal string.
Definition: wordReI.H:169
Foam::wordRe::swap
void swap(wordRe &str)
Swap contents. Self-swapping is a no-op.
Definition: wordReI.H:260