libzypp  17.16.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; }
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  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
void setFlags(const Match &flags_r)
Set new search flags.
Definition: StrMatcher.cc:323
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:260
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
bool isCompiled() const
Whether the StrMatcher is already compiled.
Definition: StrMatcher.cc:295
bool isMode(Mode rhs) const
Whether this has mode rhs.
Definition: StrMatcher.h:161
String related utilities and Regular expression matching.
std::string asString() const
String representation.
Definition: StrMatcher.cc:83
bool doMatch(const char *string_r) const
Return whether string matches.
Definition: StrMatcher.cc:298
bool isModeStringend() const
Whether this has mode STRINGEND.
Definition: StrMatcher.h:170
void setModeGlob()
Set the mode GLOB.
Definition: StrMatcher.h:198
Exceptions thrown from attribute matching.
Definition: StrMatcher.h:247
Match flags() const
Return the flags part.
Definition: StrMatcher.h:143
RWCOW_pointer< Impl > _pimpl
Pointer to implementation.
Definition: StrMatcher.h:392
bool operator==(const SetRelation::Enum &lhs, const SetCompare &rhs)
Mode mode() const
Return the mode part.
Definition: StrMatcher.cc:52
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
void turn(const Match &rhs, bool onoff)
Depending on the value of onoff, set or unset flags.
Definition: StrMatcher.h:127
void compile() const
Compile the pattern e.g.
Definition: StrMatcher.cc:292
void setSearchstring(const std::string &string_r)
Set a new searchstring.
Definition: StrMatcher.cc:304
bool operator()(const Tp &string_r) const
Return whether string matches.
Definition: StrMatcher.h:346
bool operator!=(const StrMatcher &lhs, const StrMatcher &rhs)
Definition: StrMatcher.h:402
std::ostream & operator<<(std::ostream &str, const Exception &obj)
Definition: Exception.cc:147
void setModeStringstart()
Set the mode STRINGSTART.
Definition: StrMatcher.h:189
void setModeRegex()
Set the mode REGEX.
Definition: StrMatcher.h:201
bool testAnyOf(const Match &rhs) const
Whether at least one of the rhs bits is set (or the same mode).
Definition: StrMatcher.h:105
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:168
Match operator|(const Match &lhs, const Match &rhs)
Definition: StrMatcher.h:224
Match substring.
Definition: StrMatcher.h:46
friend std::ostream & operator<<(std::ostream &str, const StrMatcher &obj)
Definition: StrMatcher.cc:326
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
int modeval() const
Return the modes integer representation.
Definition: StrMatcher.h:152
Match & operator-=(const Match &rhs)
Remove flags.
Definition: StrMatcher.h:135
MatchException Exception
Definition: StrMatcher.h:302
bool isModeStringstart() const
Whether this has mode STRINGSTART.
Definition: StrMatcher.h:167
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:145
Match operator-(Match::Mode lhs, Match::Mode rhs)
Definition: StrMatcher.h:234
int get() const
Return the integer representation.
Definition: StrMatcher.h:150
bool isModeGlob() const
Whether this has mode GLOB.
Definition: StrMatcher.h:176
Excat matching.
Definition: StrMatcher.h:43
bool operator()(const char *string_r) const
Definition: StrMatcher.h:349
static const Match ARRAYSENTINEL
LookupAttr: internal.
Definition: StrMatcher.h:71
int flagval() const
Return the flags integer representation.
Definition: StrMatcher.h:154
const Match & flags() const
The current search flags.
Definition: StrMatcher.cc:320
static const int _flagmask
Definition: StrMatcher.h:36
bool isModeRegex() const
Whether this has mode REGEX.
Definition: StrMatcher.h:179
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
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
Match(Mode val_r)
Ctor from Mode value.
Definition: StrMatcher.h:86
bool operator<(const StrMatcher &lhs, const StrMatcher &rhs)
Definition: StrMatcher.cc:335
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:1
static const Match FILES
LookupAttr: match full path when matching in filelists, otherwise just the basenames.
Definition: StrMatcher.h:75
bool isModeString() const
Whether this has mode STRING.
Definition: StrMatcher.h:164
static const int _modemask
Definition: StrMatcher.h:35
const std::string & searchstring() const
The current searchstring.
Definition: StrMatcher.cc:301
Something else.
Definition: StrMatcher.h:49
void set(const Match &rhs)
Set all of the rhs bits (setting a new mode if rhs has one).
Definition: StrMatcher.h:109
bool isModeSubstring() const
Whether this has mode SUBSTRING.
Definition: StrMatcher.h:173