Regex.h
1 /*
2  * Copyright (C) 2010-2013 Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef PT_REGEX_H
30 #define PT_REGEX_H
31 
32 #include <Pt/Api.h>
33 #include <Pt/String.h>
34 #include <stdexcept>
35 #include <cstddef>
36 
37 struct pt_regexp;
38 struct pt_regmatch_t;
39 
40 namespace Pt {
41 
42 class RegexSMatch;
43 
48 class PT_API InvalidRegex : public std::runtime_error
49 {
50  public:
52  InvalidRegex(const char* msg);
53 
55  ~InvalidRegex() throw()
56  {}
57 };
58 
173 class PT_API Regex
174 {
175  public:
177  Regex();
178 
180  explicit Regex(const Pt::Char* ex);
181 
183  explicit Regex(const Pt::String& ex);
184 
186  Regex(const Regex& other);
187 
189  ~Regex();
190 
192  Regex& operator=(const Regex& other);
193 
200  bool match(const Pt::String& str, RegexSMatch& sm) const;
201 
203  bool match(const Pt::String& str) const;
204 
211  bool match(const Char* str, RegexSMatch& sm) const;
212 
214  bool match(const Char* str) const;
215 
216  private:
217  pt_regexp* _expr;
218 };
219 
220 
225 class PT_API RegexSMatch
226 {
227  friend class Regex;
228 
229  public:
231  RegexSMatch();
232 
234  RegexSMatch(const RegexSMatch& other);
235 
237  ~RegexSMatch();
238 
240  RegexSMatch& operator=(const RegexSMatch& other);
241 
243  bool empty() const;
244 
247  std::size_t size() const;
248 
251  std::size_t maxSize() const;
252 
255  std::size_t position(std::size_t n = 0) const;
256 
259  std::size_t length(std::size_t n = 0) const;
260 
263  Pt::String str(std::size_t n = 0) const;
264 
270  Pt::String format(const Pt::String& str) const;
271 
272  private:
273  const Char* _str;
274  std::size_t _size;
275  pt_regmatch_t* _match;
276 };
277 
278 } // namespace Pt
279 
280 #endif // PT_REGEX_H
Regular Expressions for Unicode Strings.
Definition: Regex.h:173
Result of a regular expression match.
Definition: Regex.h:225
Invalid regular expression.
Definition: Regex.h:48
Unicode character type.
Definition: String.h:66
Unicode capable basic_string.
Definition: String.h:42
~InvalidRegex()
Destructor.
Definition: Regex.h:55