X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Fregmatch.h;h=19cac11027dac128f6cdd537fe771d3a2316a2d9;hb=5b1368cb791cab043f0435628cacbaff36e39b7b;hp=f5a774e601c83c13e33df582187393cae398f29a;hpb=381c89769c0ffaa14e6a92c662323b5c45e1eba3;p=libs%2Fcore.git diff --git a/source/regmatch.h b/source/regmatch.h index f5a774e..19cac11 100644 --- a/source/regmatch.h +++ b/source/regmatch.h @@ -1,3 +1,10 @@ +/* $Id$ + +This file is part of libmspstrings +Copyright © 2007 Mikko Rasa +Distributed under the LGPL +*/ + #ifndef MSP_STRINGS_REGMATCH_H_ #define MSP_STRINGS_REGMATCH_H_ @@ -8,12 +15,11 @@ namespace Msp { /** This class stores the result of a Regex being matched against a string. If the -match was successful, the RegMatch object evaluates to true, allowing it to be -used in constructs like \code if(RegMatch match=regex.match("foo")) \endcode. +match was successful, the RegMatch object evaluates to true. A RegMatch representing a successful match has one or more groups, indicating matching parts of the string. The first group (with index 0) indicates the -part matched by the whol regex. Further groups, if present, indicate parts +part matched by the whole regex. Further groups, if present, indicate parts matched by subregexes. These are ordered from left to right, by the opening parenthesis of the subregex. */ @@ -32,54 +38,42 @@ public: std::string str; //< The part of the string that matched Group(): match(false) { } + operator bool() const { return match; } }; + typedef std::vector GroupArray; - /** - Constructs a RegMatch representig a non-match. Used by Regex. - */ +private: + GroupArray groups; + +public: + /** Constructs a RegMatch representing a non-match. */ RegMatch() { } - /** - Constructs a new RegMatch from a string and groups. The length and str members - of each group are computed and need not be set. Used by Regex. - */ + /** Constructs a new RegMatch from a string and groups. The length and str + members of each group are computed and need not be set. Intended to be used + by the Regex class. */ RegMatch(const std::string &, const std::vector &); - /** - Returns a reference to a single group in the match. An exception is thrown - if the requested group does not exist. - */ + /** Returns a reference to a single group in the match. */ const Group &group(unsigned) const; - /** - Returns true if the RegMatch object represents a non-match. - */ + /** Returns true if the RegMatch object represents a non-match. */ bool empty() const { return groups.empty(); } - /** - Returns the number of groups in this match. - */ + /** Returns the number of groups in this match. */ unsigned size() const { return groups.size(); } - /** - Returns the begin offset of the whole match. - */ - unsigned begin() const { return groups.empty()?0:groups[0].begin; } + /** Returns the begin offset of the whole match. */ + unsigned begin() const { return groups.empty() ? 0 : groups[0].begin; } - /** - Returns the end offset of the whole match. - */ - unsigned end() const { return groups.empty()?0:groups[0].end; } + /** Returns the end offset of the whole match. */ + unsigned end() const { return groups.empty() ? 0 : groups[0].end; } - /** - Shortcut for the group() function. - */ + /** Shorthand for the group() function. */ const Group &operator[](unsigned i) const { return group(i); } operator bool() const { return !empty(); } -private: - std::vector groups; }; } // namespace Msp