LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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"
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  llvm_regfree(preg);
37  delete preg;
38 }
39 
40 bool Regex::isValid(std::string &Error) {
41  if (!error)
42  return true;
43 
44  size_t len = llvm_regerror(error, preg, NULL, 0);
45 
46  Error.resize(len - 1);
47  llvm_regerror(error, preg, &Error[0], len);
48  return false;
49 }
50 
51 /// getNumMatches - In a valid regex, return the number of parenthesized
52 /// matches it contains.
53 unsigned Regex::getNumMatches() const {
54  return preg->re_nsub;
55 }
56 
58  unsigned nmatch = Matches ? preg->re_nsub+1 : 0;
59 
60  // pmatch needs to have at least one element.
62  pm.resize(nmatch > 0 ? nmatch : 1);
63  pm[0].rm_so = 0;
64  pm[0].rm_eo = String.size();
65 
66  int rc = llvm_regexec(preg, String.data(), nmatch, pm.data(), REG_STARTEND);
67 
68  if (rc == REG_NOMATCH)
69  return false;
70  if (rc != 0) {
71  // regexec can fail due to invalid pattern or running out of memory.
72  error = rc;
73  return false;
74  }
75 
76  // There was a match.
77 
78  if (Matches) { // match position requested
79  Matches->clear();
80 
81  for (unsigned i = 0; i != nmatch; ++i) {
82  if (pm[i].rm_so == -1) {
83  // this group didn't match
84  Matches->push_back(StringRef());
85  continue;
86  }
87  assert(pm[i].rm_eo >= pm[i].rm_so);
88  Matches->push_back(StringRef(String.data()+pm[i].rm_so,
89  pm[i].rm_eo-pm[i].rm_so));
90  }
91  }
92 
93  return true;
94 }
95 
97  std::string *Error) {
99 
100  // Reset error, if given.
101  if (Error && !Error->empty()) *Error = "";
102 
103  // Return the input if there was no match.
104  if (!match(String, &Matches))
105  return String;
106 
107  // Otherwise splice in the replacement string, starting with the prefix before
108  // the match.
109  std::string Res(String.begin(), Matches[0].begin());
110 
111  // Then the replacement string, honoring possible substitutions.
112  while (!Repl.empty()) {
113  // Skip to the next escape.
114  std::pair<StringRef, StringRef> Split = Repl.split('\\');
115 
116  // Add the skipped substring.
117  Res += Split.first;
118 
119  // Check for terminimation and trailing backslash.
120  if (Split.second.empty()) {
121  if (Repl.size() != Split.first.size() &&
122  Error && Error->empty())
123  *Error = "replacement string contained trailing backslash";
124  break;
125  }
126 
127  // Otherwise update the replacement string and interpret escapes.
128  Repl = Split.second;
129 
130  // FIXME: We should have a StringExtras function for mapping C99 escapes.
131  switch (Repl[0]) {
132  // Treat all unrecognized characters as self-quoting.
133  default:
134  Res += Repl[0];
135  Repl = Repl.substr(1);
136  break;
137 
138  // Single character escapes.
139  case 't':
140  Res += '\t';
141  Repl = Repl.substr(1);
142  break;
143  case 'n':
144  Res += '\n';
145  Repl = Repl.substr(1);
146  break;
147 
148  // Decimal escapes are backreferences.
149  case '0': case '1': case '2': case '3': case '4':
150  case '5': case '6': case '7': case '8': case '9': {
151  // Extract the backreference number.
152  StringRef Ref = Repl.slice(0, Repl.find_first_not_of("0123456789"));
153  Repl = Repl.substr(Ref.size());
154 
155  unsigned RefValue;
156  if (!Ref.getAsInteger(10, RefValue) &&
157  RefValue < Matches.size())
158  Res += Matches[RefValue];
159  else if (Error && Error->empty())
160  *Error = "invalid backreference string '" + Ref.str() + "'";
161  break;
162  }
163  }
164  }
165 
166  // And finally the suffix.
167  Res += StringRef(Matches[0].end(), String.end() - Matches[0].end());
168 
169  return Res;
170 }
171 
173  // Check for regex metacharacters. This list was derived from our regex
174  // implementation in regcomp.c and double checked against the POSIX extended
175  // regular expression specification.
176  return Str.find_first_of("()^$|*+?.[]\\{}") == StringRef::npos;
177 }
size_t re_nsub
Definition: regex_impl.h:50
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:181
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
Definition: StringRef.h:437
StringRef substr(size_t Start, size_t N=npos) const
Definition: StringRef.h:392
int llvm_regcomp(llvm_regex_t *preg, const char *pattern, int cflags)
Definition: regcomp.c:157
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:181
void llvm_regfree(llvm_regex_t *)
Definition: regfree.c:50
Compile for matching that ignores upper/lower case distinctions.
Definition: Regex.h:33
#define REG_EXTENDED
Definition: regex_impl.h:57
const char * re_endp
Definition: regex_impl.h:51
bool isValid(std::string &Error)
Definition: Regex.cpp:40
const char * data() const
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
bool match(StringRef String, SmallVectorImpl< StringRef > *Matches=0)
Definition: Regex.cpp:57
iterator begin() const
Definition: StringRef.h:97
#define REG_ICASE
Definition: regex_impl.h:58
#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:172
enable_if_c< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Definition: StringRef.h:337
size_t find_first_not_of(char C, size_t From=0) const
Definition: StringRef.cpp:213
unsigned getNumMatches() const
Definition: Regex.cpp:53
Regex(StringRef Regex, unsigned Flags=NoFlags)
Compiles the given regular expression Regex.
Definition: Regex.cpp:22
static void Split(std::vector< std::string > &V, const StringRef S)
std::string sub(StringRef Repl, StringRef String, std::string *Error=0)
Definition: Regex.cpp:96
#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()
data - Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:135
static const size_t npos
Definition: StringRef.h:45
void resize(unsigned N)
Definition: SmallVector.h:401
size_t find_first_of(char C, size_t From=0) const
Definition: StringRef.h:269
iterator end() const
Definition: StringRef.h:99
StringRef slice(size_t Start, size_t End) const
Definition: StringRef.h:421
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110