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-2021 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  with some additional prefix-handling. The prefix-handling is
33  loosely oriented on PCRE regular expressions and provides a
34  simple means of tuning the expressions.
35 
36  The prefixes are detected as \c (?...) at the beginning of
37  the regular expression. Any unknown/unsupported prefixes are silently
38  ignored.
39 
40  - "(?!i)" :
41  one or more embedded pattern-match modifiers for the entire pattern.
42  - the \c 'i' indicates ignore-case
43  - the \c '!' (exclamation) indicates negated (inverted) matching
44  .
45 
46 SeeAlso
47  The manpage regex(7) for more information about POSIX regular expressions.
48  These differ somewhat from \c Perl and \c sed regular expressions.
49 
50 SeeAlso
51  Foam::regExp and Foam::regExpCxx
52 
53 Warning
54  This class should not be used directly.
55  Use the Foam::regExp typedef instead.
56 
57 \deprecated
58  This class will be superseded by Foam::regExpCxx as compiler support
59  for regular expressions continues to improve.
60 
61 SourceFiles
62  regExpPosix.C
63  regExpPosixI.H
64 
65 \*---------------------------------------------------------------------------*/
66 
67 #ifndef regExpPosix_H
68 #define regExpPosix_H
69 
70 #include "regExpCxx.H"
71 #include <regex.h>
72 
73 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
74 
75 namespace Foam
76 {
77 
78 // Forward Declarations
79 template<class StringType> class SubStrings;
80 
81 /*---------------------------------------------------------------------------*\
82  Class regExpPosix Declaration
83 \*---------------------------------------------------------------------------*/
84 
85 class regExpPosix
86 {
87  // Data Types
88 
89  //- Simple control types
90  enum ctrlType { EMPTY = 0, NORMAL = 1, NEGATED = 2 };
91 
92 
93  // Private Data
94 
95  //- Compiled regular expression
96  regex_t* preg_;
97 
98  //- Track if input pattern is non-empty, negated etc.
99  unsigned char ctrl_;
100 
101 
102  // Private Member Functions
103 
104  //- Assign pattern
105  bool set_pattern(const char* pattern, size_t len, bool ignoreCase);
106 
107 
108 public:
109 
110  // Public Types
111 
112  //- Type for matches - similar to std::smatch
114 
115 
116  // Static Member Data
117 
118  //- Grammar (unused) - for compatibility with Foam::regExpCxx
119  static int grammar;
120 
121 
122  // Static Member Functions
123 
124  //- Test if character is a regex meta-character
125  inline static bool is_meta(const char c) noexcept
126  {
127  return regExpCxx::is_meta(c);
128  }
129 
130  //- Test if string contains any (unquoted) meta-characters
131  inline static bool is_meta
132  (
133  const std::string& str,
134  const char quote = '\\'
135  )
136  {
137  return regExpCxx::is_meta(str, quote);
138  }
139 
140 
141  // Public Classes
142 
143  //- Functor wrapper for testing meta-characters
144  using meta = regExpCxx::meta;
145 
146 
147  // Constructors
148 
149  //- Default construct
150  inline regExpPosix() noexcept;
151 
152  //- Copy construct - disallowed
153  regExpPosix(const regExpPosix&) = delete;
154 
155  //- Move construct
156  inline regExpPosix(regExpPosix&& rgx) noexcept;
157 
158  //- Construct from character array, optionally ignore case
159  inline explicit regExpPosix
160  (
161  const char* pattern,
162  const bool ignoreCase = false
163  );
164 
165  //- Construct from string, optionally ignore case
166  inline explicit regExpPosix
167  (
168  const std::string& pattern,
169  const bool ignoreCase = false
170  );
171 
172 
173  //- Destructor
174  inline ~regExpPosix();
175 
176 
177  // Member Functions
178 
179  // Access
180 
181  //- Return true if a precompiled expression does not exist
182  inline bool empty() const noexcept;
183 
184  //- Return true if a precompiled expression exists
185  inline bool exists() const noexcept;
186 
187  //- True if pattern matching is negated
188  inline bool negated() const noexcept;
189 
190  //- Change pattern negation, return previous value
191  inline bool negate(bool on) noexcept;
192 
193  //- The number of capture groups for a non-empty,
194  //- non-negated expressions
195  inline unsigned ngroups() const;
196 
197 
198  // Editing
199 
200  //- Clear expression.
201  // \return True if expression had existed prior to the clear.
202  bool clear();
203 
204  //- Swap contents
205  inline void swap(regExpPosix& rgx);
206 
207  //- Compile pattern into a regular expression, optionally ignore case.
208  // \return True if the pattern was compiled
209  inline bool set(const char* pattern, bool ignoreCase=false);
210 
211  //- Compile pattern into a regular expression, optionally ignore case.
212  // \return True if the pattern was compiled
213  inline bool set(const std::string& pattern, bool ignoreCase=false);
214 
215 
216  // Matching/Searching
217 
218  //- Find position within the text.
219  // \return The index where it begins or string::npos if not found
220  //
221  // \note does not properly work with negated regex!
222  std::string::size_type find(const std::string& text) const;
223 
224  //- True if the regex matches the entire text.
225  // The begin-of-line (^) and end-of-line ($) anchors are implicit
226  bool match(const std::string& text) const;
227 
228  //- True if the regex matches the text, set the matches.
229  // The first group starts at index 1 (0 is the entire match).
230  // The begin-of-line (^) and end-of-line ($) anchors are implicit
231  //
232  // \note does not properly work with negated regex!
233  bool match(const std::string& text, results_type& matches) const;
234 
235  //- Return true if the regex was found within the text
236  inline bool search(const std::string& text) const;
237 
238 
239  // Member Operators
240 
241  //- Perform match on text
242  inline bool operator()(const std::string& text) const;
243 
244  //- Copy assignment - disallowed
245  void operator=(const regExpPosix&) = delete;
246 
247  //- Move assignment
248  inline void operator=(regExpPosix&& rgx);
249 
250  //- Assign and compile pattern from a character array.
251  // Matching is case sensitive.
252  inline void operator=(const char* pattern);
253 
254  //- Assign and compile pattern from string.
255  // Matching is case sensitive.
256  inline void operator=(const std::string& pattern);
257 };
258 
259 
260 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
261 
262 } // End namespace Foam
263 
264 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
265 
266 #include "regExpPosixI.H"
267 
268 #endif
269 
270 // ************************************************************************* //
Foam::regExpPosix
Wrapper around POSIX extended regular expressions with some additional prefix-handling....
Definition: regExpPosix.H:84
regExpCxx.H
Foam::regExpPosix::swap
void swap(regExpPosix &rgx)
Swap contents.
Definition: regExpPosixI.H:167
Foam::regExpPosix::search
bool search(const std::string &text) const
Return true if the regex was found within the text.
Definition: regExpPosixI.H:152
Foam::regExpPosix::regExpPosix
regExpPosix() noexcept
Default construct.
Definition: regExpPosixI.H:32
Foam::regExpPosix::negated
bool negated() const noexcept
True if pattern matching is negated.
Definition: regExpPosixI.H:97
Foam::regExpPosix::negate
bool negate(bool on) noexcept
Change pattern negation, return previous value.
Definition: regExpPosixI.H:103
Foam::regExpPosix::find
std::string::size_type find(const std::string &text) const
Find position within the text.
Definition: regExpPosix.C:151
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::is_meta
static bool is_meta(const char c) noexcept
Test if character is a regex meta-character.
Definition: regExpPosix.H:124
Foam::regExpPosix::results_type
SubStrings< std::string > results_type
Type for matches - similar to std::smatch.
Definition: regExpPosix.H:112
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:85
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: regExpPosixI.H:129
Foam::regExpPosix::grammar
static int grammar
Grammar (unused) - for compatibility with Foam::regExpCxx.
Definition: regExpPosix.H:118
Foam::regExpCxx::meta
Functor wrapper for testing meta-characters.
Definition: regExpCxx.H:146
Foam::regExpPosix::ngroups
unsigned ngroups() const
Definition: regExpPosixI.H:123
Foam::regExpPosix::match
bool match(const std::string &text) const
True if the regex matches the entire text.
Definition: regExpPosix.C:194
Foam::regExpPosix::exists
bool exists() const noexcept
Return true if a precompiled expression exists.
Definition: regExpPosixI.H:91
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.
Foam::regExpPosix::clear
bool clear()
Clear expression.
Definition: regExpPosix.C:135
Foam::regExpCxx::is_meta
static bool is_meta(const char c) noexcept
Test if character is a regex meta-character.
Definition: regExpCxxI.H:42