• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2013-2022 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.
32 struct regex_t_deleter
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
operatorregex_t_deleter39   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 {
escapeescape49   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 
59 regex_t_sptr
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<>
78 regex::regex_t_sptr
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<>
87 regex::regex_t_sptr
88 build_sptr<regex_t>();
89 
90 }// end namespace sptr_utils
91 
92 }// end namespace abigail
93 
94 #endif //__ABG_REGEX_H__
95