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

libzypp 17.38.7

Regex.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
12#include <cstdio>
13#include <cstdarg>
14
15#include <iostream>
16
17#include <zypp-core/base/Regex.h>
18#include <zypp-core/base/StringV.h>
19
20using namespace zypp;
21using namespace zypp::str;
22
23regex::regex()
24: m_flags( match_extended )
25{}
26
27 regex::regex( const std::string & str, int flags )
28{ assign( str, flags ); }
29
30regex::~regex()
31{
32 if ( m_valid )
33 regfree( &m_preg );
34}
35
36 void regex::assign( const std::string & str, int flags )
37{
38 if ( m_valid )
39 regfree( &m_preg );
40
41 m_valid = false;
42 m_str = str;
43 m_flags = flags;
44
45 static constexpr int normal = 1<<16; // deprecated legacy, use match_extended
46 if ( flags & normal ) flags &= ~normal;
47 if ( (flags & rxdefault) != rxdefault ) flags |= rxdefault; // always enforced (legacy)
48
49 if ( int err = regcomp( &m_preg, str.c_str(), flags ) ) {
50 char errbuff[100];
51 regerror( err, &m_preg, errbuff, sizeof(errbuff) );
52 ZYPP_THROW( regex_error(std::string(errbuff)) );
53 }
54 m_valid = true;
55}
56
57 bool regex::matches( const char* s, smatch & matches, int flags ) const
58{
59 if ( m_valid ) {
60 const auto possibleMatchCount = m_preg.re_nsub + 1;
61 matches.pmatch.resize( possibleMatchCount );
62 memset( matches.pmatch.data(), -1, sizeof( regmatch_t ) * ( possibleMatchCount ) );
63
64 if ( s && !regexec( &m_preg, s, matches.pmatch.size(), matches.pmatch.data(), flags ) ) {
65 matches.match_str = s;
66 return true; // good match
67 }
68 }
69 // Here: no match
70 matches.pmatch.clear();
71 matches.match_str.clear();
72 return false;
73}
74
75 bool regex::matches( const char* s ) const
76{
77 return m_valid && s && !regexec( &m_preg, s, 0, NULL, 0 );
78}
79
80 bool zypp::str::regex_match( const char* s, smatch& matches, const regex& regex )
81{ return regex.matches( s, matches ); }
82
83 bool zypp::str::regex_match( const char* s, const regex& regex )
84{ return regex.matches( s ); }
85
86smatch::smatch()
87{}
88
89std::string smatch::operator[](unsigned i) const
90{
91 if ( i < pmatch.size() && pmatch[i].rm_so != -1 )
92 return match_str.substr( pmatch[i].rm_so, pmatch[i].rm_eo-pmatch[i].rm_so );
93
94 return std::string();
95}
96
97std::string::size_type smatch::begin( unsigned i ) const
98{ return( i < pmatch.size() && pmatch[i].rm_so != -1 ? pmatch[i].rm_so : std::string::npos ); }
99
100std::string::size_type smatch::end( unsigned i ) const
101{ return( i < pmatch.size() && pmatch[i].rm_so != -1 ? pmatch[i].rm_eo : std::string::npos ); }
102
103std::string::size_type smatch::size( unsigned i ) const
104{ return( i < pmatch.size() && pmatch[i].rm_so != -1 ? pmatch[i].rm_eo-pmatch[i].rm_so : std::string::npos ); }
105
106 unsigned smatch::size() const
107{
108 unsigned matches = unsigned(-1);
109 // Get highest (pmatch[i].rm_so != -1). Just looking for the 1st
110 // (pmatch[i].rm_so == -1) is wrong as optional mayches "()?"
111 // may be embeded.
112 for ( unsigned i = 0; i < pmatch.size(); ++i )
113 {
114 if ( pmatch[i].rm_so != -1 )
115 matches = i;
116 }
117 return ++matches;
118}
119
120std::string zypp::str::regex_substitute( const std::string &s, const regex &regex, const std::string &replacement, bool global )
121{
122 std::string result;
123 strv::splitRx( s, regex, [&result,&replacement,global]( std::string_view w, unsigned, bool last ) {
124 result += w;
125 if ( !last )
126 result += replacement;
127 return global;
128 });
129 return result;
130}
ZYPP_THROW
#define ZYPP_THROW(EXCPT)
Drops a logline and throws the Exception.
Definition Exception.h:459
Regex.h
StringV.h
zypp::str::regex
Regular expression.
Definition Regex.h:95
zypp::str::regex::regex
regex()
Definition Regex.cc:23
zypp::str::regex::~regex
~regex()
Definition Regex.cc:30
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::match_extended
@ match_extended
Use POSIX Extended Regular Expression syntax when interpreting regex.
Definition Regex.h:101
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::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::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::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
str
String related utilities and Regular expression matching.
zypp::str
Provide print[f] and sprint[f] functions based on JoinFormat.
Definition PurgeKernels.h:19
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