-
Notifications
You must be signed in to change notification settings - Fork 3
/
Regex.C
326 lines (261 loc) · 8.93 KB
/
Regex.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright (c) 2005-2011, Guillaume Gimenez <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of G.Gimenez nor the names of its contributors may
* be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL G.GIMENEZ BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Authors:
* * Guillaume Gimenez <[email protected]>
*
*/
#include "raii.H"
#include <regex.h>
namespace raii {
void Regex::compile(Compiled compileFor) const {
int copts=0;
if ( compiledFor == compileFor ) return;
if ( compiledFor != NO ) regfree(&re);
if ( compileFor == MATCH ) copts |= REG_NOSUB;
if (m_caseInsensitive) copts |= REG_ICASE;
if (m_extended) copts |=REG_EXTENDED;
if (m_newLine) copts |=REG_NEWLINE;
int err = regcomp(&re,pattern.c_str(),copts);
if (err) {
char errBuf[1024];
regerror(err,&re,errBuf,1024);
regfree(&re);
compiledFor=NO;
throw RegularExpressionException(errBuf);
}
compiledFor=compileFor;
}
Regex::Regex(const String& pat,bool ext, bool icase, bool newline)
: pattern(pat), re(), m_extended(ext), m_caseInsensitive(icase), m_newLine(newline), compiledFor(NO) {}
Regex::Regex(const char* pat,bool ext, bool icase, bool newline)
: pattern(pat), re(), m_extended(ext), m_caseInsensitive(icase), m_newLine(newline), compiledFor(NO) {}
Regex::Regex(const Regex& r)
: pattern(r.pattern), re(), m_extended(r.m_extended), m_caseInsensitive(r.m_caseInsensitive), m_newLine(r.m_newLine), compiledFor(NO) {}
Regex& Regex::operator=(const Regex& r) {
pattern=r.pattern;
m_extended=r.m_extended;
m_caseInsensitive=r.m_caseInsensitive;
m_newLine=r.m_newLine;
compiledFor=NO;
return *this;
}
Regex& Regex::extended(bool e) {
m_extended=e;
return *this;
}
Regex& Regex::caseInsensitive(bool i) {
m_caseInsensitive=i;
return *this;
}
Regex& Regex::newLine(bool nl) {
m_newLine=nl;
return *this;
}
// ex: Regex("plop").matches(str);
bool Regex::matches(const String& str, bool notbol, bool noteol) const {
compile(MATCH);
int err = regexec(&re,str.c_str(),0,NULL,
(notbol?REG_NOTBOL:0) | (noteol?REG_NOTEOL:0) );
switch (err) {
case REG_NOERROR: return true;
case REG_NOMATCH: return false;
default:
char errBuf[1024];
regerror(err,&re,errBuf,1024);
throw RegularExpressionException(errBuf);
}
}
// ex: Regex("plop").replace("gr8k",str);
String Regex::replace(const String& repl, const String& str) const {
compile(REPLACE);
regmatch_t *subs;
char *buf, /* buf is where we build the replaced string */
*nbuf, /* nbuf is used when we grow the buffer */
*walkbuf; /* used to walk buf when replacing backrefs */
const char *walk; /* used to walk replacement string for backrefs */
const char *string=str.c_str();
const char *replace=repl.c_str();
int buf_len;
int pos, tmp, string_len, new_l;
int err;
string_len = str.size();
/* allocate storage for (sub-)expression-matches */
subs = (regmatch_t *)calloc(sizeof(regmatch_t),re.re_nsub+1);
/* start with a buffer that is twice the size of the stringo
we're doing replacements in */
buf_len = 2 * string_len + 1;
buf = (char*)calloc(buf_len ,sizeof(char) );
err = pos = 0;
buf[0] = '\0';
while (!err) {
//etrange... l'appel suivant segfault depuis un sighandler...
err = regexec(&re, &string[pos], re.re_nsub+1, subs, (pos ? REG_NOTBOL : 0));
if (err && err != REG_NOMATCH) {
free(subs);
free(buf);
char errBuf[1024];
regerror(err,&re,errBuf,1024);
throw RegularExpressionException(errBuf);
}
if (!err) {
/* backref replacement is done in two passes:
1) find out how long the string will be, and allocate buf
2) copy the part before match, replacement and backrefs to buf
Jaakko Hyv?tti <[email protected]>
*/
new_l = strlen(buf) + subs[0].rm_so; /* part before the match */
walk = replace;
while (*walk) {
if ( '\\' == *walk
&& isdigit((unsigned char)walk[1])
&& ((unsigned char)walk[1]) - '0' <= int(re.re_nsub)) {
if (subs[walk[1] - '0'].rm_so > -1 && subs[walk[1] - '0'].rm_eo > -1) {
new_l += subs[walk[1] - '0'].rm_eo - subs[walk[1] - '0'].rm_so;
}
walk += 2;
} else {
new_l++;
walk++;
}
}
if (new_l + 1 > buf_len) {
buf_len = 1 + buf_len + 2 * new_l;
nbuf = (char*)malloc(buf_len);
strcpy(nbuf, buf);
free(buf);
buf = nbuf;
}
tmp = strlen(buf);
/* copy the part of the string before the match */
strncat(buf, &string[pos], subs[0].rm_so);
/* copy replacement and backrefs */
walkbuf = &buf[tmp + subs[0].rm_so];
walk = replace;
while (*walk) {
if ( '\\' == *walk
&& isdigit(walk[1])
&& walk[1] - '0' <= int(re.re_nsub)) {
if (subs[walk[1] - '0'].rm_so > -1 && subs[walk[1] - '0'].rm_eo > -1
/* this next case shouldn't happen. it does. */
&& subs[walk[1] - '0'].rm_so <= subs[walk[1] - '0'].rm_eo) {
tmp = subs[walk[1] - '0'].rm_eo - subs[walk[1] - '0'].rm_so;
memcpy (walkbuf, &string[pos + subs[walk[1] - '0'].rm_so], tmp);
walkbuf += tmp;
}
walk += 2;
} else {
*walkbuf++ = *walk++;
}
}
*walkbuf = '\0';
/* and get ready to keep looking for replacements */
if (subs[0].rm_so == subs[0].rm_eo) {
if (subs[0].rm_so + pos >= string_len) {
break;
}
new_l = strlen (buf) + 1;
if (new_l + 1 > buf_len) {
buf_len = 1 + buf_len + 2 * new_l;
nbuf = (char*)calloc(buf_len, sizeof(char));
strcpy(nbuf, buf);
free(buf);
buf = nbuf;
}
pos += subs[0].rm_eo + 1;
buf [new_l-1] = string [pos-1];
buf [new_l] = '\0';
} else {
pos += subs[0].rm_eo;
}
} else { /* REG_NOMATCH */
new_l = strlen(buf) + strlen(&string[pos]);
if (new_l + 1 > buf_len) {
buf_len = new_l + 1; /* now we know exactly how long it is */
nbuf = (char*)calloc(buf_len, sizeof(char));
strcpy(nbuf, buf);
free(buf);
buf = nbuf;
}
/* stick that last bit of string on our output */
strcat(buf, &string[pos]);
}
}
/* don't want to leak memory .. */
free(subs);
/* whew. */
String retString(buf);
free(buf);
return retString;
}
Vector<String> Regex::split(const String& str, bool notbol, bool noteol) const {
compile(REPLACE);
Vector<String> vec;
const char * string = str.c_str();
int nmatch = 1;
regmatch_t pmatch[1];
regoff_t& so=pmatch[0].rm_so;
regoff_t& eo=pmatch[0].rm_eo;
int err;
while ( ( err=regexec(&re,string,nmatch, pmatch, (notbol?REG_NOTBOL:0) | (noteol?REG_NOTEOL:0)) ) == REG_NOERROR ) {
vec.push_back(String(string,so));
string+=eo;
}
if ( err == REG_NOMATCH ) {
vec.push_back(string);
}
else if (err) {
char errBuf[1024];
regerror(err,&re,errBuf,1024);
throw RegularExpressionException(errBuf);
}
return vec;
}
Vector<String> Regex::substrs(const String& str, bool notbol, bool noteol) const {
compile(REPLACE);
Vector<String> vec;
const char * string = str.c_str();
int nmatch = 1;
regmatch_t pmatch[1];
regoff_t& so=pmatch[0].rm_so;
regoff_t& eo=pmatch[0].rm_eo;
int err;
while ( ( err=regexec(&re,string,nmatch, pmatch, (notbol?REG_NOTBOL:0) | (noteol?REG_NOTEOL:0)) ) == REG_NOERROR ) {
string+=so;
vec.push_back(String(string,eo-so));
string+=eo;
}
if (err && err != REG_NOMATCH) {
char errBuf[1024];
regerror(err,&re,errBuf,1024);
throw RegularExpressionException(errBuf);
}
return vec;
}
Regex::~Regex() {
regfree(&re);
}
}