blob: 33801afa90554654d51b31d1fb11370caedf6052 [file] [log] [blame]
Guido van Rossum6f4c43d1991-12-30 01:42:57 +00001/*
Guido van Rossum6f4c43d1991-12-30 01:42:57 +00002XXX support range parameter on search
3XXX support mstop parameter on search
4*/
5
6/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00007Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
8The Netherlands.
Guido van Rossum6f4c43d1991-12-30 01:42:57 +00009
10 All Rights Reserved
11
12Permission to use, copy, modify, and distribute this software and its
13documentation for any purpose and without fee is hereby granted,
14provided that the above copyright notice appear in all copies and that
15both that copyright notice and this permission notice appear in
16supporting documentation, and that the names of Stichting Mathematisch
17Centrum or CWI not be used in advertising or publicity pertaining to
18distribution of the software without specific, written prior permission.
19
20STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
21THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
22FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
23FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
24WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
25ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
26OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27
28******************************************************************/
29
30/* Regular expression objects */
Guido van Rossum1cab95c1992-01-19 16:31:57 +000031/* This uses Tatu Ylonen's copyleft-free reimplementation of
32 GNU regular expressions */
Guido van Rossum6f4c43d1991-12-30 01:42:57 +000033
34#include "allobjects.h"
35#include "modsupport.h"
36
Guido van Rossum1cab95c1992-01-19 16:31:57 +000037#include "regexpr.h"
Guido van Rossum7f7f2741995-02-10 17:01:56 +000038#include <ctype.h>
Guido van Rossum6f4c43d1991-12-30 01:42:57 +000039
40static object *RegexError; /* Exception */
41
42typedef struct {
43 OB_HEAD
Guido van Rossum6f4c43d1991-12-30 01:42:57 +000044 struct re_pattern_buffer re_patbuf; /* The compiled expression */
45 struct re_registers re_regs; /* The registers from the last match */
Guido van Rossum6f4c43d1991-12-30 01:42:57 +000046 char re_fastmap[256]; /* Storage for fastmap */
Guido van Rossum36d330b1993-02-21 20:12:16 +000047 object *re_translate; /* String object for translate table */
48 object *re_lastok; /* String object last matched/searched */
Guido van Rossumb6775db1994-08-01 11:34:53 +000049 object *re_groupindex; /* Group name to index dictionary */
50 object *re_givenpat; /* Pattern with symbolic groups */
51 object *re_realpat; /* Pattern without symbolic groups */
Guido van Rossum6f4c43d1991-12-30 01:42:57 +000052} regexobject;
53
54/* Regex object methods */
55
56static void
57reg_dealloc(re)
58 regexobject *re;
59{
Guido van Rossum12539601995-01-26 00:37:01 +000060 XDEL(re->re_patbuf.buffer);
Guido van Rossum36d330b1993-02-21 20:12:16 +000061 XDECREF(re->re_translate);
62 XDECREF(re->re_lastok);
Guido van Rossumb6775db1994-08-01 11:34:53 +000063 XDECREF(re->re_groupindex);
64 XDECREF(re->re_givenpat);
65 XDECREF(re->re_realpat);
Guido van Rossum6f4c43d1991-12-30 01:42:57 +000066 DEL(re);
67}
68
69static object *
70makeresult(regs)
71 struct re_registers *regs;
72{
73 object *v = newtupleobject(RE_NREGS);
74 if (v != NULL) {
75 int i;
76 for (i = 0; i < RE_NREGS; i++) {
Guido van Rossum36d330b1993-02-21 20:12:16 +000077 object *w;
78 w = mkvalue("(ii)", regs->start[i], regs->end[i]);
79 if (w == NULL) {
80 XDECREF(v);
81 v = NULL;
82 break;
Guido van Rossum6f4c43d1991-12-30 01:42:57 +000083 }
84 settupleitem(v, i, w);
85 }
86 }
87 return v;
88}
89
90static object *
91reg_match(re, args)
92 regexobject *re;
93 object *args;
94{
Guido van Rossum36d330b1993-02-21 20:12:16 +000095 object *argstring;
Guido van Rossum6f4c43d1991-12-30 01:42:57 +000096 char *buffer;
Guido van Rossumd577c0c1992-01-27 16:46:19 +000097 int size;
Guido van Rossum6f4c43d1991-12-30 01:42:57 +000098 int offset;
99 int result;
Guido van Rossum36d330b1993-02-21 20:12:16 +0000100 if (getargs(args, "S", &argstring)) {
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000101 offset = 0;
102 }
Guido van Rossumd577c0c1992-01-27 16:46:19 +0000103 else {
104 err_clear();
Guido van Rossum36d330b1993-02-21 20:12:16 +0000105 if (!getargs(args, "(Si)", &argstring, &offset))
Guido van Rossumd577c0c1992-01-27 16:46:19 +0000106 return NULL;
107 }
Guido van Rossum36d330b1993-02-21 20:12:16 +0000108 buffer = getstringvalue(argstring);
109 size = getstringsize(argstring);
110 if (offset < 0 || offset > size) {
111 err_setstr(RegexError, "match offset out of range");
112 return NULL;
113 }
114 XDECREF(re->re_lastok);
115 re->re_lastok = NULL;
Guido van Rossumd577c0c1992-01-27 16:46:19 +0000116 result = re_match(&re->re_patbuf, buffer, size, offset, &re->re_regs);
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000117 if (result < -1) {
118 /* Failure like stack overflow */
119 err_setstr(RegexError, "match failure");
120 return NULL;
121 }
Guido van Rossum36d330b1993-02-21 20:12:16 +0000122 if (result >= 0) {
123 INCREF(argstring);
124 re->re_lastok = argstring;
125 }
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000126 return newintobject((long)result); /* Length of the match or -1 */
127}
Guido van Rossumd577c0c1992-01-27 16:46:19 +0000128
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000129static object *
130reg_search(re, args)
131 regexobject *re;
132 object *args;
133{
Guido van Rossum36d330b1993-02-21 20:12:16 +0000134 object *argstring;
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000135 char *buffer;
136 int size;
137 int offset;
138 int range;
139 int result;
Guido van Rossumd577c0c1992-01-27 16:46:19 +0000140
Guido van Rossum36d330b1993-02-21 20:12:16 +0000141 if (getargs(args, "S", &argstring)) {
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000142 offset = 0;
143 }
Guido van Rossumd577c0c1992-01-27 16:46:19 +0000144 else {
145 err_clear();
Guido van Rossum36d330b1993-02-21 20:12:16 +0000146 if (!getargs(args, "(Si)", &argstring, &offset))
Guido van Rossumd577c0c1992-01-27 16:46:19 +0000147 return NULL;
Guido van Rossum36d330b1993-02-21 20:12:16 +0000148 }
149 buffer = getstringvalue(argstring);
150 size = getstringsize(argstring);
151 if (offset < 0 || offset > size) {
152 err_setstr(RegexError, "search offset out of range");
153 return NULL;
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000154 }
Guido van Rossumd577c0c1992-01-27 16:46:19 +0000155 /* NB: In Emacs 18.57, the documentation for re_search[_2] and
156 the implementation don't match: the documentation states that
157 |range| positions are tried, while the code tries |range|+1
158 positions. It seems more productive to believe the code! */
Guido van Rossum2d785901992-01-26 18:12:41 +0000159 range = size - offset;
Guido van Rossum36d330b1993-02-21 20:12:16 +0000160 XDECREF(re->re_lastok);
161 re->re_lastok = NULL;
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000162 result = re_search(&re->re_patbuf, buffer, size, offset, range,
163 &re->re_regs);
164 if (result < -1) {
165 /* Failure like stack overflow */
166 err_setstr(RegexError, "match failure");
167 return NULL;
168 }
Guido van Rossum36d330b1993-02-21 20:12:16 +0000169 if (result >= 0) {
170 INCREF(argstring);
171 re->re_lastok = argstring;
172 }
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000173 return newintobject((long)result); /* Position of the match or -1 */
174}
175
Guido van Rossum36d330b1993-02-21 20:12:16 +0000176static object *
Guido van Rossumccd5bad1993-02-23 13:42:39 +0000177reg_group(re, args)
Guido van Rossum36d330b1993-02-21 20:12:16 +0000178 regexobject *re;
179 object *args;
180{
181 int i, a, b;
182 if (args != NULL && is_tupleobject(args)) {
183 int n = gettuplesize(args);
184 object *res = newtupleobject(n);
185 if (res == NULL)
186 return NULL;
187 for (i = 0; i < n; i++) {
Guido van Rossumccd5bad1993-02-23 13:42:39 +0000188 object *v = reg_group(re, gettupleitem(args, i));
Guido van Rossum36d330b1993-02-21 20:12:16 +0000189 if (v == NULL) {
190 DECREF(res);
191 return NULL;
192 }
193 settupleitem(res, i, v);
194 }
195 return res;
196 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000197 if (!getargs(args, "i", &i)) {
198 object *n;
199 err_clear();
200 if (!getargs(args, "S", &n))
201 return NULL;
202 else {
203 object *index;
204 if (re->re_groupindex == NULL)
205 index = NULL;
206 else
207 index = mappinglookup(re->re_groupindex, n);
208 if (index == NULL) {
209 err_setstr(RegexError, "group() group name doesn't exist");
210 return NULL;
211 }
212 i = getintvalue(index);
213 }
214 }
Guido van Rossum36d330b1993-02-21 20:12:16 +0000215 if (i < 0 || i >= RE_NREGS) {
Guido van Rossumccd5bad1993-02-23 13:42:39 +0000216 err_setstr(RegexError, "group() index out of range");
Guido van Rossum36d330b1993-02-21 20:12:16 +0000217 return NULL;
218 }
219 if (re->re_lastok == NULL) {
220 err_setstr(RegexError,
Guido van Rossumccd5bad1993-02-23 13:42:39 +0000221 "group() only valid after successful match/search");
Guido van Rossum36d330b1993-02-21 20:12:16 +0000222 return NULL;
223 }
224 a = re->re_regs.start[i];
225 b = re->re_regs.end[i];
226 if (a < 0 || b < 0) {
227 INCREF(None);
228 return None;
229 }
230 return newsizedstringobject(getstringvalue(re->re_lastok)+a, b-a);
231}
232
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000233static struct methodlist reg_methods[] = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000234 {"match", (method)reg_match},
235 {"search", (method)reg_search},
236 {"group", (method)reg_group},
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000237 {NULL, NULL} /* sentinel */
238};
239
240static object *
241reg_getattr(re, name)
242 regexobject *re;
243 char *name;
244{
Guido van Rossumb824fc61992-01-01 14:52:16 +0000245 if (strcmp(name, "regs") == 0) {
Guido van Rossum36d330b1993-02-21 20:12:16 +0000246 if (re->re_lastok == NULL) {
Guido van Rossumccd5bad1993-02-23 13:42:39 +0000247 INCREF(None);
248 return None;
Guido van Rossumb824fc61992-01-01 14:52:16 +0000249 }
250 return makeresult(&re->re_regs);
251 }
Guido van Rossum36d330b1993-02-21 20:12:16 +0000252 if (strcmp(name, "last") == 0) {
253 if (re->re_lastok == NULL) {
Guido van Rossumccd5bad1993-02-23 13:42:39 +0000254 INCREF(None);
255 return None;
Guido van Rossum36d330b1993-02-21 20:12:16 +0000256 }
257 INCREF(re->re_lastok);
258 return re->re_lastok;
259 }
Guido van Rossumccd5bad1993-02-23 13:42:39 +0000260 if (strcmp(name, "translate") == 0) {
261 if (re->re_translate == NULL) {
262 INCREF(None);
263 return None;
264 }
265 INCREF(re->re_translate);
266 return re->re_translate;
267 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000268 if (strcmp(name, "groupindex") == 0) {
269 if (re->re_groupindex == NULL) {
270 INCREF(None);
271 return None;
272 }
273 INCREF(re->re_groupindex);
274 return re->re_groupindex;
275 }
276 if (strcmp(name, "realpat") == 0) {
277 if (re->re_realpat == NULL) {
278 INCREF(None);
279 return None;
280 }
281 INCREF(re->re_realpat);
282 return re->re_realpat;
283 }
284 if (strcmp(name, "givenpat") == 0) {
285 if (re->re_givenpat == NULL) {
286 INCREF(None);
287 return None;
288 }
289 INCREF(re->re_givenpat);
290 return re->re_givenpat;
291 }
Guido van Rossumccd5bad1993-02-23 13:42:39 +0000292 if (strcmp(name, "__members__") == 0) {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000293 object *list = newlistobject(6);
Guido van Rossumccd5bad1993-02-23 13:42:39 +0000294 if (list) {
295 setlistitem(list, 0, newstringobject("last"));
296 setlistitem(list, 1, newstringobject("regs"));
297 setlistitem(list, 2, newstringobject("translate"));
Guido van Rossumb6775db1994-08-01 11:34:53 +0000298 setlistitem(list, 3, newstringobject("groupindex"));
299 setlistitem(list, 4, newstringobject("realpat"));
300 setlistitem(list, 5, newstringobject("givenpat"));
Guido van Rossumccd5bad1993-02-23 13:42:39 +0000301 if (err_occurred()) {
302 DECREF(list);
303 list = NULL;
304 }
305 }
306 return list;
307 }
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000308 return findmethod(reg_methods, (object *)re, name);
309}
310
311static typeobject Regextype = {
312 OB_HEAD_INIT(&Typetype)
313 0, /*ob_size*/
314 "regex", /*tp_name*/
315 sizeof(regexobject), /*tp_size*/
316 0, /*tp_itemsize*/
317 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000318 (destructor)reg_dealloc, /*tp_dealloc*/
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000319 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000320 (getattrfunc)reg_getattr, /*tp_getattr*/
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000321 0, /*tp_setattr*/
322 0, /*tp_compare*/
323 0, /*tp_repr*/
324};
325
Guido van Rossumd577c0c1992-01-27 16:46:19 +0000326static object *
Guido van Rossumb6775db1994-08-01 11:34:53 +0000327newregexobject(pattern, translate, givenpat, groupindex)
328 object *pattern;
Guido van Rossum36d330b1993-02-21 20:12:16 +0000329 object *translate;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000330 object *givenpat;
331 object *groupindex;
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000332{
333 regexobject *re;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000334 char *pat = getstringvalue(pattern);
335 int size = getstringsize(pattern);
336
Guido van Rossum36d330b1993-02-21 20:12:16 +0000337 if (translate != NULL && getstringsize(translate) != 256) {
338 err_setstr(RegexError,
339 "translation table must be 256 bytes");
340 return NULL;
341 }
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000342 re = NEWOBJ(regexobject, &Regextype);
343 if (re != NULL) {
344 char *error;
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000345 re->re_patbuf.buffer = NULL;
346 re->re_patbuf.allocated = 0;
347 re->re_patbuf.fastmap = re->re_fastmap;
Guido van Rossum36d330b1993-02-21 20:12:16 +0000348 if (translate)
349 re->re_patbuf.translate = getstringvalue(translate);
350 else
351 re->re_patbuf.translate = NULL;
352 XINCREF(translate);
353 re->re_translate = translate;
354 re->re_lastok = NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000355 re->re_groupindex = groupindex;
356 INCREF(pattern);
357 re->re_realpat = pattern;
358 INCREF(givenpat);
359 re->re_givenpat = givenpat;
Guido van Rossumd577c0c1992-01-27 16:46:19 +0000360 error = re_compile_pattern(pat, size, &re->re_patbuf);
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000361 if (error != NULL) {
362 err_setstr(RegexError, error);
363 DECREF(re);
364 re = NULL;
365 }
366 }
Guido van Rossumd577c0c1992-01-27 16:46:19 +0000367 return (object *)re;
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000368}
369
370static object *
371regex_compile(self, args)
372 object *self;
373 object *args;
374{
Guido van Rossumb6775db1994-08-01 11:34:53 +0000375 object *pat = NULL;
Guido van Rossum36d330b1993-02-21 20:12:16 +0000376 object *tran = NULL;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000377 if (!getargs(args, "S", &pat)) {
Guido van Rossum36d330b1993-02-21 20:12:16 +0000378 err_clear();
Guido van Rossumb6775db1994-08-01 11:34:53 +0000379 if (!getargs(args, "(SS)", &pat, &tran))
Guido van Rossum36d330b1993-02-21 20:12:16 +0000380 return NULL;
381 }
Guido van Rossumb6775db1994-08-01 11:34:53 +0000382 return newregexobject(pat, tran, pat, NULL);
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000383}
384
Guido van Rossumb6775db1994-08-01 11:34:53 +0000385static object *
386symcomp(pattern, gdict)
387 object *pattern;
388 object *gdict;
389{
390 char *opat = getstringvalue(pattern);
391 char *oend = opat + getstringsize(pattern);
392 int group_count = 0;
393 int escaped = 0;
394 char *o = opat;
395 char *n;
396 char name_buf[128];
397 char *g;
398 object *npattern;
399 int require_escape = re_syntax & RE_NO_BK_PARENS ? 0 : 1;
400
Guido van Rossumab28c561996-06-11 18:33:14 +0000401 if (oend == opat) {
402 INCREF(pattern);
403 return pattern;
404 }
405
Guido van Rossumb6775db1994-08-01 11:34:53 +0000406 npattern = newsizedstringobject((char*)NULL, getstringsize(pattern));
407 if (npattern == NULL)
408 return NULL;
409 n = getstringvalue(npattern);
410
411 while (o < oend) {
412 if (*o == '(' && escaped == require_escape) {
413 char *backtrack;
414 escaped = 0;
415 ++group_count;
416 *n++ = *o;
417 if (++o >= oend || *o != '<')
418 continue;
419 /* *o == '<' */
420 if (o+1 < oend && *(o+1) == '>')
421 continue;
422 backtrack = o;
423 g = name_buf;
424 for (++o; o < oend;) {
425 if (*o == '>') {
426 object *group_name = NULL;
427 object *group_index = NULL;
428 *g++ = '\0';
429 group_name = newstringobject(name_buf);
430 group_index = newintobject(group_count);
431 if (group_name == NULL || group_index == NULL
432 || mappinginsert(gdict, group_name, group_index) != 0) {
433 XDECREF(group_name);
434 XDECREF(group_index);
435 XDECREF(npattern);
436 return NULL;
437 }
438 ++o; /* eat the '>' */
439 break;
440 }
Guido van Rossum7f7f2741995-02-10 17:01:56 +0000441 if (!isalnum(Py_CHARMASK(*o)) && *o != '_') {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000442 o = backtrack;
443 break;
444 }
445 *g++ = *o++;
446 }
447 }
448 if (*o == '[' && !escaped) {
449 *n++ = *o;
450 ++o; /* eat the char following '[' */
451 *n++ = *o;
452 while (o < oend && *o != ']') {
453 ++o;
454 *n++ = *o;
455 }
456 if (o < oend)
457 ++o;
458 }
459 else if (*o == '\\') {
460 escaped = 1;
461 *n++ = *o;
462 ++o;
463 }
464 else {
465 escaped = 0;
466 *n++ = *o;
467 ++o;
468 }
469 }
470
471 if (resizestring(&npattern, n - getstringvalue(npattern)) == 0)
472 return npattern;
473 else {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000474 return NULL;
475 }
476
477}
478
479static object *
480regex_symcomp(self, args)
481 object *self;
482 object *args;
483{
484 object *pattern;
485 object *tran = NULL;
486 object *gdict = NULL;
487 object *npattern;
488 if (!getargs(args, "S", &pattern)) {
489 err_clear();
490 if (!getargs(args, "(SS)", &pattern, &tran))
491 return NULL;
492 }
493 gdict = newmappingobject();
494 if (gdict == NULL
495 || (npattern = symcomp(pattern, gdict)) == NULL) {
496 DECREF(gdict);
497 DECREF(pattern);
498 return NULL;
499 }
500 return newregexobject(npattern, tran, pattern, gdict);
501}
502
503
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000504static object *cache_pat;
505static object *cache_prog;
506
507static int
508update_cache(pat)
509 object *pat;
510{
511 if (pat != cache_pat) {
512 XDECREF(cache_pat);
513 cache_pat = NULL;
514 XDECREF(cache_prog);
515 cache_prog = regex_compile((object *)NULL, pat);
516 if (cache_prog == NULL)
517 return -1;
518 cache_pat = pat;
519 INCREF(cache_pat);
520 }
521 return 0;
522}
523
524static object *
525regex_match(self, args)
526 object *self;
527 object *args;
528{
529 object *pat, *string;
Guido van Rossum36d330b1993-02-21 20:12:16 +0000530 if (!getargs(args, "(SS)", &pat, &string))
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000531 return NULL;
532 if (update_cache(pat) < 0)
533 return NULL;
534 return reg_match((regexobject *)cache_prog, string);
535}
536
537static object *
538regex_search(self, args)
539 object *self;
540 object *args;
541{
542 object *pat, *string;
Guido van Rossum36d330b1993-02-21 20:12:16 +0000543 if (!getargs(args, "(SS)", &pat, &string))
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000544 return NULL;
545 if (update_cache(pat) < 0)
546 return NULL;
547 return reg_search((regexobject *)cache_prog, string);
548}
549
550static object *
551regex_set_syntax(self, args)
552 object *self, *args;
553{
554 int syntax;
555 if (!getintarg(args, &syntax))
556 return NULL;
557 syntax = re_set_syntax(syntax);
558 return newintobject((long)syntax);
559}
560
561static struct methodlist regex_global_methods[] = {
Guido van Rossum295d1711995-02-19 15:55:19 +0000562 {"compile", regex_compile, 0},
563 {"symcomp", regex_symcomp, 0},
564 {"match", regex_match, 0},
565 {"search", regex_search, 0},
566 {"set_syntax", regex_set_syntax, 0},
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000567 {NULL, NULL} /* sentinel */
568};
569
570initregex()
571{
Guido van Rossumccd5bad1993-02-23 13:42:39 +0000572 object *m, *d, *v;
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000573
574 m = initmodule("regex", regex_global_methods);
575 d = getmoduledict(m);
576
577 /* Initialize regex.error exception */
578 RegexError = newstringobject("regex.error");
579 if (RegexError == NULL || dictinsert(d, "error", RegexError) != 0)
580 fatal("can't define regex.error");
Guido van Rossumccd5bad1993-02-23 13:42:39 +0000581
582 /* Initialize regex.casefold constant */
583 v = newsizedstringobject((char *)NULL, 256);
584 if (v != NULL) {
585 int i;
586 char *s = getstringvalue(v);
587 for (i = 0; i < 256; i++) {
588 if (isupper(i))
589 s[i] = tolower(i);
590 else
591 s[i] = i;
592 }
593 dictinsert(d, "casefold", v);
594 DECREF(v);
595 }
Guido van Rossum6f4c43d1991-12-30 01:42:57 +0000596}