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 -------------------------------------------------------------------------------
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::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 
47 inline 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 
65 inline 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 
79 inline 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 
91 inline 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 * * * * * * * * * * * * * //
105 inline bool Foam::wordRe::isLiteral() const noexcept
106 {
107  return !bool(regexPtr_);
108 }
109 
111 inline bool Foam::wordRe::isPattern() const noexcept
112 {
113  return bool(regexPtr_);
114 }
115 
116 
117 inline 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  }
153  // Fall-through behaviour - not a regex
154  regexPtr_.reset(nullptr);
155  return false;
156 }
157 
158 
159 inline bool Foam::wordRe::compile()
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 
176 inline void Foam::wordRe::uncompile()
177 {
178  regexPtr_.reset(nullptr);
179 }
180 
181 
182 inline 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 
193 inline void Foam::wordRe::clear()
194 {
195  word::clear();
196  regexPtr_.reset(nullptr);
197 }
198 
199 
200 inline 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 
211 inline void Foam::wordRe::set(const std::string& str, const compOption opt)
212 {
213  assign(str);
214  compile(opt);
215 }
216 
217 
218 inline 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 
226 inline 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 * * * * * * * * * * * * * //
240 inline bool Foam::wordRe::operator()(const std::string& text) const
241 {
242  return match(text);
243 }
244 
245 
246 inline 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 
265 inline void Foam::wordRe::operator=(const word& str)
266 {
267  assign(str);
268  regexPtr_.reset(nullptr);
269 }
270 
271 
272 inline void Foam::wordRe::operator=(const string& str)
273 {
274  assign(str);
275  compile(wordRe::DETECT); // Auto-detect regex
276 }
277 
278 
279 inline void Foam::wordRe::operator=(const std::string& str)
280 {
281  assign(str);
282  compile(wordRe::DETECT); // Auto-detect regex
283 }
284 
285 
286 inline void Foam::wordRe::operator=(const char* str)
287 {
288  assign(str);
289  compile(wordRe::DETECT); // Auto-detect regex
290 }
291 
292 
293 inline void Foam::wordRe::operator=(wordRe&& str)
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 match(const UList< wordRe > &patterns, const std::string &text)
Return true if text matches one of the regular expressions.
Definition: stringOps.H:77
static bool is_meta(const char c) noexcept
Test if character is a regex meta-character.
Definition: regExpPosix.H:134
void swap(wordRe &str)
Swap contents. Self-swapping is a no-op.
Definition: wordReI.H:219
compOption
Enumeration with compile options.
Definition: wordRe.H:107
bool compile()
Compile as regular expression (if possible)
Definition: wordReI.H:152
Ignore case in regular expression.
Definition: wordRe.H:111
bool operator()(const std::string &text) const
Perform smart match on text, as per match()
Definition: wordReI.H:233
void operator=(const wordRe &str)
Copy assignment, retaining type (literal or regex)
Definition: wordReI.H:239
Regular expression.
Definition: wordRe.H:110
A class for handling words, derived from Foam::string.
Definition: word.H:63
wordRe()
Default construct, empty literal.
Definition: wordReI.H:33
static int debug
Debugging.
Definition: word.H:79
bool isLiteral() const noexcept
The wordRe is a literal string, not a pattern.
Definition: wordReI.H:98
patchWriters clear()
static bool valid(char c)
Is this character valid for a word?
Definition: wordI.H:52
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings...
Definition: wordRe.H:78
void clear()
Clear string and regular expression.
Definition: wordReI.H:186
static bool valid(const char c)
Test for valid wordRe character?
Definition: wordReI.H:24
const direction noexcept
Definition: Scalar.H:258
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:28
Wrapper around POSIX extended regular expressions with some additional prefix-handling. The prefix-handling is loosely oriented on PCRE regular expressions and provides a simple means of tuning the expressions.
Definition: regExpPosix.H:80
const dimensionedScalar c
Speed of light in a vacuum.
void set(const std::string &str, const compOption opt=DETECT)
Copy string, auto-test for regular expression or other options.
Definition: wordReI.H:204
void uncompile()
Mark as literal string, remove any regular expression.
Definition: wordReI.H:169
String literal.
Definition: wordRe.H:109
bool match(const std::string &text, bool literal=false) const
Smart match as regular expression or as a string.
Definition: wordReI.H:193
void swap(std::string &str)
Swap contents. Self-swapping is a no-op.
Definition: stringI.H:223
bool isPattern() const noexcept
The wordRe is a pattern, not a literal string.
Definition: wordReI.H:104
Detect if the string contains meta-characters.
Definition: wordRe.H:113