Mercurial > cpython-lru_cache-weakref
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 |
| rev | line source |
|---|---|
| 536 | 1 /* |
| 2 XXX support range parameter on search | |
| 3 XXX support mstop parameter on search | |
| 4 */ | |
| 5 | |
| 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 | 9 |
| 10 All Rights Reserved | |
| 11 | |
| 12 Permission to use, copy, modify, and distribute this software and its | |
| 13 documentation for any purpose and without fee is hereby granted, | |
| 14 provided that the above copyright notice appear in all copies and that | |
| 15 both that copyright notice and this permission notice appear in | |
| 16 supporting documentation, and that the names of Stichting Mathematisch | |
| 17 Centrum or CWI not be used in advertising or publicity pertaining to | |
| 18 distribution of the software without specific, written prior permission. | |
| 19 | |
| 20 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO | |
| 21 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 22 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE | |
| 23 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
| 24 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
| 25 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | |
| 26 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
| 27 | |
| 28 ******************************************************************/ | |
| 29 | |
| 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 | 33 |
| 34 #include "allobjects.h" | |
| 35 #include "modsupport.h" | |
| 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 | 38 #include <ctype.h> |
| 536 | 39 |
| 40 static object *RegexError; /* Exception */ | |
| 41 | |
| 42 typedef struct { | |
| 43 OB_HEAD | |
| 44 struct re_pattern_buffer re_patbuf; /* The compiled expression */ | |
| 45 struct re_registers re_regs; /* The registers from the last match */ | |
| 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 | 52 } regexobject; |
| 53 | |
| 54 /* Regex object methods */ | |
| 55 | |
| 56 static void | |
| 57 reg_dealloc(re) | |
| 58 regexobject *re; | |
| 59 { | |
| 2234 | 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 | 66 DEL(re); |
| 67 } | |
| 68 | |
| 69 static object * | |
| 70 makeresult(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++) { | |
|
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 | 83 } |
| 84 settupleitem(v, i, w); | |
| 85 } | |
| 86 } | |
| 87 return v; | |
| 88 } | |
| 89 | |
| 90 static object * | |
| 91 reg_match(re, args) | |
| 92 regexobject *re; | |
| 93 object *args; | |
| 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 | 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 | 98 int offset; |
| 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 | 101 offset = 0; |
| 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 | 117 if (result < -1) { |
| 118 /* Failure like stack overflow */ | |
| 119 err_setstr(RegexError, "match failure"); | |
| 120 return NULL; | |
| 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 | 126 return newintobject((long)result); /* Length of the match or -1 */ |
| 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 | 129 static object * |
| 130 reg_search(re, args) | |
| 131 regexobject *re; | |
| 132 object *args; | |
| 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 | 135 char *buffer; |
| 136 int size; | |
| 137 int offset; | |
| 138 int range; | |
| 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 | 142 offset = 0; |
| 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 | 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 | 162 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 } | |
|
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 | 173 return newintobject((long)result); /* Position of the match or -1 */ |
| 174 } | |
| 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 | 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 | 237 {NULL, NULL} /* sentinel */ |
| 238 }; | |
| 239 | |
| 240 static object * | |
| 241 reg_getattr(re, name) | |
| 242 regexobject *re; | |
| 243 char *name; | |
| 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 | 308 return findmethod(reg_methods, (object *)re, name); |
| 309 } | |
| 310 | |
| 311 static 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 */ | |
|
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 | 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 | 321 0, /*tp_setattr*/ |
| 322 0, /*tp_compare*/ | |
| 323 0, /*tp_repr*/ | |
| 324 }; | |
| 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 | 332 { |
| 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 | 342 re = NEWOBJ(regexobject, &Regextype); |
| 343 if (re != NULL) { | |
| 344 char *error; | |
| 345 re->re_patbuf.buffer = NULL; | |
| 346 re->re_patbuf.allocated = 0; | |
| 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 | 361 if (error != NULL) { |
| 362 err_setstr(RegexError, error); | |
| 363 DECREF(re); | |
| 364 re = NULL; | |
| 365 } | |
| 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 | 368 } |
| 369 | |
| 370 static object * | |
| 371 regex_compile(self, args) | |
| 372 object *self; | |
| 373 object *args; | |
| 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 | 383 } |
| 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 | 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 | 504 static object *cache_pat; |
| 505 static object *cache_prog; | |
| 506 | |
| 507 static int | |
| 508 update_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 | |
| 524 static object * | |
| 525 regex_match(self, args) | |
| 526 object *self; | |
| 527 object *args; | |
| 528 { | |
| 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 | 531 return NULL; |
| 532 if (update_cache(pat) < 0) | |
| 533 return NULL; | |
| 534 return reg_match((regexobject *)cache_prog, string); | |
| 535 } | |
| 536 | |
| 537 static object * | |
| 538 regex_search(self, args) | |
| 539 object *self; | |
| 540 object *args; | |
| 541 { | |
| 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 | 544 return NULL; |
| 545 if (update_cache(pat) < 0) | |
| 546 return NULL; | |
| 547 return reg_search((regexobject *)cache_prog, string); | |
| 548 } | |
| 549 | |
| 550 static object * | |
| 551 regex_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 | |
| 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 | 567 {NULL, NULL} /* sentinel */ |
| 568 }; | |
| 569 | |
| 570 initregex() | |
| 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 | 573 |
| 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"); | |
|
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 | 596 } |
