libzypp  17.23.5
Regex.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #include <cstdio>
13 #include <cstdarg>
14 
15 #include <iostream>
16 
17 #include <zypp/base/Regex.h>
18 
19 using namespace zypp;
20 using namespace zypp::str;
21 
23  : m_flags(match_extended)
24  , m_valid(false)
25 {
26 
27 }
28 
29 void regex::assign(const std::string& str,int flags)
30 {
31  m_valid = true;
32  m_str = str;
33  m_flags = flags;
34  int err;
35  char errbuff[100];
36  static const int normal = 1<<16; // deprecated legacy, use match_extended
37  if (!(flags & normal)) {
38  flags |= match_extended;
39  flags &= ~(normal);
40  }
41 
42  if ((err = regcomp(&m_preg, str.c_str(), flags))) {
43  m_valid = false;
44  regerror(err, &m_preg, errbuff, sizeof(errbuff));
45  ZYPP_THROW(regex_error(std::string(errbuff)));
46  }
47 }
48 
49 regex::regex(const std::string& str, int flags)
50 {
51  assign(str, flags);
52 }
53 
54 regex::~regex() throw()
55 {
56  if (m_valid)
57  regfree(&m_preg);
58 }
59 
60 bool zypp::str::regex_match(const char * s, smatch& matches, const regex& regex)
61 {
62  bool r = s && regex.m_valid && !regexec(&regex.m_preg, s, 12, &matches.pmatch[0], 0);
63  if (r)
64  matches.match_str = s;
65  return r;
66 }
67 
68 bool zypp::str::regex_match(const char * s, const regex& regex)
69 {
70  return s && !regexec(&regex.m_preg, s, 0, NULL, 0);
71 }
72 
74 {
75  memset(&pmatch, -1, sizeof(pmatch));
76 }
77 
78 std::string smatch::operator[](unsigned i) const
79 {
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 );
82 
83  return std::string();
84 }
85 
87 { return( i < sizeof(pmatch)/sizeof(*pmatch) && pmatch[i].rm_so != -1 ? pmatch[i].rm_so : std::string::npos ); }
88 
90 { return( i < sizeof(pmatch)/sizeof(*pmatch) && pmatch[i].rm_so != -1 ? pmatch[i].rm_eo : std::string::npos ); }
91 
93 { return( i < sizeof(pmatch)/sizeof(*pmatch) && pmatch[i].rm_so != -1 ? pmatch[i].rm_eo-pmatch[i].rm_so : std::string::npos ); }
94 
95 unsigned smatch::size() const
96 {
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 "()?"
100  // may be embeded.
101  for ( unsigned i = 0; i < sizeof(pmatch)/sizeof(*pmatch); ++i )
102  {
103  if ( pmatch[i].rm_so != -1 )
104  matches = i;
105  }
106  return ++matches;
107 }
zypp::str::regex::m_valid
bool m_valid
Definition: Regex.h:127
zypp::str::smatch::smatch
smatch()
Definition: Regex.cc:73
zypp::str::regex::m_str
std::string m_str
Definition: Regex.h:124
zypp::str::regex::assign
void assign(const std::string &s, int flags=match_extended)
Definition: Regex.cc:29
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition: Exception.h:392
zypp::str::smatch::operator[]
std::string operator[](unsigned i) const
Definition: Regex.cc:78
zypp::str::smatch::begin
std::string::size_type begin(unsigned i) const
Begin index of subexpression i in match_str (or std::string::npos)
Definition: Regex.cc:86
zypp::str::regex::regex
regex()
Definition: Regex.cc:22
zypp::str::regex::m_flags
int m_flags
Definition: Regex.h:125
zypp::str::regex_error
Exception regex_error
Definition: Regex.h:54
zypp::str::regex::~regex
~regex()
Definition: Regex.cc:54
zypp::str::smatch::end
std::string::size_type end(unsigned i) const
End index of subexpression i in match_str (or std::string::npos)
Definition: Regex.cc:89
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
zypp::str
String related utilities and Regular expression matching.
Definition: PtrTypes.h:28
Regex.h
zypp::str::smatch
Regular expression match result.
Definition: Regex.h:145
zypp::str::regex::m_preg
regex_t m_preg
Definition: Regex.h:126
zypp::str::smatch::pmatch
regmatch_t pmatch[12]
Definition: Regex.h:164
zypp::str::regex::match_extended
Use POSIX Extended Regular Expression syntax when interpreting regex.
Definition: Regex.h:93
zypp::sat::detail::size_type
SolvableIdType size_type
Definition: PoolMember.h:126
zypp::str::regex_match
bool regex_match(const std::string &s, smatch &matches, const regex &regex)
\relates regex \ingroup ZYPP_STR_REGEX \relates regex \ingroup ZYPP_STR_REGEX
Definition: Regex.h:70
str
String related utilities and Regular expression matching.
zypp::str::smatch::size
unsigned size() const
Definition: Regex.cc:95
zypp::str::smatch::match_str
std::string match_str
Definition: Regex.h:163
zypp::str::regex
Regular expression.
Definition: Regex.h:86