wordResI.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) 2017-2021 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 \*---------------------------------------------------------------------------*/
27 
28 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
29 
31 {
32  return NullObjectRef<wordRes>();
33 }
34 
35 
36 inline Foam::label Foam::wordRes::first_match
37 (
38  const UList<wordRe>& selectors,
39  const std::string& text,
40  const bool literal
41 )
42 {
43  label index = 0;
44  for (const wordRe& select : selectors)
45  {
46  if (select.match(text, literal))
47  {
48  return index;
49  }
50  ++index;
51  }
52 
53  return -1;
54 }
55 
56 
57 inline Foam::wordRe::compOption Foam::wordRes::found_matched
58 (
59  const UList<wordRe>& selectors,
60  const std::string& text
61 )
62 {
63  auto retval(wordRe::compOption::UNKNOWN);
64 
65  for (const wordRe& select : selectors)
66  {
67  if (select.isLiteral())
68  {
69  if (select.match(text, true))
70  {
71  return wordRe::compOption::LITERAL;
72  }
73  }
74  else if
75  (
76  // Only match regex once
77  retval == wordRe::compOption::UNKNOWN
78  && select.match(text, false)
79  )
80  {
81  retval = wordRe::compOption::REGEX;
82  }
83  }
84 
85  return retval;
86 }
87 
88 
89 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
90 
91 inline bool Foam::wordRes::match(const std::string& text, bool literal) const
92 {
93  return (first_match(*this, text, literal) >= 0);
94 }
95 
96 
98 Foam::wordRes::matched(const std::string& text) const
99 {
100  return found_matched(*this, text);
101 }
102 
103 
104 template<class StringType>
106 (
107  const UList<StringType>& input,
108  const bool invert
109 ) const
110 {
111  const label len = input.size();
112 
113  labelList indices(len);
114 
115  label count = 0;
116  for (label i=0; i < len; ++i)
117  {
118  if (match(input[i]) ? !invert : invert)
119  {
120  indices[count] = i;
121  ++count;
122  }
123  }
124  indices.resize(count);
125 
126  return indices;
127 }
128 
129 
130 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
131 
132 inline bool Foam::wordRes::operator()(const std::string& text) const
133 {
134  return (wordRes::first_match(*this, text) >= 0);
135 }
136 
137 
138 inline bool Foam::wordRes::matcher::operator()(const std::string& text) const
139 {
140  return (wordRes::first_match(values, text) >= 0);
141 }
142 
143 
144 // ************************************************************************* //
Foam::wordRes::matching
labelList matching(const UList< StringType > &input, const bool invert=false) const
Return list indices for all matches.
Foam::List::resize
void resize(const label len)
Adjust allocated size of list.
Definition: ListI.H:139
Foam::HashTableOps::values
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:149
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:98
Foam::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:80
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:91
Foam::wordRes::operator()
bool operator()(const std::string &text) const
Identical to match(), for use as a predicate.
Definition: wordResI.H:132
Foam::stringOps::match
bool match(const UList< wordRe > &patterns, const std::string &text)
Return true if text matches one of the regular expressions.
Definition: stringOps.H:76
Foam::BitOps::count
unsigned int count(const UList< bool > &bools, const bool val=true)
Count number of 'true' entries.
Definition: BitOps.H:77
Foam::List< label >
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
True if text matches ANY of the entries.
Definition: wordResI.H:138
Foam::input
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:55
Foam::wordRes::null
static const wordRes & null()
Return a null wordRes - a reference to the NullObject.
Definition: wordResI.H:30
Foam::wordRe::compOption
compOption
Enumeration with compile options.
Definition: wordRe.H:102