regExpCxx.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) 2017-2019 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::regExpCxx
28 
29 Description
30  Wrapper around C++11 regular expressions.
31 
32  Using either POSIX extended regular expressions or
33  <a href=
34  "http://www.cplusplus.com/reference/regex/ECMAScript"
35  >modified ECMAScript regular expression grammar</a>
36 
37  Since ECMAScript grammar may not work correctly on all installations,
38  the current default is to use extended regular expressions.
39 
40  The JAVA/PCRE '(?i)' extension is supported as a prefix to compile the
41  regular expression as being case-insensitive.
42 
43 Note
44  The C++11 regular expressions may be broken on some compilers.
45  For example, gcc 4.8 is known to fail.
46  For these systems the POSIX implementation or alternative must be used.
47 
48 Warning
49  This class should not be used directly.
50  Use the Foam::regExp typedef instead.
51 
52 SourceFiles
53  regExpCxxI.H
54  regExpCxx.C
55 
56 \*---------------------------------------------------------------------------*/
57 
58 #ifndef regExpCxx_H
59 #define regExpCxx_H
60 
61 #include <regex>
62 #include <string>
63 
64 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
65 
66 namespace Foam
67 {
68 
69 /*---------------------------------------------------------------------------*\
70  Class regExpCxx Declaration
71 \*---------------------------------------------------------------------------*/
72 
73 class regExpCxx
74 {
75  // Private Data
76 
77  //- Regular expression (using char type)
78  std::regex re_;
79 
80  //- Track if input pattern was OK - ie, has a length
81  bool ok_;
82 
83  // Private Member Functions
84 
85  //- Select grammar based on regExpCxx optimisationSwitch
86  // 0 = extended, 1 = ECMAScript
87  static inline std::regex::flag_type syntax();
88 
89 public:
90 
91  // Public Types
92 
93  //- Type for matches
94  typedef std::smatch results_type;
95 
96 
97  // Static Member Data
98 
99  //- The default grammar (extended | ECMAScript).
100  static int grammar;
101 
102 
103  // Static Member Functions
104 
105  //- Test if character appears to be a regular expression meta-character
106  // \return true if character is one of the following:
107  // - any character: '.' \n
108  // - quantifiers: '*', '+', '?' \n
109  // - grouping: '(', '|', ')' \n
110  // - range: '[', ']' \n
111  //
112  // \note The presence of '{', '}' regex bounds is not considered
113  inline static bool meta(const char c);
114 
115 
116  // Constructors
117 
118  //- Construct null
119  inline regExpCxx();
120 
121  //- Copy construct
122  inline regExpCxx(const regExpCxx& rgx);
123 
124  //- Move construct
125  inline regExpCxx(regExpCxx&& rgx);
126 
127  //- Construct from character array
128  inline explicit regExpCxx(const char* pattern);
129 
130  //- Construct from string
131  inline explicit regExpCxx(const std::string& pattern);
132 
133  //- Construct from character array, optionally ignore case
134  inline regExpCxx(const char* pattern, bool ignoreCase);
135 
136  //- Construct from string, optionally ignore case
137  inline regExpCxx(const std::string& pattern, bool ignoreCase);
138 
139 
140  //- Destructor
141  ~regExpCxx() = default;
142 
143 
144  // Member Functions
145 
146  // Access
147 
148  //- Return true if expression is empty
149  inline bool empty() const noexcept;
150 
151  //- Return true if expression is non-empty
152  inline bool exists() const noexcept;
153 
154  //- The number of capture groups for a non-empty expression
155  inline unsigned ngroups() const;
156 
157  // \return True if the pattern was set with ignore-case.
158  inline bool nocase() const;
159 
160 
161  // Editing
162 
163  //- Clear expression.
164  // \return True if expression had existed prior to the clear.
165  inline bool clear();
166 
167  //- Swap contents
168  inline void swap(regExpCxx& rgx);
169 
170  //- Compile pattern into a regular expression, optionally ignore case.
171  // \return True if the pattern was compiled
172  bool set(const char* pattern, bool ignoreCase=false);
173 
174  //- Compile pattern into a regular expression, optionally ignore case.
175  // \return True if the pattern was compiled
176  bool set(const std::string& pattern, bool ignoreCase=false);
177 
178 
179  // Matching/Searching
180 
181  //- Find position within the text.
182  // \return The index where it begins or string::npos if not found
183  inline std::string::size_type find(const std::string& text) const;
184 
185  //- True if the regex matches the entire text.
186  // The begin-of-line (^) and end-of-line ($) anchors are implicit
187  inline bool match(const std::string& text) const;
188 
189  //- True if the regex matches the text, set the matches.
190  // The first group starts at index 1 (0 is the entire match).
191  // The begin-of-line (^) and end-of-line ($) anchors are implicit
192  inline bool match(const std::string& text, results_type& matches) const;
193 
194  //- Return true if the regex was found within the text
195  inline bool search(const std::string& text) const;
196 
197 
198  // Member Operators
199 
200  //- Perform match on text
201  inline bool operator()(const std::string& text) const;
202 
203  //- Copy assignment
204  inline void operator=(const regExpCxx& rgx);
205 
206  //- Move assignment
207  inline void operator=(regExpCxx&& rgx);
208 
209  //- Assign and compile pattern from a character array.
210  // Matching is case sensitive.
211  inline void operator=(const char* pattern);
212 
213  //- Assign and compile pattern from string.
214  // Matching is case sensitive.
215  inline void operator=(const std::string& pattern);
216 };
217 
218 
219 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
220 
221 } // End namespace Foam
222 
223 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
224 
225 #include "regExpCxxI.H"
226 
227 #endif
228 
229 // ************************************************************************* //
Foam::regExpCxx::ngroups
unsigned ngroups() const
The number of capture groups for a non-empty expression.
Definition: regExpCxxI.H:129
Foam::regExpCxx::find
std::string::size_type find(const std::string &text) const
Find position within the text.
Definition: regExpCxxI.H:167
Foam::regExpCxx::~regExpCxx
~regExpCxx()=default
Destructor.
Foam::regExpCxx::set
bool set(const char *pattern, bool ignoreCase=false)
Compile pattern into a regular expression, optionally ignore case.
Definition: regExpCxx.C:111
Foam::regExpCxx::match
bool match(const std::string &text) const
True if the regex matches the entire text.
Definition: regExpCxxI.H:185
Foam::regExpCxx::regExpCxx
regExpCxx()
Construct null.
Definition: regExpCxxI.H:56
Foam::regExpCxx::empty
bool empty() const noexcept
Return true if expression is empty.
Definition: regExpCxxI.H:117
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:76
Foam::regExpCxx
Wrapper around C++11 regular expressions.
Definition: regExpCxx.H:72
Foam::regExpCxx::search
bool search(const std::string &text) const
Return true if the regex was found within the text.
Definition: regExpCxxI.H:179
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::regExpCxx::results_type
std::smatch results_type
Type for matches.
Definition: regExpCxx.H:93
Foam::regExpCxx::clear
bool clear()
Clear expression.
Definition: regExpCxxI.H:141
Foam::regExpCxx::swap
void swap(regExpCxx &rgx)
Swap contents.
Definition: regExpCxxI.H:155
Foam::regExpCxx::meta
static bool meta(const char c)
Test if character appears to be a regular expression meta-character.
Definition: regExpCxxI.H:42
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::regExpCxx::exists
bool exists() const noexcept
Return true if expression is non-empty.
Definition: regExpCxxI.H:123
Foam::regExpCxx::nocase
bool nocase() const
Definition: regExpCxxI.H:135
Foam::regExpCxx::grammar
static int grammar
The default grammar (extended | ECMAScript).
Definition: regExpCxx.H:99