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  //- 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  template<class UnaryMatchPredicate, class StringType>
75  (
76  const UnaryMatchPredicate& matcher,
77  const UList<StringType>& input,
78  const bool invert=false
79  );
80 
81 
82  //- Return list indices for strings matching the regular expression
83  // Template partial specialization of findMatchingStrings
84  template<class StringType>
86  (
87  const regExp& matcher,
88  const UList<StringType>& input,
89  const bool invert=false
90  )
91  {
92  return findMatchingStrings(matcher, input, invert);
93  }
94 
95 
96  //- Return list indices for strings matching the regular expression
97  // Template partial specialization of findMatchingStrings
98  template<class StringType>
100  (
101  const keyType& matcher,
102  const UList<StringType>& input,
103  const bool invert=false
104  )
105  {
106  return
107  (
108  matcher.isPattern()
109  ? findMatchingStrings(regExp(matcher), input, invert)
110  : findMatchingStrings(matcher, input, invert)
111  );
112  }
113 
114 
115  //- Return list indices for strings matching the regular expression
116  // Template partial specialization of findMatchingStrings
117  template<class StringType>
119  (
120  const wordRe& matcher,
121  const UList<StringType>& input,
122  const bool invert=false
123  )
124  {
125  return findMatchingStrings(matcher, input, invert);
126  }
127 
128 
129  //- Return list indices for strings matching one of the regular expression
130  // Template partial specialization of findMatchingStrings
131  template<class StringType>
133  (
134  const wordRes& matcher,
135  const UList<StringType>& input,
136  const bool invert=false
137  )
138  {
139  return findMatchingStrings(matcher, input, invert);
140  }
141 
142  //- Return list indices for strings matching one of the regular expression
143  // Template partial specialization of findMatchingStrings
144  template<class StringType>
146  (
147  const UList<wordRe>& patterns,
148  const UList<StringType>& input,
149  const bool invert=false
150  )
151  {
152  return findMatchingStrings(wordRes::matcher(patterns), input, invert);
153  }
154 
155 
156  // Subsetting multi-string matches (similar to ListOp):
157 
158  //- Extract elements of StringList when regular expression matches
159  // optionally invert the match
160  // eg, to extract all selected elements:
161  // \code
162  // subsetMatchingStrings<regExp, stringList>(myRegExp, list);
163  // \endcode
164  template<class UnaryMatchPredicate, class StringListType>
165  StringListType subsetMatchingStrings
166  (
167  const UnaryMatchPredicate& matcher,
168  const StringListType& input,
169  const bool invert=false
170  );
171 
172 
173  //- Extract elements of StringList when regular expression matches
174  // Template partial specialization of subsetMatchingStrings
175  template<class StringListType>
176  StringListType subsetStrings
177  (
178  const regExp& matcher,
179  const StringListType& input,
180  const bool invert=false
181  )
182  {
183  return subsetMatchingStrings(matcher, input, invert);
184  }
185 
186 
187  //- Extract elements of StringList when regular expression matches
188  // Template partial specialization of subsetMatchingStrings
189  template<class StringListType>
190  StringListType subsetStrings
191  (
192  const keyType& matcher,
193  const StringListType& input,
194  const bool invert=false
195  )
196  {
197  return
198  (
199  matcher.isPattern()
201  : subsetMatchingStrings(matcher, input, invert)
202  );
203  }
204 
205  //- Extract elements of StringList when regular expression matches
206  // Template partial specialization of subsetMatchingStrings
207  template<class StringListType>
208  StringListType subsetStrings
209  (
210  const wordRe& matcher,
211  const StringListType& input,
212  const bool invert=false
213  )
214  {
215  return subsetMatchingStrings(matcher, input, invert);
216  }
217 
218  //- Extract elements of StringList when regular expression matches
219  // Template partial specialization of subsetMatchingStrings
220  template<class StringListType>
221  StringListType subsetStrings
222  (
223  const wordRes& matcher,
224  const StringListType& input,
225  const bool invert=false
226  )
227  {
228  return subsetMatchingStrings(matcher, input, invert);
229  }
230 
231 
232  //- Extract elements of StringList when regular expression matches
233  // Template partial specialization of subsetMatchingStrings
234  template<class StringListType>
235  StringListType subsetStrings
236  (
237  const UList<wordRe>& patterns,
238  const StringListType& input,
239  const bool invert=false
240  )
241  {
243  }
244 
245 
246  //- Inplace extract elements of StringList when regular expression matches
247  // optionally invert the match
248  // eg, to extract all selected elements:
249  // inplaceSubsetMatchingStrings<regExp, stringList>(myRegExp, lst);
250  template<class UnaryMatchPredicate, class StringListType>
252  (
253  const UnaryMatchPredicate& matcher,
254  StringListType& input,
255  const bool invert=false
256  );
257 
258  //- Inplace extract elements of StringList when regular expression matches
259  // Template partial specialization of inplaceSubsetMatchingStrings
260  template<class StringListType>
262  (
263  const regExp& matcher,
264  StringListType& input,
265  const bool invert=false
266  )
267  {
269  }
270 
271  //- Extract elements of StringList when regular expression matches
272  // Template partial specialization of subsetMatchingStrings
273  template<class StringListType>
275  (
276  const keyType& matcher,
277  StringListType& input,
278  const bool invert=false
279  )
280  {
281  return
282  (
283  matcher.isPattern()
286  );
287  }
288 
289  //- Inplace extract elements of StringList when regular expression matches
290  // Template partial specialization of inplaceSubsetMatchingStrings
291  template<class StringListType>
293  (
294  const wordRe& matcher,
295  StringListType& input,
296  const bool invert=false
297  )
298  {
300  }
301 
302  //- Inplace extract elements of StringList when regular expression matches
303  // Template partial specialization of inplaceSubsetMatchingStrings
304  template<class StringListType>
306  (
307  const wordRes& matcher,
308  StringListType& input,
309  const bool invert=false
310  )
311  {
313  }
314 
315  //- Inplace extract elements of StringList when regular expression matches
316  // Template partial specialization of inplaceSubsetMatchingStrings
317  template<class StringListType>
319  (
320  const UList<wordRe>& regexs,
321  StringListType& input,
322  const bool invert=false
323  )
324  {
326  }
327 
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 {
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 // Uses a combination of allow and deny lists
363 //
364 // An empty 'allow' list accepts everything not in the 'deny' list.
365 // A literal match has higher priority over a regex match.
366 //
367 // Eg,
368 // \verbatim
369 // input: ( abc apple wall wall1 wall2 )
370 // allow: ( abc def "wall.*" )
371 // deny: ( "[ab].*" wall )
372 //
373 // result: (abc wall1 wall2)
374 // \endverbatim
375 //
376 // \return List indices for matches
377 template<class StringListType, class AccessOp = noOp>
379 (
380  const StringListType& input,
381  const wordRes& allow,
382  const wordRes& deny = wordRes(),
383  AccessOp aop = noOp()
384 );
385 
386 } // End namespace stringListOps
387 
388 
389 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
390 
391 } // End namespace Foam
392 
393 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
394 
395 // Housekeeping
396 
397 namespace Foam
398 {
399  //- Deprecated(2018-02) find using C-string as a regex
400  // \deprecated(2018-02) Treating string as regex may be inefficient
401  // and lead to unintended results.
402  // Use regExp, keyType, wordRe instead, or findMatchingStrings()
403  template<class StringType>
405  (
406  const char* disallowed,
407  const UList<StringType>& input,
408  const bool invert=false
409  ) = delete;
410 
411  //- Deprecated(2018-02) find using string as a regex
412  // \deprecated(2018-02) Treating string as regex may be inefficient
413  // and lead to unintended results.
414  // Use regExp, keyType, wordRe instead, or findMatchingStrings()
415  template<class StringType>
417  (
418  const std::string& disallowed,
419  const UList<StringType>& input,
420  const bool invert=false
421  ) = delete;
422 
423  //- Deprecated(2018-02) subset using C-string as a regex
424  // \deprecated(2018-02) Treating string as regex may be inefficient
425  // and lead to unintended results.
426  // Use regExp, keyType, wordRe instead, or subsetMatchingStrings()
427  template<class StringListType>
428  StringListType subsetStrings
429  (
430  const char* disallowed,
431  const StringListType& input,
432  const bool invert=false
433  ) = delete;
434 
435  //- Deprecated(2018-02) subset using string as a regex
436  // \deprecated(2018-02) Treating string as regex may be inefficient
437  // and lead to unintended results.
438  // Use regExp, keyType, wordRe instead, or subsetMatchingStrings()
439  template<class StringListType>
440  StringListType subsetStrings
441  (
442  const std::string& disallowed,
443  const StringListType& input,
444  const bool invert=false
445  ) = delete;
446 
447  //- Deprecated(2018-02) subset using C-string as a regex
448  // \deprecated(2018-02) Treating string as regex may be inefficient
449  // and lead to unintended results.
450  // Use regExp, keyType, wordRe instead, or inplaceSubsetMatchingStrings()
451  template<class StringListType>
453  (
454  const char* disallowed,
455  StringListType& input,
456  const bool invert=false
457  ) = delete;
458 
459  //- Deprecated(2018-02) subset using string as a regex
460  // \deprecated(2018-02) Treating string as regex may be inefficient
461  // and lead to unintended results.
462  // Use keyType, wordRe instead, or inplaceSubsetMatchingStrings()
463  template<class StringListType>
465  (
466  const std::string& disallowed,
467  StringListType& input,
468  const bool invert=false
469  ) = delete;
470 
471 } // End namespace Foam
472 
473 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
474 
475 #ifdef NoRepository
476  #include "stringListOpsTemplates.C"
477 #endif
478 
479 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
480 
481 #endif
482 
483 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:67
wordRes.H
Foam::keyType::isPattern
bool isPattern() const noexcept
The keyType is treated as a pattern, not as literal string.
Definition: keyTypeI.H:104
Foam::stringListOps::foundOp
Functor to determine if a string is exists in a list of strings.
Definition: stringListOps.H:345
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:262
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:82
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:80
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:68
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 with some additional prefix-handling. The prefix-handling is...
Definition: regExpCxx.H:82
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:177
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:86
Foam::stringListOps::foundOp::foundOp
foundOp(const UList< StringType > &list)
Definition: stringListOps.H:349
Foam::wordRes::matcher
Functor wrapper for matching against a List of wordRe.
Definition: wordRes.H:83
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:110
Foam::List< label >
Foam::UList< StringType >
Foam::wordRes
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:51
Foam::firstMatchingString
label firstMatchingString(const UnaryMatchPredicate &matcher, const UList< StringType > &input, const bool invert=false)
Find first list item that matches, -1 on failure.
Foam::stringListOps::foundOp::values
const UList< StringType > & values
Definition: stringListOps.H:347
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:354
Foam::UList::found
bool found(const T &val, label pos=0) const
True if the value if found in the list.
Definition: UListI.H:265
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