libzypp  17.14.0
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
unsigned size() const
Definition: Regex.cc:95
Regular expression.
Definition: Regex.h:86
regex_t m_preg
Definition: Regex.h:126
String related utilities and Regular expression matching.
std::ostream & operator<<(std::ostream &str, const regex &obj)
Definition: Regex.h:131
std::string::size_type end(unsigned i) const
End index of subexpression i in match_str (or std::string::npos)
Definition: Regex.cc:89
std::string::size_type begin(unsigned i) const
Begin index of subexpression i in match_str (or std::string::npos)
Definition: Regex.cc:86
Support for substring addressing of matches is not required.
Definition: Regex.h:92
Exception regex_error
Definition: Regex.h:54
Do not differentiate case.
Definition: Regex.h:91
bool m_valid
Definition: Regex.h:127
regex_t * get()
Expert backdoor.
Definition: Regex.h:114
regex & operator=(const regex &rhs)
Definition: Regex.h:103
std::string m_str
Definition: Regex.h:124
SolvableIdType size_type
Definition: PoolMember.h:126
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:145
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
std::string operator[](unsigned i) const
Definition: Regex.cc:78
std::string asString() const
string representation of the regular expression
Definition: Regex.h:109
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
regmatch_t pmatch[12]
Definition: Regex.h:164
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1