libzypp  15.28.6
Regex.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_BASE_REGEX_H
13 #define ZYPP_BASE_REGEX_H
14 
15 #include <iosfwd>
16 #include <string>
17 #include <regex.h>
18 
19 #include "zypp/base/Exception.h"
20 
22 namespace zypp
23 {
27  namespace str
28  {
53 
55 
56  class smatch;
57  class regex;
58 
67  bool regex_match( const char * s, smatch & matches, const regex & regex );
68 
70  inline bool regex_match(const std::string & s, smatch & matches, const regex & regex)
71  { return regex_match( s.c_str(), matches, regex ); }
72 
74  bool regex_match( const char * s, const regex & regex );
75 
77  inline bool regex_match( const std::string & s, const regex & regex )
78  { return regex_match( s.c_str(), regex ); }
79 
86  class regex
87  {
88  public:
89 
90  enum RegFlags {
91  icase = REG_ICASE,
92  nosubs = REG_NOSUB,
93  match_extended = REG_EXTENDED,
94  };
95 
96  regex();
97  regex(const std::string& s,int flags = match_extended);
98  ~regex() throw();
99 
100  regex(const regex & rhs)
101  { assign(rhs.m_str, rhs.m_flags); }
102 
103  regex & operator=(const regex & rhs)
104  { assign(rhs.m_str, rhs.m_flags); return *this; }
105 
109  std::string asString() const
110  { return m_str; }
111 
112  public:
114  regex_t * get()
115  { return & m_preg; }
116 
117  private:
118  void assign(const std::string& s,int flags = match_extended);
119 
120  private:
121  friend class smatch;
122  friend bool regex_match(const char * s, str::smatch& matches, const regex& regex);
123  friend bool regex_match(const char * s, const regex& regex);
124  std::string m_str;
125  int m_flags;
126  regex_t m_preg;
127  bool m_valid;
128  };
129 
131  inline std::ostream & operator<<( std::ostream & str, const regex & obj )
132  { return str << obj.asString(); }
133 
145  class smatch
146  {
147  public:
148  smatch();
149 
150  std::string operator[](unsigned i) const;
151 
152  unsigned size() const;
153 
155  std::string::size_type begin( unsigned i ) const;
156 
158  std::string::size_type end( unsigned i ) const;
159 
161  std::string::size_type size( unsigned i ) const;
162 
163  std::string match_str;
164  regmatch_t pmatch[12];
165  };
166 
167  } // namespace str
169 } // namespace zypp
171 #endif // ZYPP_BASE_STRING_H
Regular expression.
Definition: Regex.h:86
regex_t m_preg
Definition: Regex.h:126
std::string::size_type begin(unsigned i) const
Begin index of subexpression i in match_str (or std::string::npos)
Definition: Regex.cc:86
std::ostream & operator<<(std::ostream &str, const regex &obj)
Definition: Regex.h:131
std::string operator[](unsigned i) const
Definition: Regex.cc:78
std::string asString() const
string representation of the regular expression
Definition: Regex.h:109
Support for substring addressing of matches is not required.
Definition: Regex.h:92
Exception regex_error
Definition: Regex.h:54
unsigned size() const
Definition: Regex.cc:95
Do not differentiate case.
Definition: Regex.h:91
bool m_valid
Definition: Regex.h:127
regex & operator=(const regex &rhs)
Definition: Regex.h:103
std::string m_str
Definition: Regex.h:124
SolvableIdType size_type
Definition: PoolMember.h:152
friend bool regex_match(const char *s, str::smatch &matches, const regex &regex)
Regular expression match result.
Definition: Regex.h:145
std::string match_str
Definition: Regex.h:163
Base class for Exception.
Definition: Exception.h:143
Use POSIX Extended Regular Expression syntax when interpreting regex.
Definition: Regex.h:93
void assign(const std::string &s, int flags=match_extended)
Definition: Regex.cc:29
bool regex_match(const std::string &s, smatch &matches, const regex &regex)
regex ZYPP_STR_REGEX regex ZYPP_STR_REGEX
Definition: Regex.h:70
std::string::size_type end(unsigned i) const
End index of subexpression i in match_str (or std::string::npos)
Definition: Regex.cc:89
regmatch_t pmatch[12]
Definition: Regex.h:164