1 //===-- Regex.cpp - Regular Expression matcher implementation -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a POSIX regular expression matcher.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Regex.h"
15 #include "regex_impl.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/raw_ostream.h"
22 Regex::Regex(StringRef regex, unsigned Flags) {
24 preg = new llvm_regex();
25 preg->re_endp = regex.end();
26 if (Flags & IgnoreCase)
30 if (!(Flags & BasicRegex))
31 flags |= REG_EXTENDED;
32 error = llvm_regcomp(preg, regex.data(), flags|REG_PEND);
40 bool Regex::isValid(std::string &Error) {
44 size_t len = llvm_regerror(error, preg, NULL, 0);
47 llvm_regerror(error, preg, &Error[0], len);
51 /// getNumMatches - In a valid regex, return the number of parenthesized
52 /// matches it contains.
53 unsigned Regex::getNumMatches() const {
57 bool Regex::match(StringRef String, SmallVectorImpl<StringRef> *Matches){
58 unsigned nmatch = Matches ? preg->re_nsub+1 : 0;
60 // pmatch needs to have at least one element.
61 SmallVector<llvm_regmatch_t, 8> pm;
62 pm.resize(nmatch > 0 ? nmatch : 1);
64 pm[0].rm_eo = String.size();
66 int rc = llvm_regexec(preg, String.data(), nmatch, pm.data(), REG_STARTEND);
68 if (rc == REG_NOMATCH)
71 // regexec can fail due to invalid pattern or running out of memory.
78 if (Matches) { // match position requested
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());
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));
96 std::string Regex::sub(StringRef Repl, StringRef String,
98 SmallVector<StringRef, 8> Matches;
100 // Reset error, if given.
101 if (Error && !Error->empty()) *Error = "";
103 // Return the input if there was no match.
104 if (!match(String, &Matches))
107 // Otherwise splice in the replacement string, starting with the prefix before
109 std::string Res(String.begin(), Matches[0].begin());
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('\\');
116 // Add the skipped substring.
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";
127 // Otherwise update the replacement string and interpret escapes.
130 // FIXME: We should have a StringExtras function for mapping C99 escapes.
132 // Treat all unrecognized characters as self-quoting.
135 Repl = Repl.substr(1);
138 // Single character escapes.
141 Repl = Repl.substr(1);
145 Repl = Repl.substr(1);
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());
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() + "'";
166 // And finally the suffix.
167 Res += StringRef(Matches[0].end(), String.end() - Matches[0].end());