]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/lldb/Core/RegularExpression.h
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / include / lldb / Core / RegularExpression.h
1 //===-- RegularExpression.h -------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef liblldb_RegularExpression_h_
11 #define liblldb_RegularExpression_h_
12
13 #ifdef _WIN32
14 #include "../lib/Support/regex_impl.h"
15
16 typedef llvm_regmatch_t regmatch_t;
17 typedef llvm_regex_t regex_t;
18
19 inline int regcomp(llvm_regex_t *a, const char *b, int c) {
20   return llvm_regcomp(a, b, c);
21 }
22
23 inline size_t regerror(int a, const llvm_regex_t *b, char *c, size_t d) {
24   return llvm_regerror(a, b, c, d);
25 }
26
27 inline int regexec(const llvm_regex_t *a, const char *b, size_t c,
28                    llvm_regmatch_t d[], int e) {
29   return llvm_regexec(a, b, c, d, e);
30 }
31
32 inline void regfree(llvm_regex_t *a) { llvm_regfree(a); }
33 #else
34 #ifdef __ANDROID__
35 #include <regex>
36 #endif
37 #include <regex.h>
38 #endif
39 #include <stdint.h>
40
41 #include <string>
42 #include <vector>
43
44 namespace llvm {
45 class StringRef;
46 } // namespace llvm
47
48 namespace lldb_private {
49
50 //----------------------------------------------------------------------
51 /// @class RegularExpression RegularExpression.h "lldb/Core/RegularExpression.h"
52 /// @brief A C++ wrapper class for regex.
53 ///
54 /// This regular expression class wraps the posix regex functions
55 /// \c regcomp(), \c regerror(), \c regexec(), and \c regfree() from
56 /// the header file in \c /usr/include/regex\.h.
57 //----------------------------------------------------------------------
58 class RegularExpression {
59 public:
60   class Match {
61   public:
62     Match(uint32_t max_matches) : m_matches() {
63       if (max_matches > 0)
64         m_matches.resize(max_matches + 1);
65     }
66
67     void Clear() {
68       const size_t num_matches = m_matches.size();
69       regmatch_t invalid_match = {-1, -1};
70       for (size_t i = 0; i < num_matches; ++i)
71         m_matches[i] = invalid_match;
72     }
73
74     size_t GetSize() const { return m_matches.size(); }
75
76     regmatch_t *GetData() {
77       return (m_matches.empty() ? nullptr : m_matches.data());
78     }
79
80     bool GetMatchAtIndex(llvm::StringRef s, uint32_t idx,
81                          std::string &match_str) const;
82
83     bool GetMatchAtIndex(llvm::StringRef s, uint32_t idx,
84                          llvm::StringRef &match_str) const;
85
86     bool GetMatchSpanningIndices(llvm::StringRef s, uint32_t idx1,
87                                  uint32_t idx2,
88                                  llvm::StringRef &match_str) const;
89
90   protected:
91     std::vector<regmatch_t>
92         m_matches; ///< Where parenthesized subexpressions results are stored
93   };
94
95   //------------------------------------------------------------------
96   /// Default constructor.
97   ///
98   /// The default constructor that initializes the object state such
99   /// that it contains no compiled regular expression.
100   //------------------------------------------------------------------
101   RegularExpression();
102
103   explicit RegularExpression(llvm::StringRef string);
104
105   //------------------------------------------------------------------
106   /// Destructor.
107   ///
108   /// Any previously compiled regular expression contained in this
109   /// object will be freed.
110   //------------------------------------------------------------------
111   ~RegularExpression();
112
113   RegularExpression(const RegularExpression &rhs);
114
115   const RegularExpression &operator=(const RegularExpression &rhs);
116
117   //------------------------------------------------------------------
118   /// Compile a regular expression.
119   ///
120   /// Compile a regular expression using the supplied regular
121   /// expression text. The compiled regular expression lives
122   /// in this object so that it can be readily used for regular
123   /// expression matches. Execute() can be called after the regular
124   /// expression is compiled. Any previously compiled regular
125   /// expression contained in this object will be freed.
126   ///
127   /// @param[in] re
128   ///     A NULL terminated C string that represents the regular
129   ///     expression to compile.
130   ///
131   /// @return
132   ///     \b true if the regular expression compiles successfully,
133   ///     \b false otherwise.
134   //------------------------------------------------------------------
135   bool Compile(llvm::StringRef string);
136   bool Compile(const char *) = delete;
137
138   //------------------------------------------------------------------
139   /// Executes a regular expression.
140   ///
141   /// Execute a regular expression match using the compiled regular
142   /// expression that is already in this object against the match
143   /// string \a s. If any parens are used for regular expression
144   /// matches \a match_count should indicate the number of regmatch_t
145   /// values that are present in \a match_ptr.
146   ///
147   /// @param[in] string
148   ///     The string to match against the compile regular expression.
149   ///
150   /// @param[in] match
151   ///     A pointer to a RegularExpression::Match structure that was
152   ///     properly initialized with the desired number of maximum
153   ///     matches, or nullptr if no parenthesized matching is needed.
154   ///
155   /// @return
156   ///     \b true if \a string matches the compiled regular
157   ///     expression, \b false otherwise.
158   //------------------------------------------------------------------
159   bool Execute(llvm::StringRef string, Match *match = nullptr) const;
160   bool Execute(const char *, Match * = nullptr) = delete;
161
162   size_t GetErrorAsCString(char *err_str, size_t err_str_max_len) const;
163
164   //------------------------------------------------------------------
165   /// Free the compiled regular expression.
166   ///
167   /// If this object contains a valid compiled regular expression,
168   /// this function will free any resources it was consuming.
169   //------------------------------------------------------------------
170   void Free();
171
172   //------------------------------------------------------------------
173   /// Access the regular expression text.
174   ///
175   /// Returns the text that was used to compile the current regular
176   /// expression.
177   ///
178   /// @return
179   ///     The NULL terminated C string that was used to compile the
180   ///     current regular expression
181   //------------------------------------------------------------------
182   llvm::StringRef GetText() const;
183
184   //------------------------------------------------------------------
185   /// Test if valid.
186   ///
187   /// Test if this object contains a valid regular expression.
188   ///
189   /// @return
190   ///     \b true if the regular expression compiled and is ready
191   ///     for execution, \b false otherwise.
192   //------------------------------------------------------------------
193   bool IsValid() const;
194
195   void Clear() {
196     Free();
197     m_re.clear();
198     m_comp_err = 1;
199   }
200
201   int GetErrorCode() const { return m_comp_err; }
202
203   bool operator<(const RegularExpression &rhs) const;
204
205 private:
206   //------------------------------------------------------------------
207   // Member variables
208   //------------------------------------------------------------------
209   std::string m_re; ///< A copy of the original regular expression text
210   int m_comp_err;   ///< Error code for the regular expression compilation
211   regex_t m_preg;   ///< The compiled regular expression
212 };
213
214 } // namespace lldb_private
215
216 #endif // liblldb_RegularExpression_h_