1 //===-- Regex.cpp - Regular Expression matcher implementation -------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements a POSIX regular expression matcher.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/Regex.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/ADT/Twine.h"
17 #include "regex_impl.h"
24 Regex::Regex() : preg(nullptr), error(REG_BADPAT
) {}
26 Regex::Regex(StringRef regex
, RegexFlags Flags
) {
28 preg
= new llvm_regex();
29 preg
->re_endp
= regex
.end();
30 if (Flags
& IgnoreCase
)
34 if (!(Flags
& BasicRegex
))
35 flags
|= REG_EXTENDED
;
36 error
= llvm_regcomp(preg
, regex
.data(), flags
|REG_PEND
);
39 Regex::Regex(StringRef regex
, unsigned Flags
)
40 : Regex(regex
, static_cast<RegexFlags
>(Flags
)) {}
42 Regex::Regex(Regex
&®ex
) {
46 regex
.error
= REG_BADPAT
;
58 /// Utility to convert a regex error code into a human-readable string.
59 void RegexErrorToString(int error
, struct llvm_regex
*preg
,
61 size_t len
= llvm_regerror(error
, preg
, nullptr, 0);
63 Error
.resize(len
- 1);
64 llvm_regerror(error
, preg
, &Error
[0], len
);
69 bool Regex::isValid(std::string
&Error
) const {
73 RegexErrorToString(error
, preg
, Error
);
77 /// getNumMatches - In a valid regex, return the number of parenthesized
78 /// matches it contains.
79 unsigned Regex::getNumMatches() const {
83 bool Regex::match(StringRef String
, SmallVectorImpl
<StringRef
> *Matches
,
84 std::string
*Error
) const {
85 // Reset error, if given.
86 if (Error
&& !Error
->empty())
89 // Check if the regex itself didn't successfully compile.
90 if (Error
? !isValid(*Error
) : !isValid())
93 unsigned nmatch
= Matches
? preg
->re_nsub
+1 : 0;
95 // Update null string to empty string.
96 if (String
.data() == nullptr)
99 // pmatch needs to have at least one element.
100 SmallVector
<llvm_regmatch_t
, 8> pm
;
101 pm
.resize(nmatch
> 0 ? nmatch
: 1);
103 pm
[0].rm_eo
= String
.size();
105 int rc
= llvm_regexec(preg
, String
.data(), nmatch
, pm
.data(), REG_STARTEND
);
107 // Failure to match is not an error, it's just a normal return value.
108 // Any other error code is considered abnormal, and is logged in the Error.
109 if (rc
== REG_NOMATCH
)
113 RegexErrorToString(error
, preg
, *Error
);
117 // There was a match.
119 if (Matches
) { // match position requested
122 for (unsigned i
= 0; i
!= nmatch
; ++i
) {
123 if (pm
[i
].rm_so
== -1) {
124 // this group didn't match
125 Matches
->push_back(StringRef());
128 assert(pm
[i
].rm_eo
>= pm
[i
].rm_so
);
129 Matches
->push_back(StringRef(String
.data()+pm
[i
].rm_so
,
130 pm
[i
].rm_eo
-pm
[i
].rm_so
));
137 std::string
Regex::sub(StringRef Repl
, StringRef String
,
138 std::string
*Error
) const {
139 SmallVector
<StringRef
, 8> Matches
;
141 // Return the input if there was no match.
142 if (!match(String
, &Matches
, Error
))
143 return std::string(String
);
145 // Otherwise splice in the replacement string, starting with the prefix before
147 std::string
Res(String
.begin(), Matches
[0].begin());
149 // Then the replacement string, honoring possible substitutions.
150 while (!Repl
.empty()) {
151 // Skip to the next escape.
152 std::pair
<StringRef
, StringRef
> Split
= Repl
.split('\\');
154 // Add the skipped substring.
157 // Check for terminimation and trailing backslash.
158 if (Split
.second
.empty()) {
159 if (Repl
.size() != Split
.first
.size() &&
160 Error
&& Error
->empty())
161 *Error
= "replacement string contained trailing backslash";
165 // Otherwise update the replacement string and interpret escapes.
168 // FIXME: We should have a StringExtras function for mapping C99 escapes.
171 // Backreference with the "\g<ref>" syntax
173 if (Repl
.size() >= 4 && Repl
[1] == '<') {
174 size_t End
= Repl
.find('>');
175 StringRef Ref
= Repl
.slice(2, End
);
177 if (End
!= StringRef::npos
&& !Ref
.getAsInteger(10, RefValue
)) {
178 Repl
= Repl
.substr(End
+ 1);
179 if (RefValue
< Matches
.size())
180 Res
+= Matches
[RefValue
];
181 else if (Error
&& Error
->empty())
183 ("invalid backreference string 'g<" + Twine(Ref
) + ">'").str();
189 // Treat all unrecognized characters as self-quoting.
192 Repl
= Repl
.substr(1);
195 // Single character escapes.
198 Repl
= Repl
.substr(1);
202 Repl
= Repl
.substr(1);
205 // Decimal escapes are backreferences.
206 case '0': case '1': case '2': case '3': case '4':
207 case '5': case '6': case '7': case '8': case '9': {
208 // Extract the backreference number.
209 StringRef Ref
= Repl
.slice(0, Repl
.find_first_not_of("0123456789"));
210 Repl
= Repl
.substr(Ref
.size());
213 if (!Ref
.getAsInteger(10, RefValue
) &&
214 RefValue
< Matches
.size())
215 Res
+= Matches
[RefValue
];
216 else if (Error
&& Error
->empty())
217 *Error
= ("invalid backreference string '" + Twine(Ref
) + "'").str();
223 // And finally the suffix.
224 Res
+= StringRef(Matches
[0].end(), String
.end() - Matches
[0].end());
229 // These are the special characters matched in functions like "p_ere_exp".
230 static const char RegexMetachars
[] = "()^$|*+?.[]\\{}";
232 bool Regex::isLiteralERE(StringRef Str
) {
233 // Check for regex metacharacters. This list was derived from our regex
234 // implementation in regcomp.c and double checked against the POSIX extended
235 // regular expression specification.
236 return Str
.find_first_of(RegexMetachars
) == StringRef::npos
;
239 std::string
Regex::escape(StringRef String
) {
240 std::string RegexStr
;
241 for (char C
: String
) {
242 if (strchr(RegexMetachars
, C
))