RegularExpression.h revision 344779
150476Speter//===-- RegularExpression.h -------------------------------------*- C++ -*-===//
21802Sphk//
31802Sphk//                     The LLVM Compiler Infrastructure
4119071Sobrien//
544290Swollman// This file is distributed under the University of Illinois Open Source
644301Swollman// License. See LICENSE.TXT for details.
7143334Scperciva//
8143334Scperciva//===----------------------------------------------------------------------===//
9143334Scperciva
1055955Srgrimes#ifndef liblldb_RegularExpression_h_
11201381Sed#define liblldb_RegularExpression_h_
12201381Sed
13143334Scperciva#ifdef _WIN32
149488Sphk#include "../lib/Support/regex_impl.h"
1574385Sphk
1674385Sphktypedef llvm_regmatch_t regmatch_t;
179488Sphktypedef llvm_regex_t regex_t;
1874385Sphk
1974385Sphkinline int regcomp(llvm_regex_t *a, const char *b, int c) {
209488Sphk  return llvm_regcomp(a, b, c);
2174385Sphk}
2274385Sphk
2344437Sacheinline size_t regerror(int a, const llvm_regex_t *b, char *c, size_t d) {
2444437Sache  return llvm_regerror(a, b, c, d);
2544437Sache}
2674385Sphk
2744437Sacheinline int regexec(const llvm_regex_t *a, const char *b, size_t c,
2874385Sphk                   llvm_regmatch_t d[], int e) {
2974385Sphk  return llvm_regexec(a, b, c, d, e);
3044437Sache}
3174385Sphk
3274385Sphkinline void regfree(llvm_regex_t *a) { llvm_regfree(a); }
33143334Scperciva#else
34143334Scperciva#ifdef __ANDROID__
35143334Scperciva#include <regex>
36143334Scperciva#endif
3744290Swollman#include <regex.h>
3844301Swollman#endif
39143334Scperciva
40143334Scperciva#include <string>
411846Swollman#include <vector>
4244301Swollman
431802Sphk#include <stddef.h>
4444290Swollman#include <stdint.h>
4544301Swollman
4644290Swollmannamespace llvm {
4744290Swollmanclass StringRef;
4844301Swollman} // namespace llvm
4944301Swollman
5044301Swollmannamespace lldb_private {
5144301Swollman
5244290Swollman//----------------------------------------------------------------------
531802Sphk/// @class RegularExpression RegularExpression.h
5444290Swollman/// "lldb/Utility/RegularExpression.h"
5544290Swollman/// A C++ wrapper class for regex.
561802Sphk///
571802Sphk/// This regular expression class wraps the posix regex functions \c
5844290Swollman/// regcomp(), \c regerror(), \c regexec(), and \c regfree() from the header
5944290Swollman/// file in \c /usr/include/regex\.h.
601802Sphk//----------------------------------------------------------------------
611802Sphkclass RegularExpression {
6244290Swollmanpublic:
6344290Swollman  class Match {
641802Sphk  public:
6544290Swollman    Match(uint32_t max_matches) : m_matches() {
6644290Swollman      if (max_matches > 0)
6744290Swollman        m_matches.resize(max_matches + 1);
6844290Swollman    }
6944290Swollman
7044290Swollman    void Clear() {
7144290Swollman      const size_t num_matches = m_matches.size();
7244290Swollman      regmatch_t invalid_match = {-1, -1};
7344290Swollman      for (size_t i = 0; i < num_matches; ++i)
7444290Swollman        m_matches[i] = invalid_match;
75143334Scperciva    }
76143334Scperciva
77143334Scperciva    size_t GetSize() const { return m_matches.size(); }
78143334Scperciva
79143334Scperciva    regmatch_t *GetData() {
80143334Scperciva      return (m_matches.empty() ? nullptr : m_matches.data());
8144301Swollman    }
8244301Swollman
8344301Swollman    bool GetMatchAtIndex(llvm::StringRef s, uint32_t idx,
8444301Swollman                         std::string &match_str) const;
8544301Swollman
8644301Swollman    bool GetMatchAtIndex(llvm::StringRef s, uint32_t idx,
8794367Sru                         llvm::StringRef &match_str) const;
8894367Sru
8994367Sru    bool GetMatchSpanningIndices(llvm::StringRef s, uint32_t idx1,
9094367Sru                                 uint32_t idx2,
9194367Sru                                 llvm::StringRef &match_str) const;
921802Sphk
931802Sphk  protected:
9439063Simp    std::vector<regmatch_t>
9539063Simp        m_matches; ///< Where parenthesized subexpressions results are stored
9639063Simp  };
9739063Simp
9839063Simp  //------------------------------------------------------------------
9939063Simp  /// Default constructor.
10039063Simp  ///
10139063Simp  /// The default constructor that initializes the object state such that it
1021802Sphk  /// contains no compiled regular expression.
1031802Sphk  //------------------------------------------------------------------
10439063Simp  RegularExpression();
10539063Simp
10639063Simp  explicit RegularExpression(llvm::StringRef string);
10739063Simp
10839063Simp  //------------------------------------------------------------------
10939063Simp  /// Destructor.
11039063Simp  ///
11139063Simp  /// Any previously compiled regular expression contained in this object will
1121802Sphk  /// be freed.
1131802Sphk  //------------------------------------------------------------------
11439063Simp  ~RegularExpression();
11539063Simp
11639063Simp  RegularExpression(const RegularExpression &rhs);
11739063Simp
11839063Simp  const RegularExpression &operator=(const RegularExpression &rhs);
11939063Simp
12039063Simp  //------------------------------------------------------------------
12139063Simp  /// Compile a regular expression.
1221802Sphk  ///
12344290Swollman  /// Compile a regular expression using the supplied regular expression text.
12444290Swollman  /// The compiled regular expression lives in this object so that it can be
12544290Swollman  /// readily used for regular expression matches. Execute() can be called
12644290Swollman  /// after the regular expression is compiled. Any previously compiled
12744290Swollman  /// regular expression contained in this object will be freed.
12844290Swollman  ///
12944290Swollman  /// @param[in] re
13044290Swollman  ///     A NULL terminated C string that represents the regular
13144290Swollman  ///     expression to compile.
13244290Swollman  ///
13344290Swollman  /// @return
13444290Swollman  ///     \b true if the regular expression compiles successfully,
13544290Swollman  ///     \b false otherwise.
13644290Swollman  //------------------------------------------------------------------
13744290Swollman  bool Compile(llvm::StringRef string);
13844290Swollman  bool Compile(const char *) = delete;
13944290Swollman
14044290Swollman  //------------------------------------------------------------------
14144290Swollman  /// Executes a regular expression.
14244290Swollman  ///
14344290Swollman  /// Execute a regular expression match using the compiled regular expression
14444290Swollman  /// that is already in this object against the match string \a s. If any
14544290Swollman  /// parens are used for regular expression matches \a match_count should
14644290Swollman  /// indicate the number of regmatch_t values that are present in \a
14744290Swollman  /// match_ptr.
14844290Swollman  ///
149143334Scperciva  /// @param[in] string
150143334Scperciva  ///     The string to match against the compile regular expression.
151143334Scperciva  ///
152143334Scperciva  /// @param[in] match
153143334Scperciva  ///     A pointer to a RegularExpression::Match structure that was
154143334Scperciva  ///     properly initialized with the desired number of maximum
155143334Scperciva  ///     matches, or nullptr if no parenthesized matching is needed.
156143334Scperciva  ///
157143334Scperciva  /// @return
15844301Swollman  ///     \b true if \a string matches the compiled regular
15944301Swollman  ///     expression, \b false otherwise.
16044301Swollman  //------------------------------------------------------------------
16144301Swollman  bool Execute(llvm::StringRef string, Match *match = nullptr) const;
16244301Swollman  bool Execute(const char *, Match * = nullptr) = delete;
16344301Swollman
16444301Swollman  size_t GetErrorAsCString(char *err_str, size_t err_str_max_len) const;
16544301Swollman
16644301Swollman  //------------------------------------------------------------------
16744301Swollman  /// Free the compiled regular expression.
16844301Swollman  ///
16944301Swollman  /// If this object contains a valid compiled regular expression, this
17044301Swollman  /// function will free any resources it was consuming.
171143334Scperciva  //------------------------------------------------------------------
1722368Sbde  void Free();
1732368Sbde
174185568Sphk  //------------------------------------------------------------------
1751964Sjkh  /// Access the regular expression text.
1762368Sbde  ///
177185568Sphk  /// Returns the text that was used to compile the current regular
1781964Sjkh  /// expression.
1792368Sbde  ///
180185568Sphk  /// @return
1811964Sjkh  ///     The NULL terminated C string that was used to compile the
1822368Sbde  ///     current regular expression
1831802Sphk  //------------------------------------------------------------------
184185568Sphk  llvm::StringRef GetText() const;
18544301Swollman
18644301Swollman  //------------------------------------------------------------------
18744301Swollman  /// Test if valid.
188185568Sphk  ///
18944290Swollman  /// Test if this object contains a valid regular expression.
19044290Swollman  ///
191185568Sphk  /// @return
19244290Swollman  ///     \b true if the regular expression compiled and is ready
19344290Swollman  ///     for execution, \b false otherwise.
194185568Sphk  //------------------------------------------------------------------
195143334Scperciva  bool IsValid() const;
196143334Scperciva
19744290Swollman  void Clear() {
1981802Sphk    Free();
1991802Sphk    m_re.clear();
200    m_comp_err = 1;
201  }
202
203  int GetErrorCode() const { return m_comp_err; }
204
205  bool operator<(const RegularExpression &rhs) const;
206
207private:
208  //------------------------------------------------------------------
209  // Member variables
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
214};
215
216} // namespace lldb_private
217
218#endif // liblldb_RegularExpression_h_
219