RegularExpression.h revision 73844aa19a7360b662e2be710fc3c969d6c86606
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_DBRegex_h_ 11#define liblldb_DBRegex_h_ 12#if defined(__cplusplus) 13 14#include <regex.h> 15#include <stdint.h> 16 17#include <string> 18#include <vector> 19 20namespace lldb_private { 21 22//---------------------------------------------------------------------- 23/// @class RegularExpression RegularExpression.h "lldb/Core/RegularExpression.h" 24/// @brief A C++ wrapper class for regex. 25/// 26/// This regular expression class wraps the posix regex functions 27/// \c regcomp(), \c regerror(), \c regexec(), and \c regfree() from 28/// the header file in \c /usr/include/regex\.h. 29//---------------------------------------------------------------------- 30class RegularExpression 31{ 32public: 33 //------------------------------------------------------------------ 34 /// Default constructor. 35 /// 36 /// The default constructor that initializes the object state such 37 /// that it contains no compiled regular expression. 38 //------------------------------------------------------------------ 39 RegularExpression (); 40 41 //------------------------------------------------------------------ 42 /// Constructor that takes a regulare expression with flags. 43 /// 44 /// Constructor that compiles \a re using \a flags and stores the 45 /// resulting compiled regular expression into this object. 46 /// 47 /// @param[in] re 48 /// A c string that represents the regular expression to 49 /// compile. 50 /// 51 /// @param[in] flags 52 /// Flags that are passed the the \c regcomp() function. 53 //------------------------------------------------------------------ 54 explicit 55 RegularExpression (const char* re, int flags); 56 57 // This one uses flags = REG_EXTENDED. 58 explicit 59 RegularExpression (const char* re); 60 61 //------------------------------------------------------------------ 62 /// Destructor. 63 /// 64 /// Any previosuly compiled regular expression contained in this 65 /// object will be freed. 66 //------------------------------------------------------------------ 67 ~RegularExpression (); 68 69 RegularExpression (const RegularExpression &rhs); 70 71 const RegularExpression & operator=(const RegularExpression &rhs); 72 73 //------------------------------------------------------------------ 74 /// Compile a regular expression. 75 /// 76 /// Compile a regular expression using the supplied regular 77 /// expression text and flags. The compied regular expression lives 78 /// in this object so that it can be readily used for regular 79 /// expression matches. Execute() can be called after the regular 80 /// expression is compiled. Any previosuly compiled regular 81 /// expression contained in this object will be freed. 82 /// 83 /// @param[in] re 84 /// A NULL terminated C string that represents the regular 85 /// expression to compile. 86 /// 87 /// @param[in] flags 88 /// Flags that are passed the the \c regcomp() function. 89 /// 90 /// @return 91 /// \b true if the regular expression compiles successfully, 92 /// \b false otherwise. 93 //------------------------------------------------------------------ 94 bool 95 Compile (const char* re); 96 97 bool 98 Compile (const char* re, int flags); 99 100 //------------------------------------------------------------------ 101 /// Executes a regular expression. 102 /// 103 /// Execute a regular expression match using the compiled regular 104 /// expression that is already in this object against the match 105 /// string \a s. If any parens are used for regular expression 106 /// matches \a match_count should indicate the number of regmatch_t 107 /// values that are present in \a match_ptr. The regular expression 108 /// will be executed using the \a execute_flags 109 /// 110 /// @param[in] string 111 /// The string to match against the compile regular expression. 112 /// 113 /// @param[in] match_count 114 /// The number of regmatch_t objects in \a match_ptr 115 /// 116 /// @param[in] execute_flags 117 /// Flags to pass to the \c regexec() function. 118 /// 119 /// @return 120 /// \b true if \a string matches the compiled regular 121 /// expression, \b false otherwise. 122 //------------------------------------------------------------------ 123 bool 124 Execute (const char* string, size_t match_count = 0, int execute_flags = 0) const; 125 126 size_t 127 GetErrorAsCString (char *err_str, size_t err_str_max_len) const; 128 129 bool 130 GetMatchAtIndex (const char* s, uint32_t idx, std::string& match_str) const; 131 //------------------------------------------------------------------ 132 /// Free the compiled regular expression. 133 /// 134 /// If this object contains a valid compiled regular expression, 135 /// this function will free any resources it was consuming. 136 //------------------------------------------------------------------ 137 void 138 Free (); 139 140 //------------------------------------------------------------------ 141 /// Access the regular expression text. 142 /// 143 /// Returns the text that was used to compile the current regular 144 /// expression. 145 /// 146 /// @return 147 /// The NULL terminated C string that was used to compile the 148 /// current regular expression 149 //------------------------------------------------------------------ 150 const char* 151 GetText () const; 152 153 int 154 GetCompileFlags () const 155 { 156 return m_compile_flags; 157 } 158 159 //------------------------------------------------------------------ 160 /// Test if valid. 161 /// 162 /// Test if this object contains a valid regular expression. 163 /// 164 /// @return 165 /// \b true if the regular expression compiled and is ready 166 /// for execution, \b false otherwise. 167 //------------------------------------------------------------------ 168 bool 169 IsValid () const; 170 171 void 172 Clear () 173 { 174 Free(); 175 m_re.clear(); 176 m_compile_flags = 0; 177 m_comp_err = 1; 178 } 179 180 int 181 GetErrorCode() const 182 { 183 return m_comp_err; 184 } 185 186 bool 187 operator < (const RegularExpression& rhs) const; 188 189private: 190 //------------------------------------------------------------------ 191 // Member variables 192 //------------------------------------------------------------------ 193 std::string m_re; ///< A copy of the original regular expression text 194 int m_comp_err; ///< Error code for the regular expression compilation 195 regex_t m_preg; ///< The compiled regular expression 196 int m_compile_flags; ///< Stores the flags from the last compile. 197 mutable std::vector<regmatch_t> m_matches; ///< Where parenthesized subexpressions results are stored 198 199}; 200 201} // namespace lldb_private 202 203#endif // #if defined(__cplusplus) 204#endif // liblldb_DBRegex_h_ 205