1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/base/Regex.cc
17 #include "zypp/base/Regex.h"
20 using namespace zypp::str;
23 : m_flags(match_extended)
29 void regex::assign(const std::string& str,int flags)
36 static const int normal = 1<<16; // deprecated legacy, use match_extended
37 if (!(flags & normal)) {
38 flags |= match_extended;
42 if ((err = regcomp(&m_preg, str.c_str(), flags))) {
44 regerror(err, &m_preg, errbuff, sizeof(errbuff));
45 ZYPP_THROW(regex_error(std::string(errbuff)));
49 regex::regex(const std::string& str, int flags)
54 regex::~regex() throw()
60 bool zypp::str::regex_match(const char * s, smatch& matches, const regex& regex)
62 bool r = s && regex.m_valid && !regexec(®ex.m_preg, s, 12, &matches.pmatch[0], 0);
64 matches.match_str = s;
68 bool zypp::str::regex_match(const char * s, const regex& regex)
70 return s && !regexec(®ex.m_preg, s, 0, NULL, 0);
75 memset(&pmatch, -1, sizeof(pmatch));
78 std::string smatch::operator[](unsigned i) const
80 if ( i < sizeof(pmatch)/sizeof(*pmatch) && pmatch[i].rm_so != -1 )
81 return match_str.substr( pmatch[i].rm_so, pmatch[i].rm_eo-pmatch[i].rm_so );
86 std::string::size_type smatch::begin( unsigned i ) const
87 { return( i < sizeof(pmatch)/sizeof(*pmatch) && pmatch[i].rm_so != -1 ? pmatch[i].rm_so : std::string::npos ); }
89 std::string::size_type smatch::end( unsigned i ) const
90 { return( i < sizeof(pmatch)/sizeof(*pmatch) && pmatch[i].rm_so != -1 ? pmatch[i].rm_eo : std::string::npos ); }
92 std::string::size_type smatch::size( unsigned i ) const
93 { return( i < sizeof(pmatch)/sizeof(*pmatch) && pmatch[i].rm_so != -1 ? pmatch[i].rm_eo-pmatch[i].rm_so : std::string::npos ); }
95 unsigned smatch::size() const
97 unsigned matches = unsigned(-1);
98 // Get highest (pmatch[i].rm_so != -1). Just looking for the 1st
99 // (pmatch[i].rm_so == -1) is wrong as optional mayches "()?"
101 for ( unsigned i = 0; i < sizeof(pmatch)/sizeof(*pmatch); ++i )
103 if ( pmatch[i].rm_so != -1 )