libabigail
Toggle main menu visibility
Loading...
Searching...
No Matches
src
abg-regex.cc
Go to the documentation of this file.
1
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2
// -*- Mode: C++ -*-
3
//
4
// Copyright (C) 2016-2026 Red Hat, Inc.
5
6
/// @file
7
///
8
/// Some specialization for shared pointer utility templates.
9
///
10
11
#include "config.h"
12
13
#include <sstream>
14
#include <ostream>
15
16
#include "abg-internal.h"
17
18
// <headers defining libabigail's API go under here>
19
ABG_BEGIN_EXPORT_DECLARATIONS
20
21
#include "
abg-regex.h
"
22
#include "
abg-sptr-utils.h
"
23
24
ABG_END_EXPORT_DECLARATIONS
25
// </headers defining libabigail's API>
26
27
namespace
abigail
28
{
29
30
/// Specialization of sptr_utils::build_sptr for regex_t.
31
///
32
/// This is used to wrap a pointer to regex_t into a
33
/// shared_ptr<regex_t>.
34
///
35
/// @param p the bare pointer to regex_t to wrap into a shared_ptr<regex_t>.
36
///
37
/// @return the shared_ptr<regex_t> that wraps @p p.
38
template
<>
39
regex::regex_t_sptr
40
sptr_utils::build_sptr<regex_t>
(regex_t *p)
41
{
return
regex::regex_t_sptr
(p,
regex::regex_t_deleter
());}
42
43
/// Specialization of sptr_utils::build_sptr for regex_t.
44
///
45
/// This creates a pointer to regex_t and wraps it into a shared_ptr<regex_t>.
46
///
47
/// @return the shared_ptr<regex_t> wrapping the newly created regex_t*
48
template
<>
49
regex::regex_t_sptr
50
sptr_utils::build_sptr<regex_t>
()
51
{
return
sptr_utils::build_sptr
(
new
regex_t);}
52
53
namespace
regex
54
{
55
56
/// Escape regex special charaters in input string.
57
///
58
/// @param os the output stream being written to.
59
///
60
/// @param esc the regex_escape object holding a reference to the string
61
/// needing to be escaped.
62
///
63
/// @return the output stream.
64
std::ostream&
65
operator<<
(std::ostream& os,
const
escape
& esc)
66
{
67
// ']' and '}' are only conditionally special, so could be removed.
68
static
const
std::string specials =
"^.[]$()|*+?{}\\"
;
69
const
std::string& str = esc.ref;
70
for
(std::string::const_iterator i = str.begin(); i != str.end(); ++i)
71
{
72
if
(specials.find(*i) != std::string::npos)
73
os <<
'\\'
;
74
os << *i;
75
}
76
return
os;
77
}
78
79
/// Generate a regex pattern equivalent to testing set membership.
80
///
81
/// A string will match the resulting pattern regex, if and only if it
82
/// was present in the vector.
83
///
84
/// @param strs a vector of strings
85
///
86
/// @return a regex pattern
87
std::string
88
generate_from_strings
(
const
std::vector<std::string>& strs)
89
{
90
if
(strs.empty())
91
// This cute-looking regex does not match any string.
92
return
"^_^"
;
93
std::ostringstream os;
94
std::vector<std::string>::const_iterator i = strs.begin();
95
os <<
"^("
<<
escape
(*i++);
96
while
(i != strs.end())
97
os <<
"|"
<<
escape
(*i++);
98
os <<
")$"
;
99
return
os.str();
100
}
101
102
/// Compile a regex from a string.
103
///
104
/// The result is held in a shared pointer. This will be null if regex
105
/// compilation fails.
106
///
107
/// @param str the string representation of the regex.
108
///
109
/// @return shared pointer holder of a compiled regex object.
110
regex_t_sptr
111
compile
(
const
std::string& str)
112
{
113
regex_t_sptr
r =
sptr_utils::build_sptr
(
new
regex_t);
114
if
(regcomp(r.get(), str.c_str(), REG_EXTENDED))
115
r.reset();
116
return
r;
117
}
118
119
/// See if a string matches a regex.
120
///
121
/// @param r a shared pointer holder of a compiled regex object.
122
///
123
/// @param str a string.
124
///
125
/// @return whether there was a match.
126
bool
127
match
(
const
regex_t_sptr
& r,
const
std::string& str)
128
{
129
return
!regexec(r.get(), str.c_str(), 0, NULL, 0);
130
}
131
132
}
//end namespace regex
133
134
}
//end namespace abigail
abg-regex.h
Wrappers around regex types and functions.
abg-sptr-utils.h
Utilities to ease the wrapping of C types into std::shared_ptr.
abigail::regex
Namespace for regex types and functions.
Definition
abg-regex.cc:54
abigail::regex::match
bool match(const regex_t_sptr &r, const std::string &str)
See if a string matches a regex.
Definition
abg-regex.cc:127
abigail::regex::operator<<
std::ostream & operator<<(std::ostream &os, const escape &esc)
Escape regex special charaters in input string.
Definition
abg-regex.cc:65
abigail::regex::compile
regex_t_sptr compile(const std::string &str)
Compile a regex from a string.
Definition
abg-regex.cc:111
abigail::regex::generate_from_strings
std::string generate_from_strings(const std::vector< std::string > &strs)
Generate a regex pattern equivalent to testing set membership.
Definition
abg-regex.cc:88
abigail::regex::regex_t_sptr
std::shared_ptr< regex_t > regex_t_sptr
A convenience typedef for a shared pointer of regex_t.
Definition
abg-fwd.h:87
abigail::sptr_utils::build_sptr< regex_t >
regex::regex_t_sptr build_sptr< regex_t >()
Specialization of sptr_utils::build_sptr for regex_t.
Definition
abg-regex.cc:50
abigail::sptr_utils::build_sptr
shared_ptr< T > build_sptr()
This is to be specialized for the diverse C types that needs wrapping in shared_ptr.
abigail
Toplevel namespace for libabigail.
Definition
abg-comp-filter.cc:27
abigail::regex::escape
A class to hold a reference to a string to regex escape.
Definition
abg-regex.h:48
abigail::regex::regex_t_deleter
A delete functor for a shared_ptr of regex_t.
Definition
abg-regex.h:33
Generated by
1.17.0