annotate Modules/regexmodule.c @ 5817:48b94de85bf3 legacy-trunk

Extensive changes by AMK.
author Guido van Rossum <guido@python.org>
date Thu, 17 Jul 1997 16:29:42 +0000
parents f1afc5690b2a
children 3f59b34eb90f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /*
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 XXX support range parameter on search
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 XXX support mstop parameter on search
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 All Rights Reserved
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 ******************************************************************/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42
4600
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
43 #include <ctype.h>
d36250aefb82 Keep gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
44
615
40a0ec994c54 Use Tatu Ylonen's copyleft-free reimplementation of
Guido van Rossum <guido@python.org>
parents: 551
diff changeset
45 #include "regexpr.h"
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
47 static PyObject *RegexError; /* Exception */
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 typedef struct {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
50 PyObject_HEAD
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 struct re_pattern_buffer re_patbuf; /* The compiled expression */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 struct re_registers re_regs; /* The registers from the last match */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 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
54 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
55 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
56 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
57 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
58 PyObject *re_realpat; /* Pattern without symbolic groups */
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 } regexobject;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 /* Regex object methods */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 static void
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 reg_dealloc(re)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 regexobject *re;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
67 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
68 Py_XDECREF(re->re_translate);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
69 Py_XDECREF(re->re_lastok);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
70 Py_XDECREF(re->re_groupindex);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
71 Py_XDECREF(re->re_givenpat);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
72 Py_XDECREF(re->re_realpat);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
73 PyMem_DEL(re);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
76 static PyObject *
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 makeresult(regs)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 struct re_registers *regs;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 {
4372
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
80 PyObject *v;
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
81 int i;
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
82 static PyObject *filler = NULL;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
83
4372
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
84 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
85 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
86 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
87 return NULL;
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
88 }
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
89 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
90 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
91 return NULL;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
92
4372
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
93 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
94 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
95 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
96 PyObject *w;
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
97 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
98 w = filler;
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
99 Py_INCREF(w);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 }
4372
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
101 else
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
102 w = Py_BuildValue("(ii)", lo, hi);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
103 if (w == NULL || PyTuple_SetItem(v, i, w) < 0) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
104 Py_DECREF(v);
4372
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
105 return NULL;
cbd434b46c1a Speedup of makeresult() by keeping a filler of (-1, -1) around.
Guido van Rossum <guido@python.org>
parents: 4036
diff changeset
106 }
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 return v;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
111 static PyObject *
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
112 regobj_match(re, args)
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 regexobject *re;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
114 PyObject *args;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 {
5481
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
116 PyObject *argstring;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 char *buffer;
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
118 int size;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
119 int offset = 0;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 int result;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
121
5481
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
122 if (!PyArg_ParseTuple(args, "O|i", &argstring, &offset))
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
123 return NULL;
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
124 if (!PyArg_Parse(argstring, "s#", &buffer, &size))
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
125 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
126
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
127 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
128 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
129 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
130 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
131 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
132 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
133 result = re_match(&re->re_patbuf, buffer, size, offset, &re->re_regs);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 if (result < -1) {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 /* Failure like stack overflow */
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
136 PyErr_SetString(RegexError, "match failure");
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
139 if (result >= 0) {
5481
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
140 Py_INCREF(argstring);
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
141 re->re_lastok = argstring;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
142 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
143 return PyInt_FromLong((long)result); /* Length of the match or -1 */
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 }
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
145
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
146 static PyObject *
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
147 regobj_search(re, args)
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 regexobject *re;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
149 PyObject *args;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 {
5481
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
151 PyObject *argstring;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 char *buffer;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 int size;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
154 int offset = 0;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 int range;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 int result;
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
157
5481
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
158 if (!PyArg_ParseTuple(args, "O|i", &argstring, &offset))
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
159 return NULL;
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
160 if (!PyArg_Parse(argstring, "s#", &buffer, &size))
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
161 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
162
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
163 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
164 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
165 return NULL;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 }
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
167 /* 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
168 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
169 |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
170 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
171 range = size - offset;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
172 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
173 re->re_lastok = NULL;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 result = re_search(&re->re_patbuf, buffer, size, offset, range,
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 &re->re_regs);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 if (result < -1) {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 /* Failure like stack overflow */
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
178 PyErr_SetString(RegexError, "match failure");
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
181 if (result >= 0) {
5481
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
182 Py_INCREF(argstring);
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
183 re->re_lastok = argstring;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
184 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
185 return PyInt_FromLong((long)result); /* Position of the match or -1 */
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
187
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
188 /* get the group from the regex where index can be a string (group name) or
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
189 an integer index [0 .. 99]
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
190 */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
191 static PyObject*
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
192 group_from_index(re, index)
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
193 regexobject *re;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
194 PyObject *index;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
195 {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
196 int i, a, b;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
197 char *v;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
198
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
199 if (PyString_Check(index))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
200 if (re->re_groupindex == NULL ||
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
201 !(index = PyDict_GetItem(re->re_groupindex, index)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
202 {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
203 PyErr_SetString(RegexError,
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
204 "group() group name doesn't exist");
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
205 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
206 }
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
207
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
208 i = PyInt_AsLong(index);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
209 if (i == -1 && PyErr_Occurred())
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
210 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
211
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
212 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
213 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
214 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
215 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
216 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
217 PyErr_SetString(RegexError,
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
218 "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
219 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
220 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
221 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
222 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
223 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
224 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
225 return Py_None;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
226 }
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
227
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
228 if (!(v = PyString_AsString(re->re_lastok)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
229 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
230
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
231 return PyString_FromStringAndSize(v+a, b-a);
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
232 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
233
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
234
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
235 static PyObject *
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
236 regobj_group(re, args)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
237 regexobject *re;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
238 PyObject *args;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
239 {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
240 int n = PyTuple_Size(args);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
241 int i;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
242 PyObject *res = NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
243
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
244 if (n < 0)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
245 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
246 if (n == 0) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
247 PyErr_SetString(PyExc_TypeError, "not enough arguments");
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
248 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
249 }
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
250 if (n == 1) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
251 /* return value is a single string */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
252 PyObject *index = PyTuple_GetItem(args, 0);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
253 if (!index)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
254 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
255
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
256 return group_from_index(re, index);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
257 }
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
258
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
259 /* return value is a tuple */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
260 if (!(res = PyTuple_New(n)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
261 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
262
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
263 for (i = 0; i < n; i++) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
264 PyObject *index = PyTuple_GetItem(args, i);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
265 PyObject *group = NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
266
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
267 if (!index)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
268 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
269 if (!(group = group_from_index(re, index)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
270 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
271 if (PyTuple_SetItem(res, i, group) < 0)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
272 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
273 }
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
274 return res;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
275
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
276 finally:
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
277 Py_DECREF(res);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
278 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
279 }
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
280
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
281
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
282 static struct PyMethodDef reg_methods[] = {
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
283 {"match", (PyCFunction)regobj_match, 1},
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
284 {"search", (PyCFunction)regobj_search, 1},
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
285 {"group", (PyCFunction)regobj_group, 1},
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 {NULL, NULL} /* sentinel */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
287 };
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
288
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
289
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
290
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
291 static char* members[] = {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
292 "last", "regs", "translate",
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
293 "groupindex", "realpat", "givenpat",
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
294 NULL
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
295 };
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
296
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
297
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
298 static PyObject *
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
299 regobj_getattr(re, name)
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 regexobject *re;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 char *name;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 {
551
5a61f827f47c 'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents: 536
diff changeset
303 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
304 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
305 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
306 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
307 }
5a61f827f47c 'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents: 536
diff changeset
308 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
309 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
310 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
311 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
312 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
313 return Py_None;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
314 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
315 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
316 return re->re_lastok;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
317 }
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
318 if (strcmp(name, "translate") == 0) {
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
319 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
320 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
321 return Py_None;
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
322 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
323 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
324 return re->re_translate;
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
325 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
326 if (strcmp(name, "groupindex") == 0) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
327 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
328 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
329 return Py_None;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
330 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
331 Py_INCREF(re->re_groupindex);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
332 return re->re_groupindex;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
333 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
334 if (strcmp(name, "realpat") == 0) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
335 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
336 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
337 return Py_None;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
338 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
339 Py_INCREF(re->re_realpat);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
340 return re->re_realpat;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
341 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
342 if (strcmp(name, "givenpat") == 0) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
343 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
344 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
345 return Py_None;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
346 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
347 Py_INCREF(re->re_givenpat);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
348 return re->re_givenpat;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
349 }
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
350 if (strcmp(name, "__members__") == 0) {
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
351 int i = 0;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
352 PyObject *list = NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
353
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
354 /* okay, so it's unlikely this list will change that often.
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
355 still, it's easier to change it in just one place.
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
356 */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
357 while (members[i])
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
358 i++;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
359 if (!(list = PyList_New(i)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
360 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
361
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
362 i = 0;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
363 while (members[i]) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
364 PyObject* v = PyString_FromString(members[i]);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
365 if (!v || PyList_SetItem(list, i, v) < 0) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
366 Py_DECREF(list);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
367 return NULL;
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
368 }
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
369 i++;
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
370 }
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
371 return list;
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
372 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
373 return Py_FindMethod(reg_methods, (PyObject *)re, name);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
375
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
376 static PyTypeObject Regextype = {
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
377 PyObject_HEAD_INIT(&PyType_Type)
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
378 0, /*ob_size*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
379 "regex", /*tp_name*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
380 sizeof(regexobject), /*tp_size*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
381 0, /*tp_itemsize*/
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 /* methods */
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
383 (destructor)reg_dealloc, /*tp_dealloc*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
384 0, /*tp_print*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
385 (getattrfunc)regobj_getattr, /*tp_getattr*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
386 0, /*tp_setattr*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
387 0, /*tp_compare*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
388 0, /*tp_repr*/
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 };
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
390
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
391 /* reference counting invariants:
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
392 pattern: borrowed
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
393 translate: borrowed
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
394 givenpat: borrowed
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
395 groupindex: transferred
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
396 */
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
397 static PyObject *
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
398 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
399 PyObject *pattern;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
400 PyObject *translate;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
401 PyObject *givenpat;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
402 PyObject *groupindex;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 regexobject *re;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
405 char *pat;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
406 int size;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
407
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
408 if (!PyArg_Parse(pattern, "s#", &pat, &size))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
409 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
410
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
411 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
412 PyErr_SetString(RegexError,
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
413 "translation table must be 256 bytes");
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
414 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
415 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
416 re = PyObject_NEW(regexobject, &Regextype);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 if (re != NULL) {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 char *error;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 re->re_patbuf.buffer = NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 re->re_patbuf.allocated = 0;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 re->re_patbuf.fastmap = re->re_fastmap;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
422 if (translate) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
423 re->re_patbuf.translate = PyString_AsString(translate);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
424 if (!re->re_patbuf.translate)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
425 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
426 Py_INCREF(translate);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
427 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
428 else
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
429 re->re_patbuf.translate = NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
430 re->re_translate = translate;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
431 re->re_lastok = NULL;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
432 re->re_groupindex = groupindex;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
433 Py_INCREF(pattern);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
434 re->re_realpat = pattern;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
435 Py_INCREF(givenpat);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
436 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
437 error = re_compile_pattern(pat, size, &re->re_patbuf);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 if (error != NULL) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
439 PyErr_SetString(RegexError, error);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
440 goto finally;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
443 return (PyObject *)re;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
444 finally:
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
445 Py_DECREF(re);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
446 return NULL;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
448
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
449 static PyObject *
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 regex_compile(self, args)
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
451 PyObject *self;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
452 PyObject *args;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
454 PyObject *pat = NULL;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
455 PyObject *tran = NULL;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
456
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
457 if (!PyArg_ParseTuple(args, "S|S", &pat, &tran))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
458 return NULL;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
459 return newregexobject(pat, tran, pat, NULL);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
461
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
462 static PyObject *
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
463 symcomp(pattern, gdict)
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
464 PyObject *pattern;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
465 PyObject *gdict;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
466 {
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
467 char *opat, *oend, *o, *n, *g, *v;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
468 int group_count = 0;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
469 int sz;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
470 int escaped = 0;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
471 char name_buf[128];
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
472 PyObject *npattern;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
473 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
474
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
475 if (!(opat = PyString_AsString(pattern)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
476 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
477
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
478 if ((sz = PyString_Size(pattern)) < 0)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
479 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
480
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
481 oend = opat + sz;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
482 o = opat;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
483
3664
ad03e21fda54 Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
484 if (oend == opat) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
485 Py_INCREF(pattern);
3664
ad03e21fda54 Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
486 return pattern;
ad03e21fda54 Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
487 }
ad03e21fda54 Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
488
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
489 if (!(npattern = PyString_FromStringAndSize((char*)NULL, sz)) ||
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
490 !(n = PyString_AsString(npattern)))
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
491 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
492
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
493 while (o < oend) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
494 if (*o == '(' && escaped == require_escape) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
495 char *backtrack;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
496 escaped = 0;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
497 ++group_count;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
498 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
499 if (++o >= oend || *o != '<')
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
500 continue;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
501 /* *o == '<' */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
502 if (o+1 < oend && *(o+1) == '>')
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
503 continue;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
504 backtrack = o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
505 g = name_buf;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
506 for (++o; o < oend;) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
507 if (*o == '>') {
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
508 PyObject *group_name = NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
509 PyObject *group_index = NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
510 *g++ = '\0';
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
511 group_name = PyString_FromString(name_buf);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
512 group_index = PyInt_FromLong(group_count);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
513 if (group_name == NULL ||
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
514 group_index == NULL ||
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
515 PyDict_SetItem(gdict, group_name,
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
516 group_index) != 0)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
517 {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
518 Py_XDECREF(group_name);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
519 Py_XDECREF(group_index);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
520 Py_XDECREF(npattern);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
521 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
522 }
4917
1bcd4972da7e Nailed a couple of memory leaks, caught by Purify.
Barry Warsaw <barry@python.org>
parents: 4808
diff changeset
523 Py_DECREF(group_name);
1bcd4972da7e Nailed a couple of memory leaks, caught by Purify.
Barry Warsaw <barry@python.org>
parents: 4808
diff changeset
524 Py_DECREF(group_index);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
525 ++o; /* eat the '>' */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
526 break;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
527 }
2310
07913fb8f92f use Py_CHARMASK
Guido van Rossum <guido@python.org>
parents: 2234
diff changeset
528 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
529 o = backtrack;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
530 break;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
531 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
532 *g++ = *o++;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
533 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
534 }
4478
267ae58232d9 Fix case where you start a new group immediately after a label.
Guido van Rossum <guido@python.org>
parents: 4372
diff changeset
535 else if (*o == '[' && !escaped) {
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
536 *n++ = *o;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
537 ++o; /* eat the char following '[' */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
538 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
539 while (o < oend && *o != ']') {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
540 ++o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
541 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
542 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
543 if (o < oend)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
544 ++o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
545 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
546 else if (*o == '\\') {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
547 escaped = 1;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
548 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
549 ++o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
550 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
551 else {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
552 escaped = 0;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
553 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
554 ++o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
555 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
556 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
557
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
558 if (!(v = PyString_AsString(npattern))) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
559 Py_DECREF(npattern);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
560 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
561 }
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
562 /* _PyString_Resize() decrements npattern on failure */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
563 if (_PyString_Resize(&npattern, n - v) == 0)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
564 return npattern;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
565 else {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
566 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
567 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
568
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
569 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
570
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
571 static PyObject *
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
572 regex_symcomp(self, args)
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
573 PyObject *self;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
574 PyObject *args;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
575 {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
576 PyObject *pattern;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
577 PyObject *tran = NULL;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
578 PyObject *gdict = NULL;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
579 PyObject *npattern;
4917
1bcd4972da7e Nailed a couple of memory leaks, caught by Purify.
Barry Warsaw <barry@python.org>
parents: 4808
diff changeset
580 PyObject *retval = NULL;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
581
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
582 if (!PyArg_ParseTuple(args, "S|S", &pattern, &tran))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
583 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
584
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
585 gdict = PyDict_New();
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
586 if (gdict == NULL || (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
587 Py_DECREF(gdict);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
588 Py_DECREF(pattern);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
589 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
590 }
4917
1bcd4972da7e Nailed a couple of memory leaks, caught by Purify.
Barry Warsaw <barry@python.org>
parents: 4808
diff changeset
591 retval = newregexobject(npattern, tran, pattern, gdict);
1bcd4972da7e Nailed a couple of memory leaks, caught by Purify.
Barry Warsaw <barry@python.org>
parents: 4808
diff changeset
592 Py_DECREF(npattern);
1bcd4972da7e Nailed a couple of memory leaks, caught by Purify.
Barry Warsaw <barry@python.org>
parents: 4808
diff changeset
593 return retval;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
594 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
595
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
596
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
597 static PyObject *cache_pat;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
598 static PyObject *cache_prog;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
599
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 static int
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
601 update_cache(pat)
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
602 PyObject *pat;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 {
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
604 PyObject *tuple = Py_BuildValue("(O)", pat);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
605 int status = 0;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
606
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
607 if (!tuple)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
608 return -1;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
609
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
610 if (pat != cache_pat) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
611 Py_XDECREF(cache_pat);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
612 cache_pat = NULL;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
613 Py_XDECREF(cache_prog);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
614 cache_prog = regex_compile((PyObject *)NULL, tuple);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
615 if (cache_prog == NULL) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
616 status = -1;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
617 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
618 }
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
619 cache_pat = pat;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
620 Py_INCREF(cache_pat);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 }
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
622 finally:
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
623 Py_DECREF(tuple);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
624 return status;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
625 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
626
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
627 static PyObject *
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
628 regex_match(self, args)
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
629 PyObject *self;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
630 PyObject *args;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
631 {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
632 PyObject *pat, *string;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
633 PyObject *tuple, *v;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
634
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
635 if (!PyArg_Parse(args, "(SS)", &pat, &string))
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
637 if (update_cache(pat) < 0)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
638 return NULL;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
639
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
640 if (!(tuple = Py_BuildValue("(S)", string)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
641 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
642 v = regobj_match((regexobject *)cache_prog, tuple);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
643 Py_DECREF(tuple);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
644 return v;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
645 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
646
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
647 static PyObject *
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
648 regex_search(self, args)
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
649 PyObject *self;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
650 PyObject *args;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
651 {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
652 PyObject *pat, *string;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
653 PyObject *tuple, *v;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
654
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
655 if (!PyArg_Parse(args, "(SS)", &pat, &string))
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
656 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
657 if (update_cache(pat) < 0)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
658 return NULL;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
659
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
660 if (!(tuple = Py_BuildValue("(S)", string)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
661 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
662 v = regobj_search((regexobject *)cache_prog, tuple);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
663 Py_DECREF(tuple);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
664 return v;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
665 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
666
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
667 static PyObject *
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
668 regex_set_syntax(self, args)
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
669 PyObject *self;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
670 PyObject *args;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
671 {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
672 int syntax;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
673 if (!PyArg_Parse(args, "i", &syntax))
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
674 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
675 syntax = re_set_syntax(syntax);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
676 /* wipe the global pattern cache */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
677 Py_XDECREF(cache_pat);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
678 cache_pat = NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
679 Py_XDECREF(cache_prog);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
680 cache_prog = NULL;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
681 return PyInt_FromLong((long)syntax);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
682 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
683
5129
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
684 static PyObject *
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
685 regex_get_syntax(self, args)
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
686 PyObject *self;
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
687 PyObject *args;
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
688 {
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
689 if (!PyArg_Parse(args, ""))
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
690 return NULL;
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
691 return PyInt_FromLong((long)re_syntax);
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
692 }
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
693
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
694
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
695 static struct PyMethodDef regex_global_methods[] = {
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
696 {"compile", regex_compile, 1},
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
697 {"symcomp", regex_symcomp, 1},
2397
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2310
diff changeset
698 {"match", regex_match, 0},
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2310
diff changeset
699 {"search", regex_search, 0},
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2310
diff changeset
700 {"set_syntax", regex_set_syntax, 0},
5129
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
701 {"get_syntax", regex_get_syntax, 0},
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
702 {NULL, NULL} /* sentinel */
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
703 };
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
704
4036
f7c243d01844 Declare initregex() as returning void, as it should be.
Guido van Rossum <guido@python.org>
parents: 3803
diff changeset
705 void
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
706 initregex()
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
707 {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
708 PyObject *m, *d, *v;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
709 int i;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
710 char *s;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
711
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
712 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
713 d = PyModule_GetDict(m);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
714
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
715 /* Initialize regex.error exception */
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
716 v = RegexError = PyString_FromString("regex.error");
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
717 if (v == NULL || PyDict_SetItemString(d, "error", v) != 0)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
718 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
719
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
720 /* Initialize regex.casefold constant */
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
721 if (!(v = PyString_FromStringAndSize((char *)NULL, 256)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
722 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
723
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
724 if (!(s = PyString_AsString(v)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
725 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
726
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
727 for (i = 0; i < 256; i++) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
728 if (isupper(i))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
729 s[i] = tolower(i);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
730 else
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
731 s[i] = i;
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
732 }
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
733 if (PyDict_SetItemString(d, "casefold", v) < 0)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
734 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
735 Py_DECREF(v);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
736
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
737 if (!PyErr_Occurred())
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
738 return;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
739 finally:
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
740 Py_FatalError("can't initialize regex module");
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
741 }