Mercurial > cpython-lru_cache-weakref
annotate Modules/regexmodule.c @ 1590:ac2e5cc29949 legacy-trunk
Added tests for missing host to open_http and open_gopher
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 18 Apr 1994 09:39:56 +0000 |
| parents | 0c657db27dbf |
| children | 1940892d2fe3 |
| rev | line source |
|---|---|
| 536 | 1 /* |
| 2 XXX support range parameter on search | |
| 3 XXX support mstop parameter on search | |
| 4 */ | |
| 5 | |
| 6 /*********************************************************** | |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
7 Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
8 Amsterdam, 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" |
| 536 | 38 |
| 39 static object *RegexError; /* Exception */ | |
| 40 | |
| 41 typedef struct { | |
| 42 OB_HEAD | |
| 43 struct re_pattern_buffer re_patbuf; /* The compiled expression */ | |
| 44 struct re_registers re_regs; /* The registers from the last match */ | |
| 45 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
|
46 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
|
47 object *re_lastok; /* String object last matched/searched */ |
| 536 | 48 } regexobject; |
| 49 | |
| 50 /* Regex object methods */ | |
| 51 | |
| 52 static void | |
| 53 reg_dealloc(re) | |
| 54 regexobject *re; | |
| 55 { | |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
56 XDECREF(re->re_translate); |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
57 XDECREF(re->re_lastok); |
| 536 | 58 XDEL(re->re_patbuf.buffer); |
| 59 XDEL(re->re_patbuf.translate); | |
| 60 DEL(re); | |
| 61 } | |
| 62 | |
| 63 static object * | |
| 64 makeresult(regs) | |
| 65 struct re_registers *regs; | |
| 66 { | |
| 67 object *v = newtupleobject(RE_NREGS); | |
| 68 if (v != NULL) { | |
| 69 int i; | |
| 70 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
|
71 object *w; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
72 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
|
73 if (w == NULL) { |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
74 XDECREF(v); |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
75 v = NULL; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
76 break; |
| 536 | 77 } |
| 78 settupleitem(v, i, w); | |
| 79 } | |
| 80 } | |
| 81 return v; | |
| 82 } | |
| 83 | |
| 84 static object * | |
| 85 reg_match(re, args) | |
| 86 regexobject *re; | |
| 87 object *args; | |
| 88 { | |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
89 object *argstring; |
| 536 | 90 char *buffer; |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
91 int size; |
| 536 | 92 int offset; |
| 93 int result; | |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
94 if (getargs(args, "S", &argstring)) { |
| 536 | 95 offset = 0; |
| 96 } | |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
97 else { |
|
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
98 err_clear(); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
99 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
|
100 return NULL; |
|
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
101 } |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
102 buffer = getstringvalue(argstring); |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
103 size = getstringsize(argstring); |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
104 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
|
105 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
|
106 return NULL; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
107 } |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
108 XDECREF(re->re_lastok); |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
109 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
|
110 result = re_match(&re->re_patbuf, buffer, size, offset, &re->re_regs); |
| 536 | 111 if (result < -1) { |
| 112 /* Failure like stack overflow */ | |
| 113 err_setstr(RegexError, "match failure"); | |
| 114 return NULL; | |
| 115 } | |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
116 if (result >= 0) { |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
117 INCREF(argstring); |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
118 re->re_lastok = argstring; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
119 } |
| 536 | 120 return newintobject((long)result); /* Length of the match or -1 */ |
| 121 } | |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
122 |
| 536 | 123 static object * |
| 124 reg_search(re, args) | |
| 125 regexobject *re; | |
| 126 object *args; | |
| 127 { | |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
128 object *argstring; |
| 536 | 129 char *buffer; |
| 130 int size; | |
| 131 int offset; | |
| 132 int range; | |
| 133 int result; | |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
134 |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
135 if (getargs(args, "S", &argstring)) { |
| 536 | 136 offset = 0; |
| 137 } | |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
138 else { |
|
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
139 err_clear(); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
140 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
|
141 return NULL; |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
142 } |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
143 buffer = getstringvalue(argstring); |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
144 size = getstringsize(argstring); |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
145 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
|
146 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
|
147 return NULL; |
| 536 | 148 } |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
149 /* 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
|
150 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
|
151 |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
|
152 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
|
153 range = size - offset; |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
154 XDECREF(re->re_lastok); |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
155 re->re_lastok = NULL; |
| 536 | 156 result = re_search(&re->re_patbuf, buffer, size, offset, range, |
| 157 &re->re_regs); | |
| 158 if (result < -1) { | |
| 159 /* Failure like stack overflow */ | |
| 160 err_setstr(RegexError, "match failure"); | |
| 161 return NULL; | |
| 162 } | |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
163 if (result >= 0) { |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
164 INCREF(argstring); |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
165 re->re_lastok = argstring; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
166 } |
| 536 | 167 return newintobject((long)result); /* Position of the match or -1 */ |
| 168 } | |
| 169 | |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
170 static object * |
|
1239
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
171 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
|
172 regexobject *re; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
173 object *args; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
174 { |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
175 int i, a, b; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
176 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
|
177 int n = gettuplesize(args); |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
178 object *res = newtupleobject(n); |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
179 if (res == NULL) |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
180 return NULL; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
181 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
|
182 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
|
183 if (v == NULL) { |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
184 DECREF(res); |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
185 return NULL; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
186 } |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
187 settupleitem(res, i, v); |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
188 } |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
189 return res; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
190 } |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
191 if (!getargs(args, "i", &i)) |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
192 return NULL; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
193 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
|
194 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
|
195 return NULL; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
196 } |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
197 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
|
198 err_setstr(RegexError, |
|
1239
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
199 "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
|
200 return NULL; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
201 } |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
202 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
|
203 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
|
204 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
|
205 INCREF(None); |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
206 return None; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
207 } |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
208 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
|
209 } |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
210 |
| 536 | 211 static struct methodlist reg_methods[] = { |
| 212 {"match", reg_match}, | |
| 213 {"search", reg_search}, | |
|
1239
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
214 {"group", reg_group}, |
| 536 | 215 {NULL, NULL} /* sentinel */ |
| 216 }; | |
| 217 | |
| 218 static object * | |
| 219 reg_getattr(re, name) | |
| 220 regexobject *re; | |
| 221 char *name; | |
| 222 { | |
|
551
5a61f827f47c
'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents:
536
diff
changeset
|
223 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
|
224 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
|
225 INCREF(None); |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
226 return None; |
|
551
5a61f827f47c
'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents:
536
diff
changeset
|
227 } |
|
5a61f827f47c
'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents:
536
diff
changeset
|
228 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
|
229 } |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
230 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
|
231 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
|
232 INCREF(None); |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
233 return None; |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
234 } |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
235 INCREF(re->re_lastok); |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
236 return re->re_lastok; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
237 } |
|
1239
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
238 if (strcmp(name, "translate") == 0) { |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
239 if (re->re_translate == NULL) { |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
240 INCREF(None); |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
241 return None; |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
242 } |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
243 INCREF(re->re_translate); |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
244 return re->re_translate; |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
245 } |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
246 if (strcmp(name, "__members__") == 0) { |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
247 object *list = newlistobject(3); |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
248 if (list) { |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
249 setlistitem(list, 0, newstringobject("last")); |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
250 setlistitem(list, 1, newstringobject("regs")); |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
251 setlistitem(list, 2, newstringobject("translate")); |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
252 if (err_occurred()) { |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
253 DECREF(list); |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
254 list = NULL; |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
255 } |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
256 } |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
257 return list; |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
258 } |
| 536 | 259 return findmethod(reg_methods, (object *)re, name); |
| 260 } | |
| 261 | |
| 262 static typeobject Regextype = { | |
| 263 OB_HEAD_INIT(&Typetype) | |
| 264 0, /*ob_size*/ | |
| 265 "regex", /*tp_name*/ | |
| 266 sizeof(regexobject), /*tp_size*/ | |
| 267 0, /*tp_itemsize*/ | |
| 268 /* methods */ | |
| 269 reg_dealloc, /*tp_dealloc*/ | |
| 270 0, /*tp_print*/ | |
| 271 reg_getattr, /*tp_getattr*/ | |
| 272 0, /*tp_setattr*/ | |
| 273 0, /*tp_compare*/ | |
| 274 0, /*tp_repr*/ | |
| 275 }; | |
| 276 | |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
277 static object * |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
278 newregexobject(pat, size, translate) |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
279 char *pat; |
|
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
280 int size; |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
281 object *translate; |
| 536 | 282 { |
| 283 regexobject *re; | |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
284 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
|
285 err_setstr(RegexError, |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
286 "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
|
287 return NULL; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
288 } |
| 536 | 289 re = NEWOBJ(regexobject, &Regextype); |
| 290 if (re != NULL) { | |
| 291 char *error; | |
| 292 re->re_patbuf.buffer = NULL; | |
| 293 re->re_patbuf.allocated = 0; | |
| 294 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
|
295 if (translate) |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
296 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
|
297 else |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
298 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
|
299 XINCREF(translate); |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
300 re->re_translate = translate; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
301 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
|
302 error = re_compile_pattern(pat, size, &re->re_patbuf); |
| 536 | 303 if (error != NULL) { |
| 304 err_setstr(RegexError, error); | |
| 305 DECREF(re); | |
| 306 re = NULL; | |
| 307 } | |
| 308 } | |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
309 return (object *)re; |
| 536 | 310 } |
| 311 | |
| 312 static object * | |
| 313 regex_compile(self, args) | |
| 314 object *self; | |
| 315 object *args; | |
| 316 { | |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
317 char *pat; |
|
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
318 int size; |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
319 object *tran = NULL; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
320 if (!getargs(args, "s#", &pat, &size)) { |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
321 err_clear(); |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
322 if (!getargs(args, "(s#S)", &pat, &size, &tran)) |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
323 return NULL; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
324 } |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
325 return newregexobject(pat, size, tran); |
| 536 | 326 } |
| 327 | |
| 328 static object *cache_pat; | |
| 329 static object *cache_prog; | |
| 330 | |
| 331 static int | |
| 332 update_cache(pat) | |
| 333 object *pat; | |
| 334 { | |
| 335 if (pat != cache_pat) { | |
| 336 XDECREF(cache_pat); | |
| 337 cache_pat = NULL; | |
| 338 XDECREF(cache_prog); | |
| 339 cache_prog = regex_compile((object *)NULL, pat); | |
| 340 if (cache_prog == NULL) | |
| 341 return -1; | |
| 342 cache_pat = pat; | |
| 343 INCREF(cache_pat); | |
| 344 } | |
| 345 return 0; | |
| 346 } | |
| 347 | |
| 348 static object * | |
| 349 regex_match(self, args) | |
| 350 object *self; | |
| 351 object *args; | |
| 352 { | |
| 353 object *pat, *string; | |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
354 if (!getargs(args, "(SS)", &pat, &string)) |
| 536 | 355 return NULL; |
| 356 if (update_cache(pat) < 0) | |
| 357 return NULL; | |
| 358 return reg_match((regexobject *)cache_prog, string); | |
| 359 } | |
| 360 | |
| 361 static object * | |
| 362 regex_search(self, args) | |
| 363 object *self; | |
| 364 object *args; | |
| 365 { | |
| 366 object *pat, *string; | |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
367 if (!getargs(args, "(SS)", &pat, &string)) |
| 536 | 368 return NULL; |
| 369 if (update_cache(pat) < 0) | |
| 370 return NULL; | |
| 371 return reg_search((regexobject *)cache_prog, string); | |
| 372 } | |
| 373 | |
| 374 static object * | |
| 375 regex_set_syntax(self, args) | |
| 376 object *self, *args; | |
| 377 { | |
| 378 int syntax; | |
| 379 if (!getintarg(args, &syntax)) | |
| 380 return NULL; | |
| 381 syntax = re_set_syntax(syntax); | |
| 382 return newintobject((long)syntax); | |
| 383 } | |
| 384 | |
| 385 static struct methodlist regex_global_methods[] = { | |
| 386 {"compile", regex_compile}, | |
| 387 {"match", regex_match}, | |
| 388 {"search", regex_search}, | |
| 389 {"set_syntax", regex_set_syntax}, | |
| 390 {NULL, NULL} /* sentinel */ | |
| 391 }; | |
| 392 | |
| 393 initregex() | |
| 394 { | |
|
1239
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
395 object *m, *d, *v; |
| 536 | 396 |
| 397 m = initmodule("regex", regex_global_methods); | |
| 398 d = getmoduledict(m); | |
| 399 | |
| 400 /* Initialize regex.error exception */ | |
| 401 RegexError = newstringobject("regex.error"); | |
| 402 if (RegexError == NULL || dictinsert(d, "error", RegexError) != 0) | |
| 403 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
|
404 |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
405 /* Initialize regex.casefold constant */ |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
406 v = newsizedstringobject((char *)NULL, 256); |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
407 if (v != NULL) { |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
408 int i; |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
409 char *s = getstringvalue(v); |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
410 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
|
411 if (isupper(i)) |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
412 s[i] = tolower(i); |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
413 else |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
414 s[i] = i; |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
415 } |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
416 dictinsert(d, "casefold", v); |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
417 DECREF(v); |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
418 } |
| 536 | 419 } |
