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-2023 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  //- Smart match across entire list, returning the best match type.
74  // Stops on the first literal match, or continues to examine
75  // if a regex match occurs.
76  // \return wordRe::LITERAL, wordRe::REGEX on match and
77  // wordRe::UNKNOWN otherwise.
78  inline static wordRe::compOption found_matched
79  (
80  const UList<wordRe>& selectors,
81  const std::string& text
82  );
83 
84 
85 public:
86 
87  // Static Data / Methods
88 
89  //- Return a null wordRes - a reference to the NullObject
90  inline static const wordRes& null();
91 
92  //- Return a wordRes with duplicate entries filtered out.
93  // No distinction made between literals and regular expressions.
94  static wordRes uniq(const UList<wordRe>& input);
95 
96 
97  // Constructors
98 
99  //- Inherit constructors from List of wordRe
100  using List<wordRe>::List;
101 
102 
103  //- Destructor
104  ~wordRes() = default;
105 
106 
107  // Member Functions
108 
109  //- Filter out duplicate entries (inplace).
110  // No distinction made between literals and regular expressions.
111  void uniq();
112 
113  //- Smart match as literal or regex, stopping on the first match.
114  //
115  // \param literal Force literal match only.
116  // \return True if text matches ANY of the entries.
117  inline bool match(const std::string& text, bool literal=false) const;
118 
119  //- Smart match in the list of matchers, returning the match type.
120  // It stops if there is a literal match, or continues to examine
121  // other regexs.
122  // \return LITERAL if a lteral match was found,
123  // REGEX if any regex match was found,
124  // UNKNOWN otherwise.
125  inline wordRe::compOption matched(const std::string& text) const;
126 
127  //- Return list indices for all matches.
128  //
129  // \param input A list of string inputs to match against
130  // \param invert invert the matching logic
131  // \return indices of the matches in the input list
132  template<class StringType>
133  inline labelList matching
134  (
135  const UList<StringType>& input,
136  const bool invert=false
137  ) const;
138 
139 
140  // Member Operators
141 
142  //- Identical to match(), for use as a predicate.
143  inline bool operator()(const std::string& text) const;
144 
145 
146  // Functors
147 
148  //- Functor wrapper of a list of wordRe for matching
149  struct matcher
150  {
151  //- Construct with 'allow' matcher
152  inline matcher(const UList<wordRe>& allow);
153 
154  //- Nothing defined
155  inline bool empty() const noexcept;
156 
157  //- True if text matches ANY of the entries.
158  // Allows use as a predicate.
159  inline bool operator()(const std::string& text) const;
160 
161  private:
162  const UList<wordRe>& allow_;
163  };
164 
165 
166  //- Functor wrapper of allow/deny lists of wordRe for filtering
167  //
168  // An empty 'allow' accepts everything not in 'deny'.
169  // A literal 'allow' match has higher priority than any 'deny'.
170  // A regex 'allow' match has lower priority than any 'deny'.
171  //
172  // Example (when applied to a list of words),
173  // \verbatim
174  // input: ( abc apple test other val val1 val2 wall wall1 wall2 )
175  // allow: ( abc def "t.*" other val val1 "wall.*" )
176  // deny: ( "[ab].*" "t.*" other "val[0-9]" wall )
177  //
178  // result: (abc other val val1 wall1 wall2)
179  // \endverbatim
180  struct filter
181  {
182  //- Construct with 'allow' and 'deny' matchers
183  inline filter
184  (
185  const UList<wordRe>& allow,
186  const UList<wordRe>& deny
187  );
188 
189  //- Nothing defined
190  inline bool empty() const noexcept;
191 
192  //- True if matched but not blocked
193  inline bool operator()(const std::string& text) const;
194 
195  private:
196  const UList<wordRe>& allow_;
197  const UList<wordRe>& deny_;
198  };
199 };
200 
201 
202 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
203 
204 } // End namespace Foam
205 
206 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
207 
208 #include "wordResI.H"
209 
210 #endif
211 
212 // ************************************************************************* //
filter(const UList< wordRe > &allow, const UList< wordRe > &deny)
Construct with &#39;allow&#39; and &#39;deny&#39; matchers.
Definition: wordResI.H:143
labelList matching(const UList< StringType > &input, const bool invert=false) const
Return list indices for all matches.
wordRe::compOption matched(const std::string &text) const
Smart match in the list of matchers, returning the match type.
Definition: wordResI.H:91
UList< wordRe > wordReUList
UList of wordRe (word or regex)
Definition: wordRes.H:46
matcher(const UList< wordRe > &allow)
Construct with &#39;allow&#39; matcher.
Definition: wordResI.H:134
compOption
Enumeration with compile options.
Definition: wordRe.H:107
Functor wrapper of allow/deny lists of wordRe for filtering.
Definition: wordRes.H:216
static const wordRes & null()
Return a null wordRes - a reference to the NullObject.
Definition: wordResI.H:23
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:33
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:53
bool empty() const noexcept
Nothing defined.
Definition: wordResI.H:158
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
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:29
bool operator()(const std::string &text) const
True if text matches ANY of the entries.
Definition: wordResI.H:164
List< wordRe > wordReList
List of wordRe (word or regex)
Definition: wordRes.H:45
bool match(const std::string &text, bool literal=false) const
Smart match as literal or regex, stopping on the first match.
Definition: wordResI.H:84
Functor wrapper of a list of wordRe for matching.
Definition: wordRes.H:175
~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:125
bool operator()(const std::string &text) const
True if matched but not blocked.
Definition: wordResI.H:170
Namespace for OpenFOAM.
bool empty() const noexcept
Nothing defined.
Definition: wordResI.H:153