xref: /aosp_15_r20/external/llvm/lib/Support/Regex.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- Regex.cpp - Regular Expression matcher implementation -------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements a POSIX regular expression matcher.
11*9880d681SAndroid Build Coastguard Worker //
12*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
13*9880d681SAndroid Build Coastguard Worker 
14*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Regex.h"
15*9880d681SAndroid Build Coastguard Worker #include "regex_impl.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/SmallVector.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringRef.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Twine.h"
19*9880d681SAndroid Build Coastguard Worker #include <string>
20*9880d681SAndroid Build Coastguard Worker using namespace llvm;
21*9880d681SAndroid Build Coastguard Worker 
Regex(StringRef regex,unsigned Flags)22*9880d681SAndroid Build Coastguard Worker Regex::Regex(StringRef regex, unsigned Flags) {
23*9880d681SAndroid Build Coastguard Worker   unsigned flags = 0;
24*9880d681SAndroid Build Coastguard Worker   preg = new llvm_regex();
25*9880d681SAndroid Build Coastguard Worker   preg->re_endp = regex.end();
26*9880d681SAndroid Build Coastguard Worker   if (Flags & IgnoreCase)
27*9880d681SAndroid Build Coastguard Worker     flags |= REG_ICASE;
28*9880d681SAndroid Build Coastguard Worker   if (Flags & Newline)
29*9880d681SAndroid Build Coastguard Worker     flags |= REG_NEWLINE;
30*9880d681SAndroid Build Coastguard Worker   if (!(Flags & BasicRegex))
31*9880d681SAndroid Build Coastguard Worker     flags |= REG_EXTENDED;
32*9880d681SAndroid Build Coastguard Worker   error = llvm_regcomp(preg, regex.data(), flags|REG_PEND);
33*9880d681SAndroid Build Coastguard Worker }
34*9880d681SAndroid Build Coastguard Worker 
~Regex()35*9880d681SAndroid Build Coastguard Worker Regex::~Regex() {
36*9880d681SAndroid Build Coastguard Worker   if (preg) {
37*9880d681SAndroid Build Coastguard Worker     llvm_regfree(preg);
38*9880d681SAndroid Build Coastguard Worker     delete preg;
39*9880d681SAndroid Build Coastguard Worker   }
40*9880d681SAndroid Build Coastguard Worker }
41*9880d681SAndroid Build Coastguard Worker 
isValid(std::string & Error)42*9880d681SAndroid Build Coastguard Worker bool Regex::isValid(std::string &Error) {
43*9880d681SAndroid Build Coastguard Worker   if (!error)
44*9880d681SAndroid Build Coastguard Worker     return true;
45*9880d681SAndroid Build Coastguard Worker 
46*9880d681SAndroid Build Coastguard Worker   size_t len = llvm_regerror(error, preg, nullptr, 0);
47*9880d681SAndroid Build Coastguard Worker 
48*9880d681SAndroid Build Coastguard Worker   Error.resize(len - 1);
49*9880d681SAndroid Build Coastguard Worker   llvm_regerror(error, preg, &Error[0], len);
50*9880d681SAndroid Build Coastguard Worker   return false;
51*9880d681SAndroid Build Coastguard Worker }
52*9880d681SAndroid Build Coastguard Worker 
53*9880d681SAndroid Build Coastguard Worker /// getNumMatches - In a valid regex, return the number of parenthesized
54*9880d681SAndroid Build Coastguard Worker /// matches it contains.
getNumMatches() const55*9880d681SAndroid Build Coastguard Worker unsigned Regex::getNumMatches() const {
56*9880d681SAndroid Build Coastguard Worker   return preg->re_nsub;
57*9880d681SAndroid Build Coastguard Worker }
58*9880d681SAndroid Build Coastguard Worker 
match(StringRef String,SmallVectorImpl<StringRef> * Matches)59*9880d681SAndroid Build Coastguard Worker bool Regex::match(StringRef String, SmallVectorImpl<StringRef> *Matches){
60*9880d681SAndroid Build Coastguard Worker   unsigned nmatch = Matches ? preg->re_nsub+1 : 0;
61*9880d681SAndroid Build Coastguard Worker 
62*9880d681SAndroid Build Coastguard Worker   // pmatch needs to have at least one element.
63*9880d681SAndroid Build Coastguard Worker   SmallVector<llvm_regmatch_t, 8> pm;
64*9880d681SAndroid Build Coastguard Worker   pm.resize(nmatch > 0 ? nmatch : 1);
65*9880d681SAndroid Build Coastguard Worker   pm[0].rm_so = 0;
66*9880d681SAndroid Build Coastguard Worker   pm[0].rm_eo = String.size();
67*9880d681SAndroid Build Coastguard Worker 
68*9880d681SAndroid Build Coastguard Worker   int rc = llvm_regexec(preg, String.data(), nmatch, pm.data(), REG_STARTEND);
69*9880d681SAndroid Build Coastguard Worker 
70*9880d681SAndroid Build Coastguard Worker   if (rc == REG_NOMATCH)
71*9880d681SAndroid Build Coastguard Worker     return false;
72*9880d681SAndroid Build Coastguard Worker   if (rc != 0) {
73*9880d681SAndroid Build Coastguard Worker     // regexec can fail due to invalid pattern or running out of memory.
74*9880d681SAndroid Build Coastguard Worker     error = rc;
75*9880d681SAndroid Build Coastguard Worker     return false;
76*9880d681SAndroid Build Coastguard Worker   }
77*9880d681SAndroid Build Coastguard Worker 
78*9880d681SAndroid Build Coastguard Worker   // There was a match.
79*9880d681SAndroid Build Coastguard Worker 
80*9880d681SAndroid Build Coastguard Worker   if (Matches) { // match position requested
81*9880d681SAndroid Build Coastguard Worker     Matches->clear();
82*9880d681SAndroid Build Coastguard Worker 
83*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0; i != nmatch; ++i) {
84*9880d681SAndroid Build Coastguard Worker       if (pm[i].rm_so == -1) {
85*9880d681SAndroid Build Coastguard Worker         // this group didn't match
86*9880d681SAndroid Build Coastguard Worker         Matches->push_back(StringRef());
87*9880d681SAndroid Build Coastguard Worker         continue;
88*9880d681SAndroid Build Coastguard Worker       }
89*9880d681SAndroid Build Coastguard Worker       assert(pm[i].rm_eo >= pm[i].rm_so);
90*9880d681SAndroid Build Coastguard Worker       Matches->push_back(StringRef(String.data()+pm[i].rm_so,
91*9880d681SAndroid Build Coastguard Worker                                    pm[i].rm_eo-pm[i].rm_so));
92*9880d681SAndroid Build Coastguard Worker     }
93*9880d681SAndroid Build Coastguard Worker   }
94*9880d681SAndroid Build Coastguard Worker 
95*9880d681SAndroid Build Coastguard Worker   return true;
96*9880d681SAndroid Build Coastguard Worker }
97*9880d681SAndroid Build Coastguard Worker 
sub(StringRef Repl,StringRef String,std::string * Error)98*9880d681SAndroid Build Coastguard Worker std::string Regex::sub(StringRef Repl, StringRef String,
99*9880d681SAndroid Build Coastguard Worker                        std::string *Error) {
100*9880d681SAndroid Build Coastguard Worker   SmallVector<StringRef, 8> Matches;
101*9880d681SAndroid Build Coastguard Worker 
102*9880d681SAndroid Build Coastguard Worker   // Reset error, if given.
103*9880d681SAndroid Build Coastguard Worker   if (Error && !Error->empty()) *Error = "";
104*9880d681SAndroid Build Coastguard Worker 
105*9880d681SAndroid Build Coastguard Worker   // Return the input if there was no match.
106*9880d681SAndroid Build Coastguard Worker   if (!match(String, &Matches))
107*9880d681SAndroid Build Coastguard Worker     return String;
108*9880d681SAndroid Build Coastguard Worker 
109*9880d681SAndroid Build Coastguard Worker   // Otherwise splice in the replacement string, starting with the prefix before
110*9880d681SAndroid Build Coastguard Worker   // the match.
111*9880d681SAndroid Build Coastguard Worker   std::string Res(String.begin(), Matches[0].begin());
112*9880d681SAndroid Build Coastguard Worker 
113*9880d681SAndroid Build Coastguard Worker   // Then the replacement string, honoring possible substitutions.
114*9880d681SAndroid Build Coastguard Worker   while (!Repl.empty()) {
115*9880d681SAndroid Build Coastguard Worker     // Skip to the next escape.
116*9880d681SAndroid Build Coastguard Worker     std::pair<StringRef, StringRef> Split = Repl.split('\\');
117*9880d681SAndroid Build Coastguard Worker 
118*9880d681SAndroid Build Coastguard Worker     // Add the skipped substring.
119*9880d681SAndroid Build Coastguard Worker     Res += Split.first;
120*9880d681SAndroid Build Coastguard Worker 
121*9880d681SAndroid Build Coastguard Worker     // Check for terminimation and trailing backslash.
122*9880d681SAndroid Build Coastguard Worker     if (Split.second.empty()) {
123*9880d681SAndroid Build Coastguard Worker       if (Repl.size() != Split.first.size() &&
124*9880d681SAndroid Build Coastguard Worker           Error && Error->empty())
125*9880d681SAndroid Build Coastguard Worker         *Error = "replacement string contained trailing backslash";
126*9880d681SAndroid Build Coastguard Worker       break;
127*9880d681SAndroid Build Coastguard Worker     }
128*9880d681SAndroid Build Coastguard Worker 
129*9880d681SAndroid Build Coastguard Worker     // Otherwise update the replacement string and interpret escapes.
130*9880d681SAndroid Build Coastguard Worker     Repl = Split.second;
131*9880d681SAndroid Build Coastguard Worker 
132*9880d681SAndroid Build Coastguard Worker     // FIXME: We should have a StringExtras function for mapping C99 escapes.
133*9880d681SAndroid Build Coastguard Worker     switch (Repl[0]) {
134*9880d681SAndroid Build Coastguard Worker       // Treat all unrecognized characters as self-quoting.
135*9880d681SAndroid Build Coastguard Worker     default:
136*9880d681SAndroid Build Coastguard Worker       Res += Repl[0];
137*9880d681SAndroid Build Coastguard Worker       Repl = Repl.substr(1);
138*9880d681SAndroid Build Coastguard Worker       break;
139*9880d681SAndroid Build Coastguard Worker 
140*9880d681SAndroid Build Coastguard Worker       // Single character escapes.
141*9880d681SAndroid Build Coastguard Worker     case 't':
142*9880d681SAndroid Build Coastguard Worker       Res += '\t';
143*9880d681SAndroid Build Coastguard Worker       Repl = Repl.substr(1);
144*9880d681SAndroid Build Coastguard Worker       break;
145*9880d681SAndroid Build Coastguard Worker     case 'n':
146*9880d681SAndroid Build Coastguard Worker       Res += '\n';
147*9880d681SAndroid Build Coastguard Worker       Repl = Repl.substr(1);
148*9880d681SAndroid Build Coastguard Worker       break;
149*9880d681SAndroid Build Coastguard Worker 
150*9880d681SAndroid Build Coastguard Worker       // Decimal escapes are backreferences.
151*9880d681SAndroid Build Coastguard Worker     case '0': case '1': case '2': case '3': case '4':
152*9880d681SAndroid Build Coastguard Worker     case '5': case '6': case '7': case '8': case '9': {
153*9880d681SAndroid Build Coastguard Worker       // Extract the backreference number.
154*9880d681SAndroid Build Coastguard Worker       StringRef Ref = Repl.slice(0, Repl.find_first_not_of("0123456789"));
155*9880d681SAndroid Build Coastguard Worker       Repl = Repl.substr(Ref.size());
156*9880d681SAndroid Build Coastguard Worker 
157*9880d681SAndroid Build Coastguard Worker       unsigned RefValue;
158*9880d681SAndroid Build Coastguard Worker       if (!Ref.getAsInteger(10, RefValue) &&
159*9880d681SAndroid Build Coastguard Worker           RefValue < Matches.size())
160*9880d681SAndroid Build Coastguard Worker         Res += Matches[RefValue];
161*9880d681SAndroid Build Coastguard Worker       else if (Error && Error->empty())
162*9880d681SAndroid Build Coastguard Worker         *Error = ("invalid backreference string '" + Twine(Ref) + "'").str();
163*9880d681SAndroid Build Coastguard Worker       break;
164*9880d681SAndroid Build Coastguard Worker     }
165*9880d681SAndroid Build Coastguard Worker     }
166*9880d681SAndroid Build Coastguard Worker   }
167*9880d681SAndroid Build Coastguard Worker 
168*9880d681SAndroid Build Coastguard Worker   // And finally the suffix.
169*9880d681SAndroid Build Coastguard Worker   Res += StringRef(Matches[0].end(), String.end() - Matches[0].end());
170*9880d681SAndroid Build Coastguard Worker 
171*9880d681SAndroid Build Coastguard Worker   return Res;
172*9880d681SAndroid Build Coastguard Worker }
173*9880d681SAndroid Build Coastguard Worker 
174*9880d681SAndroid Build Coastguard Worker // These are the special characters matched in functions like "p_ere_exp".
175*9880d681SAndroid Build Coastguard Worker static const char RegexMetachars[] = "()^$|*+?.[]\\{}";
176*9880d681SAndroid Build Coastguard Worker 
isLiteralERE(StringRef Str)177*9880d681SAndroid Build Coastguard Worker bool Regex::isLiteralERE(StringRef Str) {
178*9880d681SAndroid Build Coastguard Worker   // Check for regex metacharacters.  This list was derived from our regex
179*9880d681SAndroid Build Coastguard Worker   // implementation in regcomp.c and double checked against the POSIX extended
180*9880d681SAndroid Build Coastguard Worker   // regular expression specification.
181*9880d681SAndroid Build Coastguard Worker   return Str.find_first_of(RegexMetachars) == StringRef::npos;
182*9880d681SAndroid Build Coastguard Worker }
183*9880d681SAndroid Build Coastguard Worker 
escape(StringRef String)184*9880d681SAndroid Build Coastguard Worker std::string Regex::escape(StringRef String) {
185*9880d681SAndroid Build Coastguard Worker   std::string RegexStr;
186*9880d681SAndroid Build Coastguard Worker   for (unsigned i = 0, e = String.size(); i != e; ++i) {
187*9880d681SAndroid Build Coastguard Worker     if (strchr(RegexMetachars, String[i]))
188*9880d681SAndroid Build Coastguard Worker       RegexStr += '\\';
189*9880d681SAndroid Build Coastguard Worker     RegexStr += String[i];
190*9880d681SAndroid Build Coastguard Worker   }
191*9880d681SAndroid Build Coastguard Worker 
192*9880d681SAndroid Build Coastguard Worker   return RegexStr;
193*9880d681SAndroid Build Coastguard Worker }
194