wordRes.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) 2016-2024 OpenCFD Ltd.
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 Class
27  Foam::wordRes
28 
29 Description
30  A List of wordRe with additional matching capabilities.
31 
32 SourceFiles
33  wordResI.H
34  wordRes.C
35 
36 \*---------------------------------------------------------------------------*/
37 
38 #ifndef Foam_wordRes_H
39 #define Foam_wordRes_H
40 
41 #include "wordRe.H"
42 #include "List.H"
43 
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 
46 namespace Foam
47 {
48 
49 // List types
50 typedef List<wordRe> wordReList;
51 typedef UList<wordRe> wordReUList;
52 
53 
54 /*---------------------------------------------------------------------------*\
55  Class wordRes Declaration
56 \*---------------------------------------------------------------------------*/
57 
58 class wordRes
59 :
60  public List<wordRe>
61 {
62  // Private Methods
63 
64  //- Smart match as literal or regex, stopping on the first match.
65  // \return index of first match, -1 if not found
66  inline static label first_match
67  (
68  const UList<wordRe>& selectors,
69  const std::string& text,
70  const bool literal = false
71  );
72 
73 
74 public:
75 
76  // Static Methods
77 
78  //- Return a null wordRes (reference to a nullObject).
79  //- Behaves like a empty wordRes.
80  static const wordRes& null() noexcept
81  {
82  return NullObjectRef<wordRes>();
83  }
84 
85 
86  // Constructors
87 
88  //- Inherit constructors from List of wordRe
89  using List<wordRe>::List;
90 
91 
92  //- Destructor
93  ~wordRes() = default;
94 
95 
96  // Static Functions
97 
98  //- Return a wordRes with duplicate entries filtered out.
99  // No distinction made between literals and regular expressions.
100  static wordRes uniq(const UList<wordRe>& input);
101 
102  //- Test for a match
103  inline static bool match
104  (
105  const UList<wordRe>& selectors,
106  const std::string& text,
107  bool literal = false
108  );
109 
110  //- Smart match across entire list, returning the best match type.
111  // Stops on the first literal match, or continues to examine
112  // if a regex match occurs.
113  // \return wordRe::LITERAL, wordRe::REGEX on match and
114  // wordRe::UNKNOWN otherwise.
115  inline static wordRe::compOption matched
116  (
117  const UList<wordRe>& selectors,
118  const std::string& text
119  );
120 
121  //- Determine the list indices for all matches.
122  //
123  // \return indices of the matches in the input list
124  template<class StringType>
125  inline static labelList matching
126  (
128  const wordRe& select,
130  const UList<StringType>& input,
132  const bool invert = false
133  );
134 
135  //- Determine the list indices for all matches.
136  //
137  // \return indices of the matches in the input list
138  template<class StringType>
139  inline static labelList matching
140  (
142  const UList<wordRe>& selectors,
144  const UList<StringType>& input,
146  const bool invert = false
147  );
148 
149 
150  // Member Functions
151 
152  //- Filter out duplicate entries (inplace).
153  // No distinction made between literals and regular expressions.
154  void uniq();
155 
156  //- Smart match as literal or regex, stopping on the first match.
157  //
158  // \param literal Force literal match only.
159  // \return True if text matches ANY of the entries.
160  inline bool match(const std::string& text, bool literal=false) const;
161 
162  //- Smart match in the list of matchers, returning the match type.
163  // It stops if there is a literal match, or continues to examine
164  // other regexs.
165  // \return LITERAL if a lteral match was found,
166  // REGEX if any regex match was found,
167  // UNKNOWN otherwise.
168  inline wordRe::compOption matched(const std::string& text) const;
169 
170  //- Determine the list indices for all matches.
171  //
172  // \return indices of the matches in the input list
173  template<class StringType>
174  inline labelList matching
175  (
177  const UList<StringType>& input,
179  const bool invert = false
180  ) const;
181 
182 
183  // Member Operators
184 
185  //- Identical to match(), for use as a predicate.
186  inline bool operator()(const std::string& text) const;
187 
188 
189  // Functors
190 
191  //- Functor wrapper of a list of wordRe for matching
192  struct matcher
193  {
194  //- Construct with \em select matcher(s)
195  inline explicit matcher(const UList<wordRe>& selectors) noexcept;
196 
197  //- No selectors defined
198  inline bool empty() const noexcept;
199 
200  //- True if text matches ANY of the selectors.
201  //- Always false if entries are empty.
202  // Allows use as a predicate.
203  inline bool operator()(const std::string& text) const;
204 
205  private:
206  const UList<wordRe>& select_;
207  };
208 
209 
210  //- Functor wrapper of allow/deny lists of wordRe for filtering
211  //
212  // An empty 'allow' accepts everything not in 'deny'.
213  // A literal 'allow' match has higher priority than any 'deny'.
214  // A regex 'allow' match has lower priority than any 'deny'.
215  //
216  // Example (when applied to a list of words),
217  // \verbatim
218  // input: ( abc apple test other val val1 val2 wall wall1 wall2 )
219  // allow: ( abc def "t.*" other val val1 "wall.*" )
220  // deny: ( "[ab].*" "t.*" other "val[0-9]" wall )
221  //
222  // result: (abc other val val1 wall1 wall2)
223  // \endverbatim
224  struct filter
225  {
226  //- Construct with 'allow' and 'deny' matchers
227  inline filter
228  (
229  const UList<wordRe>& allow,
230  const UList<wordRe>& deny
231  ) noexcept;
232 
233  //- Nothing defined
234  inline bool empty() const noexcept;
235 
236  //- True if matched but not blocked
237  inline bool operator()(const std::string& text) const;
238 
239  private:
240  const UList<wordRe>& allow_;
241  const UList<wordRe>& deny_;
242  };
243 };
244 
245 
246 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
247 
248 } // End namespace Foam
249 
250 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
251 
252 #include "wordResI.H"
253 
254 #endif
255 
256 // ************************************************************************* //
static wordRe::compOption matched(const UList< wordRe > &selectors, const std::string &text)
Smart match across entire list, returning the best match type.
Definition: wordResI.H:65
UList< wordRe > wordReUList
UList of wordRe (word or regex)
Definition: wordRes.H:46
List< bool > select(const label n, const labelUList &locations)
Construct a selection list of bools (all false) with the given pre-size, subsequently add specified l...
Definition: BitOps.C:134
compOption
Enumeration with compile options.
Definition: wordRe.H:107
static const wordRes & null() noexcept
Return a null wordRes (reference to a nullObject). Behaves like a empty wordRes.
Definition: wordRes.H:80
Functor wrapper of allow/deny lists of wordRe for filtering.
Definition: wordRes.H:268
static labelList matching(const wordRe &select, const UList< StringType > &input, const bool invert=false)
Determine the list indices for all matches.
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:33
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:53
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings...
Definition: wordRe.H:78
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:105
const direction noexcept
Definition: Scalar.H:258
matcher(const UList< wordRe > &selectors) noexcept
Construct with select matcher(s)
Definition: wordResI.H:197
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:30
static bool match(const UList< wordRe > &selectors, const std::string &text, bool literal=false)
Test for a match.
Definition: wordResI.H:47
List< wordRe > wordReList
List of wordRe (word or regex)
Definition: wordRes.H:45
~wordRes()=default
Destructor.
void uniq()
Filter out duplicate entries (inplace).
Definition: wordRes.C:63
bool operator()(const std::string &text) const
Identical to match(), for use as a predicate.
Definition: wordResI.H:188
Namespace for OpenFOAM.
bool empty() const noexcept
No selectors defined.
Definition: wordResI.H:216