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 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Permission to use, copy, modify, and distribute this software and its |
| 13 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 14 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 15 | both that copyright notice and this permission notice appear in |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 16 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | Centrum or CWI or Corporation for National Research Initiatives or |
| 18 | CNRI not be used in advertising or publicity pertaining to |
| 19 | distribution of the software without specific, written prior |
| 20 | permission. |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 22 | While CWI is the initial source for this software, a modified version |
| 23 | is made available by the Corporation for National Research Initiatives |
| 24 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 25 | |
| 26 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 27 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 28 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 29 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 30 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 31 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 32 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 33 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 34 | |
| 35 | ******************************************************************/ |
| 36 | |
| 37 | /* Regular expression objects */ |
Guido van Rossum | 1cab95c | 1992-01-19 16:31:57 +0000 | [diff] [blame] | 38 | /* This uses Tatu Ylonen's copyleft-free reimplementation of |
| 39 | GNU regular expressions */ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 40 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 41 | #include "Python.h" |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 43 | #include <ctype.h> |
| 44 | |
Guido van Rossum | 1cab95c | 1992-01-19 16:31:57 +0000 | [diff] [blame] | 45 | #include "regexpr.h" |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 47 | static PyObject *RegexError; /* Exception */ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 48 | |
| 49 | typedef struct { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 50 | PyObject_HEAD |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 51 | struct re_pattern_buffer re_patbuf; /* The compiled expression */ |
| 52 | struct re_registers re_regs; /* The registers from the last match */ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 53 | char re_fastmap[256]; /* Storage for fastmap */ |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 54 | PyObject *re_translate; /* String object for translate table */ |
| 55 | PyObject *re_lastok; /* String object last matched/searched */ |
| 56 | PyObject *re_groupindex; /* Group name to index dictionary */ |
| 57 | PyObject *re_givenpat; /* Pattern with symbolic groups */ |
| 58 | PyObject *re_realpat; /* Pattern without symbolic groups */ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 59 | } regexobject; |
| 60 | |
| 61 | /* Regex object methods */ |
| 62 | |
| 63 | static void |
| 64 | reg_dealloc(re) |
| 65 | regexobject *re; |
| 66 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 67 | PyMem_XDEL(re->re_patbuf.buffer); |
| 68 | Py_XDECREF(re->re_translate); |
| 69 | Py_XDECREF(re->re_lastok); |
| 70 | Py_XDECREF(re->re_groupindex); |
| 71 | Py_XDECREF(re->re_givenpat); |
| 72 | Py_XDECREF(re->re_realpat); |
| 73 | PyMem_DEL(re); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 76 | static PyObject * |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 77 | makeresult(regs) |
| 78 | struct re_registers *regs; |
| 79 | { |
Guido van Rossum | c196202 | 1996-10-08 14:18:42 +0000 | [diff] [blame] | 80 | PyObject *v; |
| 81 | int i; |
| 82 | static PyObject *filler = NULL; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | c196202 | 1996-10-08 14:18:42 +0000 | [diff] [blame] | 84 | if (filler == NULL) { |
| 85 | filler = Py_BuildValue("(ii)", -1, -1); |
| 86 | if (filler == NULL) |
| 87 | return NULL; |
| 88 | } |
| 89 | v = PyTuple_New(RE_NREGS); |
| 90 | if (v == NULL) |
| 91 | return NULL; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 92 | |
Guido van Rossum | c196202 | 1996-10-08 14:18:42 +0000 | [diff] [blame] | 93 | for (i = 0; i < RE_NREGS; i++) { |
| 94 | int lo = regs->start[i]; |
| 95 | int hi = regs->end[i]; |
| 96 | PyObject *w; |
| 97 | if (lo == -1 && hi == -1) { |
| 98 | w = filler; |
| 99 | Py_INCREF(w); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 100 | } |
Guido van Rossum | c196202 | 1996-10-08 14:18:42 +0000 | [diff] [blame] | 101 | else |
| 102 | w = Py_BuildValue("(ii)", lo, hi); |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 103 | if (w == NULL || PyTuple_SetItem(v, i, w) < 0) { |
| 104 | Py_DECREF(v); |
Guido van Rossum | c196202 | 1996-10-08 14:18:42 +0000 | [diff] [blame] | 105 | return NULL; |
| 106 | } |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 107 | } |
| 108 | return v; |
| 109 | } |
| 110 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 111 | static PyObject * |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 112 | regobj_match(re, args) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 113 | regexobject *re; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 114 | PyObject *args; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 115 | { |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 116 | char *buffer; |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 117 | int size; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 118 | int offset = 0; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 119 | int result; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 120 | |
| 121 | if (!PyArg_ParseTuple(args, "s#|i", &buffer, &size, &offset)) |
| 122 | return NULL; |
| 123 | |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 124 | if (offset < 0 || offset > size) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 125 | PyErr_SetString(RegexError, "match offset out of range"); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 126 | return NULL; |
| 127 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 128 | Py_XDECREF(re->re_lastok); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 129 | re->re_lastok = NULL; |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 130 | 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] | 131 | if (result < -1) { |
| 132 | /* Failure like stack overflow */ |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 133 | PyErr_SetString(RegexError, "match failure"); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 134 | return NULL; |
| 135 | } |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 136 | if (result >= 0) { |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 137 | PyObject* str = PyString_FromStringAndSize(buffer, size); |
| 138 | if (!str) |
| 139 | return NULL; |
| 140 | re->re_lastok = str; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 141 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 142 | return PyInt_FromLong((long)result); /* Length of the match or -1 */ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 143 | } |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 144 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 145 | static PyObject * |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 146 | regobj_search(re, args) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 147 | regexobject *re; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 148 | PyObject *args; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 149 | { |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 150 | char *buffer; |
| 151 | int size; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 152 | int offset = 0; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 153 | int range; |
| 154 | int result; |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 155 | |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 156 | if (!PyArg_ParseTuple(args, "s#|i", &buffer, &size, &offset)) |
| 157 | return NULL; |
| 158 | |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 159 | if (offset < 0 || offset > size) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 160 | PyErr_SetString(RegexError, "search offset out of range"); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 161 | return NULL; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 162 | } |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 163 | /* NB: In Emacs 18.57, the documentation for re_search[_2] and |
| 164 | the implementation don't match: the documentation states that |
| 165 | |range| positions are tried, while the code tries |range|+1 |
| 166 | positions. It seems more productive to believe the code! */ |
Guido van Rossum | 2d78590 | 1992-01-26 18:12:41 +0000 | [diff] [blame] | 167 | range = size - offset; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 168 | Py_XDECREF(re->re_lastok); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 169 | re->re_lastok = NULL; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 170 | result = re_search(&re->re_patbuf, buffer, size, offset, range, |
| 171 | &re->re_regs); |
| 172 | if (result < -1) { |
| 173 | /* Failure like stack overflow */ |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 174 | PyErr_SetString(RegexError, "match failure"); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 175 | return NULL; |
| 176 | } |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 177 | if (result >= 0) { |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 178 | PyObject* str = PyString_FromStringAndSize(buffer, size); |
| 179 | if (!str) |
| 180 | return NULL; |
| 181 | re->re_lastok = str; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 182 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 183 | return PyInt_FromLong((long)result); /* Position of the match or -1 */ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 186 | /* get the group from the regex where index can be a string (group name) or |
| 187 | an integer index [0 .. 99] |
| 188 | */ |
| 189 | static PyObject* |
| 190 | group_from_index(re, index) |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 191 | regexobject *re; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 192 | PyObject *index; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 193 | { |
| 194 | int i, a, b; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 195 | char *v; |
| 196 | |
| 197 | if (PyString_Check(index)) |
| 198 | if (re->re_groupindex == NULL || |
| 199 | !(index = PyDict_GetItem(re->re_groupindex, index))) |
| 200 | { |
| 201 | PyErr_SetString(RegexError, |
| 202 | "group() group name doesn't exist"); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 203 | return NULL; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 204 | } |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 205 | |
| 206 | i = PyInt_AsLong(index); |
| 207 | if (i == -1 && PyErr_Occurred()) |
| 208 | return NULL; |
| 209 | |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 210 | if (i < 0 || i >= RE_NREGS) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 211 | PyErr_SetString(RegexError, "group() index out of range"); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 212 | return NULL; |
| 213 | } |
| 214 | if (re->re_lastok == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 215 | PyErr_SetString(RegexError, |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 216 | "group() only valid after successful match/search"); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 217 | return NULL; |
| 218 | } |
| 219 | a = re->re_regs.start[i]; |
| 220 | b = re->re_regs.end[i]; |
| 221 | if (a < 0 || b < 0) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 222 | Py_INCREF(Py_None); |
| 223 | return Py_None; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 224 | } |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 225 | |
| 226 | if (!(v = PyString_AsString(re->re_lastok))) |
| 227 | return NULL; |
| 228 | |
| 229 | return PyString_FromStringAndSize(v+a, b-a); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 232 | |
| 233 | static PyObject * |
| 234 | regobj_group(re, args) |
| 235 | regexobject *re; |
| 236 | PyObject *args; |
| 237 | { |
| 238 | int n = PyTuple_Size(args); |
| 239 | int i; |
| 240 | PyObject *res = NULL; |
| 241 | |
| 242 | if (n < 0) |
| 243 | return NULL; |
| 244 | if (n == 0) { |
| 245 | PyErr_SetString(PyExc_TypeError, "not enough arguments"); |
| 246 | return NULL; |
| 247 | } |
| 248 | if (n == 1) { |
| 249 | /* return value is a single string */ |
| 250 | PyObject *index = PyTuple_GetItem(args, 0); |
| 251 | if (!index) |
| 252 | return NULL; |
| 253 | |
| 254 | return group_from_index(re, index); |
| 255 | } |
| 256 | |
| 257 | /* return value is a tuple */ |
| 258 | if (!(res = PyTuple_New(n))) |
| 259 | return NULL; |
| 260 | |
| 261 | for (i = 0; i < n; i++) { |
| 262 | PyObject *index = PyTuple_GetItem(args, i); |
| 263 | PyObject *group = NULL; |
| 264 | |
| 265 | if (!index) |
| 266 | goto finally; |
| 267 | if (!(group = group_from_index(re, index))) |
| 268 | goto finally; |
| 269 | if (PyTuple_SetItem(res, i, group) < 0) |
| 270 | goto finally; |
| 271 | } |
| 272 | return res; |
| 273 | |
| 274 | finally: |
| 275 | Py_DECREF(res); |
| 276 | return NULL; |
| 277 | } |
| 278 | |
| 279 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 280 | static struct PyMethodDef reg_methods[] = { |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 281 | {"match", (PyCFunction)regobj_match, 1}, |
| 282 | {"search", (PyCFunction)regobj_search, 1}, |
| 283 | {"group", (PyCFunction)regobj_group, 1}, |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 284 | {NULL, NULL} /* sentinel */ |
| 285 | }; |
| 286 | |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 287 | |
| 288 | |
| 289 | static char* members[] = { |
| 290 | "last", "regs", "translate", |
| 291 | "groupindex", "realpat", "givenpat", |
| 292 | NULL |
| 293 | }; |
| 294 | |
| 295 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 296 | static PyObject * |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 297 | regobj_getattr(re, name) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 298 | regexobject *re; |
| 299 | char *name; |
| 300 | { |
Guido van Rossum | b824fc6 | 1992-01-01 14:52:16 +0000 | [diff] [blame] | 301 | if (strcmp(name, "regs") == 0) { |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 302 | if (re->re_lastok == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 303 | Py_INCREF(Py_None); |
| 304 | return Py_None; |
Guido van Rossum | b824fc6 | 1992-01-01 14:52:16 +0000 | [diff] [blame] | 305 | } |
| 306 | return makeresult(&re->re_regs); |
| 307 | } |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 308 | if (strcmp(name, "last") == 0) { |
| 309 | if (re->re_lastok == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 310 | Py_INCREF(Py_None); |
| 311 | return Py_None; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 312 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 313 | Py_INCREF(re->re_lastok); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 314 | return re->re_lastok; |
| 315 | } |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 316 | if (strcmp(name, "translate") == 0) { |
| 317 | if (re->re_translate == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 318 | Py_INCREF(Py_None); |
| 319 | return Py_None; |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 320 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 321 | Py_INCREF(re->re_translate); |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 322 | return re->re_translate; |
| 323 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 324 | if (strcmp(name, "groupindex") == 0) { |
| 325 | if (re->re_groupindex == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 326 | Py_INCREF(Py_None); |
| 327 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 328 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 329 | Py_INCREF(re->re_groupindex); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 330 | return re->re_groupindex; |
| 331 | } |
| 332 | if (strcmp(name, "realpat") == 0) { |
| 333 | if (re->re_realpat == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 334 | Py_INCREF(Py_None); |
| 335 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 336 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 337 | Py_INCREF(re->re_realpat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 338 | return re->re_realpat; |
| 339 | } |
| 340 | if (strcmp(name, "givenpat") == 0) { |
| 341 | if (re->re_givenpat == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 342 | Py_INCREF(Py_None); |
| 343 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 344 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 345 | Py_INCREF(re->re_givenpat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 346 | return re->re_givenpat; |
| 347 | } |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 348 | if (strcmp(name, "__members__") == 0) { |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 349 | int i = 0; |
| 350 | PyObject *list = NULL; |
| 351 | |
| 352 | /* okay, so it's unlikely this list will change that often. |
| 353 | still, it's easier to change it in just one place. |
| 354 | */ |
| 355 | while (members[i]) |
| 356 | i++; |
| 357 | if (!(list = PyList_New(i))) |
| 358 | return NULL; |
| 359 | |
| 360 | i = 0; |
| 361 | while (members[i]) { |
| 362 | PyObject* v = PyString_FromString(members[i]); |
| 363 | if (!v || PyList_SetItem(list, i, v) < 0) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 364 | Py_DECREF(list); |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 365 | return NULL; |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 366 | } |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 367 | i++; |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 368 | } |
| 369 | return list; |
| 370 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 371 | return Py_FindMethod(reg_methods, (PyObject *)re, name); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 374 | static PyTypeObject Regextype = { |
| 375 | PyObject_HEAD_INIT(&PyType_Type) |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 376 | 0, /*ob_size*/ |
| 377 | "regex", /*tp_name*/ |
| 378 | sizeof(regexobject), /*tp_size*/ |
| 379 | 0, /*tp_itemsize*/ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 380 | /* methods */ |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 381 | (destructor)reg_dealloc, /*tp_dealloc*/ |
| 382 | 0, /*tp_print*/ |
| 383 | (getattrfunc)regobj_getattr, /*tp_getattr*/ |
| 384 | 0, /*tp_setattr*/ |
| 385 | 0, /*tp_compare*/ |
| 386 | 0, /*tp_repr*/ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 387 | }; |
| 388 | |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 389 | /* reference counting invariants: |
| 390 | pattern: borrowed |
| 391 | translate: borrowed |
| 392 | givenpat: borrowed |
| 393 | groupindex: transferred |
| 394 | */ |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 395 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 396 | newregexobject(pattern, translate, givenpat, groupindex) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 397 | PyObject *pattern; |
| 398 | PyObject *translate; |
| 399 | PyObject *givenpat; |
| 400 | PyObject *groupindex; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 401 | { |
| 402 | regexobject *re; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 403 | char *pat; |
| 404 | int size; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 405 | |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 406 | if (!PyArg_Parse(pattern, "s#", &pat, &size)) |
| 407 | return NULL; |
| 408 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 409 | if (translate != NULL && PyString_Size(translate) != 256) { |
| 410 | PyErr_SetString(RegexError, |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 411 | "translation table must be 256 bytes"); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 412 | return NULL; |
| 413 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 414 | re = PyObject_NEW(regexobject, &Regextype); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 415 | if (re != NULL) { |
| 416 | char *error; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 417 | re->re_patbuf.buffer = NULL; |
| 418 | re->re_patbuf.allocated = 0; |
| 419 | re->re_patbuf.fastmap = re->re_fastmap; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 420 | if (translate) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 421 | re->re_patbuf.translate = PyString_AsString(translate); |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 422 | if (!re->re_patbuf.translate) |
| 423 | goto finally; |
| 424 | Py_INCREF(translate); |
| 425 | } |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 426 | else |
| 427 | re->re_patbuf.translate = NULL; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 428 | re->re_translate = translate; |
| 429 | re->re_lastok = NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 430 | re->re_groupindex = groupindex; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 431 | Py_INCREF(pattern); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 432 | re->re_realpat = pattern; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 433 | Py_INCREF(givenpat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 434 | re->re_givenpat = givenpat; |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 435 | error = re_compile_pattern(pat, size, &re->re_patbuf); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 436 | if (error != NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 437 | PyErr_SetString(RegexError, error); |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 438 | goto finally; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 439 | } |
| 440 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 441 | return (PyObject *)re; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 442 | finally: |
| 443 | Py_DECREF(re); |
| 444 | return NULL; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 447 | static PyObject * |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 448 | regex_compile(self, args) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 449 | PyObject *self; |
| 450 | PyObject *args; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 451 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 452 | PyObject *pat = NULL; |
| 453 | PyObject *tran = NULL; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 454 | |
| 455 | if (!PyArg_ParseTuple(args, "S|S", &pat, &tran)) |
| 456 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 457 | return newregexobject(pat, tran, pat, NULL); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 460 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 461 | symcomp(pattern, gdict) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 462 | PyObject *pattern; |
| 463 | PyObject *gdict; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 464 | { |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 465 | char *opat, *oend, *o, *n, *g, *v; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 466 | int group_count = 0; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 467 | int sz; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 468 | int escaped = 0; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 469 | char name_buf[128]; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 470 | PyObject *npattern; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 471 | int require_escape = re_syntax & RE_NO_BK_PARENS ? 0 : 1; |
| 472 | |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 473 | if (!(opat = PyString_AsString(pattern))) |
| 474 | return NULL; |
| 475 | |
| 476 | if ((sz = PyString_Size(pattern)) < 0) |
| 477 | return NULL; |
| 478 | |
| 479 | oend = opat + sz; |
| 480 | o = opat; |
| 481 | |
Guido van Rossum | ab28c56 | 1996-06-11 18:33:14 +0000 | [diff] [blame] | 482 | if (oend == opat) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 483 | Py_INCREF(pattern); |
Guido van Rossum | ab28c56 | 1996-06-11 18:33:14 +0000 | [diff] [blame] | 484 | return pattern; |
| 485 | } |
| 486 | |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 487 | if (!(npattern = PyString_FromStringAndSize((char*)NULL, sz)) || |
| 488 | !(n = PyString_AsString(npattern))) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 489 | return NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 490 | |
| 491 | while (o < oend) { |
| 492 | if (*o == '(' && escaped == require_escape) { |
| 493 | char *backtrack; |
| 494 | escaped = 0; |
| 495 | ++group_count; |
| 496 | *n++ = *o; |
| 497 | if (++o >= oend || *o != '<') |
| 498 | continue; |
| 499 | /* *o == '<' */ |
| 500 | if (o+1 < oend && *(o+1) == '>') |
| 501 | continue; |
| 502 | backtrack = o; |
| 503 | g = name_buf; |
| 504 | for (++o; o < oend;) { |
| 505 | if (*o == '>') { |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 506 | PyObject *group_name = NULL; |
| 507 | PyObject *group_index = NULL; |
| 508 | *g++ = '\0'; |
| 509 | group_name = PyString_FromString(name_buf); |
| 510 | group_index = PyInt_FromLong(group_count); |
| 511 | if (group_name == NULL || |
| 512 | group_index == NULL || |
| 513 | PyDict_SetItem(gdict, group_name, |
| 514 | group_index) != 0) |
| 515 | { |
| 516 | Py_XDECREF(group_name); |
| 517 | Py_XDECREF(group_index); |
| 518 | Py_XDECREF(npattern); |
| 519 | return NULL; |
| 520 | } |
| 521 | ++o; /* eat the '>' */ |
| 522 | break; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 523 | } |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 524 | if (!isalnum(Py_CHARMASK(*o)) && *o != '_') { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 525 | o = backtrack; |
| 526 | break; |
| 527 | } |
| 528 | *g++ = *o++; |
| 529 | } |
| 530 | } |
Guido van Rossum | 0cbaff4 | 1996-10-23 17:53:06 +0000 | [diff] [blame] | 531 | else if (*o == '[' && !escaped) { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 532 | *n++ = *o; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 533 | ++o; /* eat the char following '[' */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 534 | *n++ = *o; |
| 535 | while (o < oend && *o != ']') { |
| 536 | ++o; |
| 537 | *n++ = *o; |
| 538 | } |
| 539 | if (o < oend) |
| 540 | ++o; |
| 541 | } |
| 542 | else if (*o == '\\') { |
| 543 | escaped = 1; |
| 544 | *n++ = *o; |
| 545 | ++o; |
| 546 | } |
| 547 | else { |
| 548 | escaped = 0; |
| 549 | *n++ = *o; |
| 550 | ++o; |
| 551 | } |
| 552 | } |
| 553 | |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 554 | if (!(v = PyString_AsString(npattern))) { |
| 555 | Py_DECREF(npattern); |
| 556 | return NULL; |
| 557 | } |
| 558 | /* _PyString_Resize() decrements npattern on failure */ |
| 559 | if (_PyString_Resize(&npattern, n - v) == 0) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 560 | return npattern; |
| 561 | else { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 562 | return NULL; |
| 563 | } |
| 564 | |
| 565 | } |
| 566 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 567 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 568 | regex_symcomp(self, args) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 569 | PyObject *self; |
| 570 | PyObject *args; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 571 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 572 | PyObject *pattern; |
| 573 | PyObject *tran = NULL; |
| 574 | PyObject *gdict = NULL; |
| 575 | PyObject *npattern; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 576 | |
| 577 | if (!PyArg_ParseTuple(args, "S|S", &pattern, &tran)) |
| 578 | return NULL; |
| 579 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 580 | gdict = PyDict_New(); |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 581 | if (gdict == NULL || (npattern = symcomp(pattern, gdict)) == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 582 | Py_DECREF(gdict); |
| 583 | Py_DECREF(pattern); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 584 | return NULL; |
| 585 | } |
| 586 | return newregexobject(npattern, tran, pattern, gdict); |
| 587 | } |
| 588 | |
| 589 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 590 | static PyObject *cache_pat; |
| 591 | static PyObject *cache_prog; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 592 | |
| 593 | static int |
| 594 | update_cache(pat) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 595 | PyObject *pat; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 596 | { |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 597 | PyObject *tuple = Py_BuildValue("(O)", pat); |
| 598 | int status = 0; |
| 599 | |
| 600 | if (!tuple) |
| 601 | return -1; |
| 602 | |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 603 | if (pat != cache_pat) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 604 | Py_XDECREF(cache_pat); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 605 | cache_pat = NULL; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 606 | Py_XDECREF(cache_prog); |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 607 | cache_prog = regex_compile((PyObject *)NULL, tuple); |
| 608 | if (cache_prog == NULL) { |
| 609 | status = -1; |
| 610 | goto finally; |
| 611 | } |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 612 | cache_pat = pat; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 613 | Py_INCREF(cache_pat); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 614 | } |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 615 | finally: |
| 616 | Py_DECREF(tuple); |
| 617 | return status; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 620 | static PyObject * |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 621 | regex_match(self, args) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 622 | PyObject *self; |
| 623 | PyObject *args; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 624 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 625 | PyObject *pat, *string; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 626 | PyObject *tuple, *v; |
| 627 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 628 | if (!PyArg_Parse(args, "(SS)", &pat, &string)) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 629 | return NULL; |
| 630 | if (update_cache(pat) < 0) |
| 631 | return NULL; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 632 | |
| 633 | if (!(tuple = Py_BuildValue("(S)", string))) |
| 634 | return NULL; |
| 635 | v = regobj_match((regexobject *)cache_prog, tuple); |
| 636 | Py_DECREF(tuple); |
| 637 | return v; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 640 | static PyObject * |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 641 | regex_search(self, args) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 642 | PyObject *self; |
| 643 | PyObject *args; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 644 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 645 | PyObject *pat, *string; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 646 | PyObject *tuple, *v; |
| 647 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 648 | if (!PyArg_Parse(args, "(SS)", &pat, &string)) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 649 | return NULL; |
| 650 | if (update_cache(pat) < 0) |
| 651 | return NULL; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 652 | |
| 653 | if (!(tuple = Py_BuildValue("(S)", string))) |
| 654 | return NULL; |
| 655 | v = regobj_search((regexobject *)cache_prog, tuple); |
| 656 | Py_DECREF(tuple); |
| 657 | return v; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 660 | static PyObject * |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 661 | regex_set_syntax(self, args) |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 662 | PyObject *self; |
| 663 | PyObject *args; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 664 | { |
| 665 | int syntax; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 666 | if (!PyArg_Parse(args, "i", &syntax)) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 667 | return NULL; |
| 668 | syntax = re_set_syntax(syntax); |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 669 | /* wipe the global pattern cache */ |
| 670 | Py_XDECREF(cache_pat); |
| 671 | cache_pat = NULL; |
| 672 | Py_XDECREF(cache_prog); |
| 673 | cache_prog = NULL; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 674 | return PyInt_FromLong((long)syntax); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 677 | static struct PyMethodDef regex_global_methods[] = { |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 678 | {"compile", regex_compile, 1}, |
| 679 | {"symcomp", regex_symcomp, 1}, |
Guido van Rossum | 295d171 | 1995-02-19 15:55:19 +0000 | [diff] [blame] | 680 | {"match", regex_match, 0}, |
| 681 | {"search", regex_search, 0}, |
| 682 | {"set_syntax", regex_set_syntax, 0}, |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 683 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 684 | }; |
| 685 | |
Guido van Rossum | 8f3032d | 1996-08-19 22:03:12 +0000 | [diff] [blame] | 686 | void |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 687 | initregex() |
| 688 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 689 | PyObject *m, *d, *v; |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 690 | int i; |
| 691 | char *s; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 692 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 693 | m = Py_InitModule("regex", regex_global_methods); |
| 694 | d = PyModule_GetDict(m); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 695 | |
| 696 | /* Initialize regex.error exception */ |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 697 | v = RegexError = PyString_FromString("regex.error"); |
| 698 | if (v == NULL || PyDict_SetItemString(d, "error", v) != 0) |
| 699 | goto finally; |
| 700 | |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 701 | /* Initialize regex.casefold constant */ |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 702 | if (!(v = PyString_FromStringAndSize((char *)NULL, 256))) |
| 703 | goto finally; |
| 704 | |
| 705 | if (!(s = PyString_AsString(v))) |
| 706 | goto finally; |
| 707 | |
| 708 | for (i = 0; i < 256; i++) { |
| 709 | if (isupper(i)) |
| 710 | s[i] = tolower(i); |
| 711 | else |
| 712 | s[i] = i; |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 713 | } |
Barry Warsaw | c357325 | 1996-12-20 21:56:07 +0000 | [diff] [blame] | 714 | if (PyDict_SetItemString(d, "casefold", v) < 0) |
| 715 | goto finally; |
| 716 | Py_DECREF(v); |
| 717 | |
| 718 | if (!PyErr_Occurred()) |
| 719 | return; |
| 720 | finally: |
| 721 | Py_FatalError("can't initialize regex module"); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 722 | } |