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