|
- // Implementation of the RegularExpression class
- #include <regular_expression.h>
-
- #include <iostream>
- #include <algorithm>
-
- #include <cstdlib>
- #include <cstring>
-
- using std::cerr;
- using std::endl;
- using std::flush;
- using std::string;
-
- DEFINE_EZEXCEPT(Regular_Expression_Exception)
-
- // support classes
- pcintregmatch_t::pcintregmatch_t() {
- this->::regmatch_t::rm_so = -1;
- this->::regmatch_t::rm_eo = -1;
- }
-
- pcintregmatch_t::pcintregmatch_t( const ::regmatch_t& rm ) {
- this->::regmatch_t::rm_so = rm.rm_so;
- this->::regmatch_t::rm_eo = rm.rm_eo;
- }
-
- pcintregmatch_t::operator bool() const {
- return (rm_so!=-1 && rm_eo!=-1);
- }
-
- matchresult::matchresult()
- {}
-
- matchresult::matchresult( size_t nSubExpr, std::string const& os ):
- __matches( nSubExpr ), __org_string( os )
- {}
-
- matchresult::matchresult( const matchresult::matchvec_t& mv, const std::string& os ):
- __matches( mv ), __org_string( os )
- {}
-
- matchresult::operator bool() const {
- return (__matches.size()!=0);
- }
-
- matchresult::matchvec_t::value_type matchresult::operator[]( unsigned int m ) const {
- if( m>=__matches.size() ) {
- THROW_EZEXCEPT(Regular_Expression_Exception,
- "requesting group " << m << ", but only " << __matches.size() << " groups matched");
- }
- return __matches[m];
- }
-
- std::string matchresult::operator[]( const matchvec_t::value_type& m ) const {
- if( !m ) {
- THROW_EZEXCEPT(Regular_Expression_Exception,
- "requesting string from invalid group");
- }
- return __org_string.substr(m.rm_so, (m.rm_eo-m.rm_so));
- }
-
- std::string matchresult::group( unsigned int m ) const {
- return (*this)[(*this)[m]];
- }
-
- bool operator==( const char* tocheck, const Regular_Expression& against ) {
- return against.matches( tocheck );
- }
-
- bool operator!=( const char* tocheck, const Regular_Expression& against ) {
- return !(against.matches( tocheck ));
- }
-
- bool operator==( const string& s, const Regular_Expression& against ) {
- return against.matches( s.c_str() );
- }
-
- bool operator!=( const string& s, const Regular_Expression& against ) {
- return !(against.matches(s.c_str()));
- }
-
-
- Regular_Expression::Regular_Expression( const string& pattern_text, int flags ) :
- myOriginalPattern( pattern_text )
- {
- // Compile the pattern....
- int r;
- char errbuf[ 512 ];
-
- if( (r=::regcomp(&myCompiledExpression,
- myOriginalPattern.c_str(),
- flags))!=0 ) {
- ::regerror(r, &myCompiledExpression, errbuf, sizeof(errbuf));
- THROW_EZEXCEPT(Regular_Expression_Exception,
- "Failed to compile RegEx(" << myOriginalPattern << "): " << errbuf);
- }
- }
-
- matchresult Regular_Expression::matches( const string& s ) const {
- // After compiling the regexp, the re_nsub member informs how many
- // sub expressions/match groups there were. Together with the zeroth
- // group (the whole) match, we know how many there are in total
- const size_t nSubExpr( 1 + myCompiledExpression.re_nsub );
- matchresult mr( nSubExpr, s );
-
- if( ::regexec(&myCompiledExpression, s.c_str(), nSubExpr, &mr.__matches[0], 0)==0 )
- return mr;
- return matchresult();
- }
-
- string Regular_Expression::pattern( void ) const {
- return myOriginalPattern;
- }
-
- Regular_Expression::~Regular_Expression() {
- ::regfree( &myCompiledExpression );
- }
|