regExpCxxI.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 \*---------------------------------------------------------------------------*/
27 
28 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
29 
30 inline std::regex::flag_type Foam::regExpCxx::syntax()
31 {
32  // 0 = extended, 1 = ECMAScript
33  return
34  (
36  ? std::regex::ECMAScript
37  : std::regex::extended
38  );
39 }
40 
41 
42 inline bool Foam::regExpCxx::meta(const char c)
43 {
44  return
45  (
46  (c == '.') // any character
47  || (c == '*' || c == '+' || c == '?') // quantifiers
48  || (c == '(' || c == ')' || c == '|') // grouping/branching
49  || (c == '[' || c == ']') // range
50  );
51 }
52 
53 
54 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
55 
57 :
58  re_(),
59  ok_(false)
60 {}
61 
62 
64 :
65  re_(rgx.re_),
66  ok_(rgx.ok_)
67 {}
68 
69 
71 :
72  re_(std::move(rgx.re_)),
73  ok_(rgx.ok_)
74 {
75  rgx.ok_ = false;
76 }
77 
78 
79 inline Foam::regExpCxx::regExpCxx(const char* pattern)
80 :
81  re_(),
82  ok_(false)
83 {
84  set(pattern, false);
85 }
86 
87 
88 inline Foam::regExpCxx::regExpCxx(const std::string& pattern)
89 :
90  re_(),
91  ok_(false)
92 {
93  set(pattern, false);
94 }
95 
96 
97 inline Foam::regExpCxx::regExpCxx(const char* pattern, bool ignoreCase)
98 :
99  re_(),
100  ok_(false)
101 {
102  set(pattern, ignoreCase);
103 }
104 
105 
106 inline Foam::regExpCxx::regExpCxx(const std::string& pattern, bool ignoreCase)
107 :
108  re_(),
109  ok_(false)
110 {
111  set(pattern, ignoreCase);
112 }
113 
114 
115 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
116 
117 inline bool Foam::regExpCxx::empty() const noexcept
118 {
119  return !ok_;
120 }
121 
122 
123 inline bool Foam::regExpCxx::exists() const noexcept
124 {
125  return ok_;
126 }
127 
128 
129 inline unsigned Foam::regExpCxx::ngroups() const
130 {
131  return ok_ ? re_.mark_count() : 0;
132 }
133 
134 
135 inline bool Foam::regExpCxx::nocase() const
136 {
137  return ok_ && ((re_.flags() & std::regex::icase) == std::regex::icase);
138 }
139 
140 
142 {
143  if (ok_)
144  {
145  re_.assign("");
146  ok_ = false;
147 
148  return true;
149  }
150 
151  return false;
152 }
153 
154 
156 {
157  if (this != &rgx)
158  {
159  // Self-swap is a no-op
160  re_.swap(rgx.re_);
161  std::swap(ok_, rgx.ok_);
162  }
163 }
164 
165 
167 Foam::regExpCxx::find(const std::string& text) const
168 {
169  std::smatch mat;
170  if (!text.empty() && std::regex_search(text, mat, re_))
171  {
172  return mat.position(0);
173  }
174 
175  return std::string::npos;
176 }
177 
178 
179 inline bool Foam::regExpCxx::search(const std::string& text) const
180 {
181  return (ok_ && !text.empty() && std::regex_search(text, re_));
182 }
183 
184 
185 inline bool Foam::regExpCxx::match(const std::string& text) const
186 {
187  return (ok_ && !text.empty() && std::regex_match(text, re_));
188 }
189 
190 
191 inline bool Foam::regExpCxx::match
192 (
193  const std::string& text,
194  std::smatch& matches
195 ) const
196 {
197  return std::regex_match(text, matches, re_);
198 }
199 
200 
201 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
202 
203 inline bool Foam::regExpCxx::operator()(const std::string& text) const
204 {
205  return match(text);
206 }
207 
208 
209 inline void Foam::regExpCxx::operator=(const regExpCxx& rgx)
210 {
211  re_ = rgx.re_;
212  ok_ = rgx.ok_;
213 }
214 
215 
217 {
218  clear();
219  swap(rgx);
220 }
221 
222 
223 inline void Foam::regExpCxx::operator=(const char* pattern)
224 {
225  set(pattern);
226 }
227 
228 
229 inline void Foam::regExpCxx::operator=(const std::string& pattern)
230 {
231  set(pattern);
232 }
233 
234 
235 // ************************************************************************* //
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::operator()
bool operator()(const std::string &text) const
Perform match on text.
Definition: regExpCxxI.H:203
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:75
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::regExpCxx::clear
bool clear()
Clear expression.
Definition: regExpCxxI.H:141
Foam::stringOps::match
bool match(const UList< wordRe > &patterns, const std::string &text)
Return true if text matches one of the regular expressions.
Definition: stringOps.H:75
clear
patchWriters clear()
Foam::regExpCxx::operator=
void operator=(const regExpCxx &rgx)
Copy assignment.
Definition: regExpCxxI.H:209
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