wordRe.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-2019 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 Class
28  Foam::wordRe
29 
30 Description
31  A wordRe is a Foam::word, but can contain a regular expression for
32  matching words or strings.
33 
34  By default the constructors will generally preserve the argument as a
35  string literal and the assignment operators will use the wordRe::DETECT
36  compOption to scan the string for regular expression meta characters
37  and/or invalid word characters and react accordingly.
38 
39  The exceptions are when constructing/assigning from another
40  Foam::wordRe (preserve the same type) or from a Foam::word (always
41  literal).
42 
43 Note
44  If the string contents are changed - eg, by the operator+=() or by
45  string::replace(), etc - it will be necessary to use compile() to
46  synchronize the regular expression.
47 
48 SourceFiles
49  wordRe.C
50 
51 \*---------------------------------------------------------------------------*/
52 
53 #ifndef wordRe_H
54 #define wordRe_H
55 
56 #include "word.H"
57 #include "regExp.H"
58 #include "keyType.H"
59 
60 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
61 
62 namespace Foam
63 {
64 
65 // Forward Declarations
66 class Istream;
67 class Ostream;
68 
69 /*---------------------------------------------------------------------------*\
70  Class wordRe Declaration
71 \*---------------------------------------------------------------------------*/
72 
73 class wordRe
74 :
75  public word
76 {
77  // Private Member Data
78 
79  //- The regular expression
80  regExp re_;
81 
82 
83 public:
84 
85  // Static Data Members
86 
87  //- An empty wordRe
88  static const wordRe null;
89 
90 
91  // Public Data Types
92 
93  //- Enumeration with compile options
94  // Note that 'REGEX' is implicit if 'ICASE' is specified alone.
95  enum compOption
96  {
97  LITERAL = 0,
98  REGEX = 1,
99  ICASE = 2,
100  NOCASE = 2,
101  DETECT = 4,
102  UNKNOWN = 4,
105  };
106 
107 
108  // Constructors
109 
110  //- Construct null
111  inline wordRe();
112 
113  //- Copy construct
114  inline wordRe(const wordRe& str);
115 
116  //- Move construct
117  inline wordRe(wordRe&& str);
118 
119  //- Construct from keyType, using its compile information
120  inline explicit wordRe(const keyType& str);
121 
122  //- Copy from character array, treat as a literal
123  inline explicit wordRe(const char* str);
124 
125  //- Copy from std::string, treat as a literal
126  inline explicit wordRe(const std::string& str);
127 
128  //- Copy from string, treat as a literal
129  inline explicit wordRe(const string& str);
130 
131  //- Copy from word, treat as a literal
132  inline explicit wordRe(const word& str);
133 
134  //- Copy from keyType, use specified compile option
135  inline wordRe(const keyType& str, const compOption opt);
136 
137  //- Copy from character array, use specified compile option
138  inline wordRe(const char* str, const compOption opt);
139 
140  //- Copy from std::string, use specified compile option
141  inline wordRe(const std::string& str, const compOption opt);
142 
143  //- Copy from string, use specified compile option
144  inline wordRe(const string& str, const compOption opt);
145 
146  //- Copy from word, use specified compile option
147  inline wordRe(const word& str, const compOption opt);
148 
149  //- Construct from Istream
150  // Words are treated as literals, strings with an auto-test
151  explicit wordRe(Istream& is);
152 
153 
154  // Member Functions
155 
156  //- Is this a meta character?
157  inline static bool meta(char c);
158 
159  //- Is this character valid for a wordRe?
160  // This is largely identical with what word accepts, but also
161  // permit brace-brackets, which are valid for some regexs.
162  inline static bool valid(char c);
163 
164  //- Test string for regular expression meta characters
165  inline static bool isPattern(const std::string& str);
166 
167 
168  // Access
169 
170  //- The wordRe is treated as literal string, not as pattern.
171  inline bool isLiteral() const;
172 
173  //- The wordRe is treated as a pattern, not as literal string.
174  inline bool isPattern() const;
175 
176 
177  // Infrastructure
178 
179  //- Compile the regular expression
180  inline bool compile();
181 
182  //- Possibly compile the regular expression, with greater control
183  inline bool compile(const compOption opt);
184 
185  //- Make wordRe a literal again, instead of a regular expression.
186  // Optionally strip invalid word characters.
187  inline void uncompile(bool adjust = false);
188 
189 
190  // Editing
191 
192  //- Copy string, auto-test for regular expression or other options
193  inline void set(const std::string& str, const compOption opt = DETECT);
194 
195  //- Copy string, auto-test for regular expression or other options
196  inline void set(const char* str, const compOption opt = DETECT);
197 
198  //- Clear string and regular expression
199  inline void clear();
200 
201  //- Swap contents. Self-swapping is a no-op
202  inline void swap(wordRe& str);
203 
204 
205  // Matching/Searching
206 
207  //- Smart match as regular expression or as a string.
208  // Optionally force a literal match only
209  inline bool match(const std::string& text, bool literal=false) const;
210 
211 
212  // Miscellaneous
213 
214  //- Return a string with quoted meta-characters
215  inline string quotemeta() const;
216 
217  //- Output some basic info
218  Ostream& info(Ostream& os) const;
219 
220 
221  // Member Operators
222 
223  //- Perform smart match on text, as per match()
224  // Allows use as a predicate.
225  inline bool operator()(const std::string& text) const;
226 
227 
228  //- Copy assignment, retaining type (literal or regex)
229  // Self-assignment is a no-op.
230  inline void operator=(const wordRe& str);
231 
232  //- Copy word, never a regular expression
233  inline void operator=(const word& str);
234 
235  //- Copy keyType and its type (literal or regex)
236  // Always case sensitive
237  inline void operator=(const keyType& str);
238 
239  //- Copy string, auto-test for regular expression
240  // Always case sensitive
241  inline void operator=(const string& str);
242 
243  //- Copy string, auto-test for regular expression
244  // Always case sensitive
245  inline void operator=(const std::string& str);
246 
247  //- Copy string, auto-test for regular expression
248  // Always case sensitive
249  inline void operator=(const char* str);
250 
251  //- Move assignment.
252  // Self-assignment is a no-op.
253  inline void operator=(wordRe&& str);
254 };
255 
256 
257 // IOstream Operators
258 
259 //- Read operator
260 Istream& operator>>(Istream& is, wordRe& val);
261 
262 //- Write operator
263 Ostream& operator<<(Ostream& os, const wordRe& val);
264 
265 
266 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
267 
268 } // End namespace Foam
269 
270 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271 
272 #include "wordReI.H"
273 
274 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
275 
276 #endif
277 
278 // ************************************************************************* //
Foam::wordRe::DETECT_ICASE
Combined DETECT and ICASE.
Definition: wordRe.H:103
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::wordRe::NOCASE
Definition: wordRe.H:99
Foam::wordRe::valid
static bool valid(char c)
Is this character valid for a wordRe?
Definition: wordReI.H:37
Foam::wordRe::operator=
void operator=(const wordRe &str)
Copy assignment, retaining type (literal or regex)
Definition: wordReI.H:280
Foam::wordRe::REGEX
Regular expression.
Definition: wordRe.H:97
wordReI.H
Foam::wordRe::DETECT
Detect if the string contains meta-characters.
Definition: wordRe.H:100
Foam::wordRe::info
Ostream & info(Ostream &os) const
Output some basic info.
Definition: wordRe.C:51
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:72
Foam::wordRe::compile
bool compile()
Compile the regular expression.
Definition: wordReI.H:206
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:60
Foam::wordRe::wordRe
wordRe()
Construct null.
Definition: wordReI.H:51
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::regExpCxx
Wrapper around C++11 regular expressions.
Definition: regExpCxx.H:72
Foam::wordRe::LITERAL
String literal.
Definition: wordRe.H:96
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:246
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::wordRe::quotemeta
string quotemeta() const
Return a string with quoted meta-characters.
Definition: wordReI.H:240
Foam::wordRe::ICASE
Ignore case in regular expression.
Definition: wordRe.H:98
Foam::wordRe::REGEX_ICASE
Combined REGEX and ICASE.
Definition: wordRe.H:102
Foam::wordRe::clear
void clear()
Clear string and regular expression.
Definition: wordReI.H:222
Foam::wordRe::isLiteral
bool isLiteral() const
The wordRe is treated as literal string, not as pattern.
Definition: wordReI.H:163
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::wordRe::UNKNOWN
Unknown content.
Definition: wordRe.H:101
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
word.H
Foam::wordRe::meta
static bool meta(char c)
Is this a meta character?
Definition: wordReI.H:31
keyType.H
Foam::wordRe::uncompile
void uncompile(bool adjust=false)
Make wordRe a literal again, instead of a regular expression.
Definition: wordReI.H:212
Foam::wordRe::operator()
bool operator()(const std::string &text) const
Perform smart match on text, as per match()
Definition: wordReI.H:274
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:229
Foam::wordRe::compOption
compOption
Enumeration with compile options.
Definition: wordRe.H:94
Foam::wordRe::isPattern
bool isPattern() const
The wordRe is treated as a pattern, not as literal string.
Definition: wordReI.H:169
Foam::wordRe::swap
void swap(wordRe &str)
Swap contents. Self-swapping is a no-op.
Definition: wordReI.H:260