RegularExpression.h revision 353358
1//===-- RegularExpression.h -------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef liblldb_RegularExpression_h_
10#define liblldb_RegularExpression_h_
11
12#ifdef _WIN32
13#include "../lib/Support/regex_impl.h"
14
15typedef llvm_regmatch_t regmatch_t;
16typedef llvm_regex_t regex_t;
17
18inline int regcomp(llvm_regex_t *a, const char *b, int c) {
19  return llvm_regcomp(a, b, c);
20}
21
22inline size_t regerror(int a, const llvm_regex_t *b, char *c, size_t d) {
23  return llvm_regerror(a, b, c, d);
24}
25
26inline int regexec(const llvm_regex_t *a, const char *b, size_t c,
27                   llvm_regmatch_t d[], int e) {
28  return llvm_regexec(a, b, c, d, e);
29}
30
31inline void regfree(llvm_regex_t *a) { llvm_regfree(a); }
32#else
33#ifdef __ANDROID__
34#include <regex>
35#endif
36#include <regex.h>
37#endif
38
39#include <string>
40#include <vector>
41
42#include <stddef.h>
43#include <stdint.h>
44
45namespace llvm {
46class StringRef;
47} // namespace llvm
48
49namespace lldb_private {
50
51/// \class RegularExpression RegularExpression.h
52/// "lldb/Utility/RegularExpression.h"
53/// A C++ wrapper class for regex.
54///
55/// This regular expression class wraps the posix regex functions \c
56/// regcomp(), \c regerror(), \c regexec(), and \c regfree() from the header
57/// file in \c /usr/include/regex\.h.
58class RegularExpression {
59public:
60  class Match {
61  public:
62    Match(uint32_t max_matches) : m_matches() {
63      if (max_matches > 0)
64        m_matches.resize(max_matches + 1);
65    }
66
67    void Clear() {
68      const size_t num_matches = m_matches.size();
69      regmatch_t invalid_match = {-1, -1};
70      for (size_t i = 0; i < num_matches; ++i)
71        m_matches[i] = invalid_match;
72    }
73
74    size_t GetSize() const { return m_matches.size(); }
75
76    regmatch_t *GetData() {
77      return (m_matches.empty() ? nullptr : m_matches.data());
78    }
79
80    bool GetMatchAtIndex(llvm::StringRef s, uint32_t idx,
81                         std::string &match_str) const;
82
83    bool GetMatchAtIndex(llvm::StringRef s, uint32_t idx,
84                         llvm::StringRef &match_str) const;
85
86    bool GetMatchSpanningIndices(llvm::StringRef s, uint32_t idx1,
87                                 uint32_t idx2,
88                                 llvm::StringRef &match_str) const;
89
90  protected:
91    std::vector<regmatch_t>
92        m_matches; ///< Where parenthesized subexpressions results are stored
93  };
94
95  /// Default constructor.
96  ///
97  /// The default constructor that initializes the object state such that it
98  /// contains no compiled regular expression.
99  RegularExpression();
100
101  explicit RegularExpression(llvm::StringRef string);
102
103  /// Destructor.
104  ///
105  /// Any previously compiled regular expression contained in this object will
106  /// be freed.
107  ~RegularExpression();
108
109  RegularExpression(const RegularExpression &rhs);
110
111  const RegularExpression &operator=(const RegularExpression &rhs);
112
113  /// Compile a regular expression.
114  ///
115  /// Compile a regular expression using the supplied regular expression text.
116  /// The compiled regular expression lives in this object so that it can be
117  /// readily used for regular expression matches. Execute() can be called
118  /// after the regular expression is compiled. Any previously compiled
119  /// regular expression contained in this object will be freed.
120  ///
121  /// \param[in] re
122  ///     A NULL terminated C string that represents the regular
123  ///     expression to compile.
124  ///
125  /// \return
126  ///     \b true if the regular expression compiles successfully,
127  ///     \b false otherwise.
128  bool Compile(llvm::StringRef string);
129  bool Compile(const char *) = delete;
130
131  /// Executes a regular expression.
132  ///
133  /// Execute a regular expression match using the compiled regular expression
134  /// that is already in this object against the match string \a s. If any
135  /// parens are used for regular expression matches \a match_count should
136  /// indicate the number of regmatch_t values that are present in \a
137  /// match_ptr.
138  ///
139  /// \param[in] string
140  ///     The string to match against the compile regular expression.
141  ///
142  /// \param[in] match
143  ///     A pointer to a RegularExpression::Match structure that was
144  ///     properly initialized with the desired number of maximum
145  ///     matches, or nullptr if no parenthesized matching is needed.
146  ///
147  /// \return
148  ///     \b true if \a string matches the compiled regular
149  ///     expression, \b false otherwise.
150  bool Execute(llvm::StringRef string, Match *match = nullptr) const;
151  bool Execute(const char *, Match * = nullptr) = delete;
152
153  size_t GetErrorAsCString(char *err_str, size_t err_str_max_len) const;
154
155  /// Free the compiled regular expression.
156  ///
157  /// If this object contains a valid compiled regular expression, this
158  /// function will free any resources it was consuming.
159  void Free();
160
161  /// Access the regular expression text.
162  ///
163  /// Returns the text that was used to compile the current regular
164  /// expression.
165  ///
166  /// \return
167  ///     The NULL terminated C string that was used to compile the
168  ///     current regular expression
169  llvm::StringRef GetText() const;
170
171  /// Test if valid.
172  ///
173  /// Test if this object contains a valid regular expression.
174  ///
175  /// \return
176  ///     \b true if the regular expression compiled and is ready
177  ///     for execution, \b false otherwise.
178  bool IsValid() const;
179
180  void Clear() {
181    Free();
182    m_re.clear();
183    m_comp_err = 1;
184  }
185
186  int GetErrorCode() const { return m_comp_err; }
187
188  bool operator<(const RegularExpression &rhs) const;
189
190private:
191  // Member variables
192  std::string m_re; ///< A copy of the original regular expression text
193  int m_comp_err;   ///< Status code for the regular expression compilation
194  regex_t m_preg;   ///< The compiled regular expression
195};
196
197} // namespace lldb_private
198
199#endif // liblldb_RegularExpression_h_
200