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-2018 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 
51 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
52 
53 namespace Foam
54 {
55  //- Extract list indices for all matches.
56  // The unary match predicate has the following signature:
57  // \code
58  // bool operator()(const std::string& text);
59  // \endcode
60  //
61  // \return List indices for matching strings
62  template<class UnaryMatchPredicate, class StringType>
64  (
65  const UnaryMatchPredicate& matcher,
66  const UList<StringType>& input,
67  const bool invert=false
68  );
69 
70 
71  //- Return list indices for strings matching the regular expression
72  // Template partial specialization of findMatchingStrings
73  template<class StringType>
75  (
76  const regExp& matcher,
77  const UList<StringType>& input,
78  const bool invert=false
79  )
80  {
81  return findMatchingStrings(matcher, input, invert);
82  }
83 
84 
85  //- Return list indices for strings matching the regular expression
86  // Template partial specialization of findMatchingStrings
87  template<class StringType>
89  (
90  const keyType& matcher,
91  const UList<StringType>& input,
92  const bool invert=false
93  )
94  {
95  return
96  (
97  matcher.isPattern()
98  ? findMatchingStrings(regExp(matcher), input, invert)
99  : findMatchingStrings(matcher, input, invert)
100  );
101  }
102 
103 
104  //- Return list indices for strings matching the regular expression
105  // Template partial specialization of findMatchingStrings
106  template<class StringType>
108  (
109  const wordRe& matcher,
110  const UList<StringType>& input,
111  const bool invert=false
112  )
113  {
114  return findMatchingStrings(matcher, input, invert);
115  }
116 
117 
118  //- Return list indices for strings matching one of the regular expression
119  // Template partial specialization of findMatchingStrings
120  template<class StringType>
122  (
123  const wordRes& matcher,
124  const UList<StringType>& input,
125  const bool invert=false
126  )
127  {
128  return findMatchingStrings(matcher, input, invert);
129  }
130 
131  //- Return list indices for strings matching one of the regular expression
132  // Template partial specialization of findMatchingStrings
133  template<class StringType>
135  (
136  const UList<wordRe>& patterns,
137  const UList<StringType>& input,
138  const bool invert=false
139  )
140  {
141  return findMatchingStrings(wordRes::matcher(patterns), input, invert);
142  }
143 
144 
145  // Subsetting multi-string matches (similar to ListOp):
146 
147  //- Extract elements of StringList when regular expression matches
148  // optionally invert the match
149  // eg, to extract all selected elements:
150  // \code
151  // subsetMatchingStrings<regExp, stringList>(myRegExp, list);
152  // \endcode
153  template<class UnaryMatchPredicate, class StringListType>
154  StringListType subsetMatchingStrings
155  (
156  const UnaryMatchPredicate& matcher,
157  const StringListType& input,
158  const bool invert=false
159  );
160 
161 
162  //- Extract elements of StringList when regular expression matches
163  // Template partial specialization of subsetMatchingStrings
164  template<class StringListType>
165  StringListType subsetStrings
166  (
167  const regExp& matcher,
168  const StringListType& input,
169  const bool invert=false
170  )
171  {
172  return subsetMatchingStrings(matcher, input, invert);
173  }
174 
175 
176  //- Extract elements of StringList when regular expression matches
177  // Template partial specialization of subsetMatchingStrings
178  template<class StringListType>
179  StringListType subsetStrings
180  (
181  const keyType& matcher,
182  const StringListType& input,
183  const bool invert=false
184  )
185  {
186  return
187  (
188  matcher.isPattern()
189  ? subsetMatchingStrings(regExp(matcher), input, invert)
190  : subsetMatchingStrings(matcher, input, invert)
191  );
192  }
193 
194  //- Extract elements of StringList when regular expression matches
195  // Template partial specialization of subsetMatchingStrings
196  template<class StringListType>
197  StringListType subsetStrings
198  (
199  const wordRe& matcher,
200  const StringListType& input,
201  const bool invert=false
202  )
203  {
204  return subsetMatchingStrings(matcher, input, invert);
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 wordRes& matcher,
213  const StringListType& input,
214  const bool invert=false
215  )
216  {
217  return subsetMatchingStrings(matcher, input, invert);
218  }
219 
220 
221  //- Extract elements of StringList when regular expression matches
222  // Template partial specialization of subsetMatchingStrings
223  template<class StringListType>
224  StringListType subsetStrings
225  (
226  const UList<wordRe>& patterns,
227  const StringListType& input,
228  const bool invert=false
229  )
230  {
231  return subsetMatchingStrings(wordRes::matcher(patterns), input, invert);
232  }
233 
234 
235  //- Inplace extract elements of StringList when regular expression matches
236  // optionally invert the match
237  // eg, to extract all selected elements:
238  // inplaceSubsetMatchingStrings<regExp, stringList>(myRegExp, lst);
239  template<class UnaryMatchPredicate, class StringListType>
241  (
242  const UnaryMatchPredicate& matcher,
243  StringListType& input,
244  const bool invert=false
245  );
246 
247  //- Inplace extract elements of StringList when regular expression matches
248  // Template partial specialization of inplaceSubsetMatchingStrings
249  template<class StringListType>
251  (
252  const regExp& matcher,
253  StringListType& input,
254  const bool invert=false
255  )
256  {
257  inplaceSubsetMatchingStrings(matcher, input, invert);
258  }
259 
260  //- Extract elements of StringList when regular expression matches
261  // Template partial specialization of subsetMatchingStrings
262  template<class StringListType>
264  (
265  const keyType& matcher,
266  StringListType& input,
267  const bool invert=false
268  )
269  {
270  return
271  (
272  matcher.isPattern()
273  ? inplaceSubsetMatchingStrings(regExp(matcher), input, invert)
274  : inplaceSubsetMatchingStrings(matcher, input, invert)
275  );
276  }
277 
278  //- Inplace extract elements of StringList when regular expression matches
279  // Template partial specialization of inplaceSubsetMatchingStrings
280  template<class StringListType>
282  (
283  const wordRe& matcher,
284  StringListType& input,
285  const bool invert=false
286  )
287  {
288  inplaceSubsetMatchingStrings(matcher, input, invert);
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 wordRes& 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 UList<wordRe>& regexs,
310  StringListType& input,
311  const bool invert=false
312  )
313  {
315  }
316 
317 
318 
319 
320 /*---------------------------------------------------------------------------*\
321  Namespace stringListOps Declaration
322 \*---------------------------------------------------------------------------*/
323 
324 namespace stringListOps
325 {
326 
327 //- Functor to determine if a string is exists in a list of strings.
328 // For example,
329 //
330 // \code
331 // reduce(text, stringListOps::foundOp<word>(myNames));
332 // \endcode
333 template<class StringType>
334 struct foundOp
335 {
337 
339  :
340  values(list)
341  {}
342 
343  bool operator()(const std::string& text) const
344  {
345  return values.found(text);
346  }
347 };
348 
349 } // End namespace stringListOps
350 
351 
352 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
353 
354 } // End namespace Foam
355 
356 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
357 
358 // Housekeeping
359 
360 namespace Foam
361 {
362  //- Deprecated(2018-02) find using C-string as a regex
363  // \deprecated(2018-02) Treating string as regex may be inefficient
364  // and lead to unintended results.
365  // Use regExp, keyType, wordRe instead, or findMatchingStrings()
366  template<class StringType>
368  (
369  const char* disallowed,
370  const UList<StringType>& input,
371  const bool invert=false
372  ) = delete;
373 
374  //- Deprecated(2018-02) find using string as a regex
375  // \deprecated(2018-02) Treating string as regex may be inefficient
376  // and lead to unintended results.
377  // Use regExp, keyType, wordRe instead, or findMatchingStrings()
378  template<class StringType>
380  (
381  const std::string& disallowed,
382  const UList<StringType>& input,
383  const bool invert=false
384  ) = delete;
385 
386  //- Deprecated(2018-02) subset using C-string as a regex
387  // \deprecated(2018-02) Treating string as regex may be inefficient
388  // and lead to unintended results.
389  // Use regExp, keyType, wordRe instead, or subsetMatchingStrings()
390  template<class StringListType>
391  StringListType subsetStrings
392  (
393  const char* disallowed,
394  const StringListType& input,
395  const bool invert=false
396  ) = delete;
397 
398  //- Deprecated(2018-02) subset using string as a regex
399  // \deprecated(2018-02) Treating string as regex may be inefficient
400  // and lead to unintended results.
401  // Use regExp, keyType, wordRe instead, or subsetMatchingStrings()
402  template<class StringListType>
403  StringListType subsetStrings
404  (
405  const std::string& disallowed,
406  const StringListType& input,
407  const bool invert=false
408  ) = delete;
409 
410  //- Deprecated(2018-02) subset using C-string as a regex
411  // \deprecated(2018-02) Treating string as regex may be inefficient
412  // and lead to unintended results.
413  // Use regExp, keyType, wordRe instead, or inplaceSubsetMatchingStrings()
414  template<class StringListType>
416  (
417  const char* disallowed,
418  StringListType& input,
419  const bool invert=false
420  ) = delete;
421 
422  //- Deprecated(2018-02) subset using string as a regex
423  // \deprecated(2018-02) Treating string as regex may be inefficient
424  // and lead to unintended results.
425  // Use keyType, wordRe instead, or inplaceSubsetMatchingStrings()
426  template<class StringListType>
428  (
429  const std::string& disallowed,
430  StringListType& input,
431  const bool invert=false
432  ) = delete;
433 
434 } // End namespace Foam
435 
436 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
437 
438 #ifdef NoRepository
439  #include "stringListOpsTemplates.C"
440 #endif
441 
442 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
443 
444 #endif
445 
446 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:74
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:334
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:251
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:35
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::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:166
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
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:75
Foam::stringListOps::foundOp::foundOp
foundOp(const UList< StringType > &list)
Definition: stringListOps.H:338
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::UList::found
bool found(const T &val, const label start=0) const
True if the value if found in the list.
Definition: UListI.H:212
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:336
stringListOpsTemplates.C
Foam::stringListOps::foundOp::operator()
bool operator()(const std::string &text) const
Definition: stringListOps.H:343
Foam::findMatchingStrings
labelList findMatchingStrings(const UnaryMatchPredicate &matcher, const UList< StringType > &input, const bool invert=false)
Extract list indices for all matches.
stringList.H