LLDB  mainline
RegularExpression.h
Go to the documentation of this file.
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 
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  explicit RegularExpression(llvm::StringRef string);
35 
36  ~RegularExpression() = default;
37 
39  RegularExpression(RegularExpression &&rhs) = default;
40 
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 
85 private:
86  /// A copy of the original regular expression text.
88  /// The compiled regular expression.
89  mutable llvm::Regex m_regex;
90 };
91 
92 } // namespace lldb_private
93 
94 #endif // LLDB_UTILITY_REGULAREXPRESSION_H
lldb_private::RegularExpression
Definition: RegularExpression.h:18
lldb_private::RegularExpression::operator==
bool operator==(const RegularExpression &rhs) const
Definition: RegularExpression.h:81
lldb_private::RegularExpression::operator=
RegularExpression & operator=(RegularExpression &&rhs)=default
lldb_private::RegularExpression::m_regex_text
std::string m_regex_text
A copy of the original regular expression text.
Definition: RegularExpression.h:87
lldb_private::RegularExpression::IsValid
bool IsValid() const
Test if this object contains a valid regular expression.
Definition: RegularExpression.cpp:31
lldb_private::RegularExpression::m_regex
llvm::Regex m_regex
The compiled regular expression.
Definition: RegularExpression.h:89
lldb_private::RegularExpression::RegularExpression
RegularExpression()=default
The default constructor that initializes the object state such that it contains no compiled regular e...
lldb_private::RegularExpression::~RegularExpression
~RegularExpression()=default
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::RegularExpression::GetError
llvm::Error GetError() const
Return an error if the regular expression failed to compile.
Definition: RegularExpression.cpp:35
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
Error
llvm::Error Error
Definition: UdtRecordCompleter.cpp:29
lldb_private::RegularExpression::Execute
bool Execute(llvm::StringRef string, llvm::SmallVectorImpl< llvm::StringRef > *matches=nullptr) const
Execute a regular expression match using the compiled regular expression that is already in this obje...
Definition: RegularExpression.cpp:23
llvm::SmallVectorImpl
Definition: Disassembler.h:42
lldb_private::RegularExpression::GetText
llvm::StringRef GetText() const
Access the regular expression text.
Definition: RegularExpression.cpp:33