1 //===-- RegularExpression.h -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_RegularExpression_h_
11 #define liblldb_RegularExpression_h_
14 #include "../lib/Support/regex_impl.h"
16 typedef llvm_regmatch_t regmatch_t;
17 typedef llvm_regex_t regex_t;
19 inline int regcomp(llvm_regex_t * a, const char *b, int c)
21 return llvm_regcomp(a, b, c);
24 inline size_t regerror(int a, const llvm_regex_t *b, char *c, size_t d)
26 return llvm_regerror(a, b, c, d);
29 inline int regexec(const llvm_regex_t * a, const char * b, size_t c,
30 llvm_regmatch_t d [], int e)
32 return llvm_regexec(a, b, c, d, e);
35 inline void regfree(llvm_regex_t * a)
55 namespace lldb_private {
57 //----------------------------------------------------------------------
58 /// @class RegularExpression RegularExpression.h "lldb/Core/RegularExpression.h"
59 /// @brief A C++ wrapper class for regex.
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
71 Match (uint32_t max_matches) :
75 m_matches.resize(max_matches + 1);
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;
90 return m_matches.size();
96 return (m_matches.empty() ? nullptr : m_matches.data());
100 GetMatchAtIndex (const char* s, uint32_t idx, std::string& match_str) const;
103 GetMatchAtIndex (const char* s, uint32_t idx, llvm::StringRef& match_str) const;
106 GetMatchSpanningIndices (const char* s, uint32_t idx1, uint32_t idx2, llvm::StringRef& match_str) const;
109 std::vector<regmatch_t> m_matches; ///< Where parenthesized subexpressions results are stored
112 //------------------------------------------------------------------
113 /// Default constructor.
115 /// The default constructor that initializes the object state such
116 /// that it contains no compiled regular expression.
117 //------------------------------------------------------------------
118 RegularExpression ();
121 RegularExpression (const char* re);
123 //------------------------------------------------------------------
126 /// Any previously compiled regular expression contained in this
127 /// object will be freed.
128 //------------------------------------------------------------------
129 ~RegularExpression ();
131 RegularExpression (const RegularExpression &rhs);
133 const RegularExpression & operator=(const RegularExpression &rhs);
135 //------------------------------------------------------------------
136 /// Compile a regular expression.
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.
146 /// A NULL terminated C string that represents the regular
147 /// expression to compile.
150 /// \b true if the regular expression compiles successfully,
151 /// \b false otherwise.
152 //------------------------------------------------------------------
154 Compile (const char* re);
156 //------------------------------------------------------------------
157 /// Executes a regular expression.
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.
165 /// @param[in] string
166 /// The string to match against the compile regular expression.
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.
174 /// \b true if \a string matches the compiled regular
175 /// expression, \b false otherwise.
176 //------------------------------------------------------------------
178 Execute(const char* string, Match *match = nullptr) const;
181 GetErrorAsCString (char *err_str, size_t err_str_max_len) const;
183 //------------------------------------------------------------------
184 /// Free the compiled regular expression.
186 /// If this object contains a valid compiled regular expression,
187 /// this function will free any resources it was consuming.
188 //------------------------------------------------------------------
192 //------------------------------------------------------------------
193 /// Access the regular expression text.
195 /// Returns the text that was used to compile the current regular
199 /// The NULL terminated C string that was used to compile the
200 /// current regular expression
201 //------------------------------------------------------------------
205 //------------------------------------------------------------------
208 /// Test if this object contains a valid regular expression.
211 /// \b true if the regular expression compiled and is ready
212 /// for execution, \b false otherwise.
213 //------------------------------------------------------------------
232 operator < (const RegularExpression& rhs) const;
235 //------------------------------------------------------------------
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
243 } // namespace lldb_private
245 #endif // liblldb_RegularExpression_h_