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
37struct pt_regexp;
38struct pt_regmatch_t;
39
40namespace Pt {
41
42class RegexSMatch;
43
48class PT_API InvalidRegex : public std::runtime_error
49{
50 public:
52 InvalidRegex(const char* msg);
53
55 ~InvalidRegex() throw()
56 {}
57};
58
173class PT_API Regex
174{
175 public:
178
180 explicit Regex(const Pt::Char* ex);
181
183 explicit Regex(const Pt::String& ex);
184
186 Regex(const Regex& other);
187
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
225class PT_API RegexSMatch
226{
227 friend class Regex;
228
229 public:
232
235
238
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
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
~InvalidRegex()
Destructor.
Definition Regex.h:55
InvalidRegex(const char *msg)
Construct with message.
Result of a regular expression match.
Definition Regex.h:226
std::size_t maxSize() const
Returns the max number of matches the implementation allows.
std::size_t position(std::size_t n=0) const
Returns the position of the nth match.
Pt::String str(std::size_t n=0) const
Returns the nth match.
std::size_t length(std::size_t n=0) const
Returns the size of the nth match.
bool empty() const
Returns true if no match.
RegexSMatch & operator=(const RegexSMatch &other)
Assignment operator.
RegexSMatch(const RegexSMatch &other)
Copy constructor.
Pt::String format(const Pt::String &str) const
Formats a string according to a format specifier.
RegexSMatch()
Default Constructor.
~RegexSMatch()
Destructor.
std::size_t size() const
Returns the number of matches.
bool match(const Pt::String &str, RegexSMatch &sm) const
Matches the regular experession to a string.
bool match(const Char *str) const
Returns true if string matches.
Regex(const Pt::String &ex)
Construct from regex string.
Regex & operator=(const Regex &other)
Assignment operator.
Regex(const Pt::Char *ex)
Construct from regex string.
bool match(const Char *str, RegexSMatch &sm) const
Matches the regular experession to a string.
Regex(const Regex &other)
Copy constructor.
bool match(const Pt::String &str) const
Returns true if string matches.
~Regex()
Destructor.
Regex()
Default Constructor.
Unicode capable basic_string.
Definition Api-String.h:67
Core module.
Definition Allocator.h:33
Unicode character type.
Definition String.h:67