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-2021 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()
46  or uncompile() to synchronize the regular expression.
47 
48 SourceFiles
49  wordReI.H
50  wordRe.C
51 
52 \*---------------------------------------------------------------------------*/
53 
54 #ifndef wordRe_H
55 #define wordRe_H
56 
57 #include "word.H"
58 #include "regExp.H"
59 #include "stdFoam.H"
60 
61 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
62 
63 namespace Foam
64 {
65 
66 // Forward Declarations
67 class keyType;
68 class token;
69 class wordRe;
70 Istream& operator>>(Istream& is, wordRe& val);
71 Ostream& operator<<(Ostream& os, const wordRe& val);
72 
73 //- Hashing for wordRe
74 template<> struct Hash<wordRe> : string::hasher {};
75 
76 
77 /*---------------------------------------------------------------------------*\
78  Class wordRe Declaration
79 \*---------------------------------------------------------------------------*/
80 
81 class wordRe
82 :
83  public word
84 {
85  // Private Member Data
86 
87  //- The regular expression
88  regExp re_;
89 
90 
91 public:
92 
93  // Static Data Members
94 
95  //- An empty wordRe
96  static const wordRe null;
97 
98 
99  // Public Data Types
100 
101  //- Enumeration with compile options
102  // Note that 'REGEX' is implicit if 'ICASE' is specified alone.
103  enum compOption
104  {
105  LITERAL = 0,
106  REGEX = 1,
107  ICASE = 2,
108  NOCASE = 2,
109  DETECT = 4,
110  UNKNOWN = 4,
113  };
114 
115 
116  // Constructors
117 
118  //- Default construct, empty literal
119  inline wordRe();
120 
121  //- Copy construct
122  inline wordRe(const wordRe& str);
123 
124  //- Move construct
125  inline wordRe(wordRe&& str);
126 
127  //- Implicit copy construct from word, as LITERAL
128  inline wordRe(const word& str);
129 
130  //- Implicit move construct from word, as LITERAL
131  inline wordRe(word&& str);
132 
133  //- Implicit copy construct from other string-types,
134  //- with specified compile option (default is LITERAL)
135  inline wordRe
136  (
137  const std::string& str,
138  const compOption opt = compOption::LITERAL
139  );
140 
141  //- Implicit construct from character array,
142  //- with specified compile option (default is LITERAL)
143  inline wordRe
144  (
145  const char* str,
146  const compOption opt = compOption::LITERAL
147  );
148 
149  //- Implicit copy construct from keyType, using its compile type
150  wordRe(const keyType& str);
151 
152  //- Construct from Istream by reading a token
153  // Words are treated as literals, strings with an auto-detect
154  explicit wordRe(Istream& is);
155 
156 
157  // Member Functions
158 
159  //- Test for valid wordRe character?
160  // Like Foam::word, but with brace-brackets,
161  // which are valid for some regexs.
162  inline static bool valid(const char c);
163 
164 
165  // Access
166 
167  //- The wordRe is treated as literal string, not as pattern.
168  inline bool isLiteral() const noexcept;
169 
170  //- The wordRe is treated as a pattern, not as literal string.
171  inline bool isPattern() const noexcept;
172 
173 
174  // Infrastructure
175 
176  //- Inherit all regular string assign() methods
177  using word::assign;
178 
179  //- Assign from word or string token.
180  // Words are treated as literals, strings with an auto-detect
181  // \return false if the token was the incorrect type
182  bool assign(const token& tok);
183 
184  //- Compile as regular expression
185  inline bool compile();
186 
187  //- Mark as literal string, remove any regular expression
188  inline void uncompile();
189 
190  //- Possibly compile the regular expression, with greater control
191  inline bool compile(const compOption opt);
192 
193  //- Mark as literal string, optionally stripping invalid word
194  //- characters when changing to a literal
195  inline void uncompile(bool adjust);
196 
197 
198  // Editing
199 
200  //- Copy string, auto-test for regular expression or other options
201  inline void set(const std::string& str, const compOption opt = DETECT);
202 
203  //- Copy string, auto-test for regular expression or other options
204  inline void set(const char* str, const compOption opt = DETECT);
205 
206  //- Clear string and regular expression
207  inline void clear();
208 
209  //- Swap contents. Self-swapping is a no-op
210  inline void swap(wordRe& str);
211 
212 
213  // Matching/Searching
214 
215  //- Smart match as regular expression or as a string.
216  // Optionally force a literal match only
217  inline bool match(const std::string& text, bool literal=false) const;
218 
219 
220  // Member Operators
221 
222  //- Perform smart match on text, as per match()
223  // Allows use as a predicate.
224  inline bool operator()(const std::string& text) const;
225 
226 
227  //- Copy assignment, retaining type (literal or regex)
228  // Self-assignment is a no-op.
229  inline void operator=(const wordRe& str);
230 
231  //- Copy word, never a regular expression
232  inline void operator=(const word& str);
233 
234  //- Copy keyType and its type (literal or regex)
235  // Always case sensitive
236  void operator=(const keyType& str);
237 
238  //- Copy string, auto-test for regular expression
239  // Always case sensitive
240  inline void operator=(const string& str);
241 
242  //- Copy string, auto-test for regular expression
243  // Always case sensitive
244  inline void operator=(const std::string& str);
245 
246  //- Copy string, auto-test for regular expression
247  // Always case sensitive
248  inline void operator=(const char* str);
249 
250  //- Move assignment.
251  // Self-assignment is a no-op.
252  inline void operator=(wordRe&& str);
253 };
254 
255 
256 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
257 
258 // IOstream Operators
259 
260 //- Read operator
261 Istream& operator>>(Istream& is, wordRe& val);
262 
263 //- Write operator
264 Ostream& operator<<(Ostream& os, const wordRe& val);
265 
266 
267 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
268 
269 } // End namespace Foam
270 
271 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
272 
273 #include "wordReI.H"
274 
275 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
276 
277 #endif
278 
279 // ************************************************************************* //
Foam::wordRe::isPattern
bool isPattern() const noexcept
The wordRe is treated as a pattern, not as literal string.
Definition: wordReI.H:107
Foam::wordRe::DETECT_ICASE
Combined DETECT and ICASE.
Definition: wordRe.H:111
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::wordRe::NOCASE
Definition: wordRe.H:107
Foam::wordRe::REGEX
Regular expression.
Definition: wordRe.H:105
Foam::wordRe::DETECT
Detect if the string contains meta-characters.
Definition: wordRe.H:108
Foam::operator>>
Istream & operator>>(Istream &, directionInfo &)
Definition: directionInfo.C:230
Foam::token
A token holds an item read from Istream.
Definition: token.H:68
Foam::wordRe::isLiteral
bool isLiteral() const noexcept
The wordRe is treated as literal string, not as pattern.
Definition: wordReI.H:101
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:80
Foam::wordRe::compile
bool compile()
Compile as regular expression.
Definition: wordReI.H:144
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:68
Foam::Hash
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:53
Foam::wordRe::wordRe
wordRe()
Default construct, empty literal.
Definition: wordReI.H:40
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::wordRe::valid
static bool valid(const char c)
Test for valid wordRe character?
Definition: wordReI.H:31
Foam::regExpCxx
Wrapper around C++11 regular expressions with some additional prefix-handling. The prefix-handling is...
Definition: regExpCxx.H:82
Foam::wordRe::LITERAL
String literal.
Definition: wordRe.H:104
os
OBJstream os(runTime.globalPath()/outputName)
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:185
Foam::wordRe::uncompile
void uncompile()
Mark as literal string, remove any regular expression.
Definition: wordReI.H:150
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::wordRe::ICASE
Ignore case in regular expression.
Definition: wordRe.H:106
Foam::wordRe::REGEX_ICASE
Combined REGEX and ICASE.
Definition: wordRe.H:110
Foam::wordRe::clear
void clear()
Clear string and regular expression.
Definition: wordReI.H:167
Foam::string::hasher
Hashing functor for string and derived string classes.
Definition: string.H:147
Foam::wordRe::assign
bool assign(const token &tok)
Assign from word or string token.
Definition: wordRe.C:60
stdFoam.H
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::wordRe::UNKNOWN
Unknown content (for return value).
Definition: wordRe.H:109
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::match
bool match(const std::string &text, bool literal=false) const
Smart match as regular expression or as a string.
Definition: wordReI.H:174
Foam::wordRe::compOption
compOption
Enumeration with compile options.
Definition: wordRe.H:102
Foam::wordRe::swap
void swap(wordRe &str)
Swap contents. Self-swapping is a no-op.
Definition: wordReI.H:199