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