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 -------------------------------------------------------------------------------
11 License
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 
27 InNamespace
28  Foam
29 
30 Description
31  Operations on lists of strings.
32 
33 Namespace
34  Foam::stringListOps
35 
36 Description
37  Various utility functions to work on lists of strings.
38 
39 SourceFiles
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 
54 namespace 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
108  (
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>
147  (
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  }
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()
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);
218  }
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 
335 namespace 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
344 template<class StringType>
345 struct foundOp
346 {
347  const UList<StringType>& values;
348 
349  foundOp(const UList<StringType>& list)
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
365 template<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
390 template<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 
402 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
404 } // End namespace Foam
406 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
407 
408 // Housekeeping
409 
410 namespace 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,
420  const UList<StringType>& input,
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,
432  const UList<StringType>& input,
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 // ************************************************************************* //
StringListType subsetMatchingStrings(const UnaryMatchPredicate &matcher, const StringListType &input, const bool invert=false)
Extract elements of StringList when regular expression matches.
A class for handling keywords in dictionaries.
Definition: keyType.H:66
const UList< StringType > & values
labelList findMatchingStrings(const UnaryMatchPredicate &matcher, const UList< StringType > &input, const bool invert=false)
Extract list indices for all matches.
label firstMatchingString(const UnaryMatchPredicate &matcher, const UList< StringType > &input, const bool invert=false)
Find first list item that matches, -1 on failure.
bool operator()(const std::string &text) const
bool found(const T &val, label pos=0) const
Same as contains()
Definition: UList.H:879
StringListType subsetStrings(const regExp &matcher, const StringListType &input, const bool invert=false)
Extract elements of StringList when regular expression matches.
Various functors for unary and binary operations. Can be used for parallel combine-reduce operations ...
regExpPosix regExp
Selection of preferred regular expression implementation.
Definition: regExpFwd.H:36
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
labelList findMatching(const StringListType &input, const wordRes::filter &pred, AccessOp aop=identityOp())
Return ids for items with matching names.
bool isPattern() const noexcept
The keyType is treated as a pattern, not as literal string.
Definition: keyTypeI.H:97
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:53
A functor that returns its argument unchanged (cf. C++20 std::identity) Should never be specialized...
Definition: stdFoam.H:90
void inplaceSubsetStrings(const regExp &matcher, StringListType &input, const bool invert=false)
Inplace extract elements of StringList when regular expression matches.
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:29
Wrapper around POSIX extended regular expressions with some additional prefix-handling. The prefix-handling is loosely oriented on PCRE regular expressions and provides a simple means of tuning the expressions.
Definition: regExpPosix.H:80
Functor wrapper of a list of wordRe for matching.
Definition: wordRes.H:175
List< label > labelList
A List of labels.
Definition: List.H:62
foundOp(const UList< StringType > &list)
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:92
void inplaceSubsetMatchingStrings(const UnaryMatchPredicate &matcher, StringListType &input, const bool invert=false)
Inplace extract elements of StringList when regular expression matches.
Namespace for OpenFOAM.