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-2022 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 stringListOpsTemplates.C
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,
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,
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,
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,
104 const bool invert=false
105 )
106 {
107 return
108 (
109 matcher.isPattern()
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,
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,
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>
147 (
148 const UList<wordRe>& patterns,
150 const bool invert=false
151 )
152 {
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 }
187
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()
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);
218 }
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 {
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 {
271 }
272
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()
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 {
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 {
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 is exists in a list of strings.
339// For example,
340//
341// \code
342// reduce(text, stringListOps::foundOp<word>(myNames));
343// \endcode
344template<class StringType>
346{
348
350 :
351 values(list)
352 {}
353
354 bool operator()(const std::string& text) const
355 {
356 return values.found(text);
357 }
358};
359
360
361//- Return ids for items with matching names.
362// The filter predicate is a combination of allow and deny lists
363//
364// \return List indices for matches
365template<class StringListType, class AccessOp = identityOp>
367(
368 const StringListType& input,
369 const wordRes::filter& pred,
370 AccessOp aop = identityOp()
371);
372
373//- Return ids for items with matching names.
374// Uses a combination of allow and deny lists
375//
376// An empty 'allow' accepts everything not in 'deny'.
377// A literal 'allow' match has higher priority than any 'deny'.
378// A regex 'allow' match has lower priority than any 'deny'.
379//
380// Example (when applied to a list of words),
381// \verbatim
382// input: ( abc apple test other val val1 val2 wall wall1 wall2 )
383// allow: ( abc def "t.*" other val val1 "wall.*" )
384// deny: ( "[ab].*" "t.*" other "val[0-9]" wall )
385//
386// result: (abc other val val1 wall1 wall2)
387// \endverbatim
388//
389// \return List indices for matches
390template<class StringListType, class AccessOp = identityOp>
392(
393 const StringListType& input,
394 const wordRes& allow,
395 const wordRes& deny = wordRes::null(),
396 AccessOp aop = identityOp()
397);
398
399} // End namespace stringListOps
400
401
402// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
403
404} // End namespace Foam
405
406// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
407
408// Housekeeping
409
410namespace Foam
411{
412 //- Deprecated(2018-02) find using C-string as a regex
413 // \deprecated(2018-02) Treating string as regex may be inefficient
414 // and lead to unintended results.
415 // Use regExp, keyType, wordRe instead, or findMatchingStrings()
416 template<class StringType>
418 (
419 const char* disallowed,
421 const bool invert=false
422 ) = delete;
423
424 //- Deprecated(2018-02) find using string as a regex
425 // \deprecated(2018-02) Treating string as regex may be inefficient
426 // and lead to unintended results.
427 // Use regExp, keyType, wordRe instead, or findMatchingStrings()
428 template<class StringType>
430 (
431 const std::string& disallowed,
433 const bool invert=false
434 ) = delete;
435
436 //- Deprecated(2018-02) subset using C-string as a regex
437 // \deprecated(2018-02) Treating string as regex may be inefficient
438 // and lead to unintended results.
439 // Use regExp, keyType, wordRe instead, or subsetMatchingStrings()
440 template<class StringListType>
441 StringListType subsetStrings
442 (
443 const char* disallowed,
444 const StringListType& input,
445 const bool invert=false
446 ) = delete;
447
448 //- Deprecated(2018-02) subset using string as a regex
449 // \deprecated(2018-02) Treating string as regex may be inefficient
450 // and lead to unintended results.
451 // Use regExp, keyType, wordRe instead, or subsetMatchingStrings()
452 template<class StringListType>
453 StringListType subsetStrings
454 (
455 const std::string& disallowed,
456 const StringListType& input,
457 const bool invert=false
458 ) = delete;
459
460 //- Deprecated(2018-02) subset using C-string as a regex
461 // \deprecated(2018-02) Treating string as regex may be inefficient
462 // and lead to unintended results.
463 // Use regExp, keyType, wordRe instead, or inplaceSubsetMatchingStrings()
464 template<class StringListType>
466 (
467 const char* disallowed,
468 StringListType& input,
469 const bool invert=false
470 ) = delete;
471
472 //- Deprecated(2018-02) subset using string as a regex
473 // \deprecated(2018-02) Treating string as regex may be inefficient
474 // and lead to unintended results.
475 // Use keyType, wordRe instead, or inplaceSubsetMatchingStrings()
476 template<class StringListType>
478 (
479 const std::string& disallowed,
480 StringListType& input,
481 const bool invert=false
482 ) = delete;
483
484} // End namespace Foam
485
486// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
487
488#ifdef NoRepository
489 #include "stringListOpsTemplates.C"
490#endif
491
492// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
493
494#endif
495
496// ************************************************************************* //
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
bool found(const T &val, label pos=0) const
True if the value if found in the list.
Definition: UListI.H:265
A class for handling keywords in dictionaries.
Definition: keyType.H:71
bool isPattern() const noexcept
The keyType is treated as a pattern, not as literal string.
Definition: keyTypeI.H:104
Wrapper around C++11 regular expressions with some additional prefix-handling. The prefix-handling is...
Definition: regExpCxx.H:83
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:83
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:54
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.
label firstMatchingString(const UnaryMatchPredicate &matcher, const UList< StringType > &input, const bool invert=false)
Find first list item that matches, -1 on failure.
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
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.
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.
regExpCxx regExp
Selection of preferred regular expression implementation.
Definition: regExpFwd.H:43
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:36
labelList findStrings(const regExp &matcher, const UList< StringType > &input, const bool invert=false)
Return list indices for strings matching the regular expression.
Definition: stringListOps.H:87
Various functors for unary and binary operations. Can be used for parallel combine-reduce operations ...
Functor to determine if a string is exists in a list of strings.
const UList< StringType > & values
bool operator()(const std::string &text) const
foundOp(const UList< StringType > &list)
Functor wrapper of allow/deny lists of wordRe for filtering.
Definition: wordRes.H:174
Functor wrapper of a list of wordRe for matching.
Definition: wordRes.H:143