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-2020 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 stringListOps_H
45 #define stringListOps_H
46 
47 #include "labelList.H"
48 #include "stringList.H"
49 #include "wordRes.H"
50 #include "flipOp.H"
51 
52 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
53 
54 namespace Foam
55 {
56  //- Extract list indices for all matches.
57  // The unary match predicate has the following signature:
58  // \code
59  // bool operator()(const std::string& text);
60  // \endcode
61  //
62  // \return List indices for matching strings
63  template<class UnaryMatchPredicate, class StringType>
65  (
66  const UnaryMatchPredicate& matcher,
67  const UList<StringType>& input,
68  const bool invert=false
69  );
70 
71 
72  //- Return list indices for strings matching the regular expression
73  // Template partial specialization of findMatchingStrings
74  template<class StringType>
76  (
77  const regExp& matcher,
78  const UList<StringType>& input,
79  const bool invert=false
80  )
81  {
82  return findMatchingStrings(matcher, input, invert);
83  }
84 
85 
86  //- Return list indices for strings matching the regular expression
87  // Template partial specialization of findMatchingStrings
88  template<class StringType>
90  (
91  const keyType& matcher,
92  const UList<StringType>& input,
93  const bool invert=false
94  )
95  {
96  return
97  (
98  matcher.isPattern()
100  : findMatchingStrings(matcher, input, invert)
101  );
102  }
103 
104 
105  //- Return list indices for strings matching the regular expression
106  // Template partial specialization of findMatchingStrings
107  template<class StringType>
109  (
110  const wordRe& matcher,
111  const UList<StringType>& input,
112  const bool invert=false
113  )
114  {
115  return findMatchingStrings(matcher, input, invert);
116  }
117 
118 
119  //- Return list indices for strings matching one of the regular expression
120  // Template partial specialization of findMatchingStrings
121  template<class StringType>
123  (
124  const wordRes& matcher,
125  const UList<StringType>& input,
126  const bool invert=false
127  )
128  {
129  return findMatchingStrings(matcher, input, invert);
130  }
131 
132  //- Return list indices for strings matching one of the regular expression
133  // Template partial specialization of findMatchingStrings
134  template<class StringType>
136  (
137  const UList<wordRe>& patterns,
138  const UList<StringType>& input,
139  const bool invert=false
140  )
141  {
142  return findMatchingStrings(wordRes::matcher(patterns), input, invert);
143  }
144 
145 
146  // Subsetting multi-string matches (similar to ListOp):
147 
148  //- Extract elements of StringList when regular expression matches
149  // optionally invert the match
150  // eg, to extract all selected elements:
151  // \code
152  // subsetMatchingStrings<regExp, stringList>(myRegExp, list);
153  // \endcode
154  template<class UnaryMatchPredicate, class StringListType>
155  StringListType subsetMatchingStrings
156  (
157  const UnaryMatchPredicate& matcher,
158  const StringListType& input,
159  const bool invert=false
160  );
161 
162 
163  //- Extract elements of StringList when regular expression matches
164  // Template partial specialization of subsetMatchingStrings
165  template<class StringListType>
166  StringListType subsetStrings
167  (
168  const regExp& matcher,
169  const StringListType& input,
170  const bool invert=false
171  )
172  {
173  return subsetMatchingStrings(matcher, input, invert);
174  }
175 
176 
177  //- Extract elements of StringList when regular expression matches
178  // Template partial specialization of subsetMatchingStrings
179  template<class StringListType>
180  StringListType subsetStrings
181  (
182  const keyType& matcher,
183  const StringListType& input,
184  const bool invert=false
185  )
186  {
187  return
188  (
189  matcher.isPattern()
191  : subsetMatchingStrings(matcher, input, invert)
192  );
193  }
194 
195  //- Extract elements of StringList when regular expression matches
196  // Template partial specialization of subsetMatchingStrings
197  template<class StringListType>
198  StringListType subsetStrings
199  (
200  const wordRe& matcher,
201  const StringListType& input,
202  const bool invert=false
203  )
204  {
205  return subsetMatchingStrings(matcher, input, invert);
206  }
207 
208  //- Extract elements of StringList when regular expression matches
209  // Template partial specialization of subsetMatchingStrings
210  template<class StringListType>
211  StringListType subsetStrings
212  (
213  const wordRes& matcher,
214  const StringListType& input,
215  const bool invert=false
216  )
217  {
218  return subsetMatchingStrings(matcher, input, invert);
219  }
220 
221 
222  //- Extract elements of StringList when regular expression matches
223  // Template partial specialization of subsetMatchingStrings
224  template<class StringListType>
225  StringListType subsetStrings
226  (
227  const UList<wordRe>& patterns,
228  const StringListType& input,
229  const bool invert=false
230  )
231  {
233  }
234 
235 
236  //- Inplace extract elements of StringList when regular expression matches
237  // optionally invert the match
238  // eg, to extract all selected elements:
239  // inplaceSubsetMatchingStrings<regExp, stringList>(myRegExp, lst);
240  template<class UnaryMatchPredicate, class StringListType>
242  (
243  const UnaryMatchPredicate& matcher,
244  StringListType& input,
245  const bool invert=false
246  );
247 
248  //- Inplace extract elements of StringList when regular expression matches
249  // Template partial specialization of inplaceSubsetMatchingStrings
250  template<class StringListType>
252  (
253  const regExp& matcher,
254  StringListType& input,
255  const bool invert=false
256  )
257  {
259  }
260 
261  //- Extract elements of StringList when regular expression matches
262  // Template partial specialization of subsetMatchingStrings
263  template<class StringListType>
265  (
266  const keyType& matcher,
267  StringListType& input,
268  const bool invert=false
269  )
270  {
271  return
272  (
273  matcher.isPattern()
276  );
277  }
278 
279  //- Inplace extract elements of StringList when regular expression matches
280  // Template partial specialization of inplaceSubsetMatchingStrings
281  template<class StringListType>
283  (
284  const wordRe& matcher,
285  StringListType& input,
286  const bool invert=false
287  )
288  {
290  }
291 
292  //- Inplace extract elements of StringList when regular expression matches
293  // Template partial specialization of inplaceSubsetMatchingStrings
294  template<class StringListType>
296  (
297  const wordRes& matcher,
298  StringListType& input,
299  const bool invert=false
300  )
301  {
303  }
304 
305  //- Inplace extract elements of StringList when regular expression matches
306  // Template partial specialization of inplaceSubsetMatchingStrings
307  template<class StringListType>
309  (
310  const UList<wordRe>& regexs,
311  StringListType& input,
312  const bool invert=false
313  )
314  {
316  }
317 
318 
319 
320 
321 /*---------------------------------------------------------------------------*\
322  Namespace stringListOps Declaration
323 \*---------------------------------------------------------------------------*/
324 
325 namespace stringListOps
326 {
327 
328 //- Functor to determine if a string is exists in a list of strings.
329 // For example,
330 //
331 // \code
332 // reduce(text, stringListOps::foundOp<word>(myNames));
333 // \endcode
334 template<class StringType>
335 struct foundOp
336 {
338 
340  :
341  values(list)
342  {}
343 
344  bool operator()(const std::string& text) const
345  {
346  return values.found(text);
347  }
348 };
349 
350 
351 //- Return ids for items with matching names.
352 // Uses a combination of allow and deny lists
353 //
354 // An empty 'allow' list accepts everything not in the 'deny' list.
355 // A literal match has higher priority over a regex match.
356 //
357 // Eg,
358 // \verbatim
359 // input: ( abc apple wall wall1 wall2 )
360 // allow: ( abc def "wall.*" )
361 // deny: ( "[ab].*" wall )
362 //
363 // result: (abc wall1 wall2)
364 // \endverbatim
365 //
366 // \return List indices for matches
367 template<class StringListType, class AccessOp = noOp>
369 (
370  const StringListType& input,
371  const wordRes& allow,
372  const wordRes& deny = wordRes(),
373  AccessOp aop = noOp()
374 );
375 
376 } // End namespace stringListOps
377 
378 
379 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
380 
381 } // End namespace Foam
382 
383 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
384 
385 // Housekeeping
386 
387 namespace Foam
388 {
389  //- Deprecated(2018-02) find using C-string as a regex
390  // \deprecated(2018-02) Treating string as regex may be inefficient
391  // and lead to unintended results.
392  // Use regExp, keyType, wordRe instead, or findMatchingStrings()
393  template<class StringType>
395  (
396  const char* disallowed,
397  const UList<StringType>& input,
398  const bool invert=false
399  ) = delete;
400 
401  //- Deprecated(2018-02) find using string as a regex
402  // \deprecated(2018-02) Treating string as regex may be inefficient
403  // and lead to unintended results.
404  // Use regExp, keyType, wordRe instead, or findMatchingStrings()
405  template<class StringType>
407  (
408  const std::string& disallowed,
409  const UList<StringType>& input,
410  const bool invert=false
411  ) = delete;
412 
413  //- Deprecated(2018-02) subset using C-string as a regex
414  // \deprecated(2018-02) Treating string as regex may be inefficient
415  // and lead to unintended results.
416  // Use regExp, keyType, wordRe instead, or subsetMatchingStrings()
417  template<class StringListType>
418  StringListType subsetStrings
419  (
420  const char* disallowed,
421  const StringListType& input,
422  const bool invert=false
423  ) = delete;
424 
425  //- Deprecated(2018-02) subset using string as a regex
426  // \deprecated(2018-02) Treating string as regex may be inefficient
427  // and lead to unintended results.
428  // Use regExp, keyType, wordRe instead, or subsetMatchingStrings()
429  template<class StringListType>
430  StringListType subsetStrings
431  (
432  const std::string& disallowed,
433  const StringListType& input,
434  const bool invert=false
435  ) = delete;
436 
437  //- Deprecated(2018-02) subset using C-string as a regex
438  // \deprecated(2018-02) Treating string as regex may be inefficient
439  // and lead to unintended results.
440  // Use regExp, keyType, wordRe instead, or inplaceSubsetMatchingStrings()
441  template<class StringListType>
443  (
444  const char* disallowed,
445  StringListType& input,
446  const bool invert=false
447  ) = delete;
448 
449  //- Deprecated(2018-02) subset using string as a regex
450  // \deprecated(2018-02) Treating string as regex may be inefficient
451  // and lead to unintended results.
452  // Use keyType, wordRe instead, or inplaceSubsetMatchingStrings()
453  template<class StringListType>
455  (
456  const std::string& disallowed,
457  StringListType& input,
458  const bool invert=false
459  ) = delete;
460 
461 } // End namespace Foam
462 
463 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
464 
465 #ifdef NoRepository
466  #include "stringListOpsTemplates.C"
467 #endif
468 
469 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
470 
471 #endif
472 
473 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:71
wordRes.H
Foam::keyType::isPattern
bool isPattern() const
The keyType is treated as a pattern, not as literal string.
Definition: keyTypeI.H:128
Foam::stringListOps::foundOp
Functor to determine if a string is exists in a list of strings.
Definition: stringListOps.H:335
Foam::inplaceSubsetStrings
void inplaceSubsetStrings(const regExp &matcher, StringListType &input, const bool invert=false)
Inplace extract elements of StringList when regular expression matches.
Definition: stringListOps.H:252
Foam::subsetMatchingStrings
StringListType subsetMatchingStrings(const UnaryMatchPredicate &matcher, const StringListType &input, const bool invert=false)
Extract elements of StringList when regular expression matches.
Definition: stringListOpsTemplates.C:60
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:72
Foam::invert
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:36
Foam::regExp
regExpCxx regExp
Selection of preferred regular expression implementation.
Definition: regExpFwd.H:41
Foam::keyType
A class for handling keywords in dictionaries.
Definition: keyType.H:60
labelList.H
Foam::stringListOps::findMatching
labelList findMatching(const StringListType &input, const wordRes &allow, const wordRes &deny=wordRes(), AccessOp aop=noOp())
Return ids for items with matching names.
Foam::regExpCxx
Wrapper around C++11 regular expressions.
Definition: regExpCxx.H:72
Foam::subsetStrings
StringListType subsetStrings(const regExp &matcher, const StringListType &input, const bool invert=false)
Extract elements of StringList when regular expression matches.
Definition: stringListOps.H:167
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
flipOp.H
Foam::findStrings
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:76
Foam::stringListOps::foundOp::foundOp
foundOp(const UList< StringType > &list)
Definition: stringListOps.H:339
Foam::wordRes::matcher
Functor wrapper for matching against a List of wordRe.
Definition: wordRes.H:82
Foam::inplaceSubsetMatchingStrings
void inplaceSubsetMatchingStrings(const UnaryMatchPredicate &matcher, StringListType &input, const bool invert=false)
Inplace extract elements of StringList when regular expression matches.
Definition: stringListOpsTemplates.C:88
Foam::List< label >
Foam::UList< StringType >
Foam::wordRes
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:51
Foam::stringListOps::foundOp::values
const UList< StringType > & values
Definition: stringListOps.H:337
stringListOpsTemplates.C
Foam::input
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
Foam::stringListOps::foundOp::operator()
bool operator()(const std::string &text) const
Definition: stringListOps.H:344
Foam::UList::found
bool found(const T &val, label pos=0) const
True if the value if found in the list.
Definition: UListI.H:212
Foam::findMatchingStrings
labelList findMatchingStrings(const UnaryMatchPredicate &matcher, const UList< StringType > &input, const bool invert=false)
Extract list indices for all matches.
stringList.H
Foam::noOp
Pass through value. Should never be specialized.
Definition: flipOp.H:64