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-2025 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 of any selectors against the text.
103  //
104  // \return \c false if no selectors are specified
105  // \return \c true if text matches \em any of the selectors
106  inline static bool match
107  (
108  const UList<wordRe>& selectors,
109  const std::string& text,
110  bool literal = false
111  );
112 
113  //- Smart match across entire list, returning the best match type.
114  // Stops on the first literal match, or continues to examine
115  // if a regex match occurs.
116  // \return wordRe::LITERAL, wordRe::REGEX on match and
117  // wordRe::UNKNOWN otherwise.
118  inline static wordRe::compOption matched
119  (
120  const UList<wordRe>& selectors,
121  const std::string& text
122  );
123 
124  //- Determine the list indices for all matches.
125  //
126  // \return indices of the matches in the input list
127  template<class StringType>
128  inline static labelList matching
129  (
131  const wordRe& select,
133  const UList<StringType>& input,
135  const bool invert = false
136  );
137 
138  //- Determine the list indices for all matches.
139  //
140  // \return indices of the matches in the input list
141  template<class StringType>
142  inline static labelList matching
143  (
145  const UList<wordRe>& selectors,
147  const UList<StringType>& input,
149  const bool invert = false
150  );
151 
152 
153  // Member Functions
154 
155  //- Filter out duplicate entries (inplace).
156  // No distinction made between literals and regular expressions.
157  void uniq();
158 
159  //- Smart match as literal or regex, stopping on the first match.
160  //
161  // \param literal Force literal match only.
162  // \return True if text matches ANY of the entries.
163  inline bool match(const std::string& text, bool literal=false) const;
164 
165  //- Smart match in the list of matchers, returning the match type.
166  // It stops if there is a literal match, or continues to examine
167  // other regexs.
168  // \return LITERAL if a lteral match was found,
169  // REGEX if any regex match was found,
170  // UNKNOWN otherwise.
171  inline wordRe::compOption matched(const std::string& text) const;
172 
173  //- Determine the list indices for all matches.
174  //
175  // \return indices of the matches in the input list
176  template<class StringType>
177  inline labelList matching
178  (
180  const UList<StringType>& input,
182  const bool invert = false
183  ) const;
184 
185 
186  // Member Operators
187 
188  //- Identical to match(), for use as a predicate.
189  inline bool operator()(const std::string& text) const;
190 
191 
192  // Functors
193 
194  //- Functor wrapper of a list of wordRe for matching
195  struct matcher
196  {
197  //- Construct with \em select matcher(s)
198  inline explicit matcher(const UList<wordRe>& selectors) noexcept;
199 
200  //- No selectors defined
201  inline bool empty() const noexcept;
202 
203  //- Apply matcher predicate
204  //
205  // \return \c false if no selectors are specified
206  // \return \c true if text matches \em any of the selectors
207  inline bool operator()(const std::string& text) const;
208 
209  private:
210  const UList<wordRe>& select_;
211  };
212 
213 
214  //- Functor wrapper of allow/deny lists of wordRe for filtering
215  //
216  // An empty filter accepts everything.
217  // An empty \em allow accepts everything not in \em deny.
218  // A literal \em allow match has higher priority than any \em deny.
219  // A regex \em allow match has lower priority than any \em deny.
220  //
221  // Example (when applied to a list of words),
222  // \verbatim
223  // input: ( abc apple test other val val1 val2 wall wall1 wall2 )
224  // allow: ( abc def "t.*" other val val1 "wall.*" )
225  // deny: ( "[ab].*" "t.*" other "val[0-9]" wall )
226  //
227  // result: (abc other val val1 wall1 wall2)
228  // \endverbatim
229  struct filter
230  {
231  //- Construct with \em allow and \em deny matchers
232  inline filter
233  (
234  const UList<wordRe>& allow,
235  const UList<wordRe>& deny
236  ) noexcept;
237 
238  //- No filtering defined
239  inline bool empty() const noexcept;
240 
241  //- True if filtering is defined
242  explicit operator bool() const noexcept { return !empty(); }
243 
244  //- Apply filter against specified text
245  //
246  // \return \c true if no filtering has been defined
247  // \return \c true if matched but not blocked
248  inline bool operator()(const std::string& text) const;
249 
250  private:
251  const UList<wordRe>& allow_;
252  const UList<wordRe>& deny_;
253  };
254 };
255 
256 
257 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
258 
259 } // End namespace Foam
260 
261 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
262 
263 #include "wordResI.H"
264 
265 #endif
266 
267 // ************************************************************************* //
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:274
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: scalarImpl.H:255
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:28
static bool match(const UList< wordRe > &selectors, const std::string &text, bool literal=false)
Test for a match of any selectors against the text.
Definition: wordResI.H:47
bool operator()(const std::string &text) const
Apply matcher predicate.
Definition: wordResI.H:228
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