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; |
| 83 | if (filler == NULL) { |
| 84 | filler = Py_BuildValue("(ii)", -1, -1); |
| 85 | if (filler == NULL) |
| 86 | return NULL; |
| 87 | } |
| 88 | v = PyTuple_New(RE_NREGS); |
| 89 | if (v == NULL) |
| 90 | return NULL; |
| 91 | for (i = 0; i < RE_NREGS; i++) { |
| 92 | int lo = regs->start[i]; |
| 93 | int hi = regs->end[i]; |
| 94 | PyObject *w; |
| 95 | if (lo == -1 && hi == -1) { |
| 96 | w = filler; |
| 97 | Py_INCREF(w); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 98 | } |
Guido van Rossum | c196202 | 1996-10-08 14:18:42 +0000 | [diff] [blame] | 99 | else |
| 100 | w = Py_BuildValue("(ii)", lo, hi); |
| 101 | if (w == NULL) { |
| 102 | Py_XDECREF(v); |
| 103 | return NULL; |
| 104 | } |
| 105 | PyTuple_SetItem(v, i, w); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 106 | } |
| 107 | return v; |
| 108 | } |
| 109 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 110 | static PyObject * |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 111 | reg_match(re, args) |
| 112 | regexobject *re; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 113 | PyObject *args; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 114 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 115 | PyObject *argstring; |
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; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 118 | int offset; |
| 119 | int result; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 120 | if (PyArg_Parse(args, "S", &argstring)) { |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 121 | offset = 0; |
| 122 | } |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 123 | else { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 124 | PyErr_Clear(); |
| 125 | if (!PyArg_Parse(args, "(Si)", &argstring, &offset)) |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 126 | return NULL; |
| 127 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 128 | buffer = PyString_AsString(argstring); |
| 129 | size = PyString_Size(argstring); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 130 | if (offset < 0 || offset > size) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 131 | PyErr_SetString(RegexError, "match offset out of range"); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 132 | return NULL; |
| 133 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 134 | Py_XDECREF(re->re_lastok); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 135 | re->re_lastok = NULL; |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 136 | 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] | 137 | if (result < -1) { |
| 138 | /* Failure like stack overflow */ |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 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 | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 143 | Py_INCREF(argstring); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 144 | re->re_lastok = argstring; |
| 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 * |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 150 | reg_search(re, args) |
| 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 | dfe8ad9 | 1996-07-24 00:51:20 +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; |
| 157 | int offset; |
| 158 | int range; |
| 159 | int result; |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 160 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 161 | if (PyArg_Parse(args, "S", &argstring)) { |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 162 | offset = 0; |
| 163 | } |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 164 | else { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 165 | PyErr_Clear(); |
| 166 | if (!PyArg_Parse(args, "(Si)", &argstring, &offset)) |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 167 | return NULL; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 168 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 169 | buffer = PyString_AsString(argstring); |
| 170 | size = PyString_Size(argstring); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 171 | if (offset < 0 || offset > size) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 172 | PyErr_SetString(RegexError, "search offset out of range"); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 173 | return NULL; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 174 | } |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 175 | /* NB: In Emacs 18.57, the documentation for re_search[_2] and |
| 176 | the implementation don't match: the documentation states that |
| 177 | |range| positions are tried, while the code tries |range|+1 |
| 178 | positions. It seems more productive to believe the code! */ |
Guido van Rossum | 2d78590 | 1992-01-26 18:12:41 +0000 | [diff] [blame] | 179 | range = size - offset; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 180 | Py_XDECREF(re->re_lastok); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 181 | re->re_lastok = NULL; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 182 | result = re_search(&re->re_patbuf, buffer, size, offset, range, |
| 183 | &re->re_regs); |
| 184 | if (result < -1) { |
| 185 | /* Failure like stack overflow */ |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 186 | PyErr_SetString(RegexError, "match failure"); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 187 | return NULL; |
| 188 | } |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 189 | if (result >= 0) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 190 | Py_INCREF(argstring); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 191 | re->re_lastok = argstring; |
| 192 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 193 | return PyInt_FromLong((long)result); /* Position of the match or -1 */ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 196 | static PyObject * |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 197 | reg_group(re, args) |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 198 | regexobject *re; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 199 | PyObject *args; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 200 | { |
| 201 | int i, a, b; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 202 | if (args != NULL && PyTuple_Check(args)) { |
| 203 | int n = PyTuple_Size(args); |
| 204 | PyObject *res = PyTuple_New(n); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 205 | if (res == NULL) |
| 206 | return NULL; |
| 207 | for (i = 0; i < n; i++) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 208 | PyObject *v = reg_group(re, PyTuple_GetItem(args, i)); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 209 | if (v == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 210 | Py_DECREF(res); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 211 | return NULL; |
| 212 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 213 | PyTuple_SetItem(res, i, v); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 214 | } |
| 215 | return res; |
| 216 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 217 | if (!PyArg_Parse(args, "i", &i)) { |
| 218 | PyObject *n; |
| 219 | PyErr_Clear(); |
| 220 | if (!PyArg_Parse(args, "S", &n)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 221 | return NULL; |
| 222 | else { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 223 | PyObject *index; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 224 | if (re->re_groupindex == NULL) |
| 225 | index = NULL; |
| 226 | else |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 227 | index = PyDict_GetItem(re->re_groupindex, n); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 228 | if (index == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 229 | PyErr_SetString(RegexError, "group() group name doesn't exist"); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 230 | return NULL; |
| 231 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 232 | i = PyInt_AsLong(index); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 233 | } |
| 234 | } |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 235 | if (i < 0 || i >= RE_NREGS) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 236 | PyErr_SetString(RegexError, "group() index out of range"); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 237 | return NULL; |
| 238 | } |
| 239 | if (re->re_lastok == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 240 | PyErr_SetString(RegexError, |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 241 | "group() only valid after successful match/search"); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 242 | return NULL; |
| 243 | } |
| 244 | a = re->re_regs.start[i]; |
| 245 | b = re->re_regs.end[i]; |
| 246 | if (a < 0 || b < 0) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 247 | Py_INCREF(Py_None); |
| 248 | return Py_None; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 249 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 250 | return PyString_FromStringAndSize(PyString_AsString(re->re_lastok)+a, b-a); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 253 | static struct PyMethodDef reg_methods[] = { |
| 254 | {"match", (PyCFunction)reg_match}, |
| 255 | {"search", (PyCFunction)reg_search}, |
| 256 | {"group", (PyCFunction)reg_group}, |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 257 | {NULL, NULL} /* sentinel */ |
| 258 | }; |
| 259 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 260 | static PyObject * |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 261 | reg_getattr(re, name) |
| 262 | regexobject *re; |
| 263 | char *name; |
| 264 | { |
Guido van Rossum | b824fc6 | 1992-01-01 14:52:16 +0000 | [diff] [blame] | 265 | if (strcmp(name, "regs") == 0) { |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 266 | if (re->re_lastok == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 267 | Py_INCREF(Py_None); |
| 268 | return Py_None; |
Guido van Rossum | b824fc6 | 1992-01-01 14:52:16 +0000 | [diff] [blame] | 269 | } |
| 270 | return makeresult(&re->re_regs); |
| 271 | } |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 272 | if (strcmp(name, "last") == 0) { |
| 273 | if (re->re_lastok == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 274 | Py_INCREF(Py_None); |
| 275 | return Py_None; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 276 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 277 | Py_INCREF(re->re_lastok); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 278 | return re->re_lastok; |
| 279 | } |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 280 | if (strcmp(name, "translate") == 0) { |
| 281 | if (re->re_translate == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 282 | Py_INCREF(Py_None); |
| 283 | return Py_None; |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 284 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 285 | Py_INCREF(re->re_translate); |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 286 | return re->re_translate; |
| 287 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 288 | if (strcmp(name, "groupindex") == 0) { |
| 289 | if (re->re_groupindex == 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 | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 292 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 293 | Py_INCREF(re->re_groupindex); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 294 | return re->re_groupindex; |
| 295 | } |
| 296 | if (strcmp(name, "realpat") == 0) { |
| 297 | if (re->re_realpat == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 298 | Py_INCREF(Py_None); |
| 299 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 300 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 301 | Py_INCREF(re->re_realpat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 302 | return re->re_realpat; |
| 303 | } |
| 304 | if (strcmp(name, "givenpat") == 0) { |
| 305 | if (re->re_givenpat == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 306 | Py_INCREF(Py_None); |
| 307 | return Py_None; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 308 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 309 | Py_INCREF(re->re_givenpat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 310 | return re->re_givenpat; |
| 311 | } |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 312 | if (strcmp(name, "__members__") == 0) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 313 | PyObject *list = PyList_New(6); |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 314 | if (list) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 315 | PyList_SetItem(list, 0, PyString_FromString("last")); |
| 316 | PyList_SetItem(list, 1, PyString_FromString("regs")); |
| 317 | PyList_SetItem(list, 2, PyString_FromString("translate")); |
| 318 | PyList_SetItem(list, 3, PyString_FromString("groupindex")); |
| 319 | PyList_SetItem(list, 4, PyString_FromString("realpat")); |
| 320 | PyList_SetItem(list, 5, PyString_FromString("givenpat")); |
| 321 | if (PyErr_Occurred()) { |
| 322 | Py_DECREF(list); |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 323 | list = NULL; |
| 324 | } |
| 325 | } |
| 326 | return list; |
| 327 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 328 | return Py_FindMethod(reg_methods, (PyObject *)re, name); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 331 | static PyTypeObject Regextype = { |
| 332 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 333 | 0, /*ob_size*/ |
| 334 | "regex", /*tp_name*/ |
| 335 | sizeof(regexobject), /*tp_size*/ |
| 336 | 0, /*tp_itemsize*/ |
| 337 | /* methods */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 338 | (destructor)reg_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 339 | 0, /*tp_print*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 340 | (getattrfunc)reg_getattr, /*tp_getattr*/ |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 341 | 0, /*tp_setattr*/ |
| 342 | 0, /*tp_compare*/ |
| 343 | 0, /*tp_repr*/ |
| 344 | }; |
| 345 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 346 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 347 | newregexobject(pattern, translate, givenpat, groupindex) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 348 | PyObject *pattern; |
| 349 | PyObject *translate; |
| 350 | PyObject *givenpat; |
| 351 | PyObject *groupindex; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 352 | { |
| 353 | regexobject *re; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 354 | char *pat = PyString_AsString(pattern); |
| 355 | int size = PyString_Size(pattern); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 356 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 357 | if (translate != NULL && PyString_Size(translate) != 256) { |
| 358 | PyErr_SetString(RegexError, |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 359 | "translation table must be 256 bytes"); |
| 360 | return NULL; |
| 361 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 362 | re = PyObject_NEW(regexobject, &Regextype); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 363 | if (re != NULL) { |
| 364 | char *error; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 365 | re->re_patbuf.buffer = NULL; |
| 366 | re->re_patbuf.allocated = 0; |
| 367 | re->re_patbuf.fastmap = re->re_fastmap; |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 368 | if (translate) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 369 | re->re_patbuf.translate = PyString_AsString(translate); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 370 | else |
| 371 | re->re_patbuf.translate = NULL; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 372 | Py_XINCREF(translate); |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 373 | re->re_translate = translate; |
| 374 | re->re_lastok = NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 375 | re->re_groupindex = groupindex; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 376 | Py_INCREF(pattern); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 377 | re->re_realpat = pattern; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 378 | Py_INCREF(givenpat); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 379 | re->re_givenpat = givenpat; |
Guido van Rossum | d577c0c | 1992-01-27 16:46:19 +0000 | [diff] [blame] | 380 | error = re_compile_pattern(pat, size, &re->re_patbuf); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 381 | if (error != NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 382 | PyErr_SetString(RegexError, error); |
| 383 | Py_DECREF(re); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 384 | re = NULL; |
| 385 | } |
| 386 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 387 | return (PyObject *)re; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 390 | static PyObject * |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 391 | regex_compile(self, args) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 392 | PyObject *self; |
| 393 | PyObject *args; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 394 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 395 | PyObject *pat = NULL; |
| 396 | PyObject *tran = NULL; |
| 397 | if (!PyArg_Parse(args, "S", &pat)) { |
| 398 | PyErr_Clear(); |
| 399 | if (!PyArg_Parse(args, "(SS)", &pat, &tran)) |
Guido van Rossum | 36d330b | 1993-02-21 20:12:16 +0000 | [diff] [blame] | 400 | return NULL; |
| 401 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 402 | return newregexobject(pat, tran, pat, NULL); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 405 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 406 | symcomp(pattern, gdict) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 407 | PyObject *pattern; |
| 408 | PyObject *gdict; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 409 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 410 | char *opat = PyString_AsString(pattern); |
| 411 | char *oend = opat + PyString_Size(pattern); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 412 | int group_count = 0; |
| 413 | int escaped = 0; |
| 414 | char *o = opat; |
| 415 | char *n; |
| 416 | char name_buf[128]; |
| 417 | char *g; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 418 | PyObject *npattern; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 419 | int require_escape = re_syntax & RE_NO_BK_PARENS ? 0 : 1; |
| 420 | |
Guido van Rossum | ab28c56 | 1996-06-11 18:33:14 +0000 | [diff] [blame] | 421 | if (oend == opat) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 422 | Py_INCREF(pattern); |
Guido van Rossum | ab28c56 | 1996-06-11 18:33:14 +0000 | [diff] [blame] | 423 | return pattern; |
| 424 | } |
| 425 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 426 | npattern = PyString_FromStringAndSize((char*)NULL, PyString_Size(pattern)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 427 | if (npattern == NULL) |
| 428 | return NULL; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 429 | n = PyString_AsString(npattern); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 430 | |
| 431 | while (o < oend) { |
| 432 | if (*o == '(' && escaped == require_escape) { |
| 433 | char *backtrack; |
| 434 | escaped = 0; |
| 435 | ++group_count; |
| 436 | *n++ = *o; |
| 437 | if (++o >= oend || *o != '<') |
| 438 | continue; |
| 439 | /* *o == '<' */ |
| 440 | if (o+1 < oend && *(o+1) == '>') |
| 441 | continue; |
| 442 | backtrack = o; |
| 443 | g = name_buf; |
| 444 | for (++o; o < oend;) { |
| 445 | if (*o == '>') { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 446 | PyObject *group_name = NULL; |
| 447 | PyObject *group_index = NULL; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 448 | *g++ = '\0'; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 449 | group_name = PyString_FromString(name_buf); |
| 450 | group_index = PyInt_FromLong(group_count); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 451 | if (group_name == NULL || group_index == NULL |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 452 | || PyDict_SetItem(gdict, group_name, group_index) != 0) { |
| 453 | Py_XDECREF(group_name); |
| 454 | Py_XDECREF(group_index); |
| 455 | Py_XDECREF(npattern); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 456 | return NULL; |
| 457 | } |
| 458 | ++o; /* eat the '>' */ |
| 459 | break; |
| 460 | } |
Guido van Rossum | 7f7f274 | 1995-02-10 17:01:56 +0000 | [diff] [blame] | 461 | if (!isalnum(Py_CHARMASK(*o)) && *o != '_') { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 462 | o = backtrack; |
| 463 | break; |
| 464 | } |
| 465 | *g++ = *o++; |
| 466 | } |
| 467 | } |
Guido van Rossum | 0cbaff4 | 1996-10-23 17:53:06 +0000 | [diff] [blame] | 468 | else if (*o == '[' && !escaped) { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 469 | *n++ = *o; |
| 470 | ++o; /* eat the char following '[' */ |
| 471 | *n++ = *o; |
| 472 | while (o < oend && *o != ']') { |
| 473 | ++o; |
| 474 | *n++ = *o; |
| 475 | } |
| 476 | if (o < oend) |
| 477 | ++o; |
| 478 | } |
| 479 | else if (*o == '\\') { |
| 480 | escaped = 1; |
| 481 | *n++ = *o; |
| 482 | ++o; |
| 483 | } |
| 484 | else { |
| 485 | escaped = 0; |
| 486 | *n++ = *o; |
| 487 | ++o; |
| 488 | } |
| 489 | } |
| 490 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 491 | if (_PyString_Resize(&npattern, n - PyString_AsString(npattern)) == 0) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 492 | return npattern; |
| 493 | else { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 494 | return NULL; |
| 495 | } |
| 496 | |
| 497 | } |
| 498 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 499 | static PyObject * |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 500 | regex_symcomp(self, args) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 501 | PyObject *self; |
| 502 | PyObject *args; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 503 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 504 | PyObject *pattern; |
| 505 | PyObject *tran = NULL; |
| 506 | PyObject *gdict = NULL; |
| 507 | PyObject *npattern; |
| 508 | if (!PyArg_Parse(args, "S", &pattern)) { |
| 509 | PyErr_Clear(); |
| 510 | if (!PyArg_Parse(args, "(SS)", &pattern, &tran)) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 511 | return NULL; |
| 512 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 513 | gdict = PyDict_New(); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 514 | if (gdict == NULL |
| 515 | || (npattern = symcomp(pattern, gdict)) == NULL) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 516 | Py_DECREF(gdict); |
| 517 | Py_DECREF(pattern); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 518 | return NULL; |
| 519 | } |
| 520 | return newregexobject(npattern, tran, pattern, gdict); |
| 521 | } |
| 522 | |
| 523 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 524 | static PyObject *cache_pat; |
| 525 | static PyObject *cache_prog; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 526 | |
| 527 | static int |
| 528 | update_cache(pat) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 529 | PyObject *pat; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 530 | { |
| 531 | if (pat != cache_pat) { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 532 | Py_XDECREF(cache_pat); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 533 | cache_pat = NULL; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 534 | Py_XDECREF(cache_prog); |
| 535 | cache_prog = regex_compile((PyObject *)NULL, pat); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 536 | if (cache_prog == NULL) |
| 537 | return -1; |
| 538 | cache_pat = pat; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 539 | Py_INCREF(cache_pat); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 540 | } |
| 541 | return 0; |
| 542 | } |
| 543 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 544 | static PyObject * |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 545 | regex_match(self, args) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 546 | PyObject *self; |
| 547 | PyObject *args; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 548 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 549 | PyObject *pat, *string; |
| 550 | if (!PyArg_Parse(args, "(SS)", &pat, &string)) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 551 | return NULL; |
| 552 | if (update_cache(pat) < 0) |
| 553 | return NULL; |
| 554 | return reg_match((regexobject *)cache_prog, string); |
| 555 | } |
| 556 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 557 | static PyObject * |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 558 | regex_search(self, args) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 559 | PyObject *self; |
| 560 | PyObject *args; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 561 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 562 | PyObject *pat, *string; |
| 563 | if (!PyArg_Parse(args, "(SS)", &pat, &string)) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 564 | return NULL; |
| 565 | if (update_cache(pat) < 0) |
| 566 | return NULL; |
| 567 | return reg_search((regexobject *)cache_prog, string); |
| 568 | } |
| 569 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 570 | static PyObject * |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 571 | regex_set_syntax(self, args) |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 572 | PyObject *self, *args; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 573 | { |
| 574 | int syntax; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 575 | if (!PyArg_Parse(args, "i", &syntax)) |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 576 | return NULL; |
| 577 | syntax = re_set_syntax(syntax); |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 578 | return PyInt_FromLong((long)syntax); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 579 | } |
| 580 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 581 | static struct PyMethodDef regex_global_methods[] = { |
Guido van Rossum | 295d171 | 1995-02-19 15:55:19 +0000 | [diff] [blame] | 582 | {"compile", regex_compile, 0}, |
| 583 | {"symcomp", regex_symcomp, 0}, |
| 584 | {"match", regex_match, 0}, |
| 585 | {"search", regex_search, 0}, |
| 586 | {"set_syntax", regex_set_syntax, 0}, |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 587 | {NULL, NULL} /* sentinel */ |
| 588 | }; |
| 589 | |
Guido van Rossum | 8f3032d | 1996-08-19 22:03:12 +0000 | [diff] [blame] | 590 | void |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 591 | initregex() |
| 592 | { |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 593 | PyObject *m, *d, *v; |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 594 | |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 595 | m = Py_InitModule("regex", regex_global_methods); |
| 596 | d = PyModule_GetDict(m); |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 597 | |
| 598 | /* Initialize regex.error exception */ |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 599 | RegexError = PyString_FromString("regex.error"); |
| 600 | if (RegexError == NULL || PyDict_SetItemString(d, "error", RegexError) != 0) |
| 601 | Py_FatalError("can't define regex.error"); |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 602 | |
| 603 | /* Initialize regex.casefold constant */ |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 604 | v = PyString_FromStringAndSize((char *)NULL, 256); |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 605 | if (v != NULL) { |
| 606 | int i; |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 607 | char *s = PyString_AsString(v); |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 608 | for (i = 0; i < 256; i++) { |
| 609 | if (isupper(i)) |
| 610 | s[i] = tolower(i); |
| 611 | else |
| 612 | s[i] = i; |
| 613 | } |
Guido van Rossum | dfe8ad9 | 1996-07-24 00:51:20 +0000 | [diff] [blame] | 614 | PyDict_SetItemString(d, "casefold", v); |
| 615 | Py_DECREF(v); |
Guido van Rossum | ccd5bad | 1993-02-23 13:42:39 +0000 | [diff] [blame] | 616 | } |
Guido van Rossum | 6f4c43d | 1991-12-30 01:42:57 +0000 | [diff] [blame] | 617 | } |