wordRe.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-2021 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 Class
25  Foam::wordRe
26 
27 Description
28  A wordRe is a word, but can also have a regular expression for matching
29  words.
30 
31  By default the constructors will generally preserve the argument as a string
32  literal and the assignment operators will use the wordRe::compOption::detect
33  compOption to scan the string for regular expression meta characters and/or
34  invalid word characters and react accordingly.
35 
36  The exceptions are when constructing/assigning from another
37  Foam::wordRe (preserve the same type) or from a Foam::word (always
38  literal).
39 
40 Note
41  If the string contents are changed - eg, by the operator+=() or by
42  string::replace(), etc - it will be necessary to use compile() or
43  recompile() to synchronise the regular expression.
44 
45 SourceFiles
46  wordRe.C
47 
48 \*---------------------------------------------------------------------------*/
49 
50 #ifndef wordRe_H
51 #define wordRe_H
52 
53 #include "word.H"
54 #include "regExp.H"
55 #include "keyType.H"
56 
57 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
58 
59 namespace Foam
60 {
61 
62 // Forward declaration of friend functions and operators
63 class wordRe;
64 class Istream;
65 class Ostream;
66 
67 Istream& operator>>(Istream&, wordRe&);
68 Ostream& operator<<(Ostream&, const wordRe&);
69 
70 
71 /*---------------------------------------------------------------------------*\
72  Class wordRe Declaration
73 \*---------------------------------------------------------------------------*/
74 
75 class wordRe
76 :
77  public word
78 {
79  // Private member data
80 
81  //- The regular expression
82  mutable regExp re_;
83 
84 public:
85 
86  // Static Data Members
87 
88  //- An empty wordRe
89  static const wordRe null;
90 
91 
92  // Public data types
93 
94  //- Enumeration with compile options
95  // Note that 'regexp' is implicit if 'noCase' is specified alone.
96  enum class compOption
97  {
98  literal = 0,
99  detect = 1,
100  regExp = 2,
101  noCase = 4,
104  };
105 
106 
107  //- Is this a meta character?
108  static inline bool meta(char);
109 
110  //- Test string for regular expression meta characters
111  static inline bool isPattern(const string&);
112 
113 
114  // Constructors
115 
116  //- Construct null
117  inline wordRe();
118 
119  //- Copy constructor
120  inline wordRe(const wordRe&);
121 
122  //- Construct from keyType
123  inline wordRe(const keyType&);
124 
125  //- Construct from keyType
126  inline wordRe(const keyType&, const compOption);
127 
128  //- Copy constructor of word
129  inline wordRe(const word&);
130 
131  //- Copy constructor of character array
132  // Optionally specify how it should be treated.
133  inline explicit wordRe
134  (
135  const char*,
137  );
138 
139  //- Copy constructor of string.
140  // Optionally specify how it should be treated.
141  inline explicit wordRe
142  (
143  const string&,
145  );
146 
147  //- Copy constructor of std::string
148  // Optionally specify how it should be treated.
149  inline explicit wordRe
150  (
151  const std::string&,
153  );
154 
155  //- Construct from Istream
156  // Words are treated as literals, strings with an auto-test
157  wordRe(Istream&);
158 
159 
160  // Member Functions
161 
162  // Access
163 
164  //- Should be treated as a match rather than a literal string?
165  inline bool isPattern() const;
166 
167 
168  // Infrastructure
169 
170  //- Compile the regular expression
171  inline bool compile() const;
172 
173  //- Possibly compile the regular expression, with greater control
174  inline bool compile(const compOption) const;
175 
176  //- Recompile an existing regular expression
177  inline bool recompile() const;
178 
179  //- Frees precompiled regular expression, making wordRe a literal.
180  // Optionally strips invalid word characters
181  inline void uncompile(const bool doStripInvalid = false) const;
182 
183 
184  // Editing
185 
186  //- Copy string, auto-test for regular expression or other options
187  inline void set
188  (
189  const std::string&,
191  );
192 
193  //- Copy string, auto-test for regular expression or other options
194  inline void set
195  (
196  const char*,
197  const compOption = compOption::detect
198  );
199 
200  //- Clear string and precompiled regular expression
201  inline void clear();
202 
203 
204  // Searching
205 
206  //- Smart match as regular expression or as a string
207  // Optionally force a literal match only
208  inline bool match
209  (
210  const std::string&,
211  bool literalMatch = false
212  ) const;
213 
214 
215  // Miscellaneous
216 
217  //- Return a string with quoted meta-characters
218  inline string quotemeta() const;
219 
220  //- Output some basic info
221  Ostream& info(Ostream&) const;
222 
223 
224  // Member Operators
225 
226  // Assignment
227 
228  //- Assign copy
229  // Always case sensitive
230  inline void operator=(const wordRe&);
231 
232  //- Copy word, never a regular expression
233  inline void operator=(const word&);
234 
235  //- Copy keyType, auto-test for regular expression
236  // Always case sensitive
237  inline void operator=(const keyType&);
238 
239  //- Copy string, auto-test for regular expression
240  // Always case sensitive
241  inline void operator=(const string&);
242 
243  //- Copy string, auto-test for regular expression
244  // Always case sensitive
245  inline void operator=(const std::string&);
246 
247  //- Copy string, auto-test for regular expression
248  // Always case sensitive
249  inline void operator=(const char*);
250 
251 
252  // IOstream Operators
253 
254  friend Istream& operator>>(Istream&, wordRe&);
255  friend Ostream& operator<<(Ostream&, const wordRe&);
256 };
257 
258 
259 inline int operator&
260 (
261  const wordRe::compOption co1,
262  const wordRe::compOption co2
263 );
264 
265 
266 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
267 
268 } // End namespace Foam
269 
270 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
271 
272 #include "wordReI.H"
273 
274 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
275 
276 #endif
277 
278 // ************************************************************************* //
A class for handling keywords in dictionaries.
Definition: keyType.H:66
Wrapper around POSIX extended regular expressions.
Definition: regExp.H:61
bool recompile() const
Recompile an existing regular expression.
Definition: wordReI.H:168
friend Ostream & operator<<(Ostream &, const wordRe &)
void operator=(const wordRe &)
Assign copy.
Definition: wordReI.H:239
An Istream is an abstract base class for all input systems (streams, files, token lists etc)...
Definition: Istream.H:57
compOption
Enumeration with compile options.
Definition: wordRe.H:95
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
Ostream & info(Ostream &) const
Output some basic info.
Definition: wordRe.C:103
friend Istream & operator>>(Istream &, wordRe &)
A class for handling words, derived from string.
Definition: word.H:59
Istream & operator>>(Istream &, directionInfo &)
wordRe()
Construct null.
Definition: wordReI.H:42
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
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
string quotemeta() const
Return a string with quoted meta-characters.
Definition: wordReI.H:217
treat as a string literal
ignore case in regular expression
Ostream & operator<<(Ostream &, const ensightPart &)
void uncompile(const bool doStripInvalid=false) const
Frees precompiled regular expression, making wordRe a literal.
Definition: wordReI.H:179
bool match(const std::string &, bool literalMatch=false) const
Smart match as regular expression or as a string.
Definition: wordReI.H:202
static const wordRe null
An empty wordRe.
Definition: wordRe.H:88
Namespace for OpenFOAM.