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