1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
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 the
13 Free Software Foundation; either version 2 of the License, or (at your
14 option) any later version.
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
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM; if not, write to the Free Software Foundation,
23 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 A wordRe is a word, but can also have a regular expression for matching
32 By default the constructors will generally preserve the argument as
33 string literal and the assignment operators will use the wordRe::DETECT
34 compOption to scan the string for regular expression meta characters
35 and/or invalid word characters and react accordingly.
37 The exceptions are when constructing/assigning from another
38 Foam::wordRe (preserve the same type) or from a Foam::word (always
42 If the string contents are changed - eg, by the operator+=() or by
43 string::replace(), etc - it will be necessary to use compile() or
44 recompile() to synchronize the regular expression.
50 \*---------------------------------------------------------------------------*/
55 #include <OpenFOAM/word.H>
56 #include <OSspecific/regExp.H>
58 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
63 // Forward declaration of friend functions and operators
69 Istream& operator>>(Istream&, wordRe&);
70 Ostream& operator<<(Ostream&, const wordRe&);
73 /*---------------------------------------------------------------------------*\
74 Class wordRe Declaration
75 \*---------------------------------------------------------------------------*/
81 // Private member data
83 //- The regular expression
90 //- Enumeration with compile options
91 // Note that 'REGEXP' is implicit if 'NOCASE' is specified alone.
94 LITERAL = 0, /*!< treat as a string literal */
95 DETECT = 1, /*!< treat as regular expression */
96 REGEXP = 2, /*!< detect if the string contains meta-characters */
97 NOCASE = 4, /*!< ignore case in regular expression */
98 DETECT_NOCASE = DETECT | NOCASE,
99 REGEXP_NOCASE = REGEXP | NOCASE
103 //- Is this a meta character?
104 static inline bool meta(char);
106 //- Test string for regular expression meta characters
107 static inline bool isPattern(const string&);
114 //- Construct as copy
115 inline wordRe(const wordRe&);
117 //- Construct as copy of word
118 inline wordRe(const word&);
120 //- Construct as copy of character array
121 // Optionally specify how it should be treated.
122 inline wordRe(const char*, const compOption=LITERAL);
124 //- Construct as copy of string.
125 // Optionally specify how it should be treated.
126 inline wordRe(const string&, const compOption=LITERAL);
128 //- Construct as copy of std::string
129 // Optionally specify how it should be treated.
130 inline wordRe(const std::string&, const compOption=LITERAL);
132 //- Construct from Istream
133 // Words are treated as literals, strings with an auto-test
140 //- Should be treated as a match rather than a literal string?
141 inline bool isPattern() const;
145 //- Compile the regular expression
146 inline bool compile() const;
148 //- Possibly compile the regular expression, with greater control
149 inline bool compile(const compOption) const;
151 //- Recompile an existing regular expression
152 inline bool recompile() const;
154 //- Frees precompiled regular expression, making wordRe a literal.
155 // Optionally strips invalid word characters
156 inline void uncompile(const bool doStripInvalid=false) const;
160 //- Copy string, auto-test for regular expression or other options
161 inline void set(const std::string&, const compOption=DETECT);
163 //- Copy string, auto-test for regular expression or other options
164 inline void set(const char*, const compOption=DETECT);
166 //- Clear string and precompiled regular expression
171 //- Smart match as regular expression or as a string
172 // Optionally specify a literal match only
173 inline bool match(const string&, bool literalMatch=false) const;
177 //- Return a string with quoted meta-characters
178 inline string quotemeta() const;
180 //- Output some basic info
181 Ostream& info(Ostream&) const;
189 // Always case sensitive
190 inline const wordRe& operator=(const wordRe&);
192 //- Copy word, never a regular expression
193 inline const wordRe& operator=(const word&);
195 //- Copy string, auto-test for regular expression
196 // Always case sensitive
197 inline const wordRe& operator=(const string&);
199 //- Copy string, auto-test for regular expression
200 // Always case sensitive
201 inline const wordRe& operator=(const std::string&);
203 //- Copy string, auto-test for regular expression
204 // Always case sensitive
205 inline const wordRe& operator=(const char*);
208 // IOstream operators
210 friend Istream& operator>>(Istream&, wordRe&);
211 friend Ostream& operator<<(Ostream&, const wordRe&);
215 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
217 } // End namespace Foam
219 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
223 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
227 // ************************ vim: set sw=4 sts=4 et: ************************ //