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-2021 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  re_()
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  re_(std::move(str.re_))
62 {}
63 
64 
65 inline Foam::wordRe::wordRe(const word& str)
66 :
67  word(str)
68 {}
69 
70 
72 :
73  word(std::move(str))
74 {}
75 
76 
77 inline Foam::wordRe::wordRe(const std::string& str, const compOption opt)
78 :
79  word(str, false) // No stripping
80 {
81  if (opt != wordRe::LITERAL)
82  {
83  compile(opt);
84  }
85 }
86 
87 
88 inline Foam::wordRe::wordRe(const char* str, const compOption opt)
89 :
90  word(str, false) // No stripping
91 {
92  if (opt != wordRe::LITERAL)
93  {
94  compile(opt);
95  }
96 }
97 
98 
99 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
100 
101 inline bool Foam::wordRe::isLiteral() const noexcept
102 {
103  return !re_.exists();
104 }
105 
106 
107 inline bool Foam::wordRe::isPattern() const noexcept
108 {
109  return re_.exists();
110 }
111 
112 
113 inline bool Foam::wordRe::compile(const compOption opt)
114 {
115  if (opt != wordRe::LITERAL)
116  {
117  bool comp = false;
118 
119  if (opt & wordRe::REGEX)
120  {
121  comp = true;
122  }
123  else if (opt & wordRe::DETECT)
124  {
125  comp = regExp::is_meta(*this) || !string::valid<word>(*this);
126  }
127  else if (opt & wordRe::ICASE)
128  {
129  comp = true;
130  }
131 
132  if (comp)
133  {
134  return re_.set(*this, (opt & wordRe::ICASE));
135  }
136  }
137 
138  // Fall-through behaviour - not a regex
139  re_.clear();
140  return false;
141 }
142 
143 
145 {
146  return re_.set(*this);
147 }
148 
149 
151 {
152  re_.clear();
153 }
154 
155 
156 inline void Foam::wordRe::uncompile(bool adjust)
157 {
158  // Only strip when debug is active (potentially costly operation)
159  if (adjust && isPattern() && word::debug)
160  {
161  string::stripInvalid<word>(*this);
162  }
163  re_.clear();
164 }
165 
166 
167 inline void Foam::wordRe::clear()
168 {
169  word::clear();
170  re_.clear();
171 }
172 
173 
174 inline bool Foam::wordRe::match(const std::string& text, bool literal) const
175 {
176  if (!literal && re_.exists())
177  {
178  return re_.match(text); // Match as regex
179  }
180 
181  return !compare(text); // Compare as literal
182 }
183 
184 
185 inline void Foam::wordRe::set(const std::string& str, const compOption opt)
186 {
187  assign(str);
188  compile(opt);
189 }
190 
191 
192 inline void Foam::wordRe::set(const char* str, const compOption opt)
193 {
194  assign(str);
195  compile(opt);
196 }
197 
198 
199 inline void Foam::wordRe::swap(wordRe& str)
200 {
201  if (this == &str)
202  {
203  return; // Self-swap is a no-op
204  }
205 
206  word::swap(static_cast<word&>(str));
207  re_.swap(str.re_);
208 }
209 
210 
211 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
212 
213 inline bool Foam::wordRe::operator()(const std::string& text) const
214 {
215  return match(text);
216 }
217 
218 
219 inline void Foam::wordRe::operator=(const wordRe& str)
220 {
221  if (this == &str)
222  {
223  return; // Self-assignment is a no-op
224  }
225 
226  assign(str);
227  if (str.isPattern())
228  {
229  compile();
230  }
231  else
232  {
233  re_.clear();
234  }
235 }
236 
237 
238 inline void Foam::wordRe::operator=(const word& str)
239 {
240  assign(str);
241  re_.clear();
242 }
243 
244 
245 inline void Foam::wordRe::operator=(const string& str)
246 {
247  assign(str);
248  compile(wordRe::DETECT); // Auto-detect regex
249 }
250 
251 
252 inline void Foam::wordRe::operator=(const std::string& str)
253 {
254  assign(str);
255  compile(wordRe::DETECT); // Auto-detect regex
256 }
257 
258 
259 inline void Foam::wordRe::operator=(const char* str)
260 {
261  assign(str);
262  compile(wordRe::DETECT); // Auto-detect regex
263 }
264 
265 
266 inline void Foam::wordRe::operator=(wordRe&& str)
267 {
268  if (this == &str)
269  {
270  return; // Self-assignment is a no-op
271  }
272 
273  clear();
274  swap(str);
275 }
276 
277 
278 // ************************************************************************* //
Foam::wordRe::isPattern
bool isPattern() const noexcept
The wordRe is treated as a pattern, not as literal string.
Definition: wordReI.H:107
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::wordRe::operator=
void operator=(const wordRe &str)
Copy assignment, retaining type (literal or regex)
Definition: wordReI.H:219
Foam::wordRe::REGEX
Regular expression.
Definition: wordRe.H:105
Foam::wordRe::DETECT
Detect if the string contains meta-characters.
Definition: wordRe.H:108
Foam::wordRe::isLiteral
bool isLiteral() const noexcept
The wordRe is treated as literal string, not as pattern.
Definition: wordReI.H:101
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:80
Foam::wordRe::compile
bool compile()
Compile as regular expression.
Definition: wordReI.H:144
Foam::wordRe::wordRe
wordRe()
Default construct, empty literal.
Definition: wordReI.H:40
Foam::wordRe::valid
static bool valid(const char c)
Test for valid wordRe character?
Definition: wordReI.H:31
Foam::wordRe::LITERAL
String literal.
Definition: wordRe.H:104
Foam::word::valid
static bool valid(char c)
Is this character valid for a word?
Definition: wordI.H:59
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:185
Foam::wordRe::uncompile
void uncompile()
Mark as literal string, remove any regular expression.
Definition: wordReI.H:150
Foam::wordRe::ICASE
Ignore case in regular expression.
Definition: wordRe.H:106
Foam::word::debug
static int debug
Debugging.
Definition: word.H:77
Foam::wordRe::clear
void clear()
Clear string and regular expression.
Definition: wordReI.H:167
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:76
clear
patchWriters clear()
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:224
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::regExpCxx::is_meta
static bool is_meta(const char c) noexcept
Test if character is a regex meta-character.
Definition: regExpCxxI.H:42
Foam::wordRe::operator()
bool operator()(const std::string &text) const
Perform smart match on text, as per match()
Definition: wordReI.H:213
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:174
Foam::wordRe::compOption
compOption
Enumeration with compile options.
Definition: wordRe.H:102
Foam::wordRe::swap
void swap(wordRe &str)
Swap contents. Self-swapping is a no-op.
Definition: wordReI.H:199