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)
25 namespace lldb_private {
27 //----------------------------------------------------------------------
28 /// @class RegularExpression RegularExpression.h "lldb/Core/RegularExpression.h"
29 /// @brief A C++ wrapper class for regex.
31 /// This regular expression class wraps the posix regex functions
32 /// \c regcomp(), \c regerror(), \c regexec(), and \c regfree() from
33 /// the header file in \c /usr/include/regex\.h.
34 //----------------------------------------------------------------------
35 class RegularExpression
41 Match (uint32_t max_matches) :
45 m_matches.resize(max_matches + 1);
51 const size_t num_matches = m_matches.size();
52 regmatch_t invalid_match = { -1, -1 };
53 for (size_t i=0; i<num_matches; ++i)
54 m_matches[i] = invalid_match;
60 return m_matches.size();
66 if (m_matches.empty())
68 return m_matches.data();
72 GetMatchAtIndex (const char* s, uint32_t idx, std::string& match_str) const;
75 GetMatchAtIndex (const char* s, uint32_t idx, llvm::StringRef& match_str) const;
78 GetMatchSpanningIndices (const char* s, uint32_t idx1, uint32_t idx2, llvm::StringRef& match_str) const;
82 std::vector<regmatch_t> m_matches; ///< Where parenthesized subexpressions results are stored
84 //------------------------------------------------------------------
85 /// Default constructor.
87 /// The default constructor that initializes the object state such
88 /// that it contains no compiled regular expression.
89 //------------------------------------------------------------------
92 //------------------------------------------------------------------
93 /// Constructor that takes a regulare expression with flags.
95 /// Constructor that compiles \a re using \a flags and stores the
96 /// resulting compiled regular expression into this object.
99 /// A c string that represents the regular expression to
103 /// Flags that are passed the the \c regcomp() function.
104 //------------------------------------------------------------------
106 RegularExpression (const char* re, int flags);
108 // This one uses flags = REG_EXTENDED.
110 RegularExpression (const char* re);
112 //------------------------------------------------------------------
115 /// Any previosuly compiled regular expression contained in this
116 /// object will be freed.
117 //------------------------------------------------------------------
118 ~RegularExpression ();
120 RegularExpression (const RegularExpression &rhs);
122 const RegularExpression & operator=(const RegularExpression &rhs);
124 //------------------------------------------------------------------
125 /// Compile a regular expression.
127 /// Compile a regular expression using the supplied regular
128 /// expression text and flags. The compied regular expression lives
129 /// in this object so that it can be readily used for regular
130 /// expression matches. Execute() can be called after the regular
131 /// expression is compiled. Any previosuly compiled regular
132 /// expression contained in this object will be freed.
135 /// A NULL terminated C string that represents the regular
136 /// expression to compile.
139 /// Flags that are passed the the \c regcomp() function.
142 /// \b true if the regular expression compiles successfully,
143 /// \b false otherwise.
144 //------------------------------------------------------------------
146 Compile (const char* re);
149 Compile (const char* re, int flags);
151 //------------------------------------------------------------------
152 /// Executes a regular expression.
154 /// Execute a regular expression match using the compiled regular
155 /// expression that is already in this object against the match
156 /// string \a s. If any parens are used for regular expression
157 /// matches \a match_count should indicate the number of regmatch_t
158 /// values that are present in \a match_ptr. The regular expression
159 /// will be executed using the \a execute_flags
161 /// @param[in] string
162 /// The string to match against the compile regular expression.
165 /// A pointer to a RegularExpression::Match structure that was
166 /// properly initialized with the desired number of maximum
167 /// matches, or NULL if no parenthesized matching is needed.
169 /// @param[in] execute_flags
170 /// Flags to pass to the \c regexec() function.
173 /// \b true if \a string matches the compiled regular
174 /// expression, \b false otherwise.
175 //------------------------------------------------------------------
177 Execute (const char* string, Match *match = NULL, int execute_flags = 0) const;
180 GetErrorAsCString (char *err_str, size_t err_str_max_len) const;
182 //------------------------------------------------------------------
183 /// Free the compiled regular expression.
185 /// If this object contains a valid compiled regular expression,
186 /// this function will free any resources it was consuming.
187 //------------------------------------------------------------------
191 //------------------------------------------------------------------
192 /// Access the regular expression text.
194 /// Returns the text that was used to compile the current regular
198 /// The NULL terminated C string that was used to compile the
199 /// current regular expression
200 //------------------------------------------------------------------
205 GetCompileFlags () const
207 return m_compile_flags;
210 //------------------------------------------------------------------
213 /// Test if this object contains a valid regular expression.
216 /// \b true if the regular expression compiled and is ready
217 /// for execution, \b false otherwise.
218 //------------------------------------------------------------------
238 operator < (const RegularExpression& rhs) const;
241 //------------------------------------------------------------------
243 //------------------------------------------------------------------
244 std::string m_re; ///< A copy of the original regular expression text
245 int m_comp_err; ///< Error code for the regular expression compilation
246 regex_t m_preg; ///< The compiled regular expression
247 int m_compile_flags; ///< Stores the flags from the last compile.
250 } // namespace lldb_private
252 #endif // #if defined(__cplusplus)
253 #endif // liblldb_DBRegex_h_