libzypp  13.10.6
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; }
228  { return Match(lhs) |= rhs; }
229 
231  inline Match operator-( const Match & lhs, const Match & rhs )
232  { return Match(lhs) -= 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  class Impl;
307 
308  public:
310  StrMatcher();
311 
313  StrMatcher( const std::string & search_r );
314 
316  StrMatcher( const std::string & search_r, const Match & flags_r );
317 
322  StrMatcher( const std::string & search_r, const Match::Mode & flags_r );
323 
325  StrMatcher( const std::string & search_r, int flags_r );
326 
328  explicit operator bool() const
329  { return !searchstring().empty(); }
330 
331  public:
337  template<class _Tp>
338  bool operator()( const _Tp & string_r ) const
339  { return doMatch( string_r.c_str() ); }
341  bool operator()( const char * string_r ) const
342  { return doMatch( string_r ); }
343 
344  public:
346  const std::string & searchstring() const;
347 
349  void setSearchstring( const std::string & string_r );
350 
352  void setSearchstring( const std::string & string_r, const Match & flags_r );
353 
355  const Match & flags() const;
356 
358  void setFlags( const Match & flags_r );
359 
360  public:
367  void compile() const;
368 
370  bool isCompiled() const;
371 
376  bool doMatch( const char * string_r ) const;
377 
378  private:
381  };
382 
384  std::ostream & operator<<( std::ostream & str, const StrMatcher & obj );
385 
387  bool operator==( const StrMatcher & lhs, const StrMatcher & rhs );
388 
390  inline bool operator!=( const StrMatcher & lhs, const StrMatcher & rhs )
391  { return !( lhs == rhs ); }
392 
394  bool operator<( const StrMatcher & lhs, const StrMatcher & rhs );
395 
396 } // namespace zypp
398 #endif // ZYPP_BASE_STRMATCHER_H
bool testAnyOf(const Match &rhs) const
Whether at least one of the rhs bits is set (or the same mode).
Definition: StrMatcher.h:105
bool operator()(const _Tp &string_r) const
Return whether string matches.
Definition: StrMatcher.h:338
void setFlags(const Match &flags_r)
Set new search flags.
Definition: StrMatcher.cc:303
bool isMode(Mode rhs) const
Whether this has mode rhs.
Definition: StrMatcher.h:161
Match()
Default ctor 0 or NOTHING.
Definition: StrMatcher.h:81
Match & operator|=(const Match &rhs)
Add flags.
Definition: StrMatcher.h:131
String matching option flags as used e.g.
Definition: StrMatcher.h:32
StrMatcher()
Default ctor matches nothing.
Definition: StrMatcher.cc:259
void setModeStringend()
Set the mode STRINGEND.
Definition: StrMatcher.h:192
Match nothing.
Definition: StrMatcher.h:42
static const Match SKIP_KIND
LookupAttr: skip any kind: prefix when looking at a Solvable name.
Definition: StrMatcher.h:74
Match operator-(const Match &lhs, const Match &rhs)
Definition: StrMatcher.h:231
String matching (STRING|SUBSTRING|GLOB|REGEX).
Definition: StrMatcher.h:297
bool operator==(const Match &lhs, const Match &rhs)
Definition: StrMatcher.h:217
static const Match CHECKSUMS
LookupAttr: also look for matches in checksums.
Definition: StrMatcher.h:76
Regular Expression.
Definition: StrMatcher.h:48
Match flags() const
Return the flags part.
Definition: StrMatcher.h:143
void setModeGlob()
Set the mode GLOB.
Definition: StrMatcher.h:198
int modeval() const
Return the modes integer representation.
Definition: StrMatcher.h:152
bool operator()(const char *string_r) const
Definition: StrMatcher.h:341
Exceptions thrown from attribute matching.
Definition: StrMatcher.h:247
bool isModeStringend() const
Whether this has mode STRINGEND.
Definition: StrMatcher.h:170
RWCOW_pointer< Impl > _pimpl
Pointer to implementation.
Definition: StrMatcher.h:380
void setModeSubstring()
Set the mode SUBSTRING.
Definition: StrMatcher.h:195
Match(int val_r)
Just in case one needs it.
Definition: StrMatcher.h:91
Match at string start.
Definition: StrMatcher.h:44
void unset(const Match &rhs)
Unset all of the rhs bits (unsets mode if the same as rhs).
Definition: StrMatcher.h:118
MatchException(const std::string &msg_r)
Supplied message.
Definition: StrMatcher.h:250
Unknown match mode.
Definition: StrMatcher.h:257
void turn(const Match &rhs, bool onoff)
Depending on the value of onoff, set or unset flags.
Definition: StrMatcher.h:127
void setSearchstring(const std::string &string_r)
Set a new searchstring.
Definition: StrMatcher.cc:291
bool operator!=(const StrMatcher &lhs, const StrMatcher &rhs)
Definition: StrMatcher.h:390
bool isModeGlob() const
Whether this has mode GLOB.
Definition: StrMatcher.h:176
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:120
void setModeStringstart()
Set the mode STRINGSTART.
Definition: StrMatcher.h:189
void setModeRegex()
Set the mode REGEX.
Definition: StrMatcher.h:201
void setMode(Mode rhs)
Set the mode part to rhs .
Definition: StrMatcher.h:183
static const Match NO_STORAGE_SOLVABLE
LookupAttr: internal.
Definition: StrMatcher.h:69
StrMatcher implementation.
Definition: StrMatcher.cc:167
Match operator|(const Match &lhs, const Match &rhs)
Definition: StrMatcher.h:224
bool isModeStringstart() const
Whether this has mode STRINGSTART.
Definition: StrMatcher.h:167
Match substring.
Definition: StrMatcher.h:46
friend std::ostream & operator<<(std::ostream &str, const StrMatcher &obj)
Definition: StrMatcher.cc:306
bool doMatch(const char *string_r) const
Return whether string matches.
Definition: StrMatcher.cc:285
std::string asString() const
String representation.
Definition: StrMatcher.cc:82
Match at string end.
Definition: StrMatcher.h:45
Mode
Mode flags (mutual exclusive).
Definition: StrMatcher.h:40
Invalid regular expression (failed ::regcomp).
Definition: StrMatcher.h:270
const Match & flags() const
The current search flags.
Definition: StrMatcher.cc:300
bool isCompiled() const
Whether the StrMatcher is already compiled.
Definition: StrMatcher.cc:282
bool operator==(const StrMatcher &lhs, const StrMatcher &rhs)
Definition: StrMatcher.cc:309
Match & operator-=(const Match &rhs)
Remove flags.
Definition: StrMatcher.h:135
const std::string & searchstring() const
The current searchstring.
Definition: StrMatcher.cc:288
MatchException Exception
Definition: StrMatcher.h:302
int flagval() const
Return the flags integer representation.
Definition: StrMatcher.h:154
bool isModeSubstring() const
Whether this has mode SUBSTRING.
Definition: StrMatcher.h:173
MatchUnknownModeException(const std::string &msg_r)
Supplied message.
Definition: StrMatcher.h:260
Match operator|(Match::Mode lhs, Match::Mode rhs)
Definition: StrMatcher.h:227
static const Match DISABLED_REPOS
LookupAttr: internal.
Definition: StrMatcher.h:72
void setModeString()
Set the mode STRING.
Definition: StrMatcher.h:186
static const Match NOCASE
If set, match case insensitive.
Definition: StrMatcher.h:59
bool operator!=(const Match &lhs, const Match &rhs)
Definition: StrMatcher.h:220
Base class for Exception.
Definition: Exception.h:143
bool isModeRegex() const
Whether this has mode REGEX.
Definition: StrMatcher.h:179
Match operator-(Match::Mode lhs, Match::Mode rhs)
Definition: StrMatcher.h:234
Excat matching.
Definition: StrMatcher.h:43
int get() const
Return the integer representation.
Definition: StrMatcher.h:150
static const Match ARRAYSENTINEL
LookupAttr: internal.
Definition: StrMatcher.h:71
Mode mode() const
Return the mode part.
Definition: StrMatcher.cc:51
static const int _flagmask
Definition: StrMatcher.h:36
static const Match COMPLETE_FILELIST
LookupAttr: internal.
Definition: StrMatcher.h:73
static const Match SUB
LookupAttr: internal.
Definition: StrMatcher.h:70
MatchInvalidRegexException(const std::string &msg_r)
Supplied message.
Definition: StrMatcher.h:273
Match(Mode val_r)
Ctor from Mode value.
Definition: StrMatcher.h:86
bool operator<(const StrMatcher &lhs, const StrMatcher &rhs)
Definition: StrMatcher.cc:315
static const Match FILES
LookupAttr: match full path when matching in filelists, otherwise just the basenames.
Definition: StrMatcher.h:75
void compile() const
Compile the pattern e.g.
Definition: StrMatcher.cc:279
static const int _modemask
Definition: StrMatcher.h:35
Something else.
Definition: StrMatcher.h:49
RW_pointer supporting &#39;copy on write&#39; functionality.
Definition: PtrTypes.h:438
void set(const Match &rhs)
Set all of the rhs bits (setting a new mode if rhs has one).
Definition: StrMatcher.h:109
bool isModeString() const
Whether this has mode STRING.
Definition: StrMatcher.h:164
bool test(const Match &rhs) const
Test whether all of the rhs bits are set (same mode if rhs has one).
Definition: StrMatcher.h:101