sbuild  1.6.10
sbuild-regex.h
1 /* Copyright © 2006-2007,2012 Roger Leigh <rleigh@debian.org>
2  *
3  * schroot is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * schroot is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  *********************************************************************/
18 
19 #ifndef SBUILD_REGEX_H
20 #define SBUILD_REGEX_H
21 
22 #include <istream>
23 #include <ostream>
24 #include <string>
25 
26 #include <sbuild/sbuild-config.h>
27 # ifdef HAVE_REGEX_REGEX
28 # include <regex>
29 # elif HAVE_TR1_REGEX
30 # include <tr1/regex.hpp>
31 namespace std {
32  using std::tr1::regex;
33  using std::tr1::regex_error;
34  using std::tr1::regex_match;
35  using std::tr1::regex_search;
36 }
37 # elif HAVE_BOOST_REGEX
38 # include <boost/regex.hpp>
39 namespace std {
40  using boost::regex;
41  using boost::regex_error;
42  using boost::regex_match;
43  using boost::regex_search;
44 }
45 # else
46 # error An regex implementation is not available
47 # endif
48 
49 namespace sbuild
50 {
51 
66  class regex
67  {
68  public:
70  regex ():
71  comp(),
72  rstr()
73  {}
74 
82  regex (std::string const& pattern):
83  comp(pattern, std::regex::extended),
84  rstr(pattern)
85  {}
86 
94  regex (const char *pattern):
95  comp(pattern, std::regex::extended),
96  rstr(pattern)
97  {}
98 
101  {}
102 
110  regex (const regex& rhs):
111  comp(rhs.comp),
112  rstr(rhs.rstr)
113  {}
114 
115  std::string const&
116  str() const
117  {
118  return rstr;
119  }
120 
121  bool
122  compare (regex const& rhs) const
123  {
124  return this->rstr != rhs.rstr;
125  }
126 
127  bool
128  search (std::string const& str) const
129  {
130  return std::regex_search(str, this->comp);
131  }
132 
142  template <class charT, class traits>
143  friend
144  std::basic_istream<charT,traits>&
145  operator >> (std::basic_istream<charT,traits>& stream,
146  regex& rhs)
147  {
148  std::string regex;
149 
150  if (std::getline(stream, regex))
151  {
152  rhs.comp.assign(regex, std::regex::extended);
153  rhs.rstr = regex;
154  }
155 
156  return stream;
157  }
158 
166  template <class charT, class traits>
167  friend
168  std::basic_ostream<charT,traits>&
169  operator << (std::basic_ostream<charT,traits>& stream,
170  regex const& rhs)
171  {
172  return stream << rhs.str();
173  }
174 
175  private:
177  std::regex comp;
179  std::string rstr;
180  };
181 
185  inline bool
186  regex_search (const std::string& str,
187  regex const& regex)
188  {
189  return regex.search(str);
190  }
191 
192 }
193 
194 #endif /* SBUILD_REGEX_H */
195 
196 /*
197  * Local Variables:
198  * mode:C++
199  * End:
200  */
POSIX extended regular expression.
Definition: sbuild-regex.h:66
~regex()
Definition: sbuild-regex.h:100
Debian source builder components.
Definition: sbuild-auth-null.h:24
regex()
The constructor.
Definition: sbuild-regex.h:70
STL namespace.
std::string rstr
String containing the regex.
Definition: sbuild-regex.h:179
bool regex_search(const std::string &str, regex const &regex)
Search using the regular expression.
Definition: sbuild-regex.h:186
regex(const char *pattern)
The constructor.
Definition: sbuild-regex.h:94
std::regex comp
Compiled regular expression.
Definition: sbuild-regex.h:177
regex(const regex &rhs)
The copy constructor.
Definition: sbuild-regex.h:110
regex(std::string const &pattern)
The constructor.
Definition: sbuild-regex.h:82