libzypp 17.31.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
22namespace 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 {
49 OTHER
50 };
51
59 static const Match NOCASE;
61
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;
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 ); }
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
Base class for Exception.
Definition: Exception.h:146
String matching option flags as used e.g.
Definition: StrMatcher.h:33
static const Match DISABLED_REPOS
LookupAttr: internal.
Definition: StrMatcher.h:72
int flagval() const
Return the flags integer representation.
Definition: StrMatcher.h:154
void setModeGlob()
Set the mode GLOB.
Definition: StrMatcher.h:198
Match()
Default ctor 0 or NOTHING.
Definition: StrMatcher.h:81
int get() const
Return the integer representation.
Definition: StrMatcher.h:150
std::string asString() const
String representation.
Definition: StrMatcher.cc:83
void setModeStringend()
Set the mode STRINGEND.
Definition: StrMatcher.h:192
void setModeStringstart()
Set the mode STRINGSTART.
Definition: StrMatcher.h:189
static const Match ARRAYSENTINEL
LookupAttr: internal.
Definition: StrMatcher.h:71
bool isModeSubstring() const
Whether this has mode SUBSTRING.
Definition: StrMatcher.h:173
Match operator-(const Match &lhs, const Match &rhs)
Definition: StrMatcher.h:231
bool isModeStringstart() const
Whether this has mode STRINGSTART.
Definition: StrMatcher.h:167
void setMode(Mode rhs)
Set the mode part to rhs .
Definition: StrMatcher.h:183
static const Match SUB
LookupAttr: internal.
Definition: StrMatcher.h:70
Mode
Mode flags (mutual exclusive).
Definition: StrMatcher.h:41
@ STRINGEND
Match at string end.
Definition: StrMatcher.h:45
@ NOTHING
Match nothing.
Definition: StrMatcher.h:42
@ OTHER
Something else.
Definition: StrMatcher.h:49
@ REGEX
Regular Expression.
Definition: StrMatcher.h:48
@ STRINGSTART
Match at string start.
Definition: StrMatcher.h:44
@ GLOB
Glob.
Definition: StrMatcher.h:47
@ SUBSTRING
Match substring.
Definition: StrMatcher.h:46
@ STRING
Excat matching.
Definition: StrMatcher.h:43
static const Match COMPLETE_FILELIST
LookupAttr: internal.
Definition: StrMatcher.h:73
static const Match CHECKSUMS
LookupAttr: also look for matches in checksums.
Definition: StrMatcher.h:76
Match operator|(const Match &lhs, const Match &rhs)
Definition: StrMatcher.h:224
bool isModeString() const
Whether this has mode STRING.
Definition: StrMatcher.h:164
static const int _flagmask
Definition: StrMatcher.h:36
Match(Mode val_r)
Ctor from Mode value.
Definition: StrMatcher.h:86
static const Match NO_STORAGE_SOLVABLE
LookupAttr: internal.
Definition: StrMatcher.h:69
void unset(const Match &rhs)
Unset all of the rhs bits (unsets mode if the same as rhs).
Definition: StrMatcher.h:118
static const Match FILES
LookupAttr: match full path when matching in filelists, otherwise just the basenames.
Definition: StrMatcher.h:75
void setModeRegex()
Set the mode REGEX.
Definition: StrMatcher.h:201
static const Match SKIP_KIND
LookupAttr: skip any kind: prefix when looking at a Solvable name.
Definition: StrMatcher.h:74
static const Match NOCASE
If set, match case insensitive.
Definition: StrMatcher.h:59
bool isModeGlob() const
Whether this has mode GLOB.
Definition: StrMatcher.h:176
Match(int val_r)
Just in case one needs it.
Definition: StrMatcher.h:91
bool isModeStringend() const
Whether this has mode STRINGEND.
Definition: StrMatcher.h:170
void setModeString()
Set the mode STRING.
Definition: StrMatcher.h:186
void turn(const Match &rhs, bool onoff)
Depending on the value of onoff, set or unset flags.
Definition: StrMatcher.h:127
Match flags() const
Return the flags part.
Definition: StrMatcher.h:143
int modeval() const
Return the modes integer representation.
Definition: StrMatcher.h:152
bool isModeRegex() const
Whether this has mode REGEX.
Definition: StrMatcher.h:179
static const int _modemask
Definition: StrMatcher.h:35
Mode mode() const
Return the mode part.
Definition: StrMatcher.cc:52
bool operator!=(const Match &lhs, const Match &rhs)
Definition: StrMatcher.h:220
void set(const Match &rhs)
Set all of the rhs bits (setting a new mode if rhs has one).
Definition: StrMatcher.h:109
Match & operator|=(const Match &rhs)
Add flags.
Definition: StrMatcher.h:131
Match & operator-=(const Match &rhs)
Remove flags.
Definition: StrMatcher.h:135
bool isMode(Mode rhs) const
Whether this has mode rhs.
Definition: StrMatcher.h:161
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 test(const Match &rhs) const
Test whether all of the rhs bits are set (same mode if rhs has one).
Definition: StrMatcher.h:101
bool operator==(const Match &lhs, const Match &rhs)
Definition: StrMatcher.h:217
void setModeSubstring()
Set the mode SUBSTRING.
Definition: StrMatcher.h:195
String matching (STRING|SUBSTRING|GLOB|REGEX).
Definition: StrMatcher.h:298
bool doMatch(const char *string_r) const
Return whether string matches.
Definition: StrMatcher.cc:298
friend std::ostream & operator<<(std::ostream &str, const StrMatcher &obj)
Definition: StrMatcher.cc:326
const std::string & searchstring() const
The current searchstring.
Definition: StrMatcher.cc:301
MatchException Exception
Definition: StrMatcher.h:302
const Match & flags() const
The current search flags.
Definition: StrMatcher.cc:320
RWCOW_pointer< Impl > _pimpl
Pointer to implementation.
Definition: StrMatcher.h:392
bool operator!=(const StrMatcher &lhs, const StrMatcher &rhs)
Definition: StrMatcher.h:402
StrMatcher()
Default ctor matches nothing.
Definition: StrMatcher.cc:260
void setSearchstring(const std::string &string_r)
Set a new searchstring.
Definition: StrMatcher.cc:304
void setFlags(const Match &flags_r)
Set new search flags.
Definition: StrMatcher.cc:323
bool isCompiled() const
Whether the StrMatcher is already compiled.
Definition: StrMatcher.cc:295
bool operator()(const Tp &string_r) const
Return whether string matches.
Definition: StrMatcher.h:346
bool operator()(const char *string_r) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: StrMatcher.h:349
void compile() const
Compile the pattern e.g.
Definition: StrMatcher.cc:292
String related utilities and Regular expression matching.
Easy-to use interface to the ZYPP dependency resolver.
Definition: CodePitfalls.doc:2
bool operator<(const StrMatcher &lhs, const StrMatcher &rhs)
Definition: StrMatcher.cc:335
bool operator==(const SetRelation::Enum &lhs, const SetCompare &rhs)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Match operator|(Match::Mode lhs, Match::Mode rhs)
Definition: StrMatcher.h:227
std::ostream & operator<<(std::ostream &str, const SerialNumber &obj)
Definition: SerialNumber.cc:52
Match operator-(Match::Mode lhs, Match::Mode rhs)
Definition: StrMatcher.h:234
Exceptions thrown from attribute matching.
Definition: StrMatcher.h:248
MatchException(const std::string &msg_r)
Supplied message.
Definition: StrMatcher.h:250
Invalid regular expression (failed ::regcomp).
Definition: StrMatcher.h:271
MatchInvalidRegexException(const std::string &msg_r)
Supplied message.
Definition: StrMatcher.h:273
MatchUnknownModeException(const std::string &msg_r)
Supplied message.
Definition: StrMatcher.h:260
RW_pointer supporting 'copy on write' functionality.
Definition: PtrTypes.h:459
StrMatcher implementation.
Definition: StrMatcher.cc:169