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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
/***********************************************************
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI or Corporation for National Research Initiatives or
CNRI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.
While CWI is the initial source for this software, a modified version
is made available by the Corporation for National Research Initiatives
(CNRI) at the Internet address ftp://ftp.python.org.
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
******************************************************************/
/* $Id$ */
/* Regular expression objects */
/* This uses Tatu Ylonen's copyleft-free reimplementation of
GNU regular expressions */
#include "Python.h"
#include <ctype.h>
#include "regexpr.h"
static PyObject *ReopError; /* Exception */
static PyObject *
makeresult(regs, num_regs)
struct re_registers *regs;
int num_regs;
{
PyObject *v;
int i;
static PyObject *filler = NULL;
if (filler == NULL) {
filler = Py_BuildValue("(ii)", -1, -1);
if (filler == NULL)
return NULL;
}
v = PyTuple_New(num_regs);
if (v == NULL)
return NULL;
for (i = 0; i < num_regs; i++) {
int lo = regs->start[i];
int hi = regs->end[i];
PyObject *w;
if (lo == -1 && hi == -1) {
w = filler;
Py_INCREF(w);
}
else
w = Py_BuildValue("(ii)", lo, hi);
if (w == NULL || PyTuple_SetItem(v, i, w) < 0) {
Py_DECREF(v);
return NULL;
}
}
return v;
}
static PyObject *
reop_match(self, args)
PyObject *self;
PyObject *args;
{
char *string;
int fastmaplen, stringlen;
int can_be_null, anchor, i;
int num_regs, flags, pos, result;
struct re_pattern_buffer bufp;
struct re_registers re_regs;
if (!PyArg_Parse(args, "(s#iiis#is#i)",
&(bufp.buffer), &(bufp.allocated),
&num_regs, &flags, &can_be_null,
&(bufp.fastmap), &fastmaplen,
&anchor,
&string, &stringlen,
&pos))
return NULL;
/* XXX sanity-check the input data */
bufp.used=bufp.allocated;
bufp.translate=NULL;
bufp.fastmap_accurate=1;
bufp.can_be_null=can_be_null;
bufp.uses_registers=1;
bufp.num_registers=num_regs;
bufp.anchor=anchor;
for(i=0; i<num_regs; i++) {re_regs.start[i]=-1; re_regs.end[i]=-1;}
result = re_match(&bufp,
string, stringlen, pos,
&re_regs);
if (result < -1) {
/* Failure like stack overflow */
PyErr_SetString(ReopError, "match failure");
return NULL;
}
return makeresult(&re_regs, num_regs);
}
static PyObject *
reop_search(self, args)
PyObject *self;
PyObject *args;
{
char *string;
int fastmaplen, stringlen;
int can_be_null, anchor, i;
int num_regs, flags, pos, result;
struct re_pattern_buffer bufp;
struct re_registers re_regs;
if (!PyArg_Parse(args, "(s#iiis#is#i)",
&(bufp.buffer), &(bufp.allocated),
&num_regs, &flags, &can_be_null,
&(bufp.fastmap), &fastmaplen,
&anchor,
&string, &stringlen,
&pos))
return NULL;
/* XXX sanity-check the input data */
bufp.used=bufp.allocated;
bufp.translate=NULL;
bufp.fastmap_accurate=1;
bufp.can_be_null=can_be_null;
bufp.uses_registers=1;
bufp.num_registers=1;
bufp.anchor=anchor;
for(i=0; i<num_regs; i++) {re_regs.start[i]=-1; re_regs.end[i]=-1;}
result = re_search(&bufp,
string, stringlen, pos, stringlen-pos,
&re_regs);
if (result < -1) {
/* Failure like stack overflow */
PyErr_SetString(ReopError, "match failure");
return NULL;
}
return makeresult(&re_regs, num_regs);
}
#if 0
/* Functions originally in the regsub module.
Added June 1, 1997.
*/
/* A cache of previously used patterns is maintained. Notice that if
you change the reop syntax flag, entries in the cache are
invalidated.
XXX Solution: use (syntax flag, pattern) as keys? Clear the cache
every so often, or once it gets past a certain size?
*/
static PyObject *cache_dict=NULL;
/* Accept an object; if it's a reop pattern, Py_INCREF it and return
it. If it's a string, a reop object is compiled and cached.
*/
static reopobject *
cached_compile(pattern)
PyObject *pattern;
{
reopobject *p2;
if (!PyString_Check(pattern))
{
/* It's not a string, so assume it's a compiled reop object */
/* XXX check that! */
Py_INCREF(pattern);
return (reopobject*)pattern;
}
if (cache_dict==NULL)
{
cache_dict=PyDict_New();
if (cache_dict==NULL)
{
return (reopobject*)NULL;
}
}
/* See if the pattern has already been cached; if so, return that
reop object */
p2=(reopobject*)PyDict_GetItem(cache_dict, pattern);
if (p2)
{
Py_INCREF(p2);
return (reopobject*)p2;
}
/* Compile the pattern and cache it */
p2=(reopobject*)newreopobject(pattern, NULL, pattern, NULL);
if (!p2) return p2;
PyDict_SetItem(cache_dict, pattern, (PyObject*)p2);
return p2;
}
static PyObject *
internal_split(args, retain)
PyObject *args;
int retain;
{
PyObject *newlist, *s;
reopobject *pattern;
int maxsplit=0, count=0, length, next=0, result;
int match_end=0; /* match_start is defined below */
char *start;
if (!PyArg_ParseTuple(args, "s#Oi", &start, &length, &pattern,
&maxsplit))
{
PyErr_Clear();
if (!PyArg_ParseTuple(args, "s#O", &start, &length, &pattern))
return NULL;
}
pattern=cached_compile((PyObject *)pattern);
if (!pattern) return NULL;
newlist=PyList_New(0);
if (!newlist) return NULL;
do
{
result = re_search(&pattern->re_patbuf,
start, length, next, length-next,
&pattern->re_regs);
if (result < -1)
{ /* Erk... an error happened during the reop search */
Py_DECREF(newlist);
PyErr_SetString(ReopError, "match failure");
return NULL;
}
if (next<=result)
{
int match_start=pattern->re_regs.start[0];
int oldmatch_end=match_end;
match_end=pattern->re_regs.end[0];
if (match_start==match_end)
{ /* A zero-length match; increment to the next position */
next=result+1;
match_end=oldmatch_end;
continue;
}
/* Append the string up to the start of the match */
s=PyString_FromStringAndSize(start+oldmatch_end, match_start-oldmatch_end);
if (!s)
{
Py_DECREF(newlist);
return NULL;
}
PyList_Append(newlist, s);
Py_DECREF(s);
if (retain)
{
/* Append a string containing whatever matched */
s=PyString_FromStringAndSize(start+match_start, match_end-match_start);
if (!s)
{
Py_DECREF(newlist);
return NULL;
}
PyList_Append(newlist, s);
Py_DECREF(s);
}
/* Update the pointer, and increment the count of splits */
next=match_end; count++;
}
} while (result!=-1 && !(maxsplit && maxsplit==count) &&
next<length);
s=PyString_FromStringAndSize(start+match_end, length-match_end);
if (!s)
{
Py_DECREF(newlist);
return NULL;
}
PyList_Append(newlist, s);
Py_DECREF(s);
Py_DECREF(pattern);
return newlist;
}
static PyObject *
reop_split(self, args)
PyObject *self;
PyObject *args;
{
return internal_split(args, 0);
}
static PyObject *
reop_splitx(self, args)
PyObject *self;
PyObject *args;
{
return internal_split(args, 1);
}
#endif
static struct PyMethodDef reop_global_methods[] = {
{"match", reop_match, 0},
{"search", reop_search, 0},
#if 0
{"split", reop_split, 0},
{"splitx", reop_splitx, 0},
#endif
{NULL, NULL} /* sentinel */
};
void
initreop()
{
PyObject *m, *d, *v;
int i;
char *s;
m = Py_InitModule("reop", reop_global_methods);
d = PyModule_GetDict(m);
/* Initialize reop.error exception */
v = ReopError = PyString_FromString("reop.error");
if (v == NULL || PyDict_SetItemString(d, "error", v) != 0)
goto finally;
/* Initialize reop.casefold constant */
if (!(v = PyString_FromStringAndSize((char *)NULL, 256)))
goto finally;
if (!(s = PyString_AsString(v)))
goto finally;
for (i = 0; i < 256; i++) {
if (isupper(i))
s[i] = tolower(i);
else
s[i] = i;
}
if (PyDict_SetItemString(d, "casefold", v) < 0)
goto finally;
Py_DECREF(v);
if (!PyErr_Occurred())
return;
finally:
Py_FatalError("can't initialize reop module");
}
|