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 LLDB_UTILITY_REGULAREXPRESSION_H 10 #define LLDB_UTILITY_REGULAREXPRESSION_H 11 12 #include "llvm/ADT/StringRef.h" 13 #include "llvm/Support/Error.h" 14 #include "llvm/Support/Regex.h" 15 16 namespace lldb_private { 17 18 class RegularExpression { 19 public: 20 /// The default constructor that initializes the object state such that it 21 /// contains no compiled regular expression. 22 RegularExpression() = default; 23 24 /// Constructor for a regular expression. 25 /// 26 /// Compile a regular expression using the supplied regular expression text. 27 /// The compiled regular expression lives in this object so that it can be 28 /// readily used for regular expression matches. Execute() can be called 29 /// after the regular expression is compiled. 30 /// 31 /// \param[in] string 32 /// An llvm::StringRef that represents the regular expression to compile. 33 // String is not referenced anymore after the object is constructed. 34 // 35 /// \param[in] flags 36 /// An llvm::Regex::RegexFlags that modifies the matching behavior. The 37 /// default is NoFlags. 38 explicit RegularExpression( 39 llvm::StringRef string, 40 llvm::Regex::RegexFlags flags = llvm::Regex::NoFlags); 41 42 ~RegularExpression() = default; 43 44 RegularExpression(const RegularExpression &rhs); 45 RegularExpression(RegularExpression &&rhs) = default; 46 47 RegularExpression &operator=(RegularExpression &&rhs) = default; 48 RegularExpression &operator=(const RegularExpression &rhs) = default; 49 50 /// Execute a regular expression match using the compiled regular expression 51 /// that is already in this object against the given \a string. If any parens 52 /// are used for regular expression matches. 53 /// 54 /// \param[in] string 55 /// The string to match against the compile regular expression. 56 /// 57 /// \param[out] matches 58 /// A pointer to a SmallVector to hold the matches. 59 /// 60 /// \return 61 /// true if \a string matches the compiled regular expression, false 62 /// otherwise incl. the case regular exression failed to compile. 63 bool Execute(llvm::StringRef string, 64 llvm::SmallVectorImpl<llvm::StringRef> *matches = nullptr) const; 65 66 /// Access the regular expression text. 67 /// 68 /// \return 69 /// The NULL terminated C string that was used to compile the 70 /// current regular expression 71 llvm::StringRef GetText() const; 72 73 /// Test if this object contains a valid regular expression. 74 /// 75 /// \return 76 /// true if the regular expression compiled and is ready for execution, 77 /// false otherwise. 78 bool IsValid() const; 79 80 /// Return an error if the regular expression failed to compile. 81 /// 82 /// \return 83 /// A string error if the regular expression failed to compile, success 84 /// otherwise. 85 llvm::Error GetError() const; 86 87 bool operator==(const RegularExpression &rhs) const { 88 return GetText() == rhs.GetText(); 89 } 90 91 private: 92 /// A copy of the original regular expression text. 93 std::string m_regex_text; 94 /// The compiled regular expression. 95 mutable llvm::Regex m_regex; 96 }; 97 98 } // namespace lldb_private 99 100 #endif // LLDB_UTILITY_REGULAREXPRESSION_H 101