RegularExpression.h revision 360784
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#include "llvm/ADT/StringRef.h" 13#include "llvm/Support/Error.h" 14#include "llvm/Support/Regex.h" 15 16namespace lldb_private { 17 18class RegularExpression { 19public: 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 explicit RegularExpression(llvm::StringRef string); 35 36 ~RegularExpression() = default; 37 38 RegularExpression(const RegularExpression &rhs); 39 RegularExpression(RegularExpression &&rhs) = default; 40 41 RegularExpression &operator=(RegularExpression &&rhs) = default; 42 RegularExpression &operator=(const RegularExpression &rhs) = default; 43 44 /// Execute a regular expression match using the compiled regular expression 45 /// that is already in this object against the given \a string. If any parens 46 /// are used for regular expression matches. 47 /// 48 /// \param[in] string 49 /// The string to match against the compile regular expression. 50 /// 51 /// \param[out] matches 52 /// A pointer to a SmallVector to hold the matches. 53 /// 54 /// \return 55 /// true if \a string matches the compiled regular expression, false 56 /// otherwise incl. the case regular exression failed to compile. 57 bool Execute(llvm::StringRef string, 58 llvm::SmallVectorImpl<llvm::StringRef> *matches = nullptr) const; 59 60 /// Access the regular expression text. 61 /// 62 /// \return 63 /// The NULL terminated C string that was used to compile the 64 /// current regular expression 65 llvm::StringRef GetText() const; 66 67 /// Test if this object contains a valid regular expression. 68 /// 69 /// \return 70 /// true if the regular expression compiled and is ready for execution, 71 /// false otherwise. 72 bool IsValid() const; 73 74 /// Return an error if the regular expression failed to compile. 75 /// 76 /// \return 77 /// A string error if the regular expression failed to compile, success 78 /// otherwise. 79 llvm::Error GetError() const; 80 81 bool operator==(const RegularExpression &rhs) const { 82 return GetText() == rhs.GetText(); 83 } 84 85private: 86 /// A copy of the original regular expression text. 87 std::string m_regex_text; 88 /// The compiled regular expression. 89 mutable llvm::Regex m_regex; 90}; 91 92} // namespace lldb_private 93 94#endif // liblldb_RegularExpression_h_ 95