libzypp: zypp/base/StrMatcher.h Source File

libzypp 17.25.10

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