Mercurial > cpython-lru_cache-weakref
annotate Modules/regexmodule.c @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 65d2d8e67da3 |
| children | d36250aefb82 |
| rev | line source |
|---|---|
| 536 | 1 /* |
| 2 XXX support range parameter on search | |
| 3 XXX support mstop parameter on search | |
| 4 */ | |
| 5 | |
| 6 /*********************************************************** | |
|
2061
172ba04a5191
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
7 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
172ba04a5191
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
8 The Netherlands. |
| 536 | 9 |
| 10 All Rights Reserved | |
| 11 | |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4478
diff
changeset
|
12 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4478
diff
changeset
|
13 documentation for any purpose and without fee is hereby granted, |
| 536 | 14 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4478
diff
changeset
|
15 both that copyright notice and this permission notice appear in |
| 536 | 16 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4478
diff
changeset
|
17 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4478
diff
changeset
|
18 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4478
diff
changeset
|
19 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4478
diff
changeset
|
20 permission. |
| 536 | 21 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4478
diff
changeset
|
22 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4478
diff
changeset
|
23 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4478
diff
changeset
|
24 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4478
diff
changeset
|
25 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4478
diff
changeset
|
26 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4478
diff
changeset
|
27 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4478
diff
changeset
|
28 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4478
diff
changeset
|
29 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4478
diff
changeset
|
30 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4478
diff
changeset
|
31 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4478
diff
changeset
|
32 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
4478
diff
changeset
|
33 PERFORMANCE OF THIS SOFTWARE. |
| 536 | 34 |
| 35 ******************************************************************/ | |
| 36 | |
| 37 /* Regular expression objects */ | |
|
615
40a0ec994c54
Use Tatu Ylonen's copyleft-free reimplementation of
Guido van Rossum <guido@python.org>
parents:
551
diff
changeset
|
38 /* This uses Tatu Ylonen's copyleft-free reimplementation of |
|
40a0ec994c54
Use Tatu Ylonen's copyleft-free reimplementation of
Guido van Rossum <guido@python.org>
parents:
551
diff
changeset
|
39 GNU regular expressions */ |
| 536 | 40 |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
41 #include "Python.h" |
| 536 | 42 |
|
615
40a0ec994c54
Use Tatu Ylonen's copyleft-free reimplementation of
Guido van Rossum <guido@python.org>
parents:
551
diff
changeset
|
43 #include "regexpr.h" |
| 536 | 44 |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
45 static PyObject *RegexError; /* Exception */ |
| 536 | 46 |
| 47 typedef struct { | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
48 PyObject_HEAD |
| 536 | 49 struct re_pattern_buffer re_patbuf; /* The compiled expression */ |
| 50 struct re_registers re_regs; /* The registers from the last match */ | |
| 51 char re_fastmap[256]; /* Storage for fastmap */ | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
52 PyObject *re_translate; /* String object for translate table */ |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
53 PyObject *re_lastok; /* String object last matched/searched */ |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
54 PyObject *re_groupindex; /* Group name to index dictionary */ |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
55 PyObject *re_givenpat; /* Pattern with symbolic groups */ |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
56 PyObject *re_realpat; /* Pattern without symbolic groups */ |
| 536 | 57 } regexobject; |
| 58 | |
| 59 /* Regex object methods */ | |
| 60 | |
| 61 static void | |
| 62 reg_dealloc(re) | |
| 63 regexobject *re; | |
| 64 { | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
65 PyMem_XDEL(re->re_patbuf.buffer); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
66 Py_XDECREF(re->re_translate); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
67 Py_XDECREF(re->re_lastok); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
68 Py_XDECREF(re->re_groupindex); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
69 Py_XDECREF(re->re_givenpat); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
70 Py_XDECREF(re->re_realpat); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
71 PyMem_DEL(re); |
| 536 | 72 } |
| 73 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
74 static PyObject * |
| 536 | 75 makeresult(regs) |
| 76 struct re_registers *regs; | |
| 77 { | |
|
4372
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
78 PyObject *v; |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
79 int i; |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
80 static PyObject *filler = NULL; |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
81 if (filler == NULL) { |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
82 filler = Py_BuildValue("(ii)", -1, -1); |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
83 if (filler == NULL) |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
84 return NULL; |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
85 } |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
86 v = PyTuple_New(RE_NREGS); |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
87 if (v == NULL) |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
88 return NULL; |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
89 for (i = 0; i < RE_NREGS; i++) { |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
90 int lo = regs->start[i]; |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
91 int hi = regs->end[i]; |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
92 PyObject *w; |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
93 if (lo == -1 && hi == -1) { |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
94 w = filler; |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
95 Py_INCREF(w); |
| 536 | 96 } |
|
4372
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
97 else |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
98 w = Py_BuildValue("(ii)", lo, hi); |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
99 if (w == NULL) { |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
100 Py_XDECREF(v); |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
101 return NULL; |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
102 } |
|
cbd434b46c1a
Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents:
4036
diff
changeset
|
103 PyTuple_SetItem(v, i, w); |
| 536 | 104 } |
| 105 return v; | |
| 106 } | |
| 107 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
108 static PyObject * |
| 536 | 109 reg_match(re, args) |
| 110 regexobject *re; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
111 PyObject *args; |
| 536 | 112 { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
113 PyObject *argstring; |
| 536 | 114 char *buffer; |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
115 int size; |
| 536 | 116 int offset; |
| 117 int result; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
118 if (PyArg_Parse(args, "S", &argstring)) { |
| 536 | 119 offset = 0; |
| 120 } | |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
121 else { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
122 PyErr_Clear(); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
123 if (!PyArg_Parse(args, "(Si)", &argstring, &offset)) |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
124 return NULL; |
|
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
125 } |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
126 buffer = PyString_AsString(argstring); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
127 size = PyString_Size(argstring); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
128 if (offset < 0 || offset > size) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
129 PyErr_SetString(RegexError, "match offset out of range"); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
130 return NULL; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
131 } |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
132 Py_XDECREF(re->re_lastok); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
133 re->re_lastok = NULL; |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
134 result = re_match(&re->re_patbuf, buffer, size, offset, &re->re_regs); |
| 536 | 135 if (result < -1) { |
| 136 /* Failure like stack overflow */ | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
137 PyErr_SetString(RegexError, "match failure"); |
| 536 | 138 return NULL; |
| 139 } | |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
140 if (result >= 0) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
141 Py_INCREF(argstring); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
142 re->re_lastok = argstring; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
143 } |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
144 return PyInt_FromLong((long)result); /* Length of the match or -1 */ |
| 536 | 145 } |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
146 |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
147 static PyObject * |
| 536 | 148 reg_search(re, args) |
| 149 regexobject *re; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
150 PyObject *args; |
| 536 | 151 { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
152 PyObject *argstring; |
| 536 | 153 char *buffer; |
| 154 int size; | |
| 155 int offset; | |
| 156 int range; | |
| 157 int result; | |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
158 |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
159 if (PyArg_Parse(args, "S", &argstring)) { |
| 536 | 160 offset = 0; |
| 161 } | |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
162 else { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
163 PyErr_Clear(); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
164 if (!PyArg_Parse(args, "(Si)", &argstring, &offset)) |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
165 return NULL; |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
166 } |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
167 buffer = PyString_AsString(argstring); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
168 size = PyString_Size(argstring); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
169 if (offset < 0 || offset > size) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
170 PyErr_SetString(RegexError, "search offset out of range"); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
171 return NULL; |
| 536 | 172 } |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
173 /* NB: In Emacs 18.57, the documentation for re_search[_2] and |
|
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
174 the implementation don't match: the documentation states that |
|
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
175 |range| positions are tried, while the code tries |range|+1 |
|
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
176 positions. It seems more productive to believe the code! */ |
|
628
d420af9c1b75
Make "range" one smaller (discrepancy between doc and source for regex.c!).
Guido van Rossum <guido@python.org>
parents:
615
diff
changeset
|
177 range = size - offset; |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
178 Py_XDECREF(re->re_lastok); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
179 re->re_lastok = NULL; |
| 536 | 180 result = re_search(&re->re_patbuf, buffer, size, offset, range, |
| 181 &re->re_regs); | |
| 182 if (result < -1) { | |
| 183 /* Failure like stack overflow */ | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
184 PyErr_SetString(RegexError, "match failure"); |
| 536 | 185 return NULL; |
| 186 } | |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
187 if (result >= 0) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
188 Py_INCREF(argstring); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
189 re->re_lastok = argstring; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
190 } |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
191 return PyInt_FromLong((long)result); /* Position of the match or -1 */ |
| 536 | 192 } |
| 193 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
194 static PyObject * |
|
1239
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
195 reg_group(re, args) |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
196 regexobject *re; |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
197 PyObject *args; |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
198 { |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
199 int i, a, b; |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
200 if (args != NULL && PyTuple_Check(args)) { |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
201 int n = PyTuple_Size(args); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
202 PyObject *res = PyTuple_New(n); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
203 if (res == NULL) |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
204 return NULL; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
205 for (i = 0; i < n; i++) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
206 PyObject *v = reg_group(re, PyTuple_GetItem(args, i)); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
207 if (v == NULL) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
208 Py_DECREF(res); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
209 return NULL; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
210 } |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
211 PyTuple_SetItem(res, i, v); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
212 } |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
213 return res; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
214 } |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
215 if (!PyArg_Parse(args, "i", &i)) { |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
216 PyObject *n; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
217 PyErr_Clear(); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
218 if (!PyArg_Parse(args, "S", &n)) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
219 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
220 else { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
221 PyObject *index; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
222 if (re->re_groupindex == NULL) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
223 index = NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
224 else |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
225 index = PyDict_GetItem(re->re_groupindex, n); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
226 if (index == NULL) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
227 PyErr_SetString(RegexError, "group() group name doesn't exist"); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
228 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
229 } |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
230 i = PyInt_AsLong(index); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
231 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
232 } |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
233 if (i < 0 || i >= RE_NREGS) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
234 PyErr_SetString(RegexError, "group() index out of range"); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
235 return NULL; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
236 } |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
237 if (re->re_lastok == NULL) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
238 PyErr_SetString(RegexError, |
|
1239
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
239 "group() only valid after successful match/search"); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
240 return NULL; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
241 } |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
242 a = re->re_regs.start[i]; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
243 b = re->re_regs.end[i]; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
244 if (a < 0 || b < 0) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
245 Py_INCREF(Py_None); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
246 return Py_None; |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
247 } |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
248 return PyString_FromStringAndSize(PyString_AsString(re->re_lastok)+a, b-a); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
249 } |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
250 |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
251 static struct PyMethodDef reg_methods[] = { |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
252 {"match", (PyCFunction)reg_match}, |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
253 {"search", (PyCFunction)reg_search}, |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
254 {"group", (PyCFunction)reg_group}, |
| 536 | 255 {NULL, NULL} /* sentinel */ |
| 256 }; | |
| 257 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
258 static PyObject * |
| 536 | 259 reg_getattr(re, name) |
| 260 regexobject *re; | |
| 261 char *name; | |
| 262 { | |
|
551
5a61f827f47c
'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents:
536
diff
changeset
|
263 if (strcmp(name, "regs") == 0) { |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
264 if (re->re_lastok == NULL) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
265 Py_INCREF(Py_None); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
266 return Py_None; |
|
551
5a61f827f47c
'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents:
536
diff
changeset
|
267 } |
|
5a61f827f47c
'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents:
536
diff
changeset
|
268 return makeresult(&re->re_regs); |
|
5a61f827f47c
'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents:
536
diff
changeset
|
269 } |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
270 if (strcmp(name, "last") == 0) { |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
271 if (re->re_lastok == NULL) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
272 Py_INCREF(Py_None); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
273 return Py_None; |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
274 } |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
275 Py_INCREF(re->re_lastok); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
276 return re->re_lastok; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
277 } |
|
1239
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
278 if (strcmp(name, "translate") == 0) { |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
279 if (re->re_translate == NULL) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
280 Py_INCREF(Py_None); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
281 return Py_None; |
|
1239
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
282 } |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
283 Py_INCREF(re->re_translate); |
|
1239
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
284 return re->re_translate; |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
285 } |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
286 if (strcmp(name, "groupindex") == 0) { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
287 if (re->re_groupindex == NULL) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
288 Py_INCREF(Py_None); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
289 return Py_None; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
290 } |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
291 Py_INCREF(re->re_groupindex); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
292 return re->re_groupindex; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
293 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
294 if (strcmp(name, "realpat") == 0) { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
295 if (re->re_realpat == NULL) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
296 Py_INCREF(Py_None); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
297 return Py_None; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
298 } |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
299 Py_INCREF(re->re_realpat); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
300 return re->re_realpat; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
301 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
302 if (strcmp(name, "givenpat") == 0) { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
303 if (re->re_givenpat == NULL) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
304 Py_INCREF(Py_None); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
305 return Py_None; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
306 } |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
307 Py_INCREF(re->re_givenpat); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
308 return re->re_givenpat; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
309 } |
|
1239
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
310 if (strcmp(name, "__members__") == 0) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
311 PyObject *list = PyList_New(6); |
|
1239
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
312 if (list) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
313 PyList_SetItem(list, 0, PyString_FromString("last")); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
314 PyList_SetItem(list, 1, PyString_FromString("regs")); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
315 PyList_SetItem(list, 2, PyString_FromString("translate")); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
316 PyList_SetItem(list, 3, PyString_FromString("groupindex")); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
317 PyList_SetItem(list, 4, PyString_FromString("realpat")); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
318 PyList_SetItem(list, 5, PyString_FromString("givenpat")); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
319 if (PyErr_Occurred()) { |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
320 Py_DECREF(list); |
|
1239
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
321 list = NULL; |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
322 } |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
323 } |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
324 return list; |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
325 } |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
326 return Py_FindMethod(reg_methods, (PyObject *)re, name); |
| 536 | 327 } |
| 328 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
329 static PyTypeObject Regextype = { |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
330 PyObject_HEAD_INIT(&PyType_Type) |
| 536 | 331 0, /*ob_size*/ |
| 332 "regex", /*tp_name*/ | |
| 333 sizeof(regexobject), /*tp_size*/ | |
| 334 0, /*tp_itemsize*/ | |
| 335 /* methods */ | |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
336 (destructor)reg_dealloc, /*tp_dealloc*/ |
| 536 | 337 0, /*tp_print*/ |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
338 (getattrfunc)reg_getattr, /*tp_getattr*/ |
| 536 | 339 0, /*tp_setattr*/ |
| 340 0, /*tp_compare*/ | |
| 341 0, /*tp_repr*/ | |
| 342 }; | |
| 343 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
344 static PyObject * |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
345 newregexobject(pattern, translate, givenpat, groupindex) |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
346 PyObject *pattern; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
347 PyObject *translate; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
348 PyObject *givenpat; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
349 PyObject *groupindex; |
| 536 | 350 { |
| 351 regexobject *re; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
352 char *pat = PyString_AsString(pattern); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
353 int size = PyString_Size(pattern); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
354 |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
355 if (translate != NULL && PyString_Size(translate) != 256) { |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
356 PyErr_SetString(RegexError, |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
357 "translation table must be 256 bytes"); |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
358 return NULL; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
359 } |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
360 re = PyObject_NEW(regexobject, &Regextype); |
| 536 | 361 if (re != NULL) { |
| 362 char *error; | |
| 363 re->re_patbuf.buffer = NULL; | |
| 364 re->re_patbuf.allocated = 0; | |
| 365 re->re_patbuf.fastmap = re->re_fastmap; | |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
366 if (translate) |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
367 re->re_patbuf.translate = PyString_AsString(translate); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
368 else |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
369 re->re_patbuf.translate = NULL; |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
370 Py_XINCREF(translate); |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
371 re->re_translate = translate; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
372 re->re_lastok = NULL; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
373 re->re_groupindex = groupindex; |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
374 Py_INCREF(pattern); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
375 re->re_realpat = pattern; |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
376 Py_INCREF(givenpat); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
377 re->re_givenpat = givenpat; |
|
636
d5bb8fe43df2
Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents:
628
diff
changeset
|
378 error = re_compile_pattern(pat, size, &re->re_patbuf); |
| 536 | 379 if (error != NULL) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
380 PyErr_SetString(RegexError, error); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
381 Py_DECREF(re); |
| 536 | 382 re = NULL; |
| 383 } | |
| 384 } | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
385 return (PyObject *)re; |
| 536 | 386 } |
| 387 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
388 static PyObject * |
| 536 | 389 regex_compile(self, args) |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
390 PyObject *self; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
391 PyObject *args; |
| 536 | 392 { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
393 PyObject *pat = NULL; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
394 PyObject *tran = NULL; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
395 if (!PyArg_Parse(args, "S", &pat)) { |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
396 PyErr_Clear(); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
397 if (!PyArg_Parse(args, "(SS)", &pat, &tran)) |
|
1237
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
398 return NULL; |
|
05580e00f41e
* regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents:
1019
diff
changeset
|
399 } |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
400 return newregexobject(pat, tran, pat, NULL); |
| 536 | 401 } |
| 402 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
403 static PyObject * |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
404 symcomp(pattern, gdict) |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
405 PyObject *pattern; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
406 PyObject *gdict; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
407 { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
408 char *opat = PyString_AsString(pattern); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
409 char *oend = opat + PyString_Size(pattern); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
410 int group_count = 0; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
411 int escaped = 0; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
412 char *o = opat; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
413 char *n; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
414 char name_buf[128]; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
415 char *g; |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
416 PyObject *npattern; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
417 int require_escape = re_syntax & RE_NO_BK_PARENS ? 0 : 1; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
418 |
|
3664
ad03e21fda54
Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents:
2397
diff
changeset
|
419 if (oend == opat) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
420 Py_INCREF(pattern); |
|
3664
ad03e21fda54
Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents:
2397
diff
changeset
|
421 return pattern; |
|
ad03e21fda54
Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents:
2397
diff
changeset
|
422 } |
|
ad03e21fda54
Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents:
2397
diff
changeset
|
423 |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
424 npattern = PyString_FromStringAndSize((char*)NULL, PyString_Size(pattern)); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
425 if (npattern == NULL) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
426 return NULL; |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
427 n = PyString_AsString(npattern); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
428 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
429 while (o < oend) { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
430 if (*o == '(' && escaped == require_escape) { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
431 char *backtrack; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
432 escaped = 0; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
433 ++group_count; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
434 *n++ = *o; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
435 if (++o >= oend || *o != '<') |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
436 continue; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
437 /* *o == '<' */ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
438 if (o+1 < oend && *(o+1) == '>') |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
439 continue; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
440 backtrack = o; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
441 g = name_buf; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
442 for (++o; o < oend;) { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
443 if (*o == '>') { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
444 PyObject *group_name = NULL; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
445 PyObject *group_index = NULL; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
446 *g++ = '\0'; |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
447 group_name = PyString_FromString(name_buf); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
448 group_index = PyInt_FromLong(group_count); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
449 if (group_name == NULL || group_index == NULL |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
450 || PyDict_SetItem(gdict, group_name, group_index) != 0) { |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
451 Py_XDECREF(group_name); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
452 Py_XDECREF(group_index); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
453 Py_XDECREF(npattern); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
454 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
455 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
456 ++o; /* eat the '>' */ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
457 break; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
458 } |
| 2310 | 459 if (!isalnum(Py_CHARMASK(*o)) && *o != '_') { |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
460 o = backtrack; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
461 break; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
462 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
463 *g++ = *o++; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
464 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
465 } |
|
4478
267ae58232d9
Fix case where you start a new group immediately after a label.
Guido van Rossum <guido@python.org>
parents:
4372
diff
changeset
|
466 else if (*o == '[' && !escaped) { |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
467 *n++ = *o; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
468 ++o; /* eat the char following '[' */ |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
469 *n++ = *o; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
470 while (o < oend && *o != ']') { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
471 ++o; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
472 *n++ = *o; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
473 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
474 if (o < oend) |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
475 ++o; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
476 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
477 else if (*o == '\\') { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
478 escaped = 1; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
479 *n++ = *o; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
480 ++o; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
481 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
482 else { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
483 escaped = 0; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
484 *n++ = *o; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
485 ++o; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
486 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
487 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
488 |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
489 if (_PyString_Resize(&npattern, n - PyString_AsString(npattern)) == 0) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
490 return npattern; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
491 else { |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
492 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
493 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
494 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
495 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
496 |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
497 static PyObject * |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
498 regex_symcomp(self, args) |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
499 PyObject *self; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
500 PyObject *args; |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
501 { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
502 PyObject *pattern; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
503 PyObject *tran = NULL; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
504 PyObject *gdict = NULL; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
505 PyObject *npattern; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
506 if (!PyArg_Parse(args, "S", &pattern)) { |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
507 PyErr_Clear(); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
508 if (!PyArg_Parse(args, "(SS)", &pattern, &tran)) |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
509 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
510 } |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
511 gdict = PyDict_New(); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
512 if (gdict == NULL |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
513 || (npattern = symcomp(pattern, gdict)) == NULL) { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
514 Py_DECREF(gdict); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
515 Py_DECREF(pattern); |
|
1746
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
516 return NULL; |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
517 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
518 return newregexobject(npattern, tran, pattern, gdict); |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
519 } |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
520 |
|
1940892d2fe3
Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents:
1239
diff
changeset
|
521 |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
522 static PyObject *cache_pat; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
523 static PyObject *cache_prog; |
| 536 | 524 |
| 525 static int | |
| 526 update_cache(pat) | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
527 PyObject *pat; |
| 536 | 528 { |
| 529 if (pat != cache_pat) { | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
530 Py_XDECREF(cache_pat); |
| 536 | 531 cache_pat = NULL; |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
532 Py_XDECREF(cache_prog); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
533 cache_prog = regex_compile((PyObject *)NULL, pat); |
| 536 | 534 if (cache_prog == NULL) |
| 535 return -1; | |
| 536 cache_pat = pat; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
537 Py_INCREF(cache_pat); |
| 536 | 538 } |
| 539 return 0; | |
| 540 } | |
| 541 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
542 static PyObject * |
| 536 | 543 regex_match(self, args) |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
544 PyObject *self; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
545 PyObject *args; |
| 536 | 546 { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
547 PyObject *pat, *string; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
548 if (!PyArg_Parse(args, "(SS)", &pat, &string)) |
| 536 | 549 return NULL; |
| 550 if (update_cache(pat) < 0) | |
| 551 return NULL; | |
| 552 return reg_match((regexobject *)cache_prog, string); | |
| 553 } | |
| 554 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
555 static PyObject * |
| 536 | 556 regex_search(self, args) |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
557 PyObject *self; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
558 PyObject *args; |
| 536 | 559 { |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
560 PyObject *pat, *string; |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
561 if (!PyArg_Parse(args, "(SS)", &pat, &string)) |
| 536 | 562 return NULL; |
| 563 if (update_cache(pat) < 0) | |
| 564 return NULL; | |
| 565 return reg_search((regexobject *)cache_prog, string); | |
| 566 } | |
| 567 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
568 static PyObject * |
| 536 | 569 regex_set_syntax(self, args) |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
570 PyObject *self, *args; |
| 536 | 571 { |
| 572 int syntax; | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
573 if (!PyArg_Parse(args, "i", &syntax)) |
| 536 | 574 return NULL; |
| 575 syntax = re_set_syntax(syntax); | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
576 return PyInt_FromLong((long)syntax); |
| 536 | 577 } |
| 578 | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
579 static struct PyMethodDef regex_global_methods[] = { |
|
2397
0736a7ec2f28
explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents:
2310
diff
changeset
|
580 {"compile", regex_compile, 0}, |
|
0736a7ec2f28
explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents:
2310
diff
changeset
|
581 {"symcomp", regex_symcomp, 0}, |
|
0736a7ec2f28
explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents:
2310
diff
changeset
|
582 {"match", regex_match, 0}, |
|
0736a7ec2f28
explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents:
2310
diff
changeset
|
583 {"search", regex_search, 0}, |
|
0736a7ec2f28
explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents:
2310
diff
changeset
|
584 {"set_syntax", regex_set_syntax, 0}, |
| 536 | 585 {NULL, NULL} /* sentinel */ |
| 586 }; | |
| 587 | |
|
4036
f7c243d01844
Declare initregex() as returning void, as it should be.
Guido van Rossum <guido@python.org>
parents:
3803
diff
changeset
|
588 void |
| 536 | 589 initregex() |
| 590 { | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
591 PyObject *m, *d, *v; |
| 536 | 592 |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
593 m = Py_InitModule("regex", regex_global_methods); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
594 d = PyModule_GetDict(m); |
| 536 | 595 |
| 596 /* Initialize regex.error exception */ | |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
597 RegexError = PyString_FromString("regex.error"); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
598 if (RegexError == NULL || PyDict_SetItemString(d, "error", RegexError) != 0) |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
599 Py_FatalError("can't define regex.error"); |
|
1239
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
600 |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
601 /* Initialize regex.casefold constant */ |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
602 v = PyString_FromStringAndSize((char *)NULL, 256); |
|
1239
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
603 if (v != NULL) { |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
604 int i; |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
605 char *s = PyString_AsString(v); |
|
1239
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
606 for (i = 0; i < 256; i++) { |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
607 if (isupper(i)) |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
608 s[i] = tolower(i); |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
609 else |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
610 s[i] = i; |
|
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
611 } |
|
3803
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
612 PyDict_SetItemString(d, "casefold", v); |
|
2eaa00860edc
Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents:
3664
diff
changeset
|
613 Py_DECREF(v); |
|
1239
0c657db27dbf
Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents:
1237
diff
changeset
|
614 } |
| 536 | 615 } |
