LLVM  3.7.0
Regex.cpp
Go to the documentation of this file.
1 //===-- Regex.cpp - Regular Expression matcher implementation -------------===//
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.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Support/Regex.h"
15 #include "regex_impl.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Twine.h"
19 #include <string>
20 using namespace llvm;
21 
22 Regex::Regex(StringRef regex, unsigned Flags) {
23  unsigned flags = 0;
24  preg = new llvm_regex();
25  preg->re_endp = regex.end();
26  if (Flags & IgnoreCase)
27  flags |= REG_ICASE;
28  if (Flags & Newline)
29  flags |= REG_NEWLINE;
30  if (!(Flags & BasicRegex))
31  flags |= REG_EXTENDED;
32  error = llvm_regcomp(preg, regex.data(), flags|REG_PEND);
33 }
34 
36  if (preg) {
37  llvm_regfree(preg);
38  delete preg;
39  }
40 }
41 
42 bool Regex::isValid(std::string &Error) {
43  if (!error)
44  return true;
45 
46  size_t len = llvm_regerror(error, preg, nullptr, 0);
47 
48  Error.resize(len - 1);
49  llvm_regerror(error, preg, &Error[0], len);
50  return false;
51 }
52 
53 /// getNumMatches - In a valid regex, return the number of parenthesized
54 /// matches it contains.
55 unsigned Regex::getNumMatches() const {
56  return preg->re_nsub;
57 }
58 
60  unsigned nmatch = Matches ? preg->re_nsub+1 : 0;
61 
62  // pmatch needs to have at least one element.
64  pm.resize(nmatch > 0 ? nmatch : 1);
65  pm[0].rm_so = 0;
66  pm[0].rm_eo = String.size();
67 
68  int rc = llvm_regexec(preg, String.data(), nmatch, pm.data(), REG_STARTEND);
69 
70  if (rc == REG_NOMATCH)
71  return false;
72  if (rc != 0) {
73  // regexec can fail due to invalid pattern or running out of memory.
74  error = rc;
75  return false;
76  }
77 
78  // There was a match.
79 
80  if (Matches) { // match position requested
81  Matches->clear();
82 
83  for (unsigned i = 0; i != nmatch; ++i) {
84  if (pm[i].rm_so == -1) {
85  // this group didn't match
86  Matches->push_back(StringRef());
87  continue;
88  }
89  assert(pm[i].rm_eo >= pm[i].rm_so);
90  Matches->push_back(StringRef(String.data()+pm[i].rm_so,
91  pm[i].rm_eo-pm[i].rm_so));
92  }
93  }
94 
95  return true;
96 }
97 
99  std::string *Error) {
101 
102  // Reset error, if given.
103  if (Error && !Error->empty()) *Error = "";
104 
105  // Return the input if there was no match.
106  if (!match(String, &Matches))
107  return String;
108 
109  // Otherwise splice in the replacement string, starting with the prefix before
110  // the match.
111  std::string Res(String.begin(), Matches[0].begin());
112 
113  // Then the replacement string, honoring possible substitutions.
114  while (!Repl.empty()) {
115  // Skip to the next escape.
116  std::pair<StringRef, StringRef> Split = Repl.split('\\');
117 
118  // Add the skipped substring.
119  Res += Split.first;
120 
121  // Check for terminimation and trailing backslash.
122  if (Split.second.empty()) {
123  if (Repl.size() != Split.first.size() &&
124  Error && Error->empty())
125  *Error = "replacement string contained trailing backslash";
126  break;
127  }
128 
129  // Otherwise update the replacement string and interpret escapes.
130  Repl = Split.second;
131 
132  // FIXME: We should have a StringExtras function for mapping C99 escapes.
133  switch (Repl[0]) {
134  // Treat all unrecognized characters as self-quoting.
135  default:
136  Res += Repl[0];
137  Repl = Repl.substr(1);
138  break;
139 
140  // Single character escapes.
141  case 't':
142  Res += '\t';
143  Repl = Repl.substr(1);
144  break;
145  case 'n':
146  Res += '\n';
147  Repl = Repl.substr(1);
148  break;
149 
150  // Decimal escapes are backreferences.
151  case '0': case '1': case '2': case '3': case '4':
152  case '5': case '6': case '7': case '8': case '9': {
153  // Extract the backreference number.
154  StringRef Ref = Repl.slice(0, Repl.find_first_not_of("0123456789"));
155  Repl = Repl.substr(Ref.size());
156 
157  unsigned RefValue;
158  if (!Ref.getAsInteger(10, RefValue) &&
159  RefValue < Matches.size())
160  Res += Matches[RefValue];
161  else if (Error && Error->empty())
162  *Error = ("invalid backreference string '" + Twine(Ref) + "'").str();
163  break;
164  }
165  }
166  }
167 
168  // And finally the suffix.
169  Res += StringRef(Matches[0].end(), String.end() - Matches[0].end());
170 
171  return Res;
172 }
173 
174 // These are the special characters matched in functions like "p_ere_exp".
175 static const char RegexMetachars[] = "()^$|*+?.[]\\{}";
176 
178  // Check for regex metacharacters. This list was derived from our regex
179  // implementation in regcomp.c and double checked against the POSIX extended
180  // regular expression specification.
182 }
183 
185  std::string RegexStr;
186  for (unsigned i = 0, e = String.size(); i != e; ++i) {
187  if (strchr(RegexMetachars, String[i]))
188  RegexStr += '\\';
189  RegexStr += String[i];
190  }
191 
192  return RegexStr;
193 }
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:347
size_t re_nsub
Definition: regex_impl.h:50
void push_back(const T &Elt)
Definition: SmallVector.h:222
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:240
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
#define REG_NEWLINE
Definition: regex_impl.h:60
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:450
static std::string escape(StringRef String)
Turn String into a regex by escaping its special characters.
Definition: Regex.cpp:184
StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:405
int llvm_regcomp(llvm_regex_t *preg, const char *pattern, int cflags)
Definition: regcomp.c:165
void llvm_regfree(llvm_regex_t *)
Definition: regfree.c:50
By default, the POSIX extended regular expression (ERE) syntax is assumed.
Definition: Regex.h:43
#define REG_EXTENDED
Definition: regex_impl.h:57
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
const char * re_endp
Definition: regex_impl.h:51
bool isValid(std::string &Error)
isValid - returns the error encountered during regex compilation, or matching, if any...
Definition: Regex.cpp:42
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:107
size_t llvm_regerror(int errcode, const llvm_regex_t *preg, char *errbuf, size_t errbuf_size)
Definition: regerror.c:84
static std::error_code error(DiagnosticHandlerFunction DiagnosticHandler, std::error_code EC, const Twine &Message)
Compile for newline-sensitive matching.
Definition: Regex.h:39
iterator begin() const
Definition: StringRef.h:90
#define REG_ICASE
Definition: regex_impl.h:58
#define rc(i)
static const char RegexMetachars[]
Definition: Regex.cpp:175
#define REG_STARTEND
Definition: regex_impl.h:88
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:177
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:98
size_t find_first_not_of(char C, size_t From=0) const
Find the first character in the string that is not C or npos if not found.
Definition: StringRef.cpp:212
unsigned getNumMatches() const
getNumMatches - In a valid regex, return the number of parenthesized matches it contains.
Definition: Regex.cpp:55
Regex(StringRef Regex, unsigned Flags=NoFlags)
Compiles the given regular expression Regex.
Definition: Regex.cpp:22
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
#define REG_NOMATCH
Definition: regex_impl.h:66
int llvm_regexec(const llvm_regex_t *, const char *, size_t, llvm_regmatch_t[], int)
Definition: regexec.c:141
#define REG_PEND
Definition: regex_impl.h:62
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:134
static const size_t npos
Definition: StringRef.h:44
size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition: StringRef.h:279
Compile for matching that ignores upper/lower case distinctions.
Definition: Regex.h:33
iterator end() const
Definition: StringRef.h:92
bool match(StringRef String, SmallVectorImpl< StringRef > *Matches=nullptr)
matches - Match the regex against a given String.
Definition: Regex.cpp:59
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:434
static void Split(std::vector< std::string > &V, StringRef S)
Split - Splits a string of comma separated items in to a vector of strings.
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110
void resize(size_type N)
Definition: SmallVector.h:376