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