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 const Foam::wordRes& Foam::wordRes::null()
31 {
32  return NullObjectRef<wordRes>();
33 }
34 
35 
36 inline Foam::label Foam::wordRes::first_match
37 (
38  const UList<wordRe>& selectors,
39  const std::string& text,
40  const bool literal
41 )
42 {
43  label index = 0;
44  for (const wordRe& select : selectors)
45  {
46  if (select.match(text, literal))
47  {
48  return index;
49  }
50  ++index;
51  }
52 
53  return -1;
54 }
55 
56 
57 inline Foam::wordRe::compOption Foam::wordRes::found_matched
58 (
59  const UList<wordRe>& selectors,
60  const std::string& text
61 )
62 {
63  auto retval(wordRe::compOption::UNKNOWN);
64 
65  for (const wordRe& select : selectors)
66  {
67  if (select.isLiteral())
68  {
69  if (select.match(text, true))
70  {
71  return wordRe::compOption::LITERAL;
72  }
73  }
74  else if
75  (
76  // Only match regex once
77  retval == wordRe::compOption::UNKNOWN
78  && select.match(text, false)
79  )
80  {
81  retval = wordRe::compOption::REGEX;
82  }
83  }
84 
85  return retval;
86 }
87 
88 
89 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
90 
91 inline bool Foam::wordRes::match(const std::string& text, bool literal) const
92 {
93  return (first_match(*this, text, literal) >= 0);
94 }
95 
96 
98 Foam::wordRes::matched(const std::string& text) const
99 {
100  return found_matched(*this, text);
101 }
102 
103 
104 template<class StringType>
106 (
107  const UList<StringType>& input,
108  const bool invert
109 ) const
110 {
111  const label len = input.size();
112 
113  labelList indices(len);
114 
115  label count = 0;
116  for (label i=0; i < len; ++i)
117  {
118  if (match(input[i]) ? !invert : invert)
119  {
120  indices[count] = i;
121  ++count;
122  }
123  }
124  indices.resize(count);
126  return indices;
127 }
128 
129 
130 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
131 
132 inline bool Foam::wordRes::operator()(const std::string& text) const
133 {
134  return (wordRes::first_match(*this, text) >= 0);
135 }
136 
137 
138 // * * * * * * * * * * * * * * * * Functors * * * * * * * * * * * * * * * * //
139 
141 (
142  const UList<wordRe>& allow
143 )
144 :
145  allow_(allow)
146 {}
147 
148 
150 (
151  const UList<wordRe>& allow,
152  const UList<wordRe>& deny
153 )
154 :
155  allow_(allow),
156  deny_(deny)
157 {}
159 
160 inline bool Foam::wordRes::matcher::empty() const noexcept
161 {
162  return allow_.empty();
163 }
165 inline bool Foam::wordRes::filter::empty() const noexcept
166 {
167  return (allow_.empty() && deny_.empty());
168 }
169 
171 inline bool Foam::wordRes::matcher::operator()(const std::string& text) const
172 {
173  return (wordRes::first_match(allow_, text) >= 0);
174 }
175 
176 
177 inline bool Foam::wordRes::filter::operator()(const std::string& text) const
178 {
179  if (allow_.empty())
180  {
181  // No allow specified, so accept everything that is NOT blocked
182  return (deny_.empty() || (wordRes::first_match(deny_, text) < 0));
183  }
184  else if (deny_.empty())
185  {
186  // Nothing blocked, apply accept filter
187  return (wordRes::first_match(allow_, text) >= 0);
188  }
189  else
190  {
191  // Both accept and deny filters, need to search more carefully
192  const auto result = wordRes::found_matched(allow_, text);
193 
194  return
195  (
196  result == wordRe::LITERAL
197  ? true
198  :
199  (
200  result == wordRe::REGEX
201  && (wordRes::first_match(deny_, text) < 0)
202  )
203  );
204  }
205 }
206 
207 
208 // ************************************************************************* //
bool match(const UList< wordRe > &patterns, const std::string &text)
Return true if text matches one of the regular expressions.
Definition: stringOps.H:77
filter(const UList< wordRe > &allow, const UList< wordRe > &deny)
Construct with &#39;allow&#39; and &#39;deny&#39; matchers.
Definition: wordResI.H:143
labelList matching(const UList< StringType > &input, const bool invert=false) const
Return list indices for all matches.
wordRe::compOption matched(const std::string &text) const
Smart match in the list of matchers, returning the match type.
Definition: wordResI.H:91
matcher(const UList< wordRe > &allow)
Construct with &#39;allow&#39; matcher.
Definition: wordResI.H:134
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
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 const wordRes & null()
Return a null wordRes - a reference to the NullObject.
Definition: wordResI.H:23
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:33
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:53
bool empty() const noexcept
Nothing defined.
Definition: wordResI.H:158
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
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:29
bool operator()(const std::string &text) const
True if text matches ANY of the entries.
Definition: wordResI.H:164
bool match(const std::string &text, bool literal=false) const
Smart match as literal or regex, stopping on the first match.
Definition: wordResI.H:84
String literal.
Definition: wordRe.H:109
bool operator()(const std::string &text) const
Identical to match(), for use as a predicate.
Definition: wordResI.H:125
bool operator()(const std::string &text) const
True if matched but not blocked.
Definition: wordResI.H:170
bool empty() const noexcept
Nothing defined.
Definition: wordResI.H:153