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-------------------------------------------------------------------------------
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
31{
32 return NullObjectRef<wordRes>();
33}
34
35
36inline 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
57inline 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 {
72 }
73 }
74 else if
75 (
76 // Only match regex once
78 && select.match(text, false)
79 )
80 {
82 }
83 }
84
85 return retval;
86}
87
88
89// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
90
91inline bool Foam::wordRes::match(const std::string& text, bool literal) const
92{
93 return (first_match(*this, text, literal) >= 0);
94}
95
96
98Foam::wordRes::matched(const std::string& text) const
99{
100 return found_matched(*this, text);
101}
102
103
104template<class StringType>
106(
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);
125
126 return indices;
127}
128
129
130// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
131
132inline 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{}
158
159
161{
162 return allow_.empty();
163}
164
166{
167 return (allow_.empty() && deny_.empty());
168}
169
170
171inline bool Foam::wordRes::matcher::operator()(const std::string& text) const
172{
173 return (wordRes::first_match(allow_, text) >= 0);
174}
175
176
177inline 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// ************************************************************************* //
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
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:94
Ostream & operator()() const
Output stream (master only).
Definition: ensightCaseI.H:74
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:83
compOption
Enumeration with compile options.
Definition: wordRe.H:103
@ LITERAL
String literal.
Definition: wordRe.H:104
@ REGEX
Regular expression.
Definition: wordRe.H:105
@ UNKNOWN
Unknown content (for return value).
Definition: wordRe.H:109
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:54
wordRe::compOption matched(const std::string &text) const
Smart match in the list of matchers, returning the match type.
Definition: wordResI.H:98
labelList matching(const UList< StringType > &input, const bool invert=false) const
Return list indices for all matches.
static const wordRes & null()
Return a null wordRes - a reference to the NullObject.
Definition: wordResI.H:30
bool match(const std::string &text, bool literal=false) const
Smart match as literal or regex, stopping on the first match.
Definition: wordResI.H:91
List< bool > select(const label n, const labelUList &locations)
Definition: BitOps.C:142
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
const direction noexcept
Definition: Scalar.H:223
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:36
Functor wrapper of allow/deny lists of wordRe for filtering.
Definition: wordRes.H:174
bool empty() const noexcept
Nothing defined.
Definition: wordResI.H:165
Functor wrapper of a list of wordRe for matching.
Definition: wordRes.H:143
bool empty() const noexcept
Nothing defined.
Definition: wordResI.H:160