Loading...
Searching...
No Matches
stringListOps.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) 2011-2016 OpenFOAM Foundation
9 Copyright (C) 2017-2025 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
12 This file is part of OpenFOAM.
13
14 OpenFOAM is free software: you can redistribute it and/or modify it
15 under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26
27InNamespace
28 Foam
29
30Description
31 Operations on lists of strings.
32
33Namespace
34 Foam::stringListOps
35
36Description
37 Various utility functions to work on lists of strings.
38
39SourceFiles
40 stringListOps.txx
41
42\*---------------------------------------------------------------------------*/
43
44#ifndef Foam_stringListOps_H
45#define Foam_stringListOps_H
46
47#include "labelList.H"
48#include "stringList.H"
49#include "wordRes.H"
50#include "ops.H"
51
52// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
53
54namespace Foam
55{
56 //- Find first list item that matches, -1 on failure
57 template<class UnaryMatchPredicate, class StringType>
59 (
60 const UnaryMatchPredicate& matcher,
61 const UList<StringType>& input,
62 const bool invert=false
63 );
64
65
66 //- Extract list indices for all matches.
67 // The unary match predicate has the following signature:
68 // \code
69 // bool operator()(const std::string& text);
70 // \endcode
71 //
72 // \return List indices for matching strings
73 // \see IndirectList::subset_if
74 template<class UnaryMatchPredicate, class StringType>
76 (
77 const UnaryMatchPredicate& matcher,
78 const UList<StringType>& input,
79 const bool invert=false
80 );
81
82
83 //- Return list indices for strings matching the regular expression
84 // Template partial specialization of findMatchingStrings
85 template<class StringType>
87 (
88 const regExp& matcher,
89 const UList<StringType>& input,
90 const bool invert=false
91 )
92 {
93 return findMatchingStrings(matcher, input, invert);
94 }
95
96
97 //- Return list indices for strings matching the regular expression
98 // Template partial specialization of findMatchingStrings
99 template<class StringType>
101 (
102 const keyType& matcher,
103 const UList<StringType>& input,
104 const bool invert=false
105 )
106 {
107 return
109 matcher.isPattern()
110 ? findMatchingStrings(regExp(matcher), input, invert)
111 : findMatchingStrings(matcher, input, invert)
112 );
113 }
114
115
116 //- Return list indices for strings matching the regular expression
117 // Template partial specialization of findMatchingStrings
118 template<class StringType>
120 (
121 const wordRe& matcher,
122 const UList<StringType>& input,
123 const bool invert=false
124 )
125 {
126 return findMatchingStrings(matcher, input, invert);
127 }
128
129
130 //- Return list indices for strings matching one of the regular expression
131 // Template partial specialization of findMatchingStrings
132 template<class StringType>
134 (
135 const wordRes& matcher,
136 const UList<StringType>& input,
137 const bool invert=false
138 )
139 {
140 return findMatchingStrings(matcher, input, invert);
141 }
142
143 //- Return list indices for strings matching one of the regular expression
144 // Template partial specialization of findMatchingStrings
145 template<class StringType>
148 const UList<wordRe>& patterns,
149 const UList<StringType>& input,
150 const bool invert=false
151 )
152 {
153 return findMatchingStrings(wordRes::matcher(patterns), input, invert);
154 }
155
156
157 // Subsetting multi-string matches (similar to ListOp):
158
159 //- Extract elements of StringList when regular expression matches
160 // optionally invert the match
161 // eg, to extract all selected elements:
162 // \code
163 // subsetMatchingStrings<regExp, stringList>(myRegExp, list);
164 // \endcode
165 // \see IndirectList::subset_if
166 template<class UnaryMatchPredicate, class StringListType>
167 StringListType subsetMatchingStrings
168 (
169 const UnaryMatchPredicate& matcher,
170 const StringListType& input,
171 const bool invert=false
172 );
173
174
175 //- Extract elements of StringList when regular expression matches
176 // Template partial specialization of subsetMatchingStrings
177 template<class StringListType>
178 StringListType subsetStrings
179 (
180 const regExp& matcher,
181 const StringListType& input,
182 const bool invert=false
183 )
184 {
185 return subsetMatchingStrings(matcher, input, invert);
186 }
188
189 //- Extract elements of StringList when regular expression matches
190 // Template partial specialization of subsetMatchingStrings
191 template<class StringListType>
192 StringListType subsetStrings
193 (
194 const keyType& matcher,
195 const StringListType& input,
196 const bool invert=false
197 )
198 {
199 return
200 (
201 matcher.isPattern()
202 ? subsetMatchingStrings(regExp(matcher), input, invert)
203 : subsetMatchingStrings(matcher, input, invert)
204 );
205 }
206
207 //- Extract elements of StringList when regular expression matches
208 // Template partial specialization of subsetMatchingStrings
209 template<class StringListType>
210 StringListType subsetStrings
211 (
212 const wordRe& matcher,
213 const StringListType& input,
214 const bool invert=false
215 )
216 {
217 return subsetMatchingStrings(matcher, input, invert);
219
220 //- Extract elements of StringList when regular expression matches
221 // Template partial specialization of subsetMatchingStrings
222 template<class StringListType>
223 StringListType subsetStrings
224 (
225 const wordRes& matcher,
226 const StringListType& input,
227 const bool invert=false
228 )
229 {
230 return subsetMatchingStrings(matcher, input, invert);
231 }
232
233
234 //- Extract elements of StringList when regular expression matches
235 // Template partial specialization of subsetMatchingStrings
236 template<class StringListType>
237 StringListType subsetStrings
238 (
239 const UList<wordRe>& patterns,
240 const StringListType& input,
241 const bool invert=false
242 )
243 {
244 return subsetMatchingStrings(wordRes::matcher(patterns), input, invert);
245 }
246
247
248 //- Inplace extract elements of StringList when regular expression matches
249 // optionally invert the match
250 // eg, to extract all selected elements:
251 // inplaceSubsetMatchingStrings<regExp, stringList>(myRegExp, lst);
252 template<class UnaryMatchPredicate, class StringListType>
254 (
255 const UnaryMatchPredicate& matcher,
256 StringListType& input,
257 const bool invert=false
258 );
259
260 //- Inplace extract elements of StringList when regular expression matches
261 // Template partial specialization of inplaceSubsetMatchingStrings
262 template<class StringListType>
264 (
265 const regExp& matcher,
266 StringListType& input,
267 const bool invert=false
268 )
269 {
270 inplaceSubsetMatchingStrings(matcher, input, invert);
271 }
273 //- Extract elements of StringList when regular expression matches
274 // Template partial specialization of subsetMatchingStrings
275 template<class StringListType>
277 (
278 const keyType& matcher,
279 StringListType& input,
280 const bool invert=false
281 )
282 {
283 return
284 (
285 matcher.isPattern()
286 ? inplaceSubsetMatchingStrings(regExp(matcher), input, invert)
287 : inplaceSubsetMatchingStrings(matcher, input, invert)
288 );
289 }
290
291 //- Inplace extract elements of StringList when regular expression matches
292 // Template partial specialization of inplaceSubsetMatchingStrings
293 template<class StringListType>
295 (
296 const wordRe& matcher,
297 StringListType& input,
298 const bool invert=false
299 )
300 {
301 inplaceSubsetMatchingStrings(matcher, input, invert);
302 }
303
304 //- Inplace extract elements of StringList when regular expression matches
305 // Template partial specialization of inplaceSubsetMatchingStrings
306 template<class StringListType>
308 (
309 const wordRes& matcher,
310 StringListType& input,
311 const bool invert=false
312 )
313 {
314 inplaceSubsetMatchingStrings(matcher, input, invert);
315 }
316
317 //- Inplace extract elements of StringList when regular expression matches
318 // Template partial specialization of inplaceSubsetMatchingStrings
319 template<class StringListType>
321 (
322 const UList<wordRe>& regexs,
323 StringListType& input,
324 const bool invert=false
325 )
326 {
328 }
329
330
331/*---------------------------------------------------------------------------*\
332 Namespace stringListOps Declaration
333\*---------------------------------------------------------------------------*/
334
335namespace stringListOps
336{
337
338//- Functor to determine if a string exists in a list of strings.
339template<class StringType>
340struct foundOp
343
345 :
346 values(list)
347 {}
348
349 bool operator()(const std::string& text) const
350 {
351 return values.contains(text);
352 }
353};
354
355
356//- Return ids for items with matching names.
357// The filter predicate is a combination of allow and deny lists
358//
359// \return List indices for matches
360template<class StringListType, class AccessOp = identityOp>
362(
363 const StringListType& input,
364 const wordRes::filter& pred,
365 AccessOp aop = identityOp()
366);
367
368//- Return ids for items with matching names,
369//- using a combination of allow and deny lists as per wordRes::filter
370//
371// An empty \em allow accepts everything not in \em deny.
372// A literal \em allow match has higher priority than any \em deny.
373// A regex \em allow match has lower priority than any \em deny.
374//
375// Example (when applied to a list of words),
376// \verbatim
377// input: ( abc apple test other val val1 val2 wall wall1 wall2 )
378// allow: ( abc def "t.*" other val val1 "wall.*" )
379// deny: ( "[ab].*" "t.*" other "val[0-9]" wall )
380//
381// result: (abc other val val1 wall1 wall2)
382// \endverbatim
383//
384// \returns identity list when allow/deny are both empty.
385template<class StringListType, class AccessOp = identityOp>
387(
388 const StringListType& input,
389 const wordRes& allow,
390 const wordRes& deny = wordRes::null(),
391 AccessOp aop = identityOp()
392);
393
394} // End namespace stringListOps
396
397// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
398
399} // End namespace Foam
400
401// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
402
403// Housekeeping
405namespace Foam
406{
407 //- Deprecated(2018-02) find using C-string as a regex
408 // \deprecated(2018-02) Treating string as regex may be inefficient
409 // and lead to unintended results.
410 // Use regExp, keyType, wordRe instead, or findMatchingStrings()
411 template<class StringType>
413 (
414 const char* disallowed,
415 const UList<StringType>& input,
416 const bool invert=false
417 ) = delete;
418
419 //- Deprecated(2018-02) find using string as a regex
420 // \deprecated(2018-02) Treating string as regex may be inefficient
421 // and lead to unintended results.
422 // Use regExp, keyType, wordRe instead, or findMatchingStrings()
423 template<class StringType>
425 (
426 const std::string& disallowed,
427 const UList<StringType>& input,
428 const bool invert=false
429 ) = delete;
430
431 //- Deprecated(2018-02) subset using C-string as a regex
432 // \deprecated(2018-02) Treating string as regex may be inefficient
433 // and lead to unintended results.
434 // Use regExp, keyType, wordRe instead, or subsetMatchingStrings()
435 template<class StringListType>
436 StringListType subsetStrings
437 (
438 const char* disallowed,
439 const StringListType& input,
440 const bool invert=false
441 ) = delete;
442
443 //- Deprecated(2018-02) subset using string as a regex
444 // \deprecated(2018-02) Treating string as regex may be inefficient
445 // and lead to unintended results.
446 // Use regExp, keyType, wordRe instead, or subsetMatchingStrings()
447 template<class StringListType>
448 StringListType subsetStrings
449 (
450 const std::string& disallowed,
451 const StringListType& input,
452 const bool invert=false
453 ) = delete;
454
455 //- Deprecated(2018-02) subset using C-string as a regex
456 // \deprecated(2018-02) Treating string as regex may be inefficient
457 // and lead to unintended results.
458 // Use regExp, keyType, wordRe instead, or inplaceSubsetMatchingStrings()
459 template<class StringListType>
461 (
462 const char* disallowed,
463 StringListType& input,
464 const bool invert=false
465 ) = delete;
466
467 //- Deprecated(2018-02) subset using string as a regex
468 // \deprecated(2018-02) Treating string as regex may be inefficient
469 // and lead to unintended results.
470 // Use keyType, wordRe instead, or inplaceSubsetMatchingStrings()
471 template<class StringListType>
473 (
474 const std::string& disallowed,
475 StringListType& input,
476 const bool invert=false
477 ) = delete;
478
479} // End namespace Foam
480
481// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
482
483#ifdef NoRepository
484 #include "stringListOps.txx"
485#endif
486
487// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
488
489#endif
490
491// ************************************************************************* //
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 contains(const T &val) const
True if the value is contained in the list.
Definition UListI.H:302
A class for handling keywords in dictionaries.
Definition keyType.H:69
bool isPattern() const noexcept
The keyType is treated as a pattern, not as literal string.
Definition keyTypeI.H:97
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition wordRe.H:81
A List of wordRe with additional matching capabilities.
Definition wordRes.H:56
static const wordRes & null() noexcept
Return a null wordRes (reference to a nullObject). Behaves like a empty wordRes.
Definition wordRes.H:80
labelList findMatching(const StringListType &input, const wordRes::filter &pred, AccessOp aop=identityOp())
Return ids for items with matching names.
Namespace for OpenFOAM.
void inplaceSubsetStrings(const regExp &matcher, StringListType &input, const bool invert=false)
Inplace extract elements of StringList when regular expression matches.
List< label > labelList
A List of labels.
Definition List.H:62
label firstMatchingString(const UnaryMatchPredicate &matcher, const UList< StringType > &input, const bool invert=false)
Find first list item that matches, -1 on failure.
StringListType subsetStrings(const regExp &matcher, const StringListType &input, const bool invert=false)
Extract elements of StringList when regular expression matches.
labelList findMatchingStrings(const UnaryMatchPredicate &matcher, const UList< StringType > &input, const bool invert=false)
Extract list indices for all matches.
regExpCxx regExp
Selection of preferred regular expression implementation.
Definition regExpFwd.H:37
void inplaceSubsetMatchingStrings(const UnaryMatchPredicate &matcher, StringListType &input, const bool invert=false)
Inplace extract elements of StringList when regular expression matches.
StringListType subsetMatchingStrings(const UnaryMatchPredicate &matcher, const StringListType &input, const bool invert=false)
Extract elements of StringList when regular expression matches.
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
labelList findStrings(const regExp &matcher, const UList< StringType > &input, const bool invert=false)
Return list indices for strings matching the regular expression.
Various functors for unary and binary operations. Can be used for parallel combine-reduce operations ...
A functor that returns its argument unchanged (cf. C++20 std::identity) Should never be specialized.
Definition stdFoam.H:108
const UList< StringType > & values
bool operator()(const std::string &text) const
foundOp(const UList< StringType > &list) noexcept
Functor wrapper of allow/deny lists of wordRe for filtering.
Definition wordRes.H:275
Functor wrapper of a list of wordRe for matching.
Definition wordRes.H:231