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_DBRegex_h_
11 #define liblldb_DBRegex_h_
12 #if defined(__cplusplus)
15 #include "../lib/Support/regex_impl.h"
17 typedef llvm_regmatch_t regmatch_t;
18 typedef llvm_regex_t regex_t;
20 inline int regcomp(llvm_regex_t * a, const char *b, int c)
22 return llvm_regcomp(a, b, c);
25 inline size_t regerror(int a, const llvm_regex_t *b, char *c, size_t d)
27 return llvm_regerror(a, b, c, d);
30 inline int regexec(const llvm_regex_t * a, const char * b, size_t c,
31 llvm_regmatch_t d [], int e)
33 return llvm_regexec(a, b, c, d, e);
36 inline void regfree(llvm_regex_t * a)
54 namespace lldb_private {
56 //----------------------------------------------------------------------
57 /// @class RegularExpression RegularExpression.h "lldb/Core/RegularExpression.h"
58 /// @brief A C++ wrapper class for regex.
60 /// This regular expression class wraps the posix regex functions
61 /// \c regcomp(), \c regerror(), \c regexec(), and \c regfree() from
62 /// the header file in \c /usr/include/regex\.h.
63 //----------------------------------------------------------------------
64 class RegularExpression
70 Match (uint32_t max_matches) :
74 m_matches.resize(max_matches + 1);
80 const size_t num_matches = m_matches.size();
81 regmatch_t invalid_match = { -1, -1 };
82 for (size_t i=0; i<num_matches; ++i)
83 m_matches[i] = invalid_match;
89 return m_matches.size();
95 if (m_matches.empty())
97 return m_matches.data();
101 GetMatchAtIndex (const char* s, uint32_t idx, std::string& match_str) const;
104 GetMatchAtIndex (const char* s, uint32_t idx, llvm::StringRef& match_str) const;
107 GetMatchSpanningIndices (const char* s, uint32_t idx1, uint32_t idx2, llvm::StringRef& match_str) const;
111 std::vector<regmatch_t> m_matches; ///< Where parenthesized subexpressions results are stored
113 //------------------------------------------------------------------
114 /// Default constructor.
116 /// The default constructor that initializes the object state such
117 /// that it contains no compiled regular expression.
118 //------------------------------------------------------------------
119 RegularExpression ();
121 //------------------------------------------------------------------
122 /// Constructor that takes a regulare expression with flags.
124 /// Constructor that compiles \a re using \a flags and stores the
125 /// resulting compiled regular expression into this object.
128 /// A c string that represents the regular expression to
132 /// Flags that are passed the the \c regcomp() function.
133 //------------------------------------------------------------------
135 RegularExpression (const char* re, int flags);
137 // This one uses flags = REG_EXTENDED.
139 RegularExpression (const char* re);
141 //------------------------------------------------------------------
144 /// Any previosuly compiled regular expression contained in this
145 /// object will be freed.
146 //------------------------------------------------------------------
147 ~RegularExpression ();
149 RegularExpression (const RegularExpression &rhs);
151 const RegularExpression & operator=(const RegularExpression &rhs);
153 //------------------------------------------------------------------
154 /// Compile a regular expression.
156 /// Compile a regular expression using the supplied regular
157 /// expression text and flags. The compied regular expression lives
158 /// in this object so that it can be readily used for regular
159 /// expression matches. Execute() can be called after the regular
160 /// expression is compiled. Any previosuly compiled regular
161 /// expression contained in this object will be freed.
164 /// A NULL terminated C string that represents the regular
165 /// expression to compile.
168 /// Flags that are passed the the \c regcomp() function.
171 /// \b true if the regular expression compiles successfully,
172 /// \b false otherwise.
173 //------------------------------------------------------------------
175 Compile (const char* re);
178 Compile (const char* re, int flags);
180 //------------------------------------------------------------------
181 /// Executes a regular expression.
183 /// Execute a regular expression match using the compiled regular
184 /// expression that is already in this object against the match
185 /// string \a s. If any parens are used for regular expression
186 /// matches \a match_count should indicate the number of regmatch_t
187 /// values that are present in \a match_ptr. The regular expression
188 /// will be executed using the \a execute_flags
190 /// @param[in] string
191 /// The string to match against the compile regular expression.
194 /// A pointer to a RegularExpression::Match structure that was
195 /// properly initialized with the desired number of maximum
196 /// matches, or NULL if no parenthesized matching is needed.
198 /// @param[in] execute_flags
199 /// Flags to pass to the \c regexec() function.
202 /// \b true if \a string matches the compiled regular
203 /// expression, \b false otherwise.
204 //------------------------------------------------------------------
206 Execute (const char* string, Match *match = NULL, int execute_flags = 0) const;
209 GetErrorAsCString (char *err_str, size_t err_str_max_len) const;
211 //------------------------------------------------------------------
212 /// Free the compiled regular expression.
214 /// If this object contains a valid compiled regular expression,
215 /// this function will free any resources it was consuming.
216 //------------------------------------------------------------------
220 //------------------------------------------------------------------
221 /// Access the regular expression text.
223 /// Returns the text that was used to compile the current regular
227 /// The NULL terminated C string that was used to compile the
228 /// current regular expression
229 //------------------------------------------------------------------
234 GetCompileFlags () const
236 return m_compile_flags;
239 //------------------------------------------------------------------
242 /// Test if this object contains a valid regular expression.
245 /// \b true if the regular expression compiled and is ready
246 /// for execution, \b false otherwise.
247 //------------------------------------------------------------------
267 operator < (const RegularExpression& rhs) const;
270 //------------------------------------------------------------------
272 //------------------------------------------------------------------
273 std::string m_re; ///< A copy of the original regular expression text
274 int m_comp_err; ///< Error code for the regular expression compilation
275 regex_t m_preg; ///< The compiled regular expression
276 int m_compile_flags; ///< Stores the flags from the last compile.
279 } // namespace lldb_private
281 #endif // #if defined(__cplusplus)
282 #endif // liblldb_DBRegex_h_