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) {
20 return llvm_regcomp(a, b, c);
23 inline size_t regerror(int a, const llvm_regex_t *b, char *c, size_t d) {
24 return llvm_regerror(a, b, c, d);
27 inline int regexec(const llvm_regex_t *a, const char *b, size_t c,
28 llvm_regmatch_t d[], int e) {
29 return llvm_regexec(a, b, c, d, e);
32 inline void regfree(llvm_regex_t *a) { llvm_regfree(a); }
50 namespace lldb_private {
52 //----------------------------------------------------------------------
53 /// @class RegularExpression RegularExpression.h
54 /// "lldb/Utility/RegularExpression.h"
55 /// A C++ wrapper class for regex.
57 /// This regular expression class wraps the posix regex functions \c
58 /// regcomp(), \c regerror(), \c regexec(), and \c regfree() from the header
59 /// file in \c /usr/include/regex\.h.
60 //----------------------------------------------------------------------
61 class RegularExpression {
65 Match(uint32_t max_matches) : m_matches() {
67 m_matches.resize(max_matches + 1);
71 const size_t num_matches = m_matches.size();
72 regmatch_t invalid_match = {-1, -1};
73 for (size_t i = 0; i < num_matches; ++i)
74 m_matches[i] = invalid_match;
77 size_t GetSize() const { return m_matches.size(); }
79 regmatch_t *GetData() {
80 return (m_matches.empty() ? nullptr : m_matches.data());
83 bool GetMatchAtIndex(llvm::StringRef s, uint32_t idx,
84 std::string &match_str) const;
86 bool GetMatchAtIndex(llvm::StringRef s, uint32_t idx,
87 llvm::StringRef &match_str) const;
89 bool GetMatchSpanningIndices(llvm::StringRef s, uint32_t idx1,
91 llvm::StringRef &match_str) const;
94 std::vector<regmatch_t>
95 m_matches; ///< Where parenthesized subexpressions results are stored
98 //------------------------------------------------------------------
99 /// Default constructor.
101 /// The default constructor that initializes the object state such that it
102 /// contains no compiled regular expression.
103 //------------------------------------------------------------------
106 explicit RegularExpression(llvm::StringRef string);
108 //------------------------------------------------------------------
111 /// Any previously compiled regular expression contained in this object will
113 //------------------------------------------------------------------
114 ~RegularExpression();
116 RegularExpression(const RegularExpression &rhs);
118 const RegularExpression &operator=(const RegularExpression &rhs);
120 //------------------------------------------------------------------
121 /// Compile a regular expression.
123 /// Compile a regular expression using the supplied regular expression text.
124 /// The compiled regular expression lives in this object so that it can be
125 /// readily used for regular expression matches. Execute() can be called
126 /// after the regular expression is compiled. Any previously compiled
127 /// regular expression contained in this object will be freed.
130 /// A NULL terminated C string that represents the regular
131 /// expression to compile.
134 /// \b true if the regular expression compiles successfully,
135 /// \b false otherwise.
136 //------------------------------------------------------------------
137 bool Compile(llvm::StringRef string);
138 bool Compile(const char *) = delete;
140 //------------------------------------------------------------------
141 /// Executes a regular expression.
143 /// Execute a regular expression match using the compiled regular expression
144 /// that is already in this object against the match string \a s. If any
145 /// parens are used for regular expression matches \a match_count should
146 /// indicate the number of regmatch_t values that are present in \a
149 /// @param[in] string
150 /// The string to match against the compile regular expression.
153 /// A pointer to a RegularExpression::Match structure that was
154 /// properly initialized with the desired number of maximum
155 /// matches, or nullptr if no parenthesized matching is needed.
158 /// \b true if \a string matches the compiled regular
159 /// expression, \b false otherwise.
160 //------------------------------------------------------------------
161 bool Execute(llvm::StringRef string, Match *match = nullptr) const;
162 bool Execute(const char *, Match * = nullptr) = delete;
164 size_t GetErrorAsCString(char *err_str, size_t err_str_max_len) const;
166 //------------------------------------------------------------------
167 /// Free the compiled regular expression.
169 /// If this object contains a valid compiled regular expression, this
170 /// function will free any resources it was consuming.
171 //------------------------------------------------------------------
174 //------------------------------------------------------------------
175 /// Access the regular expression text.
177 /// Returns the text that was used to compile the current regular
181 /// The NULL terminated C string that was used to compile the
182 /// current regular expression
183 //------------------------------------------------------------------
184 llvm::StringRef GetText() const;
186 //------------------------------------------------------------------
189 /// Test if this object contains a valid regular expression.
192 /// \b true if the regular expression compiled and is ready
193 /// for execution, \b false otherwise.
194 //------------------------------------------------------------------
195 bool IsValid() const;
203 int GetErrorCode() const { return m_comp_err; }
205 bool operator<(const RegularExpression &rhs) const;
208 //------------------------------------------------------------------
210 //------------------------------------------------------------------
211 std::string m_re; ///< A copy of the original regular expression text
212 int m_comp_err; ///< Status code for the regular expression compilation
213 regex_t m_preg; ///< The compiled regular expression
216 } // namespace lldb_private
218 #endif // liblldb_RegularExpression_h_