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