libzypp  17.25.1
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 regex::matches( const char *s, smatch &matches, int flags ) const
61 {
62  const auto possibleMatchCount = m_preg.re_nsub + 1;
63  matches.pmatch.resize( possibleMatchCount );
64  memset( matches.pmatch.data(), -1, sizeof( regmatch_t ) * ( possibleMatchCount ) );
65 
66  bool r = s && m_valid && !regexec( &m_preg, s, matches.pmatch.size(), matches.pmatch.data(), flags );
67  if (r)
68  matches.match_str = s;
69  return r;
70 }
71 
72 bool regex::matches( const char *s ) const
73 {
74  return s && !regexec(&m_preg, s, 0, NULL, 0);
75 }
76 
77 bool zypp::str::regex_match(const char * s, smatch& matches, const regex& regex)
78 {
79  return regex.matches( s, matches );
80 }
81 
82 bool zypp::str::regex_match(const char * s, const regex& regex)
83 {
84  return regex.matches( s );
85 }
86 
88 { }
89 
90 std::string smatch::operator[](unsigned i) const
91 {
92  if ( i < pmatch.size() && pmatch[i].rm_so != -1 )
93  return match_str.substr( pmatch[i].rm_so, pmatch[i].rm_eo-pmatch[i].rm_so );
94 
95  return std::string();
96 }
97 
99 { return( i < pmatch.size() && pmatch[i].rm_so != -1 ? pmatch[i].rm_so : std::string::npos ); }
100 
102 { return( i < pmatch.size() && pmatch[i].rm_so != -1 ? pmatch[i].rm_eo : std::string::npos ); }
103 
105 { return( i < pmatch.size() && pmatch[i].rm_so != -1 ? pmatch[i].rm_eo-pmatch[i].rm_so : std::string::npos ); }
106 
107 unsigned smatch::size() const
108 {
109  unsigned matches = unsigned(-1);
110  // Get highest (pmatch[i].rm_so != -1). Just looking for the 1st
111  // (pmatch[i].rm_so == -1) is wrong as optional mayches "()?"
112  // may be embeded.
113  for ( unsigned i = 0; i < pmatch.size(); ++i )
114  {
115  if ( pmatch[i].rm_so != -1 )
116  matches = i;
117  }
118  return ++matches;
119 }
120 
121 std::string zypp::str::regex_substitute( const std::string &s, const regex &regex, const std::string &replacement, bool global )
122 {
123  std::string result;
124  std::string::size_type off = 0;
125  int flags = regex::none;
126 
127  while ( true ) {
128 
129  smatch match;
130  if ( !regex.matches( s.data()+off, match, flags ) ) {
131  break;
132  }
133 
134  if ( match.size() ) {
135  result += s.substr( off, match.begin(0) );
136  result += replacement;
137  off = match.end(0) + off;
138  }
139 
140  if ( !global )
141  break;
142 
143  // once we passed the beginning of the string we should not match ^ anymore, except the last character was
144  // actually a newline
145  if ( off > 0 && off < s.size() && s[ off - 1 ] == '\n' )
146  flags = regex::none;
147  else
148  flags = regex::not_bol;
149  }
150 
151  result += s.substr( off );
152  return result;
153 }
zypp::str::regex::m_valid
bool m_valid
Definition: Regex.h:142
zypp::str::smatch::smatch
smatch()
Definition: Regex.cc:87
zypp::str::regex::matches
bool matches(const char *s, str::smatch &matches, int flags=none) const
Definition: Regex.cc:60
zypp::str::regex_substitute
std::string regex_substitute(const std::string &s, const regex &regex, const std::string &replacement, bool global=true)
Replaces the matched regex with the string passed in replacement.
Definition: Regex.cc:121
zypp::str::regex::m_str
std::string m_str
Definition: Regex.h:139
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:90
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:98
zypp::str::regex::regex
regex()
Definition: Regex.cc:22
zypp::str::regex::m_flags
int m_flags
Definition: Regex.h:140
zypp::str::regex::not_bol
Do not match begin of line.
Definition: Regex.h:107
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:101
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:160
zypp::str::regex::m_preg
regex_t m_preg
Definition: Regex.h:141
zypp::str::smatch::pmatch
std::vector< regmatch_t > pmatch
Definition: Regex.h:179
zypp::str::regex::match_extended
Use POSIX Extended Regular Expression syntax when interpreting regex.
Definition: Regex.h:101
zypp::sat::detail::size_type
SolvableIdType size_type
Definition: PoolMember.h:126
zypp::str::regex::none
Definition: Regex.h:106
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:107
zypp::str::smatch::match_str
std::string match_str
Definition: Regex.h:178
zypp::str::regex
Regular expression.
Definition: Regex.h:94