libzypp: zypp-core/base/Regex.h Source File

libzypp 17.38.8

Regex.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#ifndef ZYPP_BASE_REGEX_H
13#define ZYPP_BASE_REGEX_H
14
15#include <iosfwd>
16#include <string>
17#include <regex.h>
18
19#include <zypp-core/base/Exception.h>
20
22namespace zypp
23{
27 namespace str
28 {
53
54 using regex_error = Exception;
55
56 class smatch;
57 class regex;
58
67 bool regex_match( const char * s, smatch & matches, const regex & regex ) ZYPP_API;
68
70 inline bool regex_match(const std::string & s, smatch & matches, const regex & regex)
71 { return regex_match( s.c_str(), matches, regex ); }
72
74 bool regex_match( const char * s, const regex & regex ) ZYPP_API;
75
77 inline bool regex_match( const std::string & s, const regex & regex )
78 { return regex_match( s.c_str(), regex ); }
79
86 std::string regex_substitute ( const std::string & s, const regex & regex, const std::string &replacement, bool global = true ) ZYPP_API;
87
94 class ZYPP_API regex
95 {
96 public:
97
98 enum RegFlags {
99 icase = REG_ICASE,
100 nosubs = REG_NOSUB,
101 match_extended = REG_EXTENDED,
102 newline = REG_NEWLINE,
103 rxdefault = match_extended|newline
104 };
105
106 enum MatchFlags {
107 none = 0,
108 not_bol = REG_NOTBOL
109 };
110
111 regex();
112 regex( const std::string & s, int flags = rxdefault );
113 regex( const char* s, int flags = rxdefault ) : regex( std::string(s?s:""), flags ) {}
114 ~regex();
115
116 regex( const regex & rhs )
117 { assign( rhs.m_str, rhs.m_flags ); }
118
119 regex & operator=( const regex & rhs )
120 { assign( rhs.m_str, rhs.m_flags ); return *this; }
121
125 std::string asString() const
126 { return m_str; }
127
128 bool matches( const char * s, str::smatch & matches, int flags = none ) const;
129 bool matches( const std::string & s_r, str::smatch & matches_r, int flags_r = none ) const
130 { return matches( s_r.c_str(), matches_r, flags_r ); }
131
132 bool matches( const char * s ) const;
133 bool matches( const std::string & s_r ) const
134 { return matches( s_r.c_str() ); }
135
136 public:
138 regex_t * get()
139 { return & m_preg; }
140
141 private:
142 void assign( const std::string & s, int flags );
143
144 private:
145 friend class smatch;
146 std::string m_str;
147 int m_flags;
148 regex_t m_preg;
149 bool m_valid = false;
150 };
151
153 inline std::ostream & operator<<( std::ostream & str, const regex & obj )
154 { return str << obj.asString(); }
155
167 class ZYPP_API smatch
168 {
169 public:
170 smatch();
171
172 std::string operator[](unsigned i) const;
173
174 unsigned size() const;
175
177 std::string::size_type begin( unsigned i ) const;
178
180 std::string::size_type end( unsigned i ) const;
181
183 std::string::size_type size( unsigned i ) const;
184
185 std::string match_str;
186 std::vector<regmatch_t> pmatch;
187 };
188
189 } // namespace str
191} // namespace zypp
193#endif // ZYPP_BASE_STRING_H
Exception.h
zypp::Exception
Base class for Exception.
Definition Exception.h:153
zypp::str::regex
Regular expression.
Definition Regex.h:95
zypp::str::regex::get
regex_t * get()
Expert backdoor.
Definition Regex.h:138
zypp::str::regex::regex
regex()
Definition Regex.cc:23
zypp::str::regex::matches
bool matches(const std::string &s_r) const
Definition Regex.h:133
zypp::str::regex::regex
regex(const char *s, int flags=rxdefault)
Definition Regex.h:113
zypp::str::regex::m_preg
regex_t m_preg
Definition Regex.h:148
zypp::str::regex::m_str
std::string m_str
Definition Regex.h:146
zypp::str::regex::RegFlags
RegFlags
Definition Regex.h:98
zypp::str::regex::match_extended
@ match_extended
Use POSIX Extended Regular Expression syntax when interpreting regex.
Definition Regex.h:101
zypp::str::regex::icase
@ icase
Do not differentiate case.
Definition Regex.h:99
zypp::str::regex::newline
@ newline
Match newline.
Definition Regex.h:102
zypp::str::regex::rxdefault
@ rxdefault
These are enforced even if you don't pass them as flag argument.
Definition Regex.h:103
zypp::str::regex::nosubs
@ nosubs
Support for substring addressing of matches is not required.
Definition Regex.h:100
zypp::str::regex::asString
std::string asString() const
string representation of the regular expression
Definition Regex.h:125
zypp::str::regex::m_valid
bool m_valid
Definition Regex.h:149
zypp::str::regex::assign
void assign(const std::string &s, int flags)
Definition Regex.cc:36
zypp::str::regex::matches
bool matches(const std::string &s_r, str::smatch &matches_r, int flags_r=none) const
Definition Regex.h:129
zypp::str::regex::m_flags
int m_flags
Definition Regex.h:147
zypp::str::regex::matches
bool matches(const char *s, str::smatch &matches, int flags=none) const
Definition Regex.cc:57
zypp::str::regex::MatchFlags
MatchFlags
Definition Regex.h:106
zypp::str::regex::none
@ none
Definition Regex.h:107
zypp::str::regex::not_bol
@ not_bol
Do not match begin of line.
Definition Regex.h:108
zypp::str::regex::regex
regex(const regex &rhs)
Definition Regex.h:116
zypp::str::regex::operator=
regex & operator=(const regex &rhs)
Definition Regex.h:119
zypp::str::regex::smatch
friend class smatch
Definition Regex.h:145
zypp::str::smatch
Regular expression match result.
Definition Regex.h:168
zypp::str::smatch::smatch
smatch()
Definition Regex.cc:86
zypp::str::smatch::size
unsigned size() const
Definition Regex.cc:106
zypp::str::smatch::end
std::string::size_type end(unsigned i) const
End index of subexpression i in match_str (or std::string::npos).
Definition Regex.cc:100
zypp::str::smatch::begin
std::string::size_type begin(unsigned i) const
Begin index of subexpression i in match_str (or std::string::npos).
Definition Regex.cc:97
zypp::str::smatch::operator[]
std::string operator[](unsigned i) const
Definition Regex.cc:89
zypp::str::smatch::match_str
std::string match_str
Definition Regex.h:185
zypp::str::smatch::pmatch
std::vector< regmatch_t > pmatch
Definition Regex.h:186
zypp::str::regex_match
bool regex_match(const char *s, smatch &matches, const regex &regex) ZYPP_API
Regular expression matching.
Definition Regex.cc:80
std
Definition ansi.h:855
str
String related utilities and Regular expression matching.
zypp::str::ZYPP_API
std::string ZYPP_API
Definition String.h:171
zypp::str::operator<<
std::ostream & operator<<(std::ostream &str, const regex &obj)
relates: regex Stream output
Definition Regex.h:153
zypp::str::regex_substitute
std::string regex_substitute(const std::string &s, const regex &regex, const std::string &replacement, bool global=true) ZYPP_API
Replaces the matched regex with the string passed in replacement.
Definition Regex.cc:120
zypp::str::regex_error
Exception regex_error
Definition Regex.h:54
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition CodePitfalls.doc:2
zypp::ZYPP_API
const Arch Arch_empty ZYPP_API
relates: Arch This is an empty Arch represented by an empty string.
Definition Arch.h:173