1168404Spjd//===-- RegularExpression.h -------------------------------------*- C++ -*-===//
2168404Spjd//
3168404Spjd// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4168404Spjd// See https://llvm.org/LICENSE.txt for license information.
5168404Spjd// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6168404Spjd//
7168404Spjd//===----------------------------------------------------------------------===//
8168404Spjd
9168404Spjd#ifndef liblldb_RegularExpression_h_
10168404Spjd#define liblldb_RegularExpression_h_
11168404Spjd
12168404Spjd#include "llvm/ADT/StringRef.h"
13168404Spjd#include "llvm/Support/Error.h"
14168404Spjd#include "llvm/Support/Regex.h"
15168404Spjd
16168404Spjdnamespace lldb_private {
17168404Spjd
18168404Spjdclass RegularExpression {
19168404Spjdpublic:
20168404Spjd  /// The default constructor that initializes the object state such that it
21168404Spjd  /// contains no compiled regular expression.
22219089Spjd  RegularExpression() = default;
23277826Sdelphij
24321552Smav  /// Constructor for a regular expression.
25260835Sdelphij  ///
26286764Smav  /// Compile a regular expression using the supplied regular expression text.
27168404Spjd  /// The compiled regular expression lives in this object so that it can be
28168404Spjd  /// readily used for regular expression matches. Execute() can be called
29168404Spjd  /// after the regular expression is compiled.
30168404Spjd  ///
31168404Spjd  /// \param[in] string
32168404Spjd  ///     An llvm::StringRef that represents the regular expression to compile.
33168404Spjd  //      String is not referenced anymore after the object is constructed.
34168404Spjd  explicit RegularExpression(llvm::StringRef string);
35168404Spjd
36168404Spjd  ~RegularExpression() = default;
37168404Spjd
38168404Spjd  RegularExpression(const RegularExpression &rhs);
39168404Spjd  RegularExpression(RegularExpression &&rhs) = default;
40168404Spjd
41168404Spjd  RegularExpression &operator=(RegularExpression &&rhs) = default;
42168404Spjd  RegularExpression &operator=(const RegularExpression &rhs) = default;
43168404Spjd
44168404Spjd  /// Execute a regular expression match using the compiled regular expression
45168404Spjd  /// that is already in this object against the given \a string. If any parens
46168404Spjd  /// are used for regular expression matches.
47168404Spjd  ///
48168404Spjd  /// \param[in] string
49168404Spjd  ///     The string to match against the compile regular expression.
50168404Spjd  ///
51185029Spjd  /// \param[out] matches
52185029Spjd  ///     A pointer to a SmallVector to hold the matches.
53168404Spjd  ///
54168404Spjd  /// \return
55168404Spjd  ///     true if \a string matches the compiled regular expression, false
56168404Spjd  ///     otherwise incl. the case regular exression failed to compile.
57185029Spjd  bool Execute(llvm::StringRef string,
58168404Spjd               llvm::SmallVectorImpl<llvm::StringRef> *matches = nullptr) const;
59168404Spjd
60168404Spjd  /// Access the regular expression text.
61168404Spjd  ///
62251631Sdelphij  /// \return
63168404Spjd  ///     The NULL terminated C string that was used to compile the
64168404Spjd  ///     current regular expression
65168404Spjd  llvm::StringRef GetText() const;
66251631Sdelphij
67168404Spjd  /// Test if this object contains a valid regular expression.
68168404Spjd  ///
69168404Spjd  /// \return
70168404Spjd  ///     true if the regular expression compiled and is ready for execution,
71168404Spjd  ///     false otherwise.
72168404Spjd  bool IsValid() const;
73168404Spjd
74168404Spjd  /// Return an error if the regular expression failed to compile.
75168404Spjd  ///
76168404Spjd  /// \return
77168404Spjd  ///     A string error if the regular expression failed to compile, success
78168404Spjd  ///     otherwise.
79185029Spjd  llvm::Error GetError() const;
80321535Smav
81251631Sdelphij  bool operator==(const RegularExpression &rhs) const {
82168404Spjd    return GetText() == rhs.GetText();
83321535Smav  }
84168404Spjd
85286774Smavprivate:
86286774Smav  /// A copy of the original regular expression text.
87286774Smav  std::string m_regex_text;
88168404Spjd  /// The compiled regular expression.
89168404Spjd  mutable llvm::Regex m_regex;
90168404Spjd};
91168404Spjd
92168404Spjd} // namespace lldb_private
93168404Spjd
94168404Spjd#endif // liblldb_RegularExpression_h_
95168404Spjd