libzypp  17.14.0
StrMatcher.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_BASE_STRMATCHER_H
13 #define ZYPP_BASE_STRMATCHER_H
14 
15 #include <iosfwd>
16 #include <string>
17 
18 #include "zypp/base/PtrTypes.h"
19 #include "zypp/base/Exception.h"
20 
22 namespace zypp
23 {
32  class Match
33  {
34  private:
35  static const int _modemask;
36  static const int _flagmask;
37 
38  public:
40  enum Mode
41  {
47  GLOB,
50  };
51 
59  static const Match NOCASE;
60 
61 
69  static const Match NO_STORAGE_SOLVABLE;
70  static const Match SUB;
71  static const Match ARRAYSENTINEL;
72  static const Match DISABLED_REPOS;
73  static const Match COMPLETE_FILELIST;
74  static const Match SKIP_KIND;
75  static const Match FILES;
76  static const Match CHECKSUMS;
77 
78 
79  public:
82  : _val( 0 )
83  {}
84 
86  Match( Mode val_r )
87  : _val( modeval( val_r ) )
88  {}
89 
91  explicit Match( int val_r )
92  : _val( val_r )
93  {}
94 
96  explicit operator bool() const
97  { return _val; }
98 
99  public:
101  bool test( const Match & rhs ) const
102  { return ( ( flagval() & rhs.flagval() ) == rhs.flagval() ) && ( !rhs.modeval() || rhs.modeval() == modeval() ); }
103 
105  bool testAnyOf( const Match & rhs ) const
106  { return ( flagval() & rhs.flagval() ) || ( rhs.modeval() && rhs.modeval() == modeval() ); }
107 
109  void set( const Match & rhs )
110  {
111  if ( rhs.modeval() )
112  _val = rhs._val | flagval(); // also set the rhs mode
113  else
114  _val |= rhs._val; // just set the flags
115  }
116 
118  void unset( const Match & rhs )
119  {
120  if ( modeval() == rhs.modeval() )
121  _val = flagval() & ~rhs.flagval(); // also unset mode
122  else
123  _val &= ~rhs.flagval(); // just unset falgs
124  }
125 
127  void turn( const Match & rhs, bool onoff )
128  { onoff ? set( rhs ) : unset( rhs ); }
129 
131  Match & operator|=( const Match & rhs )
132  { set( rhs ); return *this; }
133 
135  Match & operator-=( const Match & rhs )
136  { unset( rhs ); return *this; }
137 
138  public:
140  Mode mode() const;
141 
143  Match flags() const
144  { return Match( flagval() ); }
145 
146  public:
150  int get() const { return _val; }
152  int modeval() const { return _val & _modemask; }
154  int flagval() const { return _val & _flagmask; }
156 
157  public:
161  bool isMode( Mode rhs ) const
162  { return modeval() == modeval( rhs ); }
164  bool isModeString() const
165  { return isMode( STRING ); }
167  bool isModeStringstart() const
168  { return isMode( STRINGSTART ); }
170  bool isModeStringend() const
171  { return isMode( STRINGEND ); }
173  bool isModeSubstring() const
174  { return isMode( SUBSTRING ); }
176  bool isModeGlob() const
177  { return isMode( GLOB ); }
179  bool isModeRegex() const
180  { return isMode( REGEX ); }
181 
183  void setMode( Mode rhs )
184  { _val = modeval( rhs ) | flagval(); }
187  { setMode( STRING ); }
190  { setMode( STRINGSTART ); }
193  { setMode( STRINGEND ); }
196  { setMode( SUBSTRING ); }
198  void setModeGlob()
199  { setMode( GLOB ); }
202  { setMode( REGEX ); }
204 
206  std::string asString() const;
207 
208  private:
210  static int modeval( Mode mode_r );
211 
212  private:
213  int _val;
214  };
215 
217  inline bool operator==( const Match & lhs, const Match & rhs )
218  { return lhs.get() == rhs.get(); }
220  inline bool operator!=( const Match & lhs, const Match & rhs )
221  { return lhs.get() != rhs.get(); }
222 
224  inline Match operator|( const Match & lhs, const Match & rhs )
225  { return Match(lhs) |= rhs; }
227  inline Match operator|( Match::Mode lhs, Match::Mode rhs )
228  { return Match(lhs) |= rhs; }
229 
231  inline Match operator-( const Match & lhs, const Match & rhs )
232  { return Match(lhs) -= rhs; }
234  inline Match operator-( Match::Mode lhs, Match::Mode rhs )
235  { return Match(lhs) -= rhs; }
236 
238  std::ostream & operator<<( std::ostream & str, Match::Mode obj );
239 
241  std::ostream & operator<<( std::ostream & str, const Match & obj );
242 
247  struct MatchException : public Exception
248  {
250  explicit MatchException( const std::string & msg_r ) : Exception( msg_r ) {}
251  };
252 
258  {
260  explicit MatchUnknownModeException( const std::string & msg_r ) : MatchException( msg_r ) {}
261 
263  MatchUnknownModeException( const Match & mode_r, const std::string & msg_r = std::string() );
264  };
265 
271  {
273  explicit MatchInvalidRegexException( const std::string & msg_r ) : MatchException( msg_r ) {}
274 
276  MatchInvalidRegexException( const std::string & regex_r, int regcomp_r );
277  };
278 
298  {
299  friend std::ostream & operator<<( std::ostream & str, const StrMatcher & obj );
300 
301  public:
303 
304  public:
306  struct Impl;
307 
308  public:
310  StrMatcher();
311 
313  StrMatcher( const std::string & search_r );
315  StrMatcher( std::string && search_r );
316 
318  StrMatcher( const std::string & search_r, const Match & flags_r );
320  StrMatcher( std::string && search_r, const Match & flags_r );
321 
326  StrMatcher( const std::string & search_r, const Match::Mode & flags_r );
328  StrMatcher( std::string && search_r, const Match::Mode & flags_r );
329 
331  StrMatcher( const std::string & search_r, int flags_r );
333  StrMatcher( std::string && search_r, int flags_r );
334 
336  explicit operator bool() const
337  { return !searchstring().empty(); }
338 
339  public:
345  template<class Tp>
346  bool operator()( const Tp & string_r ) const
347  { return doMatch( string_r.c_str() ); }
349  bool operator()( const char * string_r ) const
350  { return doMatch( string_r ); }
351 
352  public:
354  const std::string & searchstring() const;
355 
357  void setSearchstring( const std::string & string_r );
359  void setSearchstring( std::string && string_r );
360 
362  void setSearchstring( const std::string & string_r, const Match & flags_r );
364  void setSearchstring( std::string && string_r, const Match & flags_r );
365 
367  const Match & flags() const;
368 
370  void setFlags( const Match & flags_r );
371 
372  public:
379  void compile() const;
380 
382  bool isCompiled() const;
383 
388  bool doMatch( const char * string_r ) const;
389 
390  private:
393  };
394 
396  std::ostream & operator<<( std::ostream & str, const StrMatcher & obj );
397 
399  bool operator==( const StrMatcher & lhs, const StrMatcher & rhs );
400 
402  inline bool operator!=( const StrMatcher & lhs, const StrMatcher & rhs )
403  { return !( lhs == rhs ); }
404 
406  bool operator<( const StrMatcher & lhs, const StrMatcher & rhs );
407 
408 } // namespace zypp
410 #endif // ZYPP_BASE_STRMATCHER_H