regExpPosixI.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-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 \*---------------------------------------------------------------------------*/
27 
28 #include <algorithm>
29 
30 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
31 
32 inline bool Foam::regExpPosix::meta(char c)
33 {
34  return
35  (
36  (c == '.') // any character
37  || (c == '*' || c == '+' || c == '?') // quantifiers
38  || (c == '(' || c == ')' || c == '|') // grouping/branching
39  || (c == '[' || c == ']') // range
40  );
41 }
42 
43 
44 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
45 
47 :
48  preg_(nullptr)
49 {}
50 
51 
52 inline Foam::regExpPosix::regExpPosix(const char* pattern)
53 :
54  preg_(nullptr)
55 {
56  set(pattern, false);
57 }
58 
59 
60 inline Foam::regExpPosix::regExpPosix(const std::string& pattern)
61 :
62  preg_(nullptr)
63 {
64  set(pattern, false);
65 }
66 
67 
68 inline Foam::regExpPosix::regExpPosix(const char* pattern, bool ignoreCase)
69 :
70  preg_(nullptr)
71 {
72  set(pattern, ignoreCase);
73 }
74 
75 
76 inline Foam::regExpPosix::regExpPosix(const std::string& pattern, bool ignoreCase)
77 :
78  preg_(nullptr)
79 {
80  set(pattern, ignoreCase);
81 }
82 
83 
85 :
86  preg_(rgx.preg_)
87 {
88  rgx.preg_ = nullptr;
89 }
90 
91 
92 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
93 
95 {
96  clear();
97 }
98 
99 
100 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
101 
102 inline bool Foam::regExpPosix::empty() const noexcept
103 {
104  return !preg_;
105 }
106 
107 
108 inline bool Foam::regExpPosix::exists() const noexcept
109 {
110  return preg_;
111 }
112 
113 
114 inline unsigned Foam::regExpPosix::ngroups() const
115 {
116  return preg_ ? preg_->re_nsub : 0;
117 }
118 
119 
120 inline bool Foam::regExpPosix::search(const std::string& text) const
121 {
122  return std::string::npos != find(text);
123 }
124 
125 
127 {
128  if (this != &rgx)
129  {
130  // Self-swap is a no-op
131  std::swap(preg_, rgx.preg_);
132  }
133 }
134 
135 
136 // * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * * //
137 
138 inline bool Foam::regExpPosix::operator()(const std::string& text) const
139 {
140  return match(text);
141 }
142 
143 
145 {
146  clear();
147  swap(rgx);
148 }
149 
150 
151 inline void Foam::regExpPosix::operator=(const char* pattern)
152 {
153  set(pattern);
154 }
155 
156 
157 inline void Foam::regExpPosix::operator=(const std::string& pattern)
158 {
159  set(pattern);
160 }
161 
162 
163 // ************************************************************************* //
Foam::regExpPosix
Wrapper around POSIX extended regular expressions.
Definition: regExpPosix.H:75
Foam::regExpPosix::swap
void swap(regExpPosix &rgx)
Swap contents.
Definition: regExpPosixI.H:126
Foam::regExpPosix::search
bool search(const std::string &text) const
Return true if the regex was found within the text.
Definition: regExpPosixI.H:120
Foam::regExpPosix::operator=
void operator=(const regExpPosix &)=delete
Copy assignment - disallowed.
Foam::regExpPosix::~regExpPosix
~regExpPosix()
Destructor.
Definition: regExpPosixI.H:94
Foam::regExpPosix::operator()
bool operator()(const std::string &text) const
Perform match on text.
Definition: regExpPosixI.H:138
Foam::regExpPosix::meta
static bool meta(char c)
Test if character appears to be a regular expression meta-character.
Definition: regExpPosixI.H:32
Foam::regExpPosix::empty
bool empty() const noexcept
Return true if a precompiled expression does not exist.
Definition: regExpPosixI.H:102
Foam::ListOps::find
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Find index of the first occurrence that satisfies the predicate.
Foam::regExpPosix::set
bool set(const char *pattern, bool ignoreCase=false)
Compile pattern into a regular expression, optionally ignore case.
Definition: regExpPosix.C:69
Foam::regExpPosix::regExpPosix
regExpPosix()
Construct null.
Definition: regExpPosixI.H:46
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::regExpPosix::ngroups
unsigned ngroups() const
The number of capture groups for a non-empty expression.
Definition: regExpPosixI.H:114
clear
patchWriters clear()
Foam::regExpPosix::exists
bool exists() const noexcept
Return true if a precompiled expression exists.
Definition: regExpPosixI.H:108
Foam::constant::universal::c
const dimensionedScalar c
Speed of light in a vacuum.