libzypp: zypp/base/StrMatcher.cc Source File

libzypp 17.38.14

StrMatcher.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12extern "C"
13{
14#include <solv/repo.h>
15}
16
17#include <iostream>
18#include <sstream>
19
20#include <zypp-core/base/LogTools.h>
21#include <zypp-core/base/Gettext.h>
22#include <zypp-core/base/String.h>
23
24#include <zypp/base/StrMatcher.h>
25#include <zypp/sat/detail/PoolDefines.h>
26
27using std::endl;
28
30namespace zypp
31{
33 // class Match
35
36 const int Match::_modemask = SEARCH_STRINGMASK;
37 const int Match::_flagmask = ~_modemask;
38
39 // option flags
40 const Match Match::NOCASE (SEARCH_NOCASE);
41
42 // sat::LookupAttr option flags
43 const Match Match::NO_STORAGE_SOLVABLE(SEARCH_NO_STORAGE_SOLVABLE);
44 const Match Match::SUB (SEARCH_SUB);
45 const Match Match::ARRAYSENTINEL (SEARCH_ARRAYSENTINEL);
46 const Match Match::DISABLED_REPOS (SEARCH_DISABLED_REPOS);
47 const Match Match::COMPLETE_FILELIST (SEARCH_COMPLETE_FILELIST);
48 const Match Match::SKIP_KIND (SEARCH_SKIP_KIND);
49 const Match Match::FILES (SEARCH_FILES);
50 const Match Match::CHECKSUMS (SEARCH_CHECKSUMS);
51
52 Match::Mode Match::mode() const
53 {
54 switch ( modeval() )
55 {
56 case 0: return NOTHING; break;
57 case SEARCH_STRING: return STRING; break;
58 case SEARCH_STRINGSTART: return STRINGSTART; break;
59 case SEARCH_STRINGEND: return STRINGEND; break;
60 case SEARCH_SUBSTRING: return SUBSTRING; break;
61 case SEARCH_GLOB: return GLOB; break;
62 case SEARCH_REGEX: return REGEX; break;
63 }
64 return OTHER;
65 }
66
67 int Match::modeval( Mode mode_r )
68 {
69 switch ( mode_r )
70 {
71 case NOTHING: return 0; break;
72 case STRING: return SEARCH_STRING; break;
73 case STRINGSTART: return SEARCH_STRINGSTART; break;
74 case STRINGEND: return SEARCH_STRINGEND; break;
75 case SUBSTRING: return SEARCH_SUBSTRING; break;
76 case GLOB: return SEARCH_GLOB; break;
77 case REGEX: return SEARCH_REGEX; break;
78 case OTHER: return SEARCH_STRINGMASK; break;
79 }
80 return SEARCH_STRINGMASK;
81 }
82
83 std::string Match::asString() const
84 { std::ostringstream str; str << *this; return str.str(); }
85
86 std::ostream & operator<<( std::ostream & str, Match::Mode obj )
87 {
88 switch ( obj )
89 {
90#define OUTS(V) case Match::V: return str << #V; break
91 OUTS( NOTHING );
92 OUTS( STRING );
93 OUTS( STRINGSTART );
94 OUTS( STRINGEND );
95 OUTS( SUBSTRING );
96 OUTS( GLOB );
97 OUTS( REGEX );
98 OUTS( OTHER );
99#undef OUTS
100 }
101 return str << "Match::Mode::UNKNOWN";
102 }
103
104 std::ostream & operator<<( std::ostream & str, const Match & obj )
105 {
106 if ( ! obj )
107 return str << "NOTHING";
108
109 const char * sep = "|";
110 Match::Mode mode( obj.mode() );
111 switch ( mode )
112 {
113 case Match::NOTHING:
114 sep = 0; // suppress 'NOTHING|'
115 break;
116 case Match::OTHER:
117 str << mode<<"("<<obj.modeval()<<")"; // check whether libsolv has introduced new modes!
118 break;
119 default:
120 str << mode;
121 break;
122 }
123
124 int val = obj.flagval();
125 if ( val )
126 {
127#define OUTS(V) if ( val & Match::V.get() ) { val &= ~Match::V.get(); if ( sep ) str << sep; else sep = "|"; str << #V; }
128 OUTS( NOCASE );
129 OUTS( NO_STORAGE_SOLVABLE );
130 OUTS( SUB );
131 OUTS( ARRAYSENTINEL );
132 OUTS( DISABLED_REPOS );
133 OUTS( COMPLETE_FILELIST );
134 OUTS( SKIP_KIND );
135 OUTS( FILES );
136 OUTS( CHECKSUMS );
137#undef OUTS
138 if ( val )
139 {
140 if ( sep ) str << sep;
141 str << zypp::str::hexstring( val ); // check whether libsolv has introduced new flags.
142 }
143 }
144 return str;
145 }
146
148 // class MatchException
150
151 MatchUnknownModeException::MatchUnknownModeException( const Match & mode_r, const std::string & msg_r )
152 : MatchException( msg_r.empty() ? str::form(_("Unknown match mode '%s'"), mode_r.asString().c_str() )
153 : str::form(_("Unknown match mode '%s' for pattern '%s'"), mode_r.asString().c_str(), msg_r.c_str() ) )
154 {}
155
156 MatchInvalidRegexException::MatchInvalidRegexException( const std::string & regex_r, int regcomp_r )
157 : MatchException( regcomp_r ? str::form(_("Invalid regular expression '%s': regcomp returned %d"), regex_r.c_str(), regcomp_r )
158 : str::form(_("Invalid regular expression '%s'"), regex_r.c_str() ) )
159 {}
160
168 struct StrMatcher::Impl
169 {
170 Impl()
171 {}
172
173 Impl( std::string search_r, const Match & flags_r )
174 : _search( std::move(search_r) )
175 , _flags( flags_r )
176 {}
177
178 ~Impl()
179 { invalidate(); }
180
181 Impl(const Impl &) = delete;
182 Impl(Impl &&) = delete;
183 Impl &operator=(const Impl &) = delete;
184 Impl &operator=(Impl &&) = delete;
185
187 void compile() const
188 {
189 if ( !_matcher )
190 {
191 if ( _flags.mode() == Match::OTHER )
192 ZYPP_THROW( MatchUnknownModeException( _flags, _search ) );
193
194 _matcher.reset( new zypp::sat::detail::CDatamatcher );
195 int res = ::datamatcher_init( _matcher.get(), _search.c_str(), _flags.get() );
196 if ( res )
197 {
198 _matcher.reset();
199 ZYPP_THROW( MatchInvalidRegexException( _search, res ) );
200 }
201 }
202 }
203
205 bool isCompiled() const
206 { return _matcher != nullptr; }
207
209 bool doMatch( const char * string_r ) const
210 {
211 compile(); // nop if already compiled.
212
213 if ( ! string_r )
214 return false; // NULL never matches
215 return ::datamatcher_match( _matcher.get(), string_r );
216 }
217
219 const std::string & searchstring() const
220 { return _search; }
221
223 void setSearchstring( std::string string_r )
224 { invalidate(); _search = std::move(string_r); }
225
227 const Match & flags() const
228 { return _flags; }
229
231 void setFlags( const Match & flags_r )
232 { invalidate(); _flags = flags_r; }
233
234 private:
236 void invalidate()
237 {
238 if ( _matcher )
239 ::datamatcher_free( _matcher.get() );
240 _matcher.reset();
241 }
242
243 private:
244 std::string _search;
245 Match _flags;
246 mutable scoped_ptr< sat::detail::CDatamatcher> _matcher;
247
248 private:
249 friend Impl * rwcowClone<Impl>( const Impl * rhs );
251 Impl * clone() const
252 { return new Impl( _search, _flags ); }
253 };
254
256 inline std::ostream & operator<<( std::ostream & str, const StrMatcher::Impl & obj )
257 {
258 return str << "\"" << obj.searchstring() << "\"{" << obj.flags() << "}";
259 }
260
262 // class StrMatcher
264
265 StrMatcher::StrMatcher()
266 : _pimpl( new Impl )
267 {}
268
269 StrMatcher::StrMatcher( const std::string & search_r )
270 : _pimpl( new Impl( search_r, Match::STRING ) )
271 {}
272 StrMatcher::StrMatcher( std::string && search_r )
273 : _pimpl( new Impl( std::move(search_r), Match::STRING ) )
274 {}
275
276 StrMatcher::StrMatcher( const std::string & search_r, const Match & flags_r )
277 : _pimpl( new Impl( search_r, flags_r ) )
278 {}
279 StrMatcher::StrMatcher( std::string && search_r, const Match & flags_r )
280 : _pimpl( new Impl( std::move(search_r), flags_r ) )
281 {}
282
283 StrMatcher::StrMatcher( const std::string & search_r, const Match::Mode & flags_r )
284 : _pimpl( new Impl( search_r, flags_r ) )
285 {}
286 StrMatcher::StrMatcher( std::string && search_r, const Match::Mode & flags_r )
287 : _pimpl( new Impl( std::move(search_r), flags_r ) )
288 {}
289
290 StrMatcher::StrMatcher( const std::string & search_r, int flags_r )
291 : _pimpl( new Impl( search_r, Match(flags_r) ) )
292 {}
293 StrMatcher::StrMatcher( std::string && search_r, int flags_r )
294 : _pimpl( new Impl( std::move(search_r), Match(flags_r) ) )
295 {}
296
297 void StrMatcher::compile() const
298 { return _pimpl->compile(); }
299
300 bool StrMatcher::isCompiled() const
301 { return _pimpl->isCompiled(); }
302
303 bool StrMatcher::doMatch( const char * string_r ) const
304 { return _pimpl->doMatch( string_r ); }
305
306 const std::string & StrMatcher::searchstring() const
307 { return _pimpl->searchstring(); }
308
309 void StrMatcher::setSearchstring( const std::string & string_r )
310 { _pimpl->setSearchstring( string_r ); }
311 void StrMatcher::setSearchstring( std::string && string_r )
312 { _pimpl->setSearchstring( std::move(string_r) ); }
313
314 void StrMatcher::setSearchstring( const std::string & string_r, const Match & flags_r )
315 {
316 _pimpl->setSearchstring( string_r );
317 _pimpl->setFlags( flags_r );
318 }
319 void StrMatcher::setSearchstring( std::string && string_r, const Match & flags_r )
320 {
321 _pimpl->setSearchstring( std::move(string_r) );
322 _pimpl->setFlags( flags_r );
323 }
324
325 const Match & StrMatcher::flags() const
326 { return _pimpl->flags(); }
327
328 void StrMatcher::setFlags( const Match & flags_r )
329 { _pimpl->setFlags( flags_r ); }
330
331 std::ostream & operator<<( std::ostream & str, const StrMatcher & obj )
332 { return str << *obj._pimpl; }
333
334 bool operator==( const StrMatcher & lhs, const StrMatcher & rhs )
335 {
336 return ( lhs.flags() == rhs.flags()
337 && lhs.searchstring() == rhs.searchstring() );
338 }
339
340 bool operator<( const StrMatcher & lhs, const StrMatcher & rhs )
341 {
342 if ( lhs.flags().get() != rhs.flags().get() )
343 return ( lhs.flags().get() < rhs.flags().get() );
344
345 return ( lhs.searchstring() < rhs.searchstring() );
346 }
347
348} // namespace zypp
OUTS
#define OUTS(VAL)
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
Gettext.h
_
#define _(MSG)
Definition Gettext.h:39
LogTools.h
PoolDefines.h
StrMatcher.h
String.h
scoped_ptr
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::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::ARRAYSENTINEL
static const Match ARRAYSENTINEL
LookupAttr: internal.
Definition StrMatcher.h:71
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::_flagmask
static const int _flagmask
Definition StrMatcher.h:36
zypp::Match::NO_STORAGE_SOLVABLE
static const Match NO_STORAGE_SOLVABLE
LookupAttr: internal.
Definition StrMatcher.h:69
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::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::modeval
int modeval() const
Return the modes integer representation.
Definition StrMatcher.h:152
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::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:303
zypp::StrMatcher::operator<<
friend std::ostream & operator<<(std::ostream &str, const StrMatcher &obj)
relates: StrMatcher Stream output
Definition StrMatcher.cc:331
zypp::StrMatcher::searchstring
const std::string & searchstring() const
The current searchstring.
Definition StrMatcher.cc:306
zypp::StrMatcher::flags
const Match & flags() const
The current search flags.
Definition StrMatcher.cc:325
zypp::StrMatcher::_pimpl
RWCOW_pointer< Impl > _pimpl
Pointer to implementation.
Definition StrMatcher.h:392
zypp::StrMatcher::StrMatcher
StrMatcher()
Default ctor matches nothing.
Definition StrMatcher.cc:265
zypp::StrMatcher::setSearchstring
void setSearchstring(const std::string &string_r)
Set a new searchstring.
Definition StrMatcher.cc:309
zypp::StrMatcher::setFlags
void setFlags(const Match &flags_r)
Set new search flags.
Definition StrMatcher.cc:328
zypp::StrMatcher::isCompiled
bool isCompiled() const
Whether the StrMatcher is already compiled.
Definition StrMatcher.cc:300
zypp::StrMatcher::compile
void compile() const
Compile the pattern e.g.
Definition StrMatcher.cc:297
form
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition String.cc:39
std
Definition ansi.h:855
str
String related utilities and Regular expression matching.
zypp::sat::detail::CDatamatcher
::s_Datamatcher CDatamatcher
Wrapped libsolv C data type exposed as backdoor.
Definition PoolDefines.h:32
zypp::str::hexstring
std::string hexstring(char n, int w=4)
Definition String.h:325
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::operator<
bool operator<(const Capability &lhs, const Capability &rhs)
relates: Capability Arbitrary order.
Definition Capability.h:317
zypp::operator==
bool operator==(const Capability &lhs, const Capability &rhs)
relates: Capability
Definition Capability.h:309
zypp::asString
std::string asString(const Patch::Category &obj)
relates: Patch::Category string representation.
Definition Patch.cc:122
zypp::operator<<
std::ostream & operator<<(std::ostream &str, const Capabilities &obj)
relates: Capabilities Stream output
Definition Capabilities.cc:65
zypp::MatchException::MatchException
MatchException(const std::string &msg_r)
Supplied message.
Definition StrMatcher.h:250
zypp::MatchInvalidRegexException::MatchInvalidRegexException
MatchInvalidRegexException(const std::string &msg_r)
Supplied message.
Definition StrMatcher.h:273
zypp::MatchUnknownModeException::MatchUnknownModeException
MatchUnknownModeException(const std::string &msg_r)
Supplied message.
Definition StrMatcher.h:260
zypp::StrMatcher::Impl
StrMatcher implementation.
Definition StrMatcher.cc:169
zypp::StrMatcher::Impl::clone
Impl * clone() const
clone for RWCOW_pointer
Definition StrMatcher.cc:251
zypp::StrMatcher::Impl::Impl
Impl()
Definition StrMatcher.cc:170
zypp::StrMatcher::Impl::flags
const Match & flags() const
The current search flags.
Definition StrMatcher.cc:227
zypp::StrMatcher::Impl::operator=
Impl & operator=(Impl &&)=delete
zypp::StrMatcher::Impl::searchstring
const std::string & searchstring() const
The current searchstring.
Definition StrMatcher.cc:219
zypp::StrMatcher::Impl::rwcowClone
friend Impl * rwcowClone(const Impl *rhs)
zypp::StrMatcher::Impl::~Impl
~Impl()
Definition StrMatcher.cc:178
zypp::StrMatcher::Impl::compile
void compile() const
Compile the pattern.
Definition StrMatcher.cc:187
zypp::StrMatcher::Impl::Impl
Impl(Impl &&)=delete
zypp::StrMatcher::Impl::isCompiled
bool isCompiled() const
Whether the pattern is already compiled.
Definition StrMatcher.cc:205
zypp::StrMatcher::Impl::_flags
Match _flags
Definition StrMatcher.cc:245
zypp::StrMatcher::Impl::doMatch
bool doMatch(const char *string_r) const
Return whether string matches.
Definition StrMatcher.cc:209
zypp::StrMatcher::Impl::invalidate
void invalidate()
Has to be called if _search or _flags change.
Definition StrMatcher.cc:236
zypp::StrMatcher::Impl::setSearchstring
void setSearchstring(std::string string_r)
Set a new searchstring.
Definition StrMatcher.cc:223
zypp::StrMatcher::Impl::operator=
Impl & operator=(const Impl &)=delete
zypp::StrMatcher::Impl::_search
std::string _search
Definition StrMatcher.cc:244
zypp::StrMatcher::Impl::Impl
Impl(std::string search_r, const Match &flags_r)
Definition StrMatcher.cc:173
zypp::StrMatcher::Impl::setFlags
void setFlags(const Match &flags_r)
Set new search flags.
Definition StrMatcher.cc:231
zypp::StrMatcher::Impl::Impl
Impl(const Impl &)=delete
zypp::StrMatcher::Impl::_matcher
scoped_ptr< sat::detail::CDatamatcher > _matcher
Definition StrMatcher.cc:246