regExpPosixI.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-2021 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 #include <algorithm>
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
33 :
34  preg_(nullptr),
35  ctrl_(0)
36 {}
37 
38 
40 :
41  preg_(rgx.preg_),
42  ctrl_(rgx.ctrl_)
43 {
44  rgx.preg_ = nullptr;
45  rgx.ctrl_ = 0;
46 }
47 
48 
50 (
51  const char* pattern,
52  const bool ignoreCase
53 )
54 :
55  preg_(nullptr),
56  ctrl_(0)
57 {
58  set(pattern, ignoreCase);
59 }
60 
61 
63 (
64  const std::string& pattern,
65  const bool ignoreCase
66 )
67 :
68  preg_(nullptr),
69  ctrl_(0)
70 {
71  set(pattern, ignoreCase);
72 }
73 
74 
75 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
76 
78 {
79  clear();
80 }
81 
82 
83 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
84 
85 inline bool Foam::regExpPosix::empty() const noexcept
86 {
87  return !preg_;
88 }
89 
90 
91 inline bool Foam::regExpPosix::exists() const noexcept
92 {
93  return preg_;
94 }
95 
96 
97 inline bool Foam::regExpPosix::negated() const noexcept
98 {
99  return (ctrl_ == ctrlType::NEGATED);
100 }
101 
102 
103 inline bool Foam::regExpPosix::negate(bool on) noexcept
104 {
105  bool old(ctrl_ == ctrlType::NEGATED);
106 
107  if (on)
108  {
109  if (ctrl_)
110  {
111  ctrl_ = ctrlType::NEGATED;
112  }
113  }
114  else if (old)
115  {
116  ctrl_ = ctrlType::NORMAL;
117  }
118 
119  return old;
120 }
121 
122 
123 inline unsigned Foam::regExpPosix::ngroups() const
124 {
125  return (preg_ && ctrl_ == ctrlType::NORMAL) ? preg_->re_nsub : 0;
126 }
127 
128 
129 inline bool Foam::regExpPosix::set(const char* pattern, bool ignoreCase)
130 {
131  // Silently handle nullptr
132  return set_pattern
133  (
134  pattern,
135  (pattern ? std::char_traits<char>::length(pattern) : 0),
136  ignoreCase
137  );
138 }
139 
140 
141 inline bool Foam::regExpPosix::set(const std::string& pattern, bool ignoreCase)
142 {
143  return set_pattern
144  (
145  pattern.data(),
146  pattern.length(),
147  ignoreCase
148  );
149 }
150 
151 
152 inline bool Foam::regExpPosix::search(const std::string& text) const
153 {
154  if (!ctrl_)
155  {
156  return false;
157  }
158  else if (text.empty())
159  {
160  return (ctrl_ == ctrlType::NEGATED);
161  }
162 
163  return std::string::npos != find(text);
164 }
165 
166 
168 {
169  if (this != &rgx)
170  {
171  // Self-swap is a no-op
172  std::swap(preg_, rgx.preg_);
173  std::swap(ctrl_, rgx.ctrl_);
174  }
175 }
176 
177 
178 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
179 
180 inline bool Foam::regExpPosix::operator()(const std::string& text) const
181 {
182  return match(text);
183 }
184 
185 
187 {
188  if (this != &rgx)
189  {
190  // Self-assignment is a no-op
191  clear();
192  swap(rgx);
193  }
194 }
195 
196 
197 inline void Foam::regExpPosix::operator=(const char* pattern)
198 {
199  set(pattern);
200 }
201 
202 
203 inline void Foam::regExpPosix::operator=(const std::string& pattern)
204 {
205  set(pattern);
206 }
207 
208 
209 // ************************************************************************* //
Foam::regExpPosix
Wrapper around POSIX extended regular expressions with some additional prefix-handling....
Definition: regExpPosix.H:84
Foam::regExpPosix::swap
void swap(regExpPosix &rgx)
Swap contents.
Definition: regExpPosixI.H:167
Foam::BitOps::set
void set(List< bool > &bools, const labelRange &range)
Set the specified range 'on' in a boolList.
Definition: BitOps.C:37
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::operator=
void operator=(const regExpPosix &)=delete
Copy assignment - disallowed.
Foam::regExpPosix::~regExpPosix
~regExpPosix()
Destructor.
Definition: regExpPosixI.H:77
Foam::regExpPosix::operator()
bool operator()(const std::string &text) const
Perform match on text.
Definition: regExpPosixI.H:180
Foam::regExpPosix::empty
bool empty() const noexcept
Return true if a precompiled expression does not exist.
Definition: regExpPosixI.H:85
Foam::ListOps::find
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Find index of the first occurrence that satisfies the predicate.
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::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:76
Foam::regExpPosix::ngroups
unsigned ngroups() const
Definition: regExpPosixI.H:123
clear
patchWriters clear()
Foam::regExpPosix::exists
bool exists() const noexcept
Return true if a precompiled expression exists.
Definition: regExpPosixI.H:91