LLVM: lib/Support/Regex.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
18
19#include
20#include
21
22using namespace llvm;
23
25
27 unsigned flags = 0;
37}
38
41
43 preg = regex.preg;
44 error = regex.error;
45 regex.preg = nullptr;
47}
48
50 if (preg) {
52 delete preg;
53 }
54}
55
56namespace {
57
58
59void RegexErrorToString(int error, struct llvm_regex *preg,
60 std::string &Error) {
62
63 Error.resize(len - 1);
65}
66
67}
68
70 if (!error)
71 return true;
72
73 RegexErrorToString(error, preg, Error);
74 return false;
75}
76
77
78
81}
82
84 std::string *Error) const {
85
88
89
91 return false;
92
93 unsigned nmatch = Matches ? preg->re_nsub+1 : 0;
94
95
96 if (String.data() == nullptr)
98
99
101 pm.resize(nmatch > 0 ? nmatch : 1);
102 pm[0].rm_so = 0;
103 pm[0].rm_eo = String.size();
104
106
107
108
110 return false;
111 if (rc != 0) {
113 RegexErrorToString(error, preg, *Error);
114 return false;
115 }
116
117
118
119 if (Matches) {
120 Matches->clear();
121
122 for (unsigned i = 0; i != nmatch; ++i) {
123 if (pm[i].rm_so == -1) {
124
126 continue;
127 }
128 assert(pm[i].rm_eo >= pm[i].rm_so);
130 pm[i].rm_eo-pm[i].rm_so));
131 }
132 }
133
134 return true;
135}
136
138 std::string *Error) const {
140
141
143 return std::string(String);
144
145
146
147 std::string Res(String.begin(), Matches[0].begin());
148
149
150 while (!Repl.empty()) {
151
152 std::pair<StringRef, StringRef> Split = Repl.split('\\');
153
154
155 Res += Split.first;
156
157
158 if (Split.second.empty()) {
159 if (Repl.size() != Split.first.size() &&
161 *Error = "replacement string contained trailing backslash";
162 break;
163 }
164
165
166 Repl = Split.second;
167
168
169 switch (Repl[0]) {
170
171
172 case 'g':
173 if (Repl.size() >= 4 && Repl[1] == '<') {
174 size_t End = Repl.find('>');
176 unsigned RefValue;
179 if (RefValue < Matches.size())
180 Res += Matches[RefValue];
183 ("invalid backreference string 'g<" + Twine(Ref) + ">'").str();
184 break;
185 }
186 }
187 [[fallthrough]];
188
189
190 default:
191 Res += Repl[0];
192 Repl = Repl.substr(1);
193 break;
194
195
196 case 't':
197 Res += '\t';
198 Repl = Repl.substr(1);
199 break;
200 case 'n':
201 Res += '\n';
202 Repl = Repl.substr(1);
203 break;
204
205
206 case '0': case '1': case '2': case '3': case '4':
207 case '5': case '6': case '7': case '8': case '9': {
208
211
212 unsigned RefValue;
213 if (.getAsInteger(10, RefValue) &&
214 RefValue < Matches.size())
215 Res += Matches[RefValue];
217 *Error = ("invalid backreference string '" + Twine(Ref) + "'").str();
218 break;
219 }
220 }
221 }
222
223
225
226 return Res;
227}
228
229
231
233
234
235
237}
238
240 std::string RegexStr;
243 RegexStr += '\\';
244 RegexStr += C;
245 }
246
247 return RegexStr;
248}
static const char RegexMetachars[]
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
Lightweight error class with error context and mandatory checking.
@ Newline
Compile for newline-sensitive matching.
@ IgnoreCase
Compile for matching that ignores upper/lower case distinctions.
@ BasicRegex
By default, the POSIX extended regular expression (ERE) syntax is assumed.
static std::string escape(StringRef String)
Turn String into a regex by escaping its special characters.
std::string sub(StringRef Repl, StringRef String, std::string *Error=nullptr) const
sub - Return the result of replacing the first match of the regex in String with the Repl string.
static bool isLiteralERE(StringRef Str)
If this function returns true, ^Str$ is an extended regular expression that matches Str and only Str.
unsigned getNumMatches() const
getNumMatches - In a valid regex, return the number of parenthesized matches it contains.
bool match(StringRef String, SmallVectorImpl< StringRef > *Matches=nullptr, std::string *Error=nullptr) const
matches - Match the regex against a given String.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
constexpr bool empty() const
empty - Check if the string is empty.
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
constexpr size_t size() const
size - Get the string size.
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
static constexpr size_t npos
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.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
@ Ref
The access may reference the value stored in memory.
int llvm_regcomp(llvm_regex_t *preg, const char *pattern, int cflags)
size_t llvm_regerror(int errcode, const llvm_regex_t *preg, char *errbuf, size_t errbuf_size)
void llvm_regfree(llvm_regex_t *)
int llvm_regexec(const llvm_regex_t *, const char *, size_t, llvm_regmatch_t[], int)