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