libzypp  11.13.5
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  optimize = 0,
93  icase = REG_ICASE,
94  nosubs = REG_NOSUB,
95  match_extended = REG_EXTENDED,
96  normal = 1<<16
97  };
98 
99  regex();
100  regex(const std::string& s,int flags = match_extended);
101  ~regex() throw();
102 
103  regex(const regex & rhs)
104  { assign(rhs.m_str, rhs.m_flags); }
105 
106  regex & operator=(const regex & rhs)
107  { assign(rhs.m_str, rhs.m_flags); return *this; }
108 
112  std::string asString() const
113  { return m_str; }
114 
115  public:
117  regex_t * get()
118  { return & m_preg; }
119 
120  private:
121  void assign(const std::string& s,int flags = match_extended);
122 
123  private:
124  friend class smatch;
125  friend bool regex_match(const char * s, str::smatch& matches, const regex& regex);
126  friend bool regex_match(const char * s, const regex& regex);
127  std::string m_str;
128  int m_flags;
129  regex_t m_preg;
130  bool m_valid;
131  };
132 
134  inline std::ostream & operator<<( std::ostream & str, const regex & obj )
135  { return str << obj.asString(); }
136 
148  class smatch
149  {
150  public:
151  smatch();
152 
153  std::string operator[](unsigned i) const;
154 
155  unsigned size() const;
156 
157  std::string match_str;
158  regmatch_t pmatch[12];
159  };
160 
161  } // namespace str
163 } // namespace zypp
165 #endif // ZYPP_BASE_STRING_H