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-2018 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
32  string literal and the assignment operators will use the wordRe::DETECT
33  compOption to scan the string for regular expression meta characters
34  and/or 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 synchronize 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 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  //- Construct as copy
120  inline wordRe(const wordRe&);
121 
122  //- Construct from keyType
123  inline explicit wordRe(const keyType&);
124 
125  //- Construct from keyType
126  inline wordRe(const keyType&, const compOption);
127 
128  //- Construct as copy of word
129  inline explicit wordRe(const word&);
130 
131  //- Construct as copy of character array
132  // Optionally specify how it should be treated.
133  inline explicit wordRe(const char*, const compOption = LITERAL);
134 
135  //- Construct as copy of string.
136  // Optionally specify how it should be treated.
137  inline explicit wordRe(const string&, const compOption = LITERAL);
138 
139  //- Construct as copy of std::string
140  // Optionally specify how it should be treated.
141  inline explicit wordRe(const std::string&, const compOption = LITERAL);
142 
143  //- Construct from Istream
144  // Words are treated as literals, strings with an auto-test
145  wordRe(Istream&);
146 
147 
148  // Member functions
149 
150  // Access
151 
152  //- Should be treated as a match rather than a literal string?
153  inline bool isPattern() const;
154 
155 
156  // Infrastructure
157 
158  //- Compile the regular expression
159  inline bool compile() const;
160 
161  //- Possibly compile the regular expression, with greater control
162  inline bool compile(const compOption) const;
163 
164  //- Recompile an existing regular expression
165  inline bool recompile() const;
166 
167  //- Frees precompiled regular expression, making wordRe a literal.
168  // Optionally strips invalid word characters
169  inline void uncompile(const bool doStripInvalid = false) const;
170 
171 
172  // Editing
173 
174  //- Copy string, auto-test for regular expression or other options
175  inline void set(const std::string&, const compOption = DETECT);
176 
177  //- Copy string, auto-test for regular expression or other options
178  inline void set(const char*, const compOption = DETECT);
179 
180  //- Clear string and precompiled regular expression
181  inline void clear();
182 
183 
184  // Searching
185 
186  //- Smart match as regular expression or as a string
187  // Optionally force a literal match only
188  inline bool match
189  (
190  const std::string&,
191  bool literalMatch = false
192  ) const;
193 
194 
195  // Miscellaneous
196 
197  //- Return a string with quoted meta-characters
198  inline string quotemeta() const;
199 
200  //- Output some basic info
201  Ostream& info(Ostream&) const;
202 
203 
204  // Member operators
205 
206  // Assignment
207 
208  //- Assign copy
209  // Always case sensitive
210  inline void operator=(const wordRe&);
211 
212  //- Copy word, never a regular expression
213  inline void operator=(const word&);
214 
215  //- Copy keyType, auto-test for regular expression
216  // Always case sensitive
217  inline void operator=(const keyType&);
218 
219  //- Copy string, auto-test for regular expression
220  // Always case sensitive
221  inline void operator=(const string&);
222 
223  //- Copy string, auto-test for regular expression
224  // Always case sensitive
225  inline void operator=(const std::string&);
226 
227  //- Copy string, auto-test for regular expression
228  // Always case sensitive
229  inline void operator=(const char*);
230 
231 
232  // IOstream operators
233 
234  friend Istream& operator>>(Istream&, wordRe&);
235  friend Ostream& operator<<(Ostream&, const wordRe&);
236 };
237 
238 
239 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
240 
241 } // End namespace Foam
242 
243 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
244 
245 #include "wordReI.H"
246 
247 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
248 
249 #endif
250 
251 // ************************************************************************* //
A class for handling keywords in dictionaries.
Definition: keyType.H:64
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
ignore case in regular expression
Definition: wordRe.H:100
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
treat as regular expression
Definition: wordRe.H:99
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
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:53
string quotemeta() const
Return a string with quoted meta-characters.
Definition: wordReI.H:217
Ostream & operator<<(Ostream &, const ensightPart &)
treat as a string literal
Definition: wordRe.H:97
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.
detect if the string contains meta-characters
Definition: wordRe.H:98