libabigail
abg-regex.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2013-2023 Red Hat, Inc.
5 
6 /// @file
7 ///
8 /// Wrappers around regex types and functions.
9 
10 #ifndef __ABG_REGEX_H__
11 #define __ABG_REGEX_H__
12 
13 #include <regex.h>
14 
15 #include <memory>
16 #include <string>
17 #include <vector>
18 
19 #include "abg-sptr-utils.h"
20 
21 namespace abigail
22 {
23 
24 /// Namespace for regex types and functions.
25 namespace regex
26 {
27 
28 /// A convenience typedef for a shared pointer of regex_t.
29 typedef std::shared_ptr<regex_t> regex_t_sptr;
30 
31 /// A delete functor for a shared_ptr of regex_t.
33 {
34  /// The operator called to de-allocate the pointer to regex_t
35  /// embedded in a shared_ptr<regex_t>
36  ///
37  /// @param r the pointer to regex_t to de-allocate.
38  void
39  operator()(::regex_t* r)
40  {
41  regfree(r);
42  delete r;
43  }
44 };//end struct regex_deleter
45 
46 /// A class to hold a reference to a string to regex escape.
47 struct escape
48 {
49  escape(const std::string& str) : ref(str) { }
50  const std::string& ref;
51 };
52 
53 std::ostream&
54 operator<<(std::ostream& os, const escape& esc);
55 
56 std::string
57 generate_from_strings(const std::vector<std::string>& strs);
58 
60 compile(const std::string& str);
61 
62 bool
63 match(const regex_t_sptr& r, const std::string& str);
64 
65 }// end namespace regex
66 
67 namespace sptr_utils
68 {
69 /// Specialization of sptr_utils::build_sptr for regex_t.
70 ///
71 /// This is used to wrap a pointer to regex_t into a
72 /// shared_ptr<regex_t>.
73 ///
74 /// @param p the bare pointer to regex_t to wrap into a shared_ptr<regex_t>.
75 ///
76 /// @return the shared_ptr<regex_t> that wraps @p p.
77 template<>
79 build_sptr<regex_t>(regex_t *p);
80 
81 /// Specialization of sptr_utils::build_sptr for regex_t.
82 ///
83 /// This creates a pointer to regex_t and wraps it into a shared_ptr<regex_t>.
84 ///
85 /// @return the shared_ptr<regex_t> wrapping the newly created regex_t*
86 template<>
89 
90 }// end namespace sptr_utils
91 
92 }// end namespace abigail
93 
94 #endif //__ABG_REGEX_H__
Utilities to ease the wrapping of C types into std::shared_ptr.
bool match(const regex_t_sptr &r, const std::string &str)
See if a string matches a regex.
Definition: abg-regex.cc:127
std::ostream & operator<<(std::ostream &os, const escape &esc)
Escape regex special charaters in input string.
Definition: abg-regex.cc:65
regex_t_sptr compile(const std::string &str)
Compile a regex from a string.
Definition: abg-regex.cc:111
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
std::shared_ptr< regex_t > regex_t_sptr
A convenience typedef for a shared pointer of regex_t.
Definition: abg-fwd.h:88
regex::regex_t_sptr build_sptr< regex_t >(regex_t *p)
Specialization of sptr_utils::build_sptr for regex_t.
Definition: abg-regex.cc:40
Toplevel namespace for libabigail.
A class to hold a reference to a string to regex escape.
Definition: abg-regex.h:48
A delete functor for a shared_ptr of regex_t.
Definition: abg-regex.h:33
void operator()(::regex_t *r)
The operator called to de-allocate the pointer to regex_t embedded in a shared_ptr<regex_t>
Definition: abg-regex.h:39