FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 char* str, const compOption opt)
69 :
70  word(str, false),
71  re_()
72 {
73  compile(opt);
74 }
75 
76 
77 inline Foam::wordRe::wordRe(const string& str, const compOption opt)
78 :
79  word(str, false),
80  re_()
81 {
82  compile(opt);
83 }
84 
85 
86 inline Foam::wordRe::wordRe(const std::string& str, const compOption opt)
87 :
88  word(str, false),
89  re_()
90 {
91  compile(opt);
92 }
93 
94 
95 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
96 
97 inline bool Foam::wordRe::isPattern() const
98 {
99  return re_.exists();
100 }
101 
102 
103 inline bool Foam::wordRe::compile(const compOption opt) const
104 {
105  bool doCompile = false;
106 
107  if (opt & wordRe::REGEXP)
108  {
109  doCompile = true;
110  }
111  else if (opt & wordRe::DETECT)
112  {
113  if (string::meta<regExp>(*this) || !string::valid<word>(*this))
114  {
115  doCompile = true;
116  }
117  }
118  else if (opt & wordRe::NOCASE)
119  {
120  doCompile = true;
121  }
122 
123 
124  if (doCompile)
125  {
126  re_.set(*this, (opt & wordRe::NOCASE));
127  }
128  else
129  {
130  re_.clear();
131  }
132 
133  return re_.exists();
134 }
135 
136 
137 inline bool Foam::wordRe::compile() const
138 {
139  re_ = *this;
140  return re_.exists();
141 }
142 
143 
144 inline bool Foam::wordRe::recompile() const
145 {
146  if (re_.exists())
147  {
148  re_ = *this;
149  }
150 
151  return re_.exists();
152 }
153 
154 
155 inline void Foam::wordRe::uncompile(const bool doStripInvalid) const
156 {
157  if (re_.clear())
158  {
159  // skip stripping unless debug is active to avoid costly operations
160  if (word::debug && doStripInvalid)
161  {
162  string::stripInvalid<word>
163  (
164  const_cast<word&>(static_cast<const word&>(*this))
165  );
166  }
167  }
168 }
169 
170 
171 inline void Foam::wordRe::clear()
172 {
173  word::clear();
174  re_.clear();
175 }
176 
177 
178 inline bool Foam::wordRe::match(const string& str, bool literalMatch) const
179 {
180  if (literalMatch || !re_.exists())
181  {
182  // check as string
183  return (*this == str);
184  }
185  else
186  {
187  // check as regex
188  return re_.match(str);
189  }
190 }
191 
192 
194 {
195  return string::quotemeta<regExp>(*this);
196 }
197 
198 
199 inline void Foam::wordRe::set(const std::string& str, const compOption opt)
200 {
201  string::operator=(str);
202  compile(opt);
203 }
204 
205 
206 inline void Foam::wordRe::set(const char* str, const compOption opt)
207 {
208  string::operator=(str);
209  compile(opt);
210 }
211 
212 
213 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
214 
215 inline const Foam::wordRe& Foam::wordRe::operator=(const wordRe& str)
216 {
217  string::operator=(str);
218 
219  if (str.isPattern())
220  {
221  compile();
222  }
223  else
224  {
225  re_.clear();
226  }
227  return *this;
228 }
229 
230 
231 inline const Foam::wordRe& Foam::wordRe::operator=(const word& str)
232 {
233  word::operator=(str);
234  re_.clear();
235  return *this;
236 }
237 
238 
239 inline const Foam::wordRe& Foam::wordRe::operator=(const string& str)
240 {
241  string::operator=(str);
242  compile(DETECT); // auto-detect regex
243  return *this;
244 }
245 
246 
247 inline const Foam::wordRe& Foam::wordRe::operator=(const std::string& str)
248 {
249  string::operator=(str);
250  compile(DETECT); // auto-detect regex
251  return *this;
252 }
253 
254 
255 inline const Foam::wordRe& Foam::wordRe::operator=(const char* str)
256 {
257  string::operator=(str);
258  compile(DETECT); // auto-detect regex
259  return *this;
260 }
261 
262 
263 // ************************ vim: set sw=4 sts=4 et: ************************ //