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-2022 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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
27Class
28 Foam::wordRe
29
30Description
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
43Note
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
48SourceFiles
49 wordReI.H
50 wordRe.C
51
52\*---------------------------------------------------------------------------*/
53
54#ifndef Foam_wordRe_H
55#define Foam_wordRe_H
56
57#include "word.H"
58#include "regExp.H"
59#include "stdFoam.H"
60
61// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
62
63namespace Foam
64{
65
66// Forward Declarations
67class keyType;
68class token;
69class wordRe;
70Istream& operator>>(Istream& is, wordRe& val);
71Ostream& operator<<(Ostream& os, const wordRe& val);
72
73//- Hashing for wordRe
74template<> struct Hash<wordRe> : string::hasher {};
75
76
77/*---------------------------------------------------------------------------*\
78 Class wordRe Declaration
79\*---------------------------------------------------------------------------*/
81class wordRe
82:
83 public word
84{
85 // Private Member Data
86
87 //- The regular expression
88 std::unique_ptr<Foam::regExp> regexPtr_;
89
90
91public:
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
106 REGEX = 1,
107 ICASE = 2,
108 NOCASE = 2,
109 DETECT = 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,
139 );
140
141 //- Implicit construct from character array,
142 //- with specified compile option (default is LITERAL)
143 inline wordRe
144 (
145 const char* str,
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 a literal string, not a pattern.
168 inline bool isLiteral() const noexcept;
169
170 //- The wordRe is a pattern, not a 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 (if possible)
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
261Istream& operator>>(Istream& is, wordRe& val);
262
263//- Write operator
264Ostream& 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// ************************************************************************* //
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:64
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A class for handling keywords in dictionaries.
Definition: keyType.H:71
A token holds an item read from Istream.
Definition: token.H:69
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:83
bool assign(const token &tok)
Assign from word or string token.
Definition: wordRe.C:64
void set(const std::string &str, const compOption opt=DETECT)
Copy string, auto-test for regular expression or other options.
Definition: wordReI.H:211
bool isLiteral() const noexcept
The wordRe is a literal string, not a pattern.
Definition: wordReI.H:105
static const wordRe null
An empty wordRe.
Definition: wordRe.H:95
static bool valid(const char c)
Test for valid wordRe character?
Definition: wordReI.H:31
bool compile()
Compile as regular expression (if possible)
Definition: wordReI.H:159
wordRe()
Default construct, empty literal.
Definition: wordReI.H:40
compOption
Enumeration with compile options.
Definition: wordRe.H:103
@ LITERAL
String literal.
Definition: wordRe.H:104
@ DETECT
Detect if the string contains meta-characters.
Definition: wordRe.H:108
@ DETECT_ICASE
Combined DETECT and ICASE.
Definition: wordRe.H:111
@ REGEX
Regular expression.
Definition: wordRe.H:105
@ ICASE
Ignore case in regular expression.
Definition: wordRe.H:106
@ REGEX_ICASE
Combined REGEX and ICASE.
Definition: wordRe.H:110
@ UNKNOWN
Unknown content (for return value).
Definition: wordRe.H:109
bool isPattern() const noexcept
The wordRe is a pattern, not a literal string.
Definition: wordReI.H:111
void clear()
Clear string and regular expression.
Definition: wordReI.H:193
void uncompile()
Mark as literal string, remove any regular expression.
Definition: wordReI.H:176
bool match(const std::string &text, bool literal=false) const
Smart match as regular expression or as a string.
Definition: wordReI.H:200
void swap(wordRe &str)
Swap contents. Self-swapping is a no-op.
Definition: wordReI.H:226
A class for handling words, derived from Foam::string.
Definition: word.H:68
OBJstream os(runTime.globalPath()/outputName)
Namespace for OpenFOAM.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Istream & operator>>(Istream &, directionInfo &)
const direction noexcept
Definition: Scalar.H:223
Includes some standard C++ headers, defines global macros and templates used in multiple places by Op...
Hash function class. The default definition is for primitives. Non-primitives used to hash entries on...
Definition: Hash.H:54
Hashing functor for string and derived string classes.
Definition: string.H:148