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-2019 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 wordRes_H
39 #define 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  inline static bool found_match
60  (
61  const UList<wordRe>& patterns,
62  const std::string& text,
63  bool literal=false
64  );
65 
66  //- Smart match across entire list, returning the match type.
67  // Stops on the first literal match, or continues to examine
68  // if a regex match occurs.
69  // \return wordRe::LITERAL, wordRe::REGEX on match and
70  // wordRe::UNKNOWN otherwise.
71  inline static wordRe::compOption found_matched
72  (
73  const UList<wordRe>& patterns,
74  const std::string& text
75  );
76 
77 
78 public:
79 
80  // Public Classes
81 
82  //- Functor wrapper for matching against a List of wordRe
83  struct matcher
84  {
85  const UList<wordRe>& values;
86 
87  matcher(const UList<wordRe>& list)
88  :
89  values(list)
90  {}
91 
92  //- Return true if string matches ANY of the regular expressions
93  // Allows use as a predicate.
94  bool operator()(const std::string& text) const
95  {
96  return found_match(values, text);
97  }
98  };
99 
100 
101  // Factory Methods
102 
103  //- Return a null wordRes - a reference to the NullObject
104  inline static const wordRes& null();
105 
106  //- Return a wordRes with duplicate entries filtered out.
107  // No distinction made between literals or regular expressions.
108  static wordRes uniq(const UList<wordRe>& input);
109 
110 
111  // Constructors
112 
113  //- Inherit constructors from List of wordRe
114  using List<wordRe>::List;
115 
116 
117  //- Destructor
118  ~wordRes() = default;
119 
120 
121  // Member Functions
122 
123  //- Filter out duplicate entries (inplace).
124  // No distinction made between literals or regular expressions.
125  void uniq();
126 
127  //- Smart match as literal or regex, stopping on the first match.
128  //
129  // \param literal Force literal match only.
130  // \return True if text matches ANY of the entries.
131  inline bool match(const std::string& text, bool literal=false) const;
132 
133  //- Smart match in the list of matchers, returning the match type.
134  // It stops if there is a literal match, or continues to examine
135  // other regexs.
136  // \return LITERAL if a lteral match was found, REGEX if a regex
137  // match was found and UNKNOWN otherwise.
138  inline wordRe::compOption matched(const std::string& text) const;
139 
140  //- Extract list indices for all matches.
141  //
142  // \param input A list of string inputs to match against
143  // \param invert invert the matching logic
144  // \return The locations (indices) in the input list where match()
145  // is true
146  template<class StringType>
147  inline labelList matching
148  (
149  const UList<StringType>& input,
150  const bool invert=false
151  ) const;
152 
153 
154  // Member Operators
155 
156  //- Identical to match(), for use as a predicate.
157  inline bool operator()(const std::string& text) const;
158 };
159 
160 
161 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
162 
163 } // End namespace Foam
164 
165 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
166 
167 #include "wordResI.H"
168 
169 #endif
170 
171 // ************************************************************************* //
Foam::labelList
List< label > labelList
A List of labels.
Definition: List.H:71
Foam::wordRes::matching
labelList matching(const UList< StringType > &input, const bool invert=false) const
Extract list indices for all matches.
Foam::wordRes::matched
wordRe::compOption matched(const std::string &text) const
Smart match in the list of matchers, returning the match type.
Definition: wordResI.H:94
Foam::invert
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:36
Foam::wordRes::match
bool match(const std::string &text, bool literal=false) const
Smart match as literal or regex, stopping on the first match.
Definition: wordResI.H:87
Foam::wordRes::operator()
bool operator()(const std::string &text) const
Identical to match(), for use as a predicate.
Definition: wordResI.H:128
Foam::wordRes::~wordRes
~wordRes()=default
Destructor.
Foam::wordRes::uniq
void uniq()
Filter out duplicate entries (inplace).
Definition: wordRes.C:70
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::List< wordRe >::List
constexpr List() noexcept
Null constructor.
Definition: ListI.H:94
Foam::wordRes::matcher::matcher
matcher(const UList< wordRe > &list)
Definition: wordRes.H:86
Foam::wordRes::matcher
Functor wrapper for matching against a List of wordRe.
Definition: wordRes.H:82
Foam::wordRes::matcher::values
const UList< wordRe > & values
Definition: wordRes.H:84
wordReList.H
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:102
Foam::UList
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:103
Foam::wordRes
A List of wordRe with additional matching capabilities.
Definition: wordRes.H:51
Foam::wordRes::matcher::operator()
bool operator()(const std::string &text) const
Return true if string matches ANY of the regular expressions.
Definition: wordRes.H:93
Foam::wordRe::compOption
compOption
Enumeration with compile options.
Definition: wordRe.H:94
wordResI.H