annotate Modules/regexmodule.c @ 3720:b341def4eef8 legacy-trunk

Ignore latex intermediate products.
author Guido van Rossum <guido@python.org>
date Wed, 26 Jun 1996 19:30:01 +0000
parents ad03e21fda54
children 2eaa00860edc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /*
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 XXX support range parameter on search
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 XXX support mstop parameter on search
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 /***********************************************************
2061
172ba04a5191 Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
7 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
172ba04a5191 Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
8 The Netherlands.
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 All Rights Reserved
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 Permission to use, copy, modify, and distribute this software and its
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 documentation for any purpose and without fee is hereby granted,
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 provided that the above copyright notice appear in all copies and that
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 both that copyright notice and this permission notice appear in
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 supporting documentation, and that the names of Stichting Mathematisch
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 Centrum or CWI not be used in advertising or publicity pertaining to
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 distribution of the software without specific, written prior permission.
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 ******************************************************************/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 /* Regular expression objects */
615
40a0ec994c54 Use Tatu Ylonen's copyleft-free reimplementation of
Guido van Rossum <guido@python.org>
parents: 551
diff changeset
31 /* This uses Tatu Ylonen's copyleft-free reimplementation of
40a0ec994c54 Use Tatu Ylonen's copyleft-free reimplementation of
Guido van Rossum <guido@python.org>
parents: 551
diff changeset
32 GNU regular expressions */
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 #include "allobjects.h"
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 #include "modsupport.h"
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
615
40a0ec994c54 Use Tatu Ylonen's copyleft-free reimplementation of
Guido van Rossum <guido@python.org>
parents: 551
diff changeset
37 #include "regexpr.h"
2310
07913fb8f92f use Py_CHARMASK
Guido van Rossum <guido@python.org>
parents: 2234
diff changeset
38 #include <ctype.h>
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 static object *RegexError; /* Exception */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 typedef struct {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 OB_HEAD
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 struct re_pattern_buffer re_patbuf; /* The compiled expression */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 struct re_registers re_regs; /* The registers from the last match */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 char re_fastmap[256]; /* Storage for fastmap */
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
47 object *re_translate; /* String object for translate table */
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
48 object *re_lastok; /* String object last matched/searched */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
49 object *re_groupindex; /* Group name to index dictionary */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
50 object *re_givenpat; /* Pattern with symbolic groups */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
51 object *re_realpat; /* Pattern without symbolic groups */
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 } regexobject;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 /* Regex object methods */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 static void
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 reg_dealloc(re)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 regexobject *re;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 {
2234
7b6540496f55 mem leak
Guido van Rossum <guido@python.org>
parents: 2061
diff changeset
60 XDEL(re->re_patbuf.buffer);
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
61 XDECREF(re->re_translate);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
62 XDECREF(re->re_lastok);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
63 XDECREF(re->re_groupindex);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
64 XDECREF(re->re_givenpat);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
65 XDECREF(re->re_realpat);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 DEL(re);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 static object *
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 makeresult(regs)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 struct re_registers *regs;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 object *v = newtupleobject(RE_NREGS);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 if (v != NULL) {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 int i;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 for (i = 0; i < RE_NREGS; i++) {
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
77 object *w;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
78 w = mkvalue("(ii)", regs->start[i], regs->end[i]);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
79 if (w == NULL) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
80 XDECREF(v);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
81 v = NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
82 break;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 settupleitem(v, i, w);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 return v;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 static object *
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 reg_match(re, args)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 regexobject *re;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 object *args;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 {
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
95 object *argstring;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 char *buffer;
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
97 int size;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 int offset;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 int result;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
100 if (getargs(args, "S", &argstring)) {
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 offset = 0;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 }
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
103 else {
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
104 err_clear();
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
105 if (!getargs(args, "(Si)", &argstring, &offset))
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
106 return NULL;
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
107 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
108 buffer = getstringvalue(argstring);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
109 size = getstringsize(argstring);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
110 if (offset < 0 || offset > size) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
111 err_setstr(RegexError, "match offset out of range");
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
112 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
113 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
114 XDECREF(re->re_lastok);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
115 re->re_lastok = NULL;
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
116 result = re_match(&re->re_patbuf, buffer, size, offset, &re->re_regs);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 if (result < -1) {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 /* Failure like stack overflow */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
119 err_setstr(RegexError, "match failure");
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
122 if (result >= 0) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
123 INCREF(argstring);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
124 re->re_lastok = argstring;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
125 }
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 return newintobject((long)result); /* Length of the match or -1 */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 }
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
128
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 static object *
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 reg_search(re, args)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 regexobject *re;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 object *args;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 {
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
134 object *argstring;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 char *buffer;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 int size;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 int offset;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 int range;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 int result;
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
140
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
141 if (getargs(args, "S", &argstring)) {
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 offset = 0;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 }
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
144 else {
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
145 err_clear();
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
146 if (!getargs(args, "(Si)", &argstring, &offset))
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
147 return NULL;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
148 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
149 buffer = getstringvalue(argstring);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
150 size = getstringsize(argstring);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
151 if (offset < 0 || offset > size) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
152 err_setstr(RegexError, "search offset out of range");
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
153 return NULL;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 }
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
155 /* NB: In Emacs 18.57, the documentation for re_search[_2] and
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
156 the implementation don't match: the documentation states that
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
157 |range| positions are tried, while the code tries |range|+1
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
158 positions. It seems more productive to believe the code! */
628
d420af9c1b75 Make "range" one smaller (discrepancy between doc and source for regex.c!).
Guido van Rossum <guido@python.org>
parents: 615
diff changeset
159 range = size - offset;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
160 XDECREF(re->re_lastok);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
161 re->re_lastok = NULL;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 result = re_search(&re->re_patbuf, buffer, size, offset, range,
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 &re->re_regs);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 if (result < -1) {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 /* Failure like stack overflow */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 err_setstr(RegexError, "match failure");
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
169 if (result >= 0) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
170 INCREF(argstring);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
171 re->re_lastok = argstring;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
172 }
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 return newintobject((long)result); /* Position of the match or -1 */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
176 static object *
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
177 reg_group(re, args)
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
178 regexobject *re;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
179 object *args;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
180 {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
181 int i, a, b;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
182 if (args != NULL && is_tupleobject(args)) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
183 int n = gettuplesize(args);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
184 object *res = newtupleobject(n);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
185 if (res == NULL)
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
186 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
187 for (i = 0; i < n; i++) {
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
188 object *v = reg_group(re, gettupleitem(args, i));
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
189 if (v == NULL) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
190 DECREF(res);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
191 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
192 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
193 settupleitem(res, i, v);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
194 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
195 return res;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
196 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
197 if (!getargs(args, "i", &i)) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
198 object *n;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
199 err_clear();
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
200 if (!getargs(args, "S", &n))
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
201 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
202 else {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
203 object *index;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
204 if (re->re_groupindex == NULL)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
205 index = NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
206 else
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
207 index = mappinglookup(re->re_groupindex, n);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
208 if (index == NULL) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
209 err_setstr(RegexError, "group() group name doesn't exist");
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
210 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
211 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
212 i = getintvalue(index);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
213 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
214 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
215 if (i < 0 || i >= RE_NREGS) {
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
216 err_setstr(RegexError, "group() index out of range");
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
217 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
218 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
219 if (re->re_lastok == NULL) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
220 err_setstr(RegexError,
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
221 "group() only valid after successful match/search");
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
222 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
223 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
224 a = re->re_regs.start[i];
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
225 b = re->re_regs.end[i];
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
226 if (a < 0 || b < 0) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
227 INCREF(None);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
228 return None;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
229 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
230 return newsizedstringobject(getstringvalue(re->re_lastok)+a, b-a);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
231 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
232
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
233 static struct methodlist reg_methods[] = {
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
234 {"match", (method)reg_match},
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
235 {"search", (method)reg_search},
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
236 {"group", (method)reg_group},
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 {NULL, NULL} /* sentinel */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 };
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
239
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 static object *
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 reg_getattr(re, name)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 regexobject *re;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 char *name;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 {
551
5a61f827f47c 'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents: 536
diff changeset
245 if (strcmp(name, "regs") == 0) {
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
246 if (re->re_lastok == NULL) {
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
247 INCREF(None);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
248 return None;
551
5a61f827f47c 'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents: 536
diff changeset
249 }
5a61f827f47c 'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents: 536
diff changeset
250 return makeresult(&re->re_regs);
5a61f827f47c 'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents: 536
diff changeset
251 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
252 if (strcmp(name, "last") == 0) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
253 if (re->re_lastok == NULL) {
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
254 INCREF(None);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
255 return None;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
256 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
257 INCREF(re->re_lastok);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
258 return re->re_lastok;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
259 }
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
260 if (strcmp(name, "translate") == 0) {
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
261 if (re->re_translate == NULL) {
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
262 INCREF(None);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
263 return None;
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
264 }
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
265 INCREF(re->re_translate);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
266 return re->re_translate;
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
267 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
268 if (strcmp(name, "groupindex") == 0) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
269 if (re->re_groupindex == NULL) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
270 INCREF(None);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
271 return None;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
272 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
273 INCREF(re->re_groupindex);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
274 return re->re_groupindex;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
275 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
276 if (strcmp(name, "realpat") == 0) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
277 if (re->re_realpat == NULL) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
278 INCREF(None);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
279 return None;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
280 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
281 INCREF(re->re_realpat);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
282 return re->re_realpat;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
283 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
284 if (strcmp(name, "givenpat") == 0) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
285 if (re->re_givenpat == NULL) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
286 INCREF(None);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
287 return None;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
288 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
289 INCREF(re->re_givenpat);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
290 return re->re_givenpat;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
291 }
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
292 if (strcmp(name, "__members__") == 0) {
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
293 object *list = newlistobject(6);
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
294 if (list) {
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
295 setlistitem(list, 0, newstringobject("last"));
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
296 setlistitem(list, 1, newstringobject("regs"));
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
297 setlistitem(list, 2, newstringobject("translate"));
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
298 setlistitem(list, 3, newstringobject("groupindex"));
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
299 setlistitem(list, 4, newstringobject("realpat"));
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
300 setlistitem(list, 5, newstringobject("givenpat"));
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
301 if (err_occurred()) {
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
302 DECREF(list);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
303 list = NULL;
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
304 }
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
305 }
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
306 return list;
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
307 }
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 return findmethod(reg_methods, (object *)re, name);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
310
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 static typeobject Regextype = {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 OB_HEAD_INIT(&Typetype)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 0, /*ob_size*/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 "regex", /*tp_name*/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 sizeof(regexobject), /*tp_size*/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 0, /*tp_itemsize*/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 /* methods */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
318 (destructor)reg_dealloc, /*tp_dealloc*/
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 0, /*tp_print*/
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
320 (getattrfunc)reg_getattr, /*tp_getattr*/
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 0, /*tp_setattr*/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 0, /*tp_compare*/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 0, /*tp_repr*/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 };
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
325
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
326 static object *
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
327 newregexobject(pattern, translate, givenpat, groupindex)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
328 object *pattern;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
329 object *translate;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
330 object *givenpat;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
331 object *groupindex;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 regexobject *re;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
334 char *pat = getstringvalue(pattern);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
335 int size = getstringsize(pattern);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
336
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
337 if (translate != NULL && getstringsize(translate) != 256) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
338 err_setstr(RegexError,
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
339 "translation table must be 256 bytes");
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
340 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
341 }
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 re = NEWOBJ(regexobject, &Regextype);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 if (re != NULL) {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 char *error;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 re->re_patbuf.buffer = NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 re->re_patbuf.allocated = 0;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 re->re_patbuf.fastmap = re->re_fastmap;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
348 if (translate)
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
349 re->re_patbuf.translate = getstringvalue(translate);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
350 else
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
351 re->re_patbuf.translate = NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
352 XINCREF(translate);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
353 re->re_translate = translate;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
354 re->re_lastok = NULL;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
355 re->re_groupindex = groupindex;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
356 INCREF(pattern);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
357 re->re_realpat = pattern;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
358 INCREF(givenpat);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
359 re->re_givenpat = givenpat;
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
360 error = re_compile_pattern(pat, size, &re->re_patbuf);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 if (error != NULL) {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 err_setstr(RegexError, error);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 DECREF(re);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 re = NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 }
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
367 return (object *)re;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
369
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 static object *
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 regex_compile(self, args)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 object *self;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 object *args;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 {
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
375 object *pat = NULL;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
376 object *tran = NULL;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
377 if (!getargs(args, "S", &pat)) {
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
378 err_clear();
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
379 if (!getargs(args, "(SS)", &pat, &tran))
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
380 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
381 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
382 return newregexobject(pat, tran, pat, NULL);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
384
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
385 static object *
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
386 symcomp(pattern, gdict)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
387 object *pattern;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
388 object *gdict;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
389 {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
390 char *opat = getstringvalue(pattern);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
391 char *oend = opat + getstringsize(pattern);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
392 int group_count = 0;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
393 int escaped = 0;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
394 char *o = opat;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
395 char *n;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
396 char name_buf[128];
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
397 char *g;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
398 object *npattern;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
399 int require_escape = re_syntax & RE_NO_BK_PARENS ? 0 : 1;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
400
3664
ad03e21fda54 Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
401 if (oend == opat) {
ad03e21fda54 Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
402 INCREF(pattern);
ad03e21fda54 Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
403 return pattern;
ad03e21fda54 Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
404 }
ad03e21fda54 Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
405
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
406 npattern = newsizedstringobject((char*)NULL, getstringsize(pattern));
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
407 if (npattern == NULL)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
408 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
409 n = getstringvalue(npattern);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
410
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
411 while (o < oend) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
412 if (*o == '(' && escaped == require_escape) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
413 char *backtrack;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
414 escaped = 0;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
415 ++group_count;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
416 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
417 if (++o >= oend || *o != '<')
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
418 continue;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
419 /* *o == '<' */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
420 if (o+1 < oend && *(o+1) == '>')
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
421 continue;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
422 backtrack = o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
423 g = name_buf;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
424 for (++o; o < oend;) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
425 if (*o == '>') {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
426 object *group_name = NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
427 object *group_index = NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
428 *g++ = '\0';
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
429 group_name = newstringobject(name_buf);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
430 group_index = newintobject(group_count);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
431 if (group_name == NULL || group_index == NULL
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
432 || mappinginsert(gdict, group_name, group_index) != 0) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
433 XDECREF(group_name);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
434 XDECREF(group_index);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
435 XDECREF(npattern);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
436 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
437 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
438 ++o; /* eat the '>' */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
439 break;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
440 }
2310
07913fb8f92f use Py_CHARMASK
Guido van Rossum <guido@python.org>
parents: 2234
diff changeset
441 if (!isalnum(Py_CHARMASK(*o)) && *o != '_') {
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
442 o = backtrack;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
443 break;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
444 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
445 *g++ = *o++;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
446 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
447 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
448 if (*o == '[' && !escaped) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
449 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
450 ++o; /* eat the char following '[' */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
451 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
452 while (o < oend && *o != ']') {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
453 ++o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
454 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
455 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
456 if (o < oend)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
457 ++o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
458 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
459 else if (*o == '\\') {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
460 escaped = 1;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
461 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
462 ++o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
463 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
464 else {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
465 escaped = 0;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
466 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
467 ++o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
468 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
469 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
470
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
471 if (resizestring(&npattern, n - getstringvalue(npattern)) == 0)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
472 return npattern;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
473 else {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
474 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
475 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
476
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
477 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
478
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
479 static object *
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
480 regex_symcomp(self, args)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
481 object *self;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
482 object *args;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
483 {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
484 object *pattern;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
485 object *tran = NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
486 object *gdict = NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
487 object *npattern;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
488 if (!getargs(args, "S", &pattern)) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
489 err_clear();
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
490 if (!getargs(args, "(SS)", &pattern, &tran))
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
491 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
492 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
493 gdict = newmappingobject();
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
494 if (gdict == NULL
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
495 || (npattern = symcomp(pattern, gdict)) == NULL) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
496 DECREF(gdict);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
497 DECREF(pattern);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
498 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
499 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
500 return newregexobject(npattern, tran, pattern, gdict);
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
501 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
502
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
503
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 static object *cache_pat;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
505 static object *cache_prog;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
506
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
507 static int
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
508 update_cache(pat)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 object *pat;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
511 if (pat != cache_pat) {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
512 XDECREF(cache_pat);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
513 cache_pat = NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
514 XDECREF(cache_prog);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
515 cache_prog = regex_compile((object *)NULL, pat);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
516 if (cache_prog == NULL)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
517 return -1;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 cache_pat = pat;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
519 INCREF(cache_pat);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 return 0;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
522 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
523
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
524 static object *
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 regex_match(self, args)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 object *self;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
527 object *args;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
528 {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 object *pat, *string;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
530 if (!getargs(args, "(SS)", &pat, &string))
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
531 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 if (update_cache(pat) < 0)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
533 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
534 return reg_match((regexobject *)cache_prog, string);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
535 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
537 static object *
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
538 regex_search(self, args)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
539 object *self;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 object *args;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
541 {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
542 object *pat, *string;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
543 if (!getargs(args, "(SS)", &pat, &string))
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
544 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
545 if (update_cache(pat) < 0)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
546 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
547 return reg_search((regexobject *)cache_prog, string);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
549
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
550 static object *
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 regex_set_syntax(self, args)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 object *self, *args;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 int syntax;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
555 if (!getintarg(args, &syntax))
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
556 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
557 syntax = re_set_syntax(syntax);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
558 return newintobject((long)syntax);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
559 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
560
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
561 static struct methodlist regex_global_methods[] = {
2397
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2310
diff changeset
562 {"compile", regex_compile, 0},
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2310
diff changeset
563 {"symcomp", regex_symcomp, 0},
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2310
diff changeset
564 {"match", regex_match, 0},
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2310
diff changeset
565 {"search", regex_search, 0},
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2310
diff changeset
566 {"set_syntax", regex_set_syntax, 0},
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
567 {NULL, NULL} /* sentinel */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
568 };
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
569
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
570 initregex()
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 {
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
572 object *m, *d, *v;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
573
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 m = initmodule("regex", regex_global_methods);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 d = getmoduledict(m);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
576
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 /* Initialize regex.error exception */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 RegexError = newstringobject("regex.error");
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
579 if (RegexError == NULL || dictinsert(d, "error", RegexError) != 0)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 fatal("can't define regex.error");
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
581
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
582 /* Initialize regex.casefold constant */
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
583 v = newsizedstringobject((char *)NULL, 256);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
584 if (v != NULL) {
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
585 int i;
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
586 char *s = getstringvalue(v);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
587 for (i = 0; i < 256; i++) {
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
588 if (isupper(i))
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
589 s[i] = tolower(i);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
590 else
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
591 s[i] = i;
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
592 }
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
593 dictinsert(d, "casefold", v);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
594 DECREF(v);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
595 }
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 }