[go: up one dir, main page]

Menu

[897c37]: / modules / regexp.c  Maximize  Restore  History

Download this file

346 lines (287 with data), 8.3 kB

  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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
** $Id: regexp.c,v 1.4 2008/12/18 04:18:43 dredd Exp $
**
** $Source: /cvsroot/swlpc/swlpc/modules/regexp.c,v $
** $Revision: 1.4 $
** $Date: 2008/12/18 04:18:43 $
** $State: Exp $
**
** Author: Geoff Wong, 1999
**
** See the file "Copying" distributed with this file.
**
** An interface to the GNU regular expression library.
*/
#include <stdlib.h>
#include <regex.h>
#include "stack.h"
/*
* Name: index
* Purpose: return the first index of a pat in string (or array?) if
* there is a match, else -1
* Ignores the first 'ignore' number of matches
*/
#define MAX_BUF 1024
Val * lpc_regerror(int code, regex_t * pat)
{
char buf[MAX_BUF];
regerror(code, pat, buf, MAX_BUF);
// CHECK:
regfree(pat);
efun_error("%d: %s", code, buf);
return make_number(-1);
}
Val * lpc_reg_index(Val * str, Val * match, Val * ignore)
{
regex_t rpat;
regmatch_t * pmatch;
int nmatch;
int err;
err = regcomp(&rpat, match->u.string->str, REG_EXTENDED);
if (err) return lpc_regerror(err, &rpat);
nmatch = 1 + ignore->u.number;
pmatch = (regmatch_t *) malloc(nmatch * sizeof(regmatch_t));
err = regexec(&rpat, match->u.string->str, nmatch, pmatch, 0);
if (err) return lpc_regerror(err, &rpat);
// CHECK:
regfree(&rpat);
return make_number(pmatch[ignore->u.number].rm_so);
}
/*
* Name: index
* Purpose: return the last index of a pat in string if
* there is a match, else -1
* Ignores the first 'ignore' number of matches
* going backwards from the end of the string
*/
Val * lpc_reg_rindex(Val * str, Val * pat, Val * ignore)
{
return make_number(-1);
}
/*
* Name: lpc_index_all
* Purpose: return an array of all locations which matched
* the given pat in str (a string)
*/
Val * lpc_reg_index_all(Val * str, Val * match)
{
regex_t rpat;
regmatch_t * pmatch;
int nmatch = 128;
int err;
int i = 0, size;
Val * ret;
err = regcomp(&rpat, match->u.string->str, REG_EXTENDED);
if (err) return lpc_regerror(err, &rpat);
/* the following 3 lines should be in a loop and repeated */
/* until we get ALL of the possible matches */
pmatch = (regmatch_t *) malloc(nmatch * sizeof(regmatch_t));
err = regexec(&rpat, match->u.string->str, nmatch, pmatch, 0);
if (err) return lpc_regerror(err, &rpat);
if (pmatch[i].rm_so == -1)
{
/* no matches! */
regfree(&rpat);
return allocate_array(0);
}
for (i = nmatch - 1; i >= 0; i--)
{
if (pmatch[i].rm_so != -1) break;
}
size = i + 1;
ret = allocate_array(size);
for (i = 0; i < size; i++)
{
assign_value(&ret->u.vec->item[i], make_number(pmatch[i].rm_so));
}
// CHECK:
regfree(&rpat);
return ret;
}
/*
* Name: split
* Purpose: split a string by a regular expression
* Returns: an array of strings
* if max == 0 - do all possible in split & sub
*/
#define MAX_SPLIT 1024
Val * lpc_reg_split(Val * str, Val * match, Val * max)
{
regex_t rpat;
regmatch_t pmatch[1];
int nmatch = 1, num = 0;
int len, err, m, i, start = 0;
int so[MAX_SPLIT];
int eo[MAX_SPLIT];
char * buf;
Val * ret;
err = regcomp(&rpat, match->u.string->str, REG_EXTENDED);
if (err) return lpc_regerror(err,&rpat);
m = max->u.number;
if (!m) m = MAX_SPLIT;
for (i = 0; i < m; i++)
{
err = regexec(&rpat, &(match->u.string->str[start]),
nmatch, pmatch, REG_NOTBOL);
if (err) return lpc_regerror(err,&rpat);
if (pmatch[0].rm_so != -1)
{
num++;
so[i] = pmatch[0].rm_so;
eo[i] = pmatch[0].rm_eo;
start = pmatch[0].rm_eo + 1;
}
else break;
}
ret = allocate_array(num);
start = 0;
buf = (char *) malloc((str->u.string->length + 1) * sizeof(char));
for (i = 0; i < num; i++)
{
len = so[i] - start;
strncpy(buf, &(match->u.string->str[start]), len);
assign_value(&ret->u.vec->item[i], make_string(buf));
start = eo[i] + 1;
}
free(buf);
regfree(&rpat);
return ret;
}
/*
* Name: sub
* Purpose: given a regular expression, substitute max
* matches with the given replacement, 0 == unlimited
*/
Val * lpc_reg_sub(Val * str, Val * match, Val * repl, Val * max)
{
regex_t rpat;
regmatch_t pmatch[1];
int nmatch = 1, num = 0;
int err, m, i, upto, start = 0;
int so[MAX_SPLIT];
int eo[MAX_SPLIT];
char * newstr;
int rl, len, ol = 0;
Val * ret;
err = regcomp(&rpat, match->u.string->str, REG_EXTENDED);
if (err) return lpc_regerror(err,&rpat);
m = max->u.number;
if (!m) m = MAX_SPLIT;
for (i = 0; i < m; i++)
{
err = regexec(&rpat, &(match->u.string->str[start]),
nmatch, pmatch, REG_NOTBOL);
if (err) return lpc_regerror(err,&rpat);
if (pmatch[0].rm_so != -1)
{
num++;
so[i] = pmatch[0].rm_so;
eo[i] = pmatch[0].rm_eo;
start = pmatch[0].rm_eo + 1;
ol = ol + (eo[i] - so[i]);
}
else break;
}
rl = repl->u.string->length * num - ol;
newstr = (char *) malloc(sizeof(char) * (str->u.string->length + rl + 1));
start = 0;
upto = 0;
len = so[0];
for (i = 0; i < num; i++)
{
strncpy(&newstr[start], &(str->u.string->str[upto]), len);
strncpy(&newstr[start + len], repl->u.string->str, repl->u.string->length);
start = start + len + repl->u.string->length;
upto = eo[i] + 1;
len = so[i+1] - eo[i];
}
ret = make_string(newstr);
free(newstr);
regfree(&rpat);
return ret;
}
/*
* Name: lpc_reg_match
* Purpose: given an array of strings check which strings match
* the given pattern
* Returns: an array of strings which matched
* in the original array.
*/
Val * lpc_reg_match(Val * pattern, Val * a)
{
regex_t prog_pat;
regmatch_t pmatch[1];
int nmatch = 1;
struct vector *arr;
int matches, *element_match, i, p, len;
Val *ret;
char *buf;
int err;
err = regcomp(&prog_pat, pattern->u.string->str, REG_EXTENDED);
if (err) return lpc_regerror(err,&prog_pat);
#if 0
if (a->type == T_STRING)
{
err = regexec(&prog_pat, a->u.string->str,
nmatch, pmatch, 0);
if (err) return lpc_regerror(err,&prog_pat);
if (pmatch[0].so != -1) return make_number(1);
return make_number(0);
}
#endif
/* Must have received an array . I only want to have to call
* regexp once, but dont know the size of the
* returning array until its finished, so I make
* a tempory array that signifies if the corresponding
* array element matches. */
arr = a->u.vec;
if (arr->size == 0)
{
/* stupid person match on an empty array */
regfree(&prog_pat);
return allocate_array(0);
}
element_match = (int *) malloc(sizeof(int) * arr->size);
matches = 0;
for (i = 0; i < arr->size; i++)
{
element_match[i] = 0;
if (arr->item[i].type != T_STRING) continue;
err = regexec(&prog_pat, arr->item[i].u.string->str, nmatch, pmatch, 0);
if (err == REG_NOMATCH) continue;
else if (err) return lpc_regerror(err,&prog_pat);
if (pmatch[0].rm_so != -1) // == 0??
{
matches++;
element_match[i] = 1;
}
}
/* Ok, we know which ones match. Now we just need to put them
* into a new array */
ret = allocate_array(matches);
for (i = 0, p = 0; p < matches; i++)
{
if (element_match[i])
{
len = sizeof(char) * arr->item[i].u.string->length;
buf = (char *) malloc(len + 1);
memcpy(buf, arr->item[i].u.string->str, len);
assign_value(&ret->u.vec->item[p], make_nstring(buf,len));
p++;
free(buf);
/* Should I free (buff) ? */
}
}
free(element_match);
regfree(&prog_pat);
return ret;
}
/*
* Name: escape
* Purpose: escape a string, all non-alphanumeric chars
* are backslashed.
*/
Val * lpc_escape(Val * str)
{
return Const(0);
}