stringListOps.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 InNamespace
25  Foam
26 
27 Description
28  Operations on lists of strings.
29 
30 SourceFiles
31  stringListOpsTemplates.C
32 
33 \*---------------------------------------------------------------------------*/
34 
35 #ifndef stringListOps_H
36 #define stringListOps_H
37 
38 #include "regExp.H"
39 #include "labelList.H"
40 #include "stringList.H"
41 #include "wordReList.H"
42 #include "wordReListMatcher.H"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48  // single-string matches:
49 
50  //- Return true if string matches one of the regular expressions
51  inline bool findStrings
52  (
53  const wordReListMatcher& matcher,
54  const std::string& str
55  )
56  {
57  return matcher.match(str);
58  }
59 
60  // multi-string matches:
61 
62  //- Return list indices for matching strings
63  template<class Matcher, class StringType>
65  (
66  const Matcher&,
67  const UList<StringType>&,
68  const bool invert=false
69  );
70 
71  //- Return list indices for strings matching the regular expression
72  // Template partial specialisation of findMatchingStrings
73  template<class StringType>
75  (
76  const regExp& re,
77  const UList<StringType>& lst,
78  const bool invert=false
79  )
80  {
81  return findMatchingStrings(re, lst, invert);
82  }
83 
84  //- Return list indices for strings matching the regular expression
85  // Template partial specialisation of findMatchingStrings
86  template<class StringType>
88  (
89  const char* rePattern,
90  const UList<StringType>& lst,
91  const bool invert=false
92  )
93  {
94  const regExp re(rePattern);
95  return findStrings(re, lst, invert);
96  }
97 
98  //- Return list indices for strings matching the regular expression
99  // Template partial specialisation of findMatchingStrings
100  template<class StringType>
102  (
103  const std::string& rePattern,
104  const UList<StringType>& lst,
105  const bool invert=false
106  )
107  {
108  const regExp re(rePattern);
109  return findMatchingStrings(re, lst, invert);
110  }
111 
112  //- Return list indices for strings matching the regular expression
113  // Template partial specialisation of findMatchingStrings
114  template<class StringType>
116  (
117  const wordRe& wre,
118  const UList<StringType>& lst,
119  const bool invert=false
120  )
121  {
122  return findMatchingStrings(wre, lst, invert);
123  }
124 
125 
126  //- Return list indices for strings matching one of the regular expression
127  // Template partial specialisation of findMatchingStrings
128  template<class StringType>
130  (
131  const wordReListMatcher& matcher,
132  const UList<StringType>& lst,
133  const bool invert=false
134  )
135  {
136  return findMatchingStrings(matcher, lst, invert);
137  }
138 
139  // subsetting multi-string matches (similar to ListOp):
140 
141  //- Extract elements of StringList when regular expression matches
142  // optionally invert the match
143  // eg, to extract all selected elements:
144  // subsetMatchingStrings<regExp, stringList>(myRegExp, lst);
145  template<class Matcher, class StringListType>
146  StringListType subsetMatchingStrings
147  (
148  const Matcher&,
149  const StringListType&,
150  const bool invert=false
151  );
152 
153  //- Extract elements of StringList when regular expression matches
154  // Template partial specialisation of subsetMatchingStrings
155  template<class StringListType>
156  StringListType subsetStrings
157  (
158  const regExp& re,
159  const StringListType& lst,
160  const bool invert=false
161  )
162  {
163  return subsetMatchingStrings(re, lst, invert);
164  }
165 
166  //- Extract elements of StringList when regular expression matches
167  // Template partial specialisation of subsetMatchingStrings
168  template<class StringListType>
169  StringListType subsetStrings
170  (
171  const char* rePattern,
172  const StringListType& lst,
173  const bool invert=false
174  )
175  {
176  const regExp re(rePattern);
177  return subsetMatchingStrings(re, lst, invert);
178  }
179 
180  //- Extract elements of StringList when regular expression matches
181  // Template partial specialisation of subsetMatchingStrings
182  template<class StringListType>
183  StringListType subsetStrings
184  (
185  const std::string& rePattern,
186  const StringListType& lst,
187  const bool invert=false
188  )
189  {
190  const regExp re(rePattern);
191  return subsetMatchingStrings(re, lst, invert);
192  }
193 
194  //- Extract elements of StringList when regular expression matches
195  // Template partial specialisation of subsetMatchingStrings
196  template<class StringListType>
197  StringListType subsetStrings
198  (
199  const wordRe& wre,
200  const StringListType& lst,
201  const bool invert=false
202  )
203  {
204  return subsetMatchingStrings(wre, lst, invert);
205  }
206 
207  //- Extract elements of StringList when regular expression matches
208  // Template partial specialisation of subsetMatchingStrings
209  template<class StringListType>
210  StringListType subsetStrings
211  (
212  const wordReListMatcher& matcher,
213  const StringListType& lst,
214  const bool invert=false
215  )
216  {
217  return subsetMatchingStrings(matcher, lst, invert);
218  }
219 
220 
221  //- Inplace extract elements of StringList when regular expression matches
222  // optionally invert the match
223  // eg, to extract all selected elements:
224  // inplaceSubsetMatchingStrings<regExp, stringList>(myRegExp, lst);
225  template<class Matcher, class StringListType>
227  (
228  const Matcher&,
229  StringListType&,
230  const bool invert=false
231  );
232 
233  //- Inplace extract elements of StringList when regular expression matches
234  // Template partial specialisation of inplaceSubsetMatchingStrings
235  template<class StringListType>
237  (
238  const regExp& re,
239  StringListType& lst,
240  const bool invert=false
241  )
242  {
244  }
245 
246  //- Inplace extract elements of StringList when regular expression matches
247  // Template partial specialisation of inplaceSubsetMatchingStrings
248  template<class StringListType>
250  (
251  const char* rePattern,
252  StringListType& lst,
253  const bool invert=false
254  )
255  {
256  const regExp re(rePattern);
258  }
259 
260  //- Inplace extract elements of StringList when regular expression matches
261  // Template partial specialisation of inplaceSubsetMatchingStrings
262  template<class StringListType>
264  (
265  const std::string& rePattern,
266  StringListType& lst,
267  const bool invert=false
268  )
269  {
270  const regExp re(rePattern);
272  }
273 
274  //- Inplace extract elements of StringList when regular expression matches
275  // Template partial specialisation of inplaceSubsetMatchingStrings
276  template<class StringListType>
278  (
279  const wordRe& wre,
280  StringListType& lst,
281  const bool invert=false
282  )
283  {
285  }
286 
287  //- Inplace extract elements of StringList when regular expression matches
288  // Template partial specialisation of inplaceSubsetMatchingStrings
289  template<class StringListType>
291  (
292  const wordReListMatcher& matcher,
293  StringListType& lst,
294  const bool invert=false
295  )
296  {
297  inplaceSubsetMatchingStrings(matcher, lst, invert);
298  }
299 
300 }
301 
302 
303 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
304 
305 #ifdef NoRepository
306  #include "stringListOpsTemplates.C"
307 #endif
308 
309 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
310 
311 #endif
312 
313 // ************************************************************************* //
StringListType subsetMatchingStrings(const Matcher &, const StringListType &, const bool invert=false)
Extract elements of StringList when regular expression matches.
Wrapper around POSIX extended regular expressions.
Definition: regExp.H:61
void inplaceSubsetStrings(const regExp &re, StringListType &lst, const bool invert=false)
Inplace extract elements of StringList when regular expression matches.
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
bool findStrings(const wordReListMatcher &matcher, const std::string &str)
Return true if string matches one of the regular expressions.
Definition: stringListOps.H:52
labelList findMatchingStrings(const Matcher &, const UList< StringType > &, const bool invert=false)
Return list indices for matching strings.
labelList invert(const label len, const labelUList &)
Invert one-to-one map. Unmapped elements will be -1.
Definition: ListOps.C:37
A wordRe is a word, but can also have a regular expression for matching words.
Definition: wordRe.H:74
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:60
A wrapper for matching a List of wordRe.
StringListType subsetStrings(const regExp &re, const StringListType &lst, const bool invert=false)
Extract elements of StringList when regular expression matches.
const dimensionedScalar re
Classical electron radius: default SI units: [m].
bool match(const string &, bool literalMatch=false) const
Return true if string matches any of the regular expressions.
void inplaceSubsetMatchingStrings(const Matcher &, StringListType &, const bool invert=false)
Inplace extract elements of StringList when regular expression matches.
Namespace for OpenFOAM.