wordReI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2018 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 // * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * * //
27 
28 inline bool Foam::wordRe::meta(char c)
29 {
30  return regExp::meta(c);
31 }
32 
33 
34 inline bool Foam::wordRe::isPattern(const string& str)
35 {
36  return string::meta<regExp>(str);
37 }
38 
39 
40 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
41 
43 :
44  word(),
45  re_()
46 {}
47 
48 
49 inline Foam::wordRe::wordRe(const wordRe& str)
50 :
51  word(str),
52  re_()
53 {
54  if (str.isPattern())
55  {
56  compile();
57  }
58 }
59 
60 
61 inline Foam::wordRe::wordRe(const word& str)
62 :
63  word(str),
64  re_()
65 {}
66 
67 
68 inline Foam::wordRe::wordRe(const keyType& str)
69 :
70  word(str, false),
71  re_()
72 {
73  if (str.isPattern())
74  {
75  compile();
76  }
77 }
78 
79 
80 inline Foam::wordRe::wordRe(const keyType& str, const compOption opt)
81 :
82  word(str, false),
83  re_()
84 {
85  if (str.isPattern())
86  {
87  compile(opt);
88  }
89 }
90 
91 
92 inline Foam::wordRe::wordRe(const char* str, const compOption opt)
93 :
94  word(str, false),
95  re_()
96 {
97  compile(opt);
98 }
99 
100 
101 inline Foam::wordRe::wordRe(const string& str, const compOption opt)
102 :
103  word(str, false),
104  re_()
105 {
106  compile(opt);
107 }
108 
109 
110 inline Foam::wordRe::wordRe(const std::string& str, const compOption opt)
111 :
112  word(str, false),
113  re_()
114 {
115  compile(opt);
116 }
117 
118 
119 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
120 
121 inline bool Foam::wordRe::isPattern() const
122 {
123  return re_.exists();
124 }
125 
126 
127 inline bool Foam::wordRe::compile(const compOption opt) const
128 {
129  bool doCompile = false;
130 
131  if (opt & compOption::regExp)
132  {
133  doCompile = true;
134  }
135  else if (opt & compOption::detect)
136  {
137  if (string::meta<regExp>(*this) || !string::valid<word>(*this))
138  {
139  doCompile = true;
140  }
141  }
142  else if (opt & compOption::noCase)
143  {
144  doCompile = true;
145  }
146 
147 
148  if (doCompile)
149  {
150  re_.set(*this, (opt & compOption::noCase));
151  }
152  else
153  {
154  re_.clear();
155  }
156 
157  return re_.exists();
158 }
159 
160 
161 inline bool Foam::wordRe::compile() const
162 {
163  re_ = *this;
164  return re_.exists();
165 }
166 
167 
168 inline bool Foam::wordRe::recompile() const
169 {
170  if (re_.exists())
171  {
172  re_ = *this;
173  }
174 
175  return re_.exists();
176 }
177 
178 
179 inline void Foam::wordRe::uncompile(const bool doStripInvalid) const
180 {
181  if (re_.clear())
182  {
183  // skip stripping unless debug is active to avoid costly operations
184  if (word::debug && doStripInvalid)
185  {
186  string::stripInvalid<word>
187  (
188  const_cast<word&>(static_cast<const word&>(*this))
189  );
190  }
191  }
192 }
193 
194 
195 inline void Foam::wordRe::clear()
196 {
197  word::clear();
198  re_.clear();
199 }
200 
201 
202 inline bool Foam::wordRe::match(const std::string& str, bool literalMatch) const
203 {
204  if (literalMatch || !re_.exists())
205  {
206  // check as string
207  return (str == *this);
208  }
209  else
210  {
211  // check as regex
212  return re_.match(str);
213  }
214 }
215 
216 
218 {
219  return string::quotemeta<regExp>(*this);
220 }
221 
222 
223 inline void Foam::wordRe::set(const std::string& str, const compOption opt)
224 {
225  string::operator=(str);
226  compile(opt);
227 }
228 
229 
230 inline void Foam::wordRe::set(const char* str, const compOption opt)
231 {
232  string::operator=(str);
233  compile(opt);
234 }
235 
236 
237 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
238 
239 inline void Foam::wordRe::operator=(const wordRe& str)
240 {
241  string::operator=(str);
242 
243  if (str.isPattern())
244  {
245  compile();
246  }
247  else
248  {
249  re_.clear();
250  }
251 }
252 
253 
254 inline void Foam::wordRe::operator=(const word& str)
255 {
256  word::operator=(str);
257  re_.clear();
258 }
259 
260 
261 inline void Foam::wordRe::operator=(const keyType& str)
262 {
263  string::operator=(str);
264  if (str.isPattern())
265  {
266  compile();
267  }
268 }
269 
270 
271 inline void Foam::wordRe::operator=(const string& str)
272 {
273  string::operator=(str);
274  compile(compOption::detect); // auto-detect regex
275 }
276 
277 
278 inline void Foam::wordRe::operator=(const std::string& str)
279 {
280  string::operator=(str);
281  compile(compOption::detect); // auto-detect regex
282 }
283 
284 
285 inline void Foam::wordRe::operator=(const char* str)
286 {
287  string::operator=(str);
288  compile(compOption::detect); // auto-detect regex
289 }
290 
291 
292 inline int Foam::operator&
293 (
294  const wordRe::compOption co1,
295  const wordRe::compOption co2
296 )
297 {
298  return int(co1) & int(co2);
299 }
300 
301 
302 // ************************************************************************* //
A class for handling keywords in dictionaries.
Definition: keyType.H:64
bool clear() const
Release precompiled expression.
Definition: regExp.C:169
bool match(const std::string &) const
Return true if it matches the entire string.
Definition: regExp.C:201
bool recompile() const
Recompile an existing regular expression.
Definition: wordReI.H:168
void operator=(const wordRe &)
Assign copy.
Definition: wordReI.H:239
tUEqn clear()
static bool meta(char c)
Is character a regular expression meta-character?
Definition: regExp.H:92
treat as regular expression
compOption
Enumeration with compile options.
Definition: wordRe.H:95
void operator=(const word &)
Definition: wordI.H:132
bool isPattern() const
Should be treated as a match rather than a literal string?
Definition: wordReI.H:121
static bool meta(char)
Is this a meta character?
Definition: wordReI.H:28
bool compile() const
Compile the regular expression.
Definition: wordReI.H:161
bool exists() const
Does a precompiled expression exist?
Definition: regExp.H:134
void set(const std::string &, const compOption=compOption::detect)
Copy string, auto-test for regular expression or other options.
Definition: wordReI.H:223
A class for handling words, derived from string.
Definition: word.H:59
static bool isPattern(const string &)
Test string for regular expression meta characters.
Definition: wordReI.H:34
wordRe()
Construct null.
Definition: wordReI.H:42
static int debug
Definition: word.H:74
detect if the string contains meta-characters
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:74
void clear()
Clear string and precompiled regular expression.
Definition: wordReI.H:195
string quotemeta() const
Return a string with quoted meta-characters.
Definition: wordReI.H:217
ignore case in regular expression
const dimensionedScalar c
Speed of light in a vacuum.
void operator=(const string &)
Definition: stringI.H:229
void uncompile(const bool doStripInvalid=false) const
Frees precompiled regular expression, making wordRe a literal.
Definition: wordReI.H:179
void set(const char *, const bool ignoreCase=false) const
Compile pattern into a regular expression,.
Definition: regExp.C:116
A class for handling character strings derived from std::string.
Definition: string.H:74
bool match(const std::string &, bool literalMatch=false) const
Smart match as regular expression or as a string.
Definition: wordReI.H:202
bool isPattern() const
Should be treated as a match rather than a literal string.
Definition: keyTypeI.H:76