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-2018 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 bool Foam::wordRes::found_match
37 (
38  const UList<wordRe>& patterns,
39  const std::string& text,
40  bool literal
41 )
42 {
43  for (const wordRe& select : patterns)
44  {
45  if (select.match(text, literal))
46  {
47  return true;
48  }
49  }
50 
51  return false;
52 }
53 
54 
55 inline Foam::wordRe::compOption Foam::wordRes::found_matched
56 (
57  const UList<wordRe>& patterns,
58  const std::string& text
59 )
60 {
61  auto retval(wordRe::compOption::UNKNOWN);
62 
63  for (const wordRe& select : patterns)
64  {
65  if (select.isLiteral())
66  {
67  if (select.match(text, true))
68  {
69  return wordRe::compOption::LITERAL;
70  }
71  }
72  else if (wordRe::compOption::UNKNOWN == retval)
73  {
74  if (select.match(text, false))
75  {
76  retval = wordRe::compOption::REGEX;
77  }
78  }
79  }
80 
81  return retval;
82 }
83 
84 
85 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
86 
87 inline bool Foam::wordRes::match(const std::string& text, bool literal) const
88 {
89  return found_match(*this, text, literal);
90 }
91 
92 
94 Foam::wordRes::matched(const std::string& text) const
95 {
96  return found_matched(*this, text);
97 }
98 
99 
100 template<class StringType>
102 (
103  const UList<StringType>& input,
104  const bool invert
105 ) const
106 {
107  const label len = input.size();
108 
109  labelList indices(len);
110 
111  label count = 0;
112  for (label i=0; i < len; ++i)
113  {
114  if (match(input[i]) ? !invert : invert)
115  {
116  indices[count] = i;
117  ++count;
118  }
119  }
120  indices.resize(count);
121 
122  return indices;
123 }
124 
125 
126 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
127 
128 inline bool Foam::wordRes::operator()(const std::string& text) const
129 {
130  return found_match(*this, text);
131 }
132 
133 
134 // ************************************************************************* //
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::wordRe
A wordRe is a Foam::word, but can contain a regular expression for matching words or strings.
Definition: wordRe.H:72
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::List::resize
void resize(const label newSize)
Adjust allocated size of list.
Definition: ListI.H:139
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:75
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::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:94