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