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 | Copyright (C) 2011-2013 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 & wordRe::REGEXP)
132  {
133  doCompile = true;
134  }
135  else if (opt & wordRe::DETECT)
136  {
137  if (string::meta<regExp>(*this) || !string::valid<word>(*this))
138  {
139  doCompile = true;
140  }
141  }
142  else if (opt & wordRe::NOCASE)
143  {
144  doCompile = true;
145  }
146 
147 
148  if (doCompile)
149  {
150  re_.set(*this, (opt & wordRe::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 const Foam::wordRe& 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  return *this;
252 }
253 
254 
255 inline const Foam::wordRe& Foam::wordRe::operator=(const word& str)
256 {
257  word::operator=(str);
258  re_.clear();
259  return *this;
260 }
261 
262 
264 {
265  string::operator=(str);
266  if (str.isPattern())
267  {
268  compile();
269  }
270  return *this;
271 }
272 
273 
274 inline const Foam::wordRe& Foam::wordRe::operator=(const string& str)
275 {
276  string::operator=(str);
277  compile(DETECT); // auto-detect regex
278  return *this;
279 }
280 
281 
282 inline const Foam::wordRe& Foam::wordRe::operator=(const std::string& str)
283 {
284  string::operator=(str);
285  compile(DETECT); // auto-detect regex
286  return *this;
287 }
288 
289 
290 inline const Foam::wordRe& Foam::wordRe::operator=(const char* str)
291 {
292  string::operator=(str);
293  compile(DETECT); // auto-detect regex
294  return *this;
295 }
296 
297 
298 // ************************************************************************* //
static int debug
Definition: word.H:74
bool isPattern() const
Should be treated as a match rather than a literal string?
Definition: wordReI.H:121
static bool meta(char c)
Is character a regular expression meta-character?
Definition: regExp.H:98
compOption
Enumeration with compile options.
Definition: wordRe.H:95
bool match(const std::string &, bool literalMatch=false) const
Smart match as regular expression or as a string.
Definition: wordReI.H:202
A class for handling words, derived from string.
Definition: word.H:59
A class for handling character strings derived from std::string.
Definition: string.H:74
bool isPattern() const
Should be treated as a match rather than a literal string.
Definition: keyTypeI.H:76
bool match(const std::string &) const
Return true if it matches the entire string.
Definition: regExp.C:203
A class for handling keywords in dictionaries.
Definition: keyType.H:56
const word & operator=(const word &)
Definition: wordI.H:134
void set(const std::string &, const compOption=DETECT)
Copy string, auto-test for regular expression or other options.
Definition: wordReI.H:223
void set(const char *, const bool ignoreCase=false) const
Compile pattern into a regular expression,.
Definition: regExp.C:116
bool recompile() const
Recompile an existing regular expression.
Definition: wordReI.H:168
bool compile() const
Compile the regular expression.
Definition: wordReI.H:161
wordRe()
Construct null.
Definition: wordReI.H:42
bool exists() const
Does a precompiled expression exist?
Definition: regExp.H:137
static bool isPattern(const string &)
Test string for regular expression meta characters.
Definition: wordReI.H:34
const dimensionedScalar c
Speed of light in a vacuum.
void clear()
Clear string and precompiled regular expression.
Definition: wordReI.H:195
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:74
UEqn clear()
bool clear() const
Release precompiled expression.
Definition: regExp.C:171
void uncompile(const bool doStripInvalid=false) const
Frees precompiled regular expression, making wordRe a literal.
Definition: wordReI.H:179
string quotemeta() const
Return a string with quoted meta-characters.
Definition: wordReI.H:217
const wordRe & operator=(const wordRe &)
Assign copy.
Definition: wordReI.H:239
static bool meta(char)
Is this a meta character?
Definition: wordReI.H:28