Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 2 | XXX support range parameter on search |
| 3 | XXX support mstop parameter on search |
| 4 | */ |
| 5 | |
| 6 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 7 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 8 | The Netherlands. |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 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 */ |
Guido van Rossum | 1cab95c | 1992-01-19 16:31:57 +0000 | [diff] [blame] | 31 | /* This uses Tatu Ylonen's copyleft-free reimplementation of |
| 32 | GNU regular expressions */ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 33 | |
| 34 | #include "allobjects.h" |
| 35 | #include "modsupport.h" |
| 36 | |
Guido van Rossum | 1cab95c | 1992-01-19 16:31:57 +0000 | [diff] [blame] | 37 | #include "regexpr.h" |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 38 | #include <ctype.h> |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 39 | |
| 40 | static object *RegexError; /* Exception */ |
| 41 | |
| 42 | typedef struct { |
| 43 | OB_HEAD |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 44 | struct re_pattern_buffer re_patbuf; /* The compiled expression */ |
| 45 | struct re_registers re_regs; /* The registers from the last match */ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 46 | char re_fastmap[256]; /* Storage for fastmap */ |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 47 | object *re_translate; /* String object for translate table */ |
| 48 | object *re_lastok; /* String object last matched/searched */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 49 | object *re_groupindex; /* Group name to index dictionary */ |
| 50 | object *re_givenpat; /* Pattern with symbolic groups */ |
| 51 | object *re_realpat; /* Pattern without symbolic groups */ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 52 | } regexobject; |
| 53 | |
| 54 | /* Regex object methods */ |
| 55 | |
| 56 | static void |
| 57 | reg_dealloc(re) |
| 58 | regexobject *re; |
| 59 | { |
Guido van Rossum | 1253960 | 1995-01-26 00:37:01 +0000 | [diff] [blame] | 60 | XDEL(re->re_patbuf.buffer); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 61 | XDECREF(re->re_translate); |
| 62 | XDECREF(re->re_lastok); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 63 | XDECREF(re->re_groupindex); |
| 64 | XDECREF(re->re_givenpat); |
| 65 | XDECREF(re->re_realpat); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 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++) { |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 77 | object *w; |
| 78 | w = mkvalue("(ii)", regs->start[i], regs->end[i]); |
| 79 | if (w == NULL) { |
| 80 | XDECREF(v); |
| 81 | v = NULL; |
| 82 | break; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 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 | { |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 95 | object *argstring; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 96 | char *buffer; |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 97 | int size; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 98 | int offset; |
| 99 | int result; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 100 | if (getargs(args, "S", &argstring)) { |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 101 | offset = 0; |
| 102 | } |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 103 | else { |
| 104 | err_clear(); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 105 | if (!getargs(args, "(Si)", &argstring, &offset)) |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 106 | return NULL; |
| 107 | } |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 108 | buffer = getstringvalue(argstring); |
| 109 | size = getstringsize(argstring); |
| 110 | if (offset < 0 || offset > size) { |
| 111 | err_setstr(RegexError, "match offset out of range"); |
| 112 | return NULL; |
| 113 | } |
| 114 | XDECREF(re->re_lastok); |
| 115 | re->re_lastok = NULL; |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 116 | result = re_match(&re->re_patbuf, buffer, size, offset, &re->re_regs); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 117 | if (result < -1) { |
| 118 | /* Failure like stack overflow */ |
| 119 | err_setstr(RegexError, "match failure"); |
| 120 | return NULL; |
| 121 | } |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 122 | if (result >= 0) { |
| 123 | INCREF(argstring); |
| 124 | re->re_lastok = argstring; |
| 125 | } |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 126 | return newintobject((long)result); /* Length of the match or -1 */ |
| 127 | } |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 128 | |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 129 | static object * |
| 130 | reg_search(re, args) |
| 131 | regexobject *re; |
| 132 | object *args; |
| 133 | { |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 134 | object *argstring; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 135 | char *buffer; |
| 136 | int size; |
| 137 | int offset; |
| 138 | int range; |
| 139 | int result; |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 140 | |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 141 | if (getargs(args, "S", &argstring)) { |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 142 | offset = 0; |
| 143 | } |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 144 | else { |
| 145 | err_clear(); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 146 | if (!getargs(args, "(Si)", &argstring, &offset)) |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 147 | return NULL; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 148 | } |
| 149 | buffer = getstringvalue(argstring); |
| 150 | size = getstringsize(argstring); |
| 151 | if (offset < 0 || offset > size) { |
| 152 | err_setstr(RegexError, "search offset out of range"); |
| 153 | return NULL; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 154 | } |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 155 | /* NB: In Emacs 18.57, the documentation for re_search[_2] and |
| 156 | the implementation don't match: the documentation states that |
| 157 | |range| positions are tried, while the code tries |range|+1 |
| 158 | positions. It seems more productive to believe the code! */ |
Guido van Rossum | 2d78590 | 1992-01-26 18:12:41 +0000 | [diff] [blame] | 159 | range = size - offset; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 160 | XDECREF(re->re_lastok); |
| 161 | re->re_lastok = NULL; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 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 | } |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 169 | if (result >= 0) { |
| 170 | INCREF(argstring); |
| 171 | re->re_lastok = argstring; |
| 172 | } |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 173 | return newintobject((long)result); /* Position of the match or -1 */ |
| 174 | } |
| 175 | |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 176 | static object * |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 177 | reg_group(re, args) |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 178 | regexobject *re; |
| 179 | object *args; |
| 180 | { |
| 181 | int i, a, b; |
| 182 | if (args != NULL && is_tupleobject(args)) { |
| 183 | int n = gettuplesize(args); |
| 184 | object *res = newtupleobject(n); |
| 185 | if (res == NULL) |
| 186 | return NULL; |
| 187 | for (i = 0; i < n; i++) { |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 188 | object *v = reg_group(re, gettupleitem(args, i)); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 189 | if (v == NULL) { |
| 190 | DECREF(res); |
| 191 | return NULL; |
| 192 | } |
| 193 | settupleitem(res, i, v); |
| 194 | } |
| 195 | return res; |
| 196 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 197 | if (!getargs(args, "i", &i)) { |
| 198 | object *n; |
| 199 | err_clear(); |
| 200 | if (!getargs(args, "S", &n)) |
| 201 | return NULL; |
| 202 | else { |
| 203 | object *index; |
| 204 | if (re->re_groupindex == NULL) |
| 205 | index = NULL; |
| 206 | else |
| 207 | index = mappinglookup(re->re_groupindex, n); |
| 208 | if (index == NULL) { |
| 209 | err_setstr(RegexError, "group() group name doesn't exist"); |
| 210 | return NULL; |
| 211 | } |
| 212 | i = getintvalue(index); |
| 213 | } |
| 214 | } |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 215 | if (i < 0 || i >= RE_NREGS) { |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 216 | err_setstr(RegexError, "group() index out of range"); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 217 | return NULL; |
| 218 | } |
| 219 | if (re->re_lastok == NULL) { |
| 220 | err_setstr(RegexError, |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 221 | "group() only valid after successful match/search"); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 222 | return NULL; |
| 223 | } |
| 224 | a = re->re_regs.start[i]; |
| 225 | b = re->re_regs.end[i]; |
| 226 | if (a < 0 || b < 0) { |
| 227 | INCREF(None); |
| 228 | return None; |
| 229 | } |
| 230 | return newsizedstringobject(getstringvalue(re->re_lastok)+a, b-a); |
| 231 | } |
| 232 | |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 233 | static struct methodlist reg_methods[] = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 234 | {"match", (method)reg_match}, |
| 235 | {"search", (method)reg_search}, |
| 236 | {"group", (method)reg_group}, |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 237 | {NULL, NULL} /* sentinel */ |
| 238 | }; |
| 239 | |
| 240 | static object * |
| 241 | reg_getattr(re, name) |
| 242 | regexobject *re; |
| 243 | char *name; |
| 244 | { |
Guido van Rossum | b824fc6 | 1992-01-01 14:52:16 +0000 | [diff] [blame] | 245 | if (strcmp(name, "regs") == 0) { |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 246 | if (re->re_lastok == NULL) { |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 247 | INCREF(None); |
| 248 | return None; |
Guido van Rossum | b824fc6 | 1992-01-01 14:52:16 +0000 | [diff] [blame] | 249 | } |
| 250 | return makeresult(&re->re_regs); |
| 251 | } |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 252 | if (strcmp(name, "last") == 0) { |
| 253 | if (re->re_lastok == NULL) { |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 254 | INCREF(None); |
| 255 | return None; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 256 | } |
| 257 | INCREF(re->re_lastok); |
| 258 | return re->re_lastok; |
| 259 | } |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 260 | if (strcmp(name, "translate") == 0) { |
| 261 | if (re->re_translate == NULL) { |
| 262 | INCREF(None); |
| 263 | return None; |
| 264 | } |
| 265 | INCREF(re->re_translate); |
| 266 | return re->re_translate; |
| 267 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 268 | if (strcmp(name, "groupindex") == 0) { |
| 269 | if (re->re_groupindex == NULL) { |
| 270 | INCREF(None); |
| 271 | return None; |
| 272 | } |
| 273 | INCREF(re->re_groupindex); |
| 274 | return re->re_groupindex; |
| 275 | } |
| 276 | if (strcmp(name, "realpat") == 0) { |
| 277 | if (re->re_realpat == NULL) { |
| 278 | INCREF(None); |
| 279 | return None; |
| 280 | } |
| 281 | INCREF(re->re_realpat); |
| 282 | return re->re_realpat; |
| 283 | } |
| 284 | if (strcmp(name, "givenpat") == 0) { |
| 285 | if (re->re_givenpat == NULL) { |
| 286 | INCREF(None); |
| 287 | return None; |
| 288 | } |
| 289 | INCREF(re->re_givenpat); |
| 290 | return re->re_givenpat; |
| 291 | } |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 292 | if (strcmp(name, "__members__") == 0) { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 293 | object *list = newlistobject(6); |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 294 | if (list) { |
| 295 | setlistitem(list, 0, newstringobject("last")); |
| 296 | setlistitem(list, 1, newstringobject("regs")); |
| 297 | setlistitem(list, 2, newstringobject("translate")); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 298 | setlistitem(list, 3, newstringobject("groupindex")); |
| 299 | setlistitem(list, 4, newstringobject("realpat")); |
| 300 | setlistitem(list, 5, newstringobject("givenpat")); |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 301 | if (err_occurred()) { |
| 302 | DECREF(list); |
| 303 | list = NULL; |
| 304 | } |
| 305 | } |
| 306 | return list; |
| 307 | } |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 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 */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 318 | (destructor)reg_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 319 | 0, /*tp_print*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 320 | (getattrfunc)reg_getattr, /*tp_getattr*/ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 321 | 0, /*tp_setattr*/ |
| 322 | 0, /*tp_compare*/ |
| 323 | 0, /*tp_repr*/ |
| 324 | }; |
| 325 | |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 326 | static object * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 327 | newregexobject(pattern, translate, givenpat, groupindex) |
| 328 | object *pattern; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 329 | object *translate; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 330 | object *givenpat; |
| 331 | object *groupindex; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 332 | { |
| 333 | regexobject *re; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 334 | char *pat = getstringvalue(pattern); |
| 335 | int size = getstringsize(pattern); |
| 336 | |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 337 | if (translate != NULL && getstringsize(translate) != 256) { |
| 338 | err_setstr(RegexError, |
| 339 | "translation table must be 256 bytes"); |
| 340 | return NULL; |
| 341 | } |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 342 | re = NEWOBJ(regexobject, &Regextype); |
| 343 | if (re != NULL) { |
| 344 | char *error; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 345 | re->re_patbuf.buffer = NULL; |
| 346 | re->re_patbuf.allocated = 0; |
| 347 | re->re_patbuf.fastmap = re->re_fastmap; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 348 | if (translate) |
| 349 | re->re_patbuf.translate = getstringvalue(translate); |
| 350 | else |
| 351 | re->re_patbuf.translate = NULL; |
| 352 | XINCREF(translate); |
| 353 | re->re_translate = translate; |
| 354 | re->re_lastok = NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 355 | re->re_groupindex = groupindex; |
| 356 | INCREF(pattern); |
| 357 | re->re_realpat = pattern; |
| 358 | INCREF(givenpat); |
| 359 | re->re_givenpat = givenpat; |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 360 | error = re_compile_pattern(pat, size, &re->re_patbuf); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 361 | if (error != NULL) { |
| 362 | err_setstr(RegexError, error); |
| 363 | DECREF(re); |
| 364 | re = NULL; |
| 365 | } |
| 366 | } |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 367 | return (object *)re; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static object * |
| 371 | regex_compile(self, args) |
| 372 | object *self; |
| 373 | object *args; |
| 374 | { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 375 | object *pat = NULL; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 376 | object *tran = NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 377 | if (!getargs(args, "S", &pat)) { |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 378 | err_clear(); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 379 | if (!getargs(args, "(SS)", &pat, &tran)) |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 380 | return NULL; |
| 381 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 382 | return newregexobject(pat, tran, pat, NULL); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 385 | static object * |
| 386 | symcomp(pattern, gdict) |
| 387 | object *pattern; |
| 388 | object *gdict; |
| 389 | { |
| 390 | char *opat = getstringvalue(pattern); |
| 391 | char *oend = opat + getstringsize(pattern); |
| 392 | int group_count = 0; |
| 393 | int escaped = 0; |
| 394 | char *o = opat; |
| 395 | char *n; |
| 396 | char name_buf[128]; |
| 397 | char *g; |
| 398 | object *npattern; |
| 399 | int require_escape = re_syntax & RE_NO_BK_PARENS ? 0 : 1; |
| 400 | |
| 401 | npattern = newsizedstringobject((char*)NULL, getstringsize(pattern)); |
| 402 | if (npattern == NULL) |
| 403 | return NULL; |
| 404 | n = getstringvalue(npattern); |
| 405 | |
| 406 | while (o < oend) { |
| 407 | if (*o == '(' && escaped == require_escape) { |
| 408 | char *backtrack; |
| 409 | escaped = 0; |
| 410 | ++group_count; |
| 411 | *n++ = *o; |
| 412 | if (++o >= oend || *o != '<') |
| 413 | continue; |
| 414 | /* *o == '<' */ |
| 415 | if (o+1 < oend && *(o+1) == '>') |
| 416 | continue; |
| 417 | backtrack = o; |
| 418 | g = name_buf; |
| 419 | for (++o; o < oend;) { |
| 420 | if (*o == '>') { |
| 421 | object *group_name = NULL; |
| 422 | object *group_index = NULL; |
| 423 | *g++ = '\0'; |
| 424 | group_name = newstringobject(name_buf); |
| 425 | group_index = newintobject(group_count); |
| 426 | if (group_name == NULL || group_index == NULL |
| 427 | || mappinginsert(gdict, group_name, group_index) != 0) { |
| 428 | XDECREF(group_name); |
| 429 | XDECREF(group_index); |
| 430 | XDECREF(npattern); |
| 431 | return NULL; |
| 432 | } |
| 433 | ++o; /* eat the '>' */ |
| 434 | break; |
| 435 | } |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 436 | if (!isalnum(Py_CHARMASK(*o)) && *o != '_') { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 437 | o = backtrack; |
| 438 | break; |
| 439 | } |
| 440 | *g++ = *o++; |
| 441 | } |
| 442 | } |
| 443 | if (*o == '[' && !escaped) { |
| 444 | *n++ = *o; |
| 445 | ++o; /* eat the char following '[' */ |
| 446 | *n++ = *o; |
| 447 | while (o < oend && *o != ']') { |
| 448 | ++o; |
| 449 | *n++ = *o; |
| 450 | } |
| 451 | if (o < oend) |
| 452 | ++o; |
| 453 | } |
| 454 | else if (*o == '\\') { |
| 455 | escaped = 1; |
| 456 | *n++ = *o; |
| 457 | ++o; |
| 458 | } |
| 459 | else { |
| 460 | escaped = 0; |
| 461 | *n++ = *o; |
| 462 | ++o; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | if (resizestring(&npattern, n - getstringvalue(npattern)) == 0) |
| 467 | return npattern; |
| 468 | else { |
| 469 | DECREF(npattern); |
| 470 | return NULL; |
| 471 | } |
| 472 | |
| 473 | } |
| 474 | |
| 475 | static object * |
| 476 | regex_symcomp(self, args) |
| 477 | object *self; |
| 478 | object *args; |
| 479 | { |
| 480 | object *pattern; |
| 481 | object *tran = NULL; |
| 482 | object *gdict = NULL; |
| 483 | object *npattern; |
| 484 | if (!getargs(args, "S", &pattern)) { |
| 485 | err_clear(); |
| 486 | if (!getargs(args, "(SS)", &pattern, &tran)) |
| 487 | return NULL; |
| 488 | } |
| 489 | gdict = newmappingobject(); |
| 490 | if (gdict == NULL |
| 491 | || (npattern = symcomp(pattern, gdict)) == NULL) { |
| 492 | DECREF(gdict); |
| 493 | DECREF(pattern); |
| 494 | return NULL; |
| 495 | } |
| 496 | return newregexobject(npattern, tran, pattern, gdict); |
| 497 | } |
| 498 | |
| 499 | |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 500 | static object *cache_pat; |
| 501 | static object *cache_prog; |
| 502 | |
| 503 | static int |
| 504 | update_cache(pat) |
| 505 | object *pat; |
| 506 | { |
| 507 | if (pat != cache_pat) { |
| 508 | XDECREF(cache_pat); |
| 509 | cache_pat = NULL; |
| 510 | XDECREF(cache_prog); |
| 511 | cache_prog = regex_compile((object *)NULL, pat); |
| 512 | if (cache_prog == NULL) |
| 513 | return -1; |
| 514 | cache_pat = pat; |
| 515 | INCREF(cache_pat); |
| 516 | } |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | static object * |
| 521 | regex_match(self, args) |
| 522 | object *self; |
| 523 | object *args; |
| 524 | { |
| 525 | object *pat, *string; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 526 | if (!getargs(args, "(SS)", &pat, &string)) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 527 | return NULL; |
| 528 | if (update_cache(pat) < 0) |
| 529 | return NULL; |
| 530 | return reg_match((regexobject *)cache_prog, string); |
| 531 | } |
| 532 | |
| 533 | static object * |
| 534 | regex_search(self, args) |
| 535 | object *self; |
| 536 | object *args; |
| 537 | { |
| 538 | object *pat, *string; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 539 | if (!getargs(args, "(SS)", &pat, &string)) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 540 | return NULL; |
| 541 | if (update_cache(pat) < 0) |
| 542 | return NULL; |
| 543 | return reg_search((regexobject *)cache_prog, string); |
| 544 | } |
| 545 | |
| 546 | static object * |
| 547 | regex_set_syntax(self, args) |
| 548 | object *self, *args; |
| 549 | { |
| 550 | int syntax; |
| 551 | if (!getintarg(args, &syntax)) |
| 552 | return NULL; |
| 553 | syntax = re_set_syntax(syntax); |
| 554 | return newintobject((long)syntax); |
| 555 | } |
| 556 | |
| 557 | static struct methodlist regex_global_methods[] = { |
Guido van Rossum | 295d171 | 1995-02-19 15:55:19 +0000 | [diff] [blame] | 558 | {"compile", regex_compile, 0}, |
| 559 | {"symcomp", regex_symcomp, 0}, |
| 560 | {"match", regex_match, 0}, |
| 561 | {"search", regex_search, 0}, |
| 562 | {"set_syntax", regex_set_syntax, 0}, |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 563 | {NULL, NULL} /* sentinel */ |
| 564 | }; |
| 565 | |
| 566 | initregex() |
| 567 | { |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 568 | object *m, *d, *v; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 569 | |
| 570 | m = initmodule("regex", regex_global_methods); |
| 571 | d = getmoduledict(m); |
| 572 | |
| 573 | /* Initialize regex.error exception */ |
| 574 | RegexError = newstringobject("regex.error"); |
| 575 | if (RegexError == NULL || dictinsert(d, "error", RegexError) != 0) |
| 576 | fatal("can't define regex.error"); |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 577 | |
| 578 | /* Initialize regex.casefold constant */ |
| 579 | v = newsizedstringobject((char *)NULL, 256); |
| 580 | if (v != NULL) { |
| 581 | int i; |
| 582 | char *s = getstringvalue(v); |
| 583 | for (i = 0; i < 256; i++) { |
| 584 | if (isupper(i)) |
| 585 | s[i] = tolower(i); |
| 586 | else |
| 587 | s[i] = i; |
| 588 | } |
| 589 | dictinsert(d, "casefold", v); |
| 590 | DECREF(v); |
| 591 | } |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 592 | } |