regExpPosix.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-2017 OpenFOAM Foundation
9  Copyright (C) 2017-2019 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::regExpPosix
29 
30 Description
31  Wrapper around POSIX extended regular expressions.
32 
33  The PCRE '(?i)' extension is provided to compile the regular expression
34  as being case-insensitive.
35 
36 SeeAlso
37  The manpage regex(7) for more information about POSIX regular expressions.
38  These differ somewhat from \c Perl and \c sed regular expressions.
39 
40 SeeAlso
41  Foam::regExp and Foam::regExpCxx
42 
43 Warning
44  This class should not be used directly.
45  Use the Foam::regExp typedef instead.
46 
47 \deprecated
48  This class will be superseded by Foam::regExpCxx as compiler support
49  for regular expressions continues to improve.
50 
51 SourceFiles
52  regExpPosixI.H
53  regExpPosix.C
54 
55 \*---------------------------------------------------------------------------*/
56 
57 #ifndef regExpPosix_H
58 #define regExpPosix_H
59 
60 #include <regex.h>
61 #include <string>
62 
63 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
64 
65 namespace Foam
66 {
67 
68 // Forward Declarations
69 template<class String> class SubStrings;
70 
71 
72 /*---------------------------------------------------------------------------*\
73  Class regExpPosix Declaration
74 \*---------------------------------------------------------------------------*/
75 
76 class regExpPosix
77 {
78  // Private Data
79 
80  //- Precompiled regular expression
81  regex_t* preg_;
82 
83 public:
84 
85  // Public Types
86 
87  //- Type for matches - similar to std::smatch
89 
90 
91  // Static Member Data
92 
93  //- Grammar (unused) - for compatibility with Foam::regExpCxx
94  static int grammar;
95 
96 
97  // Static Member Functions
98 
99  //- Test if character appears to be a regular expression meta-character
100  // \return true if character is one of the following:
101  // - any character: '.' \n
102  // - quantifiers: '*', '+', '?' \n
103  // - grouping: '(', '|', ')' \n
104  // - range: '[', ']' \n
105  //
106  // \note The presence of '{', '}' regex bounds is not considered
107  inline static bool meta(char c);
108 
109 
110  // Constructors
111 
112  //- Construct null
113  inline regExpPosix();
114 
115  //- Copy construct - disallowed
116  regExpPosix(const regExpPosix&) = delete;
117 
118  //- Move construct
119  inline regExpPosix(regExpPosix&& rgx);
120 
121  //- Construct from character array
122  inline explicit regExpPosix(const char* pattern);
123 
124  //- Construct from string
125  inline explicit regExpPosix(const std::string& pattern);
126 
127  //- Construct from character array, optionally ignore case
128  inline regExpPosix(const char* pattern, bool ignoreCase);
129 
130  //- Construct from string, optionally ignore case
131  inline regExpPosix(const std::string& pattern, bool ignoreCase);
132 
133 
134  //- Destructor
135  inline ~regExpPosix();
136 
137 
138  // Member Functions
139 
140  // Access
141 
142  //- Return true if a precompiled expression does not exist
143  inline bool empty() const noexcept;
144 
145  //- Return true if a precompiled expression exists
146  inline bool exists() const noexcept;
147 
148  //- The number of capture groups for a non-empty expression
149  inline unsigned ngroups() const;
150 
151 
152  // Editing
153 
154  //- Clear expression.
155  // \return True if expression had existed prior to the clear.
156  bool clear();
157 
158  //- Swap contents
159  inline void swap(regExpPosix& rgx);
160 
161  //- Compile pattern into a regular expression, optionally ignore case.
162  // \return True if the pattern was compiled
163  bool set(const char* pattern, bool ignoreCase=false);
164 
165  //- Compile pattern into a regular expression, optionally ignore case.
166  // \return True if the pattern was compiled
167  bool set(const std::string& pattern, bool ignoreCase=false);
168 
169 
170  // Matching/Searching
171 
172  //- Find position within the text.
173  // \return The index where it begins or string::npos if not found
174  std::string::size_type find(const std::string& text) const;
175 
176  //- True if the regex matches the entire text.
177  // The begin-of-line (^) and end-of-line ($) anchors are implicit
178  bool match(const std::string& text) const;
179 
180  //- True if the regex matches the text, set the matches.
181  // The first group starts at index 1 (0 is the entire match).
182  // The begin-of-line (^) and end-of-line ($) anchors are implicit
183  bool match(const std::string& text, results_type& matches) const;
184 
185  //- Return true if the regex was found within the text
186  inline bool search(const std::string& text) const;
187 
188 
189  // Member Operators
190 
191  //- Perform match on text
192  inline bool operator()(const std::string& text) const;
193 
194  //- Copy assignment - disallowed
195  void operator=(const regExpPosix&) = delete;
196 
197  //- Move assignment
198  inline void operator=(regExpPosix&& rgx);
199 
200  //- Assign and compile pattern from a character array.
201  // Matching is case sensitive.
202  inline void operator=(const char* pattern);
203 
204  //- Assign and compile pattern from string.
205  // Matching is case sensitive.
206  inline void operator=(const std::string& pattern);
207 };
208 
209 
210 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
211 
212 } // End namespace Foam
213 
214 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
215 
216 #include "regExpPosixI.H"
217 
218 #endif
219 
220 // ************************************************************************* //
Foam::regExpPosix
Wrapper around POSIX extended regular expressions.
Definition: regExpPosix.H:75
Foam::regExpPosix::swap
void swap(regExpPosix &rgx)
Swap contents.
Definition: regExpPosixI.H:126
Foam::regExpPosix::search
bool search(const std::string &text) const
Return true if the regex was found within the text.
Definition: regExpPosixI.H:120
Foam::regExpPosix::find
std::string::size_type find(const std::string &text) const
Find position within the text.
Definition: regExpPosix.C:126
Foam::SubStrings
Sub-ranges of a string with a structure similar to std::match_results, but without the underlying reg...
Definition: CStringList.H:63
Foam::regExpPosix::~regExpPosix
~regExpPosix()
Destructor.
Definition: regExpPosixI.H:94
Foam::regExpPosix::results_type
SubStrings< std::string > results_type
Type for matches - similar to std::smatch.
Definition: regExpPosix.H:87
Foam::regExpPosix::meta
static bool meta(char c)
Test if character appears to be a regular expression meta-character.
Definition: regExpPosixI.H:32
size_type
graph_traits< Graph >::vertices_size_type size_type
Definition: SloanRenumber.C:76
Foam::regExpPosix::empty
bool empty() const noexcept
Return true if a precompiled expression does not exist.
Definition: regExpPosixI.H:102
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::regExpPosix::set
bool set(const char *pattern, bool ignoreCase=false)
Compile pattern into a regular expression, optionally ignore case.
Definition: regExpPosix.C:69
Foam::regExpPosix::grammar
static int grammar
Grammar (unused) - for compatibility with Foam::regExpCxx.
Definition: regExpPosix.H:93
Foam::regExpPosix::regExpPosix
regExpPosix()
Construct null.
Definition: regExpPosixI.H:46
Foam::regExpPosix::ngroups
unsigned ngroups() const
The number of capture groups for a non-empty expression.
Definition: regExpPosixI.H:114
Foam::regExpPosix::match
bool match(const std::string &text) const
True if the regex matches the entire text.
Definition: regExpPosix.C:143
Foam::regExpPosix::exists
bool exists() const noexcept
Return true if a precompiled expression exists.
Definition: regExpPosixI.H:108
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::regExpPosix::clear
bool clear()
Clear expression.
Definition: regExpPosix.C:54