wordResI.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-2022 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 Foam::label Foam::wordRes::first_match
31 (
32  const UList<wordRe>& selectors,
33  const std::string& text,
34  const bool literal
35 )
36 {
37  label index = 0;
38  for (const wordRe& select : selectors)
39  {
40  if (select.match(text, literal))
41  {
42  return index;
43  }
44  ++index;
45  }
46 
47  return -1;
48 }
49 
50 
51 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
52 
53 inline bool Foam::wordRes::match
54 (
55  const UList<wordRe>& selectors,
56  const std::string& text,
57  bool literal
58 )
59 {
60  for (const wordRe& select : selectors)
61  {
62  if (select.match(text, literal))
63  {
64  return true;
65  }
66  }
67  return false;
68 }
69 
70 
72 (
73  const UList<wordRe>& selectors,
74  const std::string& text
75 )
76 {
77  auto retval(wordRe::compOption::UNKNOWN);
78 
79  for (const wordRe& select : selectors)
80  {
81  if (select.isLiteral())
82  {
83  if (select.match(text, true))
84  {
85  return wordRe::compOption::LITERAL;
86  }
87  }
88  else if
89  (
90  // Only match regex once
91  retval == wordRe::compOption::UNKNOWN
92  && select.match(text, false)
93  )
94  {
95  retval = wordRe::compOption::REGEX;
96  }
97  }
98 
99  return retval;
100 }
101 
102 
103 template<class StringType>
105 (
106  const wordRe& select,
107  const UList<StringType>& input,
108  const bool invert
109 )
110 {
111  if (select.empty() && !invert)
112  {
113  return labelList();
114  }
115 
116  const label len = input.size();
117 
118  labelList indices(len);
119 
120  label count = 0;
121  for (label i = 0; i < len; ++i)
122  {
123  if (select.match(input[i]) ? !invert : invert)
124  {
125  indices[count] = i;
126  ++count;
127  }
128  }
129  indices.resize(count);
131  return indices;
132 }
133 
134 
135 template<class StringType>
137 (
138  const UList<wordRe>& selectors,
139  const UList<StringType>& input,
140  const bool invert
141 )
142 {
143  if (selectors.empty() && !invert)
144  {
145  return labelList();
146  }
147 
148  const label len = input.size();
149 
150  labelList indices(len);
151 
152  label count = 0;
153  for (label i = 0; i < len; ++i)
154  {
155  if (wordRes::match(selectors, input[i]) ? !invert : invert)
156  {
157  indices[count] = i;
158  ++count;
159  }
160  }
161  indices.resize(count);
163  return indices;
164 }
165 
166 
167 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
168 
169 inline bool Foam::wordRes::match(const std::string& text, bool literal) const
170 {
171  return wordRes::match(*this, text, literal);
172 }
173 
174 
176 Foam::wordRes::matched(const std::string& text) const
177 {
178  return wordRes::matched(*this, text);
179 }
180 
181 
182 template<class StringType>
184 (
185  const UList<StringType>& input,
186  const bool invert
187 ) const
188 {
189  return wordRes::matching(*this, input, invert);
190 }
191 
192 
193 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
194 
195 inline bool Foam::wordRes::operator()(const std::string& text) const
196 {
197  return wordRes::match(*this, text);
198 }
199 
200 
201 // * * * * * * * * * * * * * * * * Functors * * * * * * * * * * * * * * * * //
202 
204 (
205  const UList<wordRe>& selectors
207 :
208  select_(selectors)
209 {}
210 
211 
213 (
214  const UList<wordRe>& allow,
215  const UList<wordRe>& deny
217 :
218  allow_(allow),
219  deny_(deny)
220 {}
222 
223 inline bool Foam::wordRes::matcher::empty() const noexcept
224 {
225  return select_.empty();
226 }
228 inline bool Foam::wordRes::filter::empty() const noexcept
229 {
230  return (allow_.empty() && deny_.empty());
231 }
232 
234 inline bool Foam::wordRes::matcher::operator()(const std::string& text) const
235 {
236  return wordRes::match(select_, text);
237 }
238 
239 
240 inline bool Foam::wordRes::filter::operator()(const std::string& text) const
241 {
242  if (allow_.empty())
243  {
244  // No allow specified, so accept everything that is NOT blocked
245  return (deny_.empty() || !wordRes::match(deny_, text));
246  }
247  else if (deny_.empty())
248  {
249  // Nothing blocked, apply accept filter
250  return wordRes::match(allow_, text);
251  }
252  else
253  {
254  // Both accept and deny filters, need to search more carefully
255  const auto result = wordRes::matched(allow_, text);
256 
257  return
258  (
259  result == wordRe::LITERAL
260  ? true
261  :
262  (
263  result == wordRe::REGEX
264  && !wordRes::match(deny_, text)
265  )
266  );
267  }
268 }
269 
270 
271 // ************************************************************************* //
static wordRe::compOption matched(const UList< wordRe > &selectors, const std::string &text)
Smart match across entire list, returning the best match type.
Definition: wordResI.H:65
List< bool > select(const label n, const labelUList &locations)
Construct a selection list of bools (all false) with the given pre-size, subsequently add specified l...
Definition: BitOps.C:134
compOption
Enumeration with compile options.
Definition: wordRe.H:107
bool empty() const noexcept
True if List is empty (ie, size() is zero)
Definition: UList.H:675
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of &#39;true&#39; entries.
Definition: BitOps.H:73
Regular expression.
Definition: wordRe.H:110
static labelList matching(const wordRe &select, const UList< StringType > &input, const bool invert=false)
Determine the list indices for all matches.
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:33
bool empty() const noexcept
Nothing defined.
Definition: wordResI.H:221
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings...
Definition: wordRe.H:78
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
const direction noexcept
Definition: Scalar.H:258
matcher(const UList< wordRe > &selectors) noexcept
Construct with select matcher(s)
Definition: wordResI.H:197
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:30
static bool match(const UList< wordRe > &selectors, const std::string &text, bool literal=false)
Test for a match.
Definition: wordResI.H:47
bool operator()(const std::string &text) const
True if text matches ANY of the selectors. Always false if entries are empty.
Definition: wordResI.H:227
filter(const UList< wordRe > &allow, const UList< wordRe > &deny) noexcept
Construct with &#39;allow&#39; and &#39;deny&#39; matchers.
Definition: wordResI.H:206
String literal.
Definition: wordRe.H:109
List< label > labelList
A List of labels.
Definition: List.H:62
bool operator()(const std::string &text) const
Identical to match(), for use as a predicate.
Definition: wordResI.H:188
bool operator()(const std::string &text) const
True if matched but not blocked.
Definition: wordResI.H:233
bool empty() const noexcept
No selectors defined.
Definition: wordResI.H:216