Loading...
Searching...
No Matches
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-2025 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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
30inline 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 }
47 return -1;
48}
49
50
51// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
52
53inline 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 {
86 }
87 }
88 else if
89 (
90 // Only match regex once
92 && select.match(text, false)
93 )
94 {
96 }
97 }
98
99 return retval;
100}
101
102
103template<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);
130
131 return indices;
132}
133
134
135template<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
169inline bool Foam::wordRes::match(const std::string& text, bool literal) const
170{
171 return wordRes::match(*this, text, literal);
172}
173
174
176Foam::wordRes::matched(const std::string& text) const
177{
178 return wordRes::matched(*this, text);
179}
180
181
182template<class StringType>
184(
185 const UList<StringType>& input,
186 const bool invert
187) const
189 return wordRes::matching(*this, input, invert);
190}
191
192
193// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
194
195inline bool Foam::wordRes::operator()(const std::string& text) const
197 return wordRes::match(*this, text);
198}
199
200
201// * * * * * * * * * * * * * * * * Functors * * * * * * * * * * * * * * * * //
202
204(
205 const UList<wordRe>& selectors
206) noexcept
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{}
221
223inline bool Foam::wordRes::matcher::empty() const noexcept
224{
225 return select_.empty();
226}
227
229inline bool Foam::wordRes::filter::empty() const noexcept
230{
231 return (allow_.empty() && deny_.empty());
232}
233
235inline bool Foam::wordRes::matcher::operator()(const std::string& text) const
236{
237 return wordRes::match(select_, text);
238}
239
240
241inline bool Foam::wordRes::filter::operator()(const std::string& text) const
242{
243 if (allow_.empty())
244 {
245 // Did not specify allow
246 // => accept everything that is NOT blocked
247 return (deny_.empty() || !wordRes::match(deny_, text));
248 }
249 else if (deny_.empty())
250 {
251 // Specified allow but did not specify blocked
252 // => select with accept filter
253 return wordRes::match(allow_, text);
254 }
255 else
256 {
257 // Both accept and deny filters, need to search more carefully
258 const auto result = wordRes::matched(allow_, text);
259
260 return
261 (
262 result == wordRe::LITERAL
263 ? true
264 :
265 (
266 result == wordRe::REGEX
267 && !wordRes::match(deny_, text)
268 )
269 );
270 }
271}
272
273
274// ************************************************************************* //
void resize(const label len)
Adjust allocated size of list.
Definition ListI.H:153
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
bool empty() const noexcept
True if List is empty (ie, size() is zero).
Definition UList.H:701
void size(const label n)
Older name for setAddressableSize.
Definition UList.H:118
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition wordRe.H:81
compOption
Enumeration with compile options.
Definition wordRe.H:108
@ LITERAL
String literal.
Definition wordRe.H:109
@ REGEX
Regular expression.
Definition wordRe.H:110
@ UNKNOWN
Unknown content (for return value).
Definition wordRe.H:114
static labelList matching(const wordRe &select, const UList< StringType > &input, const bool invert=false)
Determine the list indices for all matches.
bool operator()(const std::string &text) const
Identical to match(), for use as a predicate.
Definition wordResI.H:188
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
static bool match(const UList< wordRe > &selectors, const std::string &text, bool literal=false)
Test for a match of any selectors against the text.
Definition wordResI.H:47
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:139
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition BitOps.H:73
List< label > labelList
A List of labels.
Definition List.H:62
const direction noexcept
Definition scalarImpl.H:265
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition ListOps.C:28
filter(const UList< wordRe > &allow, const UList< wordRe > &deny) noexcept
Construct with allow and deny matchers.
Definition wordResI.H:206
bool empty() const noexcept
No filtering defined.
Definition wordResI.H:222
bool operator()(const std::string &text) const
Apply filter against specified text.
Definition wordResI.H:234
bool empty() const noexcept
No selectors defined.
Definition wordResI.H:216
bool operator()(const std::string &text) const
Apply matcher predicate.
Definition wordResI.H:228
matcher(const UList< wordRe > &selectors) noexcept
Construct with select matcher(s).
Definition wordResI.H:197