LLVM  9.0.0svn
Regex.h
Go to the documentation of this file.
1 //===-- Regex.h - Regular Expression matcher implementation -*- 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 // This file implements a POSIX regular expression matcher. Both Basic and
10 // Extended POSIX regular expressions (ERE) are supported. EREs were extended
11 // to support backreferences in matches.
12 // This implementation also supports matching strings with embedded NUL chars.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_SUPPORT_REGEX_H
17 #define LLVM_SUPPORT_REGEX_H
18 
19 #include <string>
20 
21 struct llvm_regex;
22 
23 namespace llvm {
24  class StringRef;
25  template<typename T> class SmallVectorImpl;
26 
27  class Regex {
28  public:
29  enum {
31  /// Compile for matching that ignores upper/lower case distinctions.
33  /// Compile for newline-sensitive matching. With this flag '[^' bracket
34  /// expressions and '.' never match newline. A ^ anchor matches the
35  /// null string after any newline in the string in addition to its normal
36  /// function, and the $ anchor matches the null string before any
37  /// newline in the string in addition to its normal function.
39  /// By default, the POSIX extended regular expression (ERE) syntax is
40  /// assumed. Pass this flag to turn on basic regular expressions (BRE)
41  /// instead.
43  };
44 
45  Regex();
46  /// Compiles the given regular expression \p Regex.
47  Regex(StringRef Regex, unsigned Flags = NoFlags);
48  Regex(const Regex &) = delete;
49  Regex &operator=(Regex regex) {
50  std::swap(preg, regex.preg);
51  std::swap(error, regex.error);
52  return *this;
53  }
54  Regex(Regex &&regex);
55  ~Regex();
56 
57  /// isValid - returns the error encountered during regex compilation, or
58  /// matching, if any.
59  bool isValid(std::string &Error) const;
60 
61  /// getNumMatches - In a valid regex, return the number of parenthesized
62  /// matches it contains. The number filled in by match will include this
63  /// many entries plus one for the whole regex (as element 0).
64  unsigned getNumMatches() const;
65 
66  /// matches - Match the regex against a given \p String.
67  ///
68  /// \param Matches - If given, on a successful match this will be filled in
69  /// with references to the matched group expressions (inside \p String),
70  /// the first group is always the entire pattern.
71  ///
72  /// This returns true on a successful match.
73  bool match(StringRef String, SmallVectorImpl<StringRef> *Matches = nullptr);
74 
75  /// sub - Return the result of replacing the first match of the regex in
76  /// \p String with the \p Repl string. Backreferences like "\0" in the
77  /// replacement string are replaced with the appropriate match substring.
78  ///
79  /// Note that the replacement string has backslash escaping performed on
80  /// it. Invalid backreferences are ignored (replaced by empty strings).
81  ///
82  /// \param Error If non-null, any errors in the substitution (invalid
83  /// backreferences, trailing backslashes) will be recorded as a non-empty
84  /// string.
85  std::string sub(StringRef Repl, StringRef String,
86  std::string *Error = nullptr);
87 
88  /// If this function returns true, ^Str$ is an extended regular
89  /// expression that matches Str and only Str.
90  static bool isLiteralERE(StringRef Str);
91 
92  /// Turn String into a regex by escaping its special characters.
93  static std::string escape(StringRef String);
94 
95  private:
96  struct llvm_regex *preg;
97  int error;
98  };
99 }
100 
101 #endif // LLVM_SUPPORT_REGEX_H
Compile for newline-sensitive matching.
Definition: Regex.h:38
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static std::string escape(StringRef String)
Turn String into a regex by escaping its special characters.
Definition: Regex.cpp:200
#define error(X)
By default, the POSIX extended regular expression (ERE) syntax is assumed.
Definition: Regex.h:42
static bool isLiteralERE(StringRef Str)
If this function returns true, ^Str$ is an extended regular expression that matches Str and only Str...
Definition: Regex.cpp:193
std::string sub(StringRef Repl, StringRef String, std::string *Error=nullptr)
sub - Return the result of replacing the first match of the regex in String with the Repl string...
Definition: Regex.cpp:114
Regex & operator=(Regex regex)
Definition: Regex.h:49
bool isValid(std::string &Error) const
isValid - returns the error encountered during regex compilation, or matching, if any...
Definition: Regex.cpp:55
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:940
Compile for matching that ignores upper/lower case distinctions.
Definition: Regex.h:32
unsigned getNumMatches() const
getNumMatches - In a valid regex, return the number of parenthesized matches it contains.
Definition: Regex.cpp:68
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
bool match(StringRef String, SmallVectorImpl< StringRef > *Matches=nullptr)
matches - Match the regex against a given String.
Definition: Regex.cpp:72
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48