]> git.tdb.fi Git - libs/core.git/blob - source/regmatch.h
Add Id tags and copyright notices to a few files that were missing them
[libs/core.git] / source / regmatch.h
1 /* $Id$
2
3 This file is part of libmspstrings
4 Copyright © 2007 Mikko Rasa
5 Distributed under the LGPL
6 */
7 #ifndef MSP_STRINGS_REGMATCH_H_
8 #define MSP_STRINGS_REGMATCH_H_
9
10 #include <string>
11 #include <vector>
12
13 namespace Msp {
14
15 /**
16 This class stores the result of a Regex being matched against a string.  If the
17 match was successful, the RegMatch object evaluates to true, allowing it to be
18 used in constructs like \code if(RegMatch match=regex.match("foo")) \endcode.
19
20 A RegMatch representing a successful match has one or more groups, indicating
21 matching parts of the string.  The first group (with index 0) indicates the
22 part matched by the whol regex.  Further groups, if present, indicate parts
23 matched by subregexes.  These are ordered from left to right, by the opening
24 parenthesis of the subregex.
25 */
26 class RegMatch
27 {
28 public:
29         /**
30         A single subregex of the match.
31         */
32         struct Group
33         {
34                 bool match;       //< Whether or not this group matched
35                 unsigned begin;   //< First offset of the match
36                 unsigned end;     //< One-past-last offset
37                 unsigned length;  //< Length of the match (end-begin)
38                 std::string str;  //< The part of the string that matched
39
40                 Group(): match(false) { }
41                 operator bool() const { return match; }
42         };
43         typedef std::vector<Group> GroupArray;
44
45         /**
46         Constructs a RegMatch representig a non-match.  Used by Regex.
47         */
48         RegMatch() { }
49
50         /**
51         Constructs a new RegMatch from a string and groups.  The length and str members
52         of each group are computed and need not be set.  Used by Regex.
53         */
54         RegMatch(const std::string &, const std::vector<Group> &);
55
56         /**
57         Returns a reference to a single group in the match.  An exception is thrown
58         if the requested group does not exist.
59         */
60         const Group &group(unsigned) const;
61
62         /**
63         Returns true if the RegMatch object represents a non-match.
64         */
65         bool empty() const { return groups.empty(); }
66
67         /**
68         Returns the number of groups in this match.
69         */
70         unsigned size() const { return groups.size(); }
71
72         /**
73         Returns the begin offset of the whole match.
74         */
75         unsigned begin() const { return groups.empty()?0:groups[0].begin; }
76
77         /**
78         Returns the end offset of the whole match.
79         */
80         unsigned end() const { return groups.empty()?0:groups[0].end; }
81
82         /**
83         Shortcut for the group() function.
84         */
85         const Group &operator[](unsigned i) const { return group(i); }
86
87         operator bool() const { return !empty(); }
88 private:
89         std::vector<Group> groups;
90 };
91
92 } // namespace Msp
93
94 #endif