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