annotate Modules/regexmodule.c @ 12357:5b59ea41db4e legacy-trunk

Install the docs (with fewer rules). Add descriptions for the system variables.
author Guido van Rossum <guido@python.org>
date Mon, 10 Apr 2000 19:36:27 +0000
parents dc65c5f6ad7c
children 1c7c2dd1beb1
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
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
122 if (!PyArg_ParseTuple(args, "O|i:match", &argstring, &offset))
5481
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;
9307
928ea5a409e5 Use the t# format where appropriate. Greg Stein.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
124 if (!PyArg_Parse(argstring, "t#", &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;
6094
a22fca7a5c5a Casts by Jack to shut up the Mac compiler.
Guido van Rossum <guido@python.org>
parents: 6014
diff changeset
133 result = _Py_re_match(&re->re_patbuf, (unsigned char *)buffer, size, offset,
6014
2f36e0bb37c3 Use _Py_re_match/search instead of re_match/search; these may become
Guido van Rossum <guido@python.org>
parents: 6004
diff changeset
134 &re->re_regs);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 if (result < -1) {
6004
3f59b34eb90f 1.5a3 prerelease 1 from AMK
Guido van Rossum <guido@python.org>
parents: 5481
diff changeset
136 /* Serious failure of some sort; if re_match didn't
3f59b34eb90f 1.5a3 prerelease 1 from AMK
Guido van Rossum <guido@python.org>
parents: 5481
diff changeset
137 set an exception, raise a generic error */
3f59b34eb90f 1.5a3 prerelease 1 from AMK
Guido van Rossum <guido@python.org>
parents: 5481
diff changeset
138 if (!PyErr_Occurred())
3f59b34eb90f 1.5a3 prerelease 1 from AMK
Guido van Rossum <guido@python.org>
parents: 5481
diff changeset
139 PyErr_SetString(RegexError, "match failure");
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
142 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
143 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
144 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
145 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
146 return PyInt_FromLong((long)result); /* Length of the match or -1 */
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 }
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
148
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
149 static PyObject *
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
150 regobj_search(re, args)
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 regexobject *re;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
152 PyObject *args;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 {
5481
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
154 PyObject *argstring;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 char *buffer;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 int size;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
157 int offset = 0;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 int range;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 int result;
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
160
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
161 if (!PyArg_ParseTuple(args, "O|i:search", &argstring, &offset))
5481
f1afc5690b2a Fix big ineficciency in regobj.search/match (introduced by Barry in an
Guido van Rossum <guido@python.org>
parents: 5129
diff changeset
162 return NULL;
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
163 if (!PyArg_Parse(argstring, "t#:search", &buffer, &size))
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
164 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
165
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
166 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
167 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
168 return NULL;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 }
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
170 /* 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
171 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
172 |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
173 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
174 range = size - offset;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
175 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
176 re->re_lastok = NULL;
6094
a22fca7a5c5a Casts by Jack to shut up the Mac compiler.
Guido van Rossum <guido@python.org>
parents: 6014
diff changeset
177 result = _Py_re_search(&re->re_patbuf, (unsigned char *)buffer, size, offset, range,
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 &re->re_regs);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 if (result < -1) {
6004
3f59b34eb90f 1.5a3 prerelease 1 from AMK
Guido van Rossum <guido@python.org>
parents: 5481
diff changeset
180 /* Serious failure of some sort; if re_match didn't
3f59b34eb90f 1.5a3 prerelease 1 from AMK
Guido van Rossum <guido@python.org>
parents: 5481
diff changeset
181 set an exception, raise a generic error */
3f59b34eb90f 1.5a3 prerelease 1 from AMK
Guido van Rossum <guido@python.org>
parents: 5481
diff changeset
182 if (!PyErr_Occurred())
3f59b34eb90f 1.5a3 prerelease 1 from AMK
Guido van Rossum <guido@python.org>
parents: 5481
diff changeset
183 PyErr_SetString(RegexError, "match failure");
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
186 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
187 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
188 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
189 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
190 return PyInt_FromLong((long)result); /* Position of the match or -1 */
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
192
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
193 /* 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
194 an integer index [0 .. 99]
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
195 */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
196 static PyObject*
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
197 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
198 regexobject *re;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
199 PyObject *index;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
200 {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
201 int i, a, b;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
202 char *v;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
203
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
204 if (PyString_Check(index))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
205 if (re->re_groupindex == NULL ||
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
206 !(index = PyDict_GetItem(re->re_groupindex, index)))
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 PyErr_SetString(RegexError,
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
209 "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
210 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
211 }
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
212
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
213 i = PyInt_AsLong(index);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
214 if (i == -1 && PyErr_Occurred())
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
215 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
216
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
217 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
218 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
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 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
222 PyErr_SetString(RegexError,
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
223 "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
224 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
225 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
226 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
227 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
228 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
229 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
230 return Py_None;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
231 }
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
232
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
233 if (!(v = PyString_AsString(re->re_lastok)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
234 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
235
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
236 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
237 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
238
4808
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 static PyObject *
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
241 regobj_group(re, args)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
242 regexobject *re;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
243 PyObject *args;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
244 {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
245 int n = PyTuple_Size(args);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
246 int i;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
247 PyObject *res = NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
248
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
249 if (n < 0)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
250 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
251 if (n == 0) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
252 PyErr_SetString(PyExc_TypeError, "not enough arguments");
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
253 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
254 }
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
255 if (n == 1) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
256 /* return value is a single string */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
257 PyObject *index = PyTuple_GetItem(args, 0);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
258 if (!index)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
259 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
260
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
261 return group_from_index(re, index);
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
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
264 /* return value is a tuple */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
265 if (!(res = PyTuple_New(n)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
266 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
267
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
268 for (i = 0; i < n; i++) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
269 PyObject *index = PyTuple_GetItem(args, i);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
270 PyObject *group = NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
271
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
272 if (!index)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
273 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
274 if (!(group = group_from_index(re, index)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
275 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
276 if (PyTuple_SetItem(res, i, group) < 0)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
277 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
278 }
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
279 return res;
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 finally:
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
282 Py_DECREF(res);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
283 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
284 }
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
285
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
286
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
287 static struct PyMethodDef reg_methods[] = {
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
288 {"match", (PyCFunction)regobj_match, 1},
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
289 {"search", (PyCFunction)regobj_search, 1},
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
290 {"group", (PyCFunction)regobj_group, 1},
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 {NULL, NULL} /* sentinel */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 };
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
293
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
294
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 static char* members[] = {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
297 "last", "regs", "translate",
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
298 "groupindex", "realpat", "givenpat",
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
299 NULL
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
300 };
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
301
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
302
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
303 static PyObject *
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
304 regobj_getattr(re, name)
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 regexobject *re;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 char *name;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 {
551
5a61f827f47c 'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents: 536
diff changeset
308 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
309 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
310 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
311 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
312 }
5a61f827f47c 'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents: 536
diff changeset
313 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
314 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
315 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
316 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
317 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
318 return Py_None;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
319 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
320 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
321 return re->re_lastok;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
322 }
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
323 if (strcmp(name, "translate") == 0) {
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
324 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
325 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
326 return Py_None;
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
327 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
328 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
329 return re->re_translate;
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
330 }
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
331 if (strcmp(name, "groupindex") == 0) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
332 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
333 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
334 return Py_None;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
335 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
336 Py_INCREF(re->re_groupindex);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
337 return re->re_groupindex;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
338 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
339 if (strcmp(name, "realpat") == 0) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
340 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
341 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
342 return Py_None;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
343 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
344 Py_INCREF(re->re_realpat);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
345 return re->re_realpat;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
346 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
347 if (strcmp(name, "givenpat") == 0) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
348 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
349 Py_INCREF(Py_None);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
350 return Py_None;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
351 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
352 Py_INCREF(re->re_givenpat);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
353 return re->re_givenpat;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
354 }
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
355 if (strcmp(name, "__members__") == 0) {
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
356 int i = 0;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
357 PyObject *list = NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
358
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
359 /* 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
360 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
361 */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
362 while (members[i])
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
363 i++;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
364 if (!(list = PyList_New(i)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
365 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
366
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
367 i = 0;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
368 while (members[i]) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
369 PyObject* v = PyString_FromString(members[i]);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
370 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
371 Py_DECREF(list);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
372 return NULL;
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
373 }
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
374 i++;
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
375 }
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
376 return list;
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
377 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
378 return Py_FindMethod(reg_methods, (PyObject *)re, name);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
380
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
381 static PyTypeObject Regextype = {
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
382 PyObject_HEAD_INIT(&PyType_Type)
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
383 0, /*ob_size*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
384 "regex", /*tp_name*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
385 sizeof(regexobject), /*tp_size*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
386 0, /*tp_itemsize*/
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 /* methods */
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
388 (destructor)reg_dealloc, /*tp_dealloc*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
389 0, /*tp_print*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
390 (getattrfunc)regobj_getattr, /*tp_getattr*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
391 0, /*tp_setattr*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
392 0, /*tp_compare*/
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
393 0, /*tp_repr*/
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 };
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
395
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
396 /* reference counting invariants:
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
397 pattern: borrowed
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
398 translate: borrowed
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
399 givenpat: borrowed
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
400 groupindex: transferred
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
401 */
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
402 static PyObject *
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
403 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
404 PyObject *pattern;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
405 PyObject *translate;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
406 PyObject *givenpat;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
407 PyObject *groupindex;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 regexobject *re;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
410 char *pat;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
411 int size;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
412
9307
928ea5a409e5 Use the t# format where appropriate. Greg Stein.
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
413 if (!PyArg_Parse(pattern, "t#", &pat, &size))
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
414 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
415
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
416 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
417 PyErr_SetString(RegexError,
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
418 "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
419 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
420 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
421 re = PyObject_NEW(regexobject, &Regextype);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 if (re != NULL) {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
423 char *error;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 re->re_patbuf.buffer = NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 re->re_patbuf.allocated = 0;
6094
a22fca7a5c5a Casts by Jack to shut up the Mac compiler.
Guido van Rossum <guido@python.org>
parents: 6014
diff changeset
426 re->re_patbuf.fastmap = (unsigned char *)re->re_fastmap;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
427 if (translate) {
6094
a22fca7a5c5a Casts by Jack to shut up the Mac compiler.
Guido van Rossum <guido@python.org>
parents: 6014
diff changeset
428 re->re_patbuf.translate = (unsigned char *)PyString_AsString(translate);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
429 if (!re->re_patbuf.translate)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
430 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
431 Py_INCREF(translate);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
432 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
433 else
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
434 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
435 re->re_translate = translate;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
436 re->re_lastok = NULL;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
437 re->re_groupindex = groupindex;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
438 Py_INCREF(pattern);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
439 re->re_realpat = pattern;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
440 Py_INCREF(givenpat);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
441 re->re_givenpat = givenpat;
6220
ed4924f3b2a5 Change [_Py_]re_compile_pattern() to return a char*.
Guido van Rossum <guido@python.org>
parents: 6094
diff changeset
442 error = _Py_re_compile_pattern((unsigned char *)pat, size, &re->re_patbuf);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 if (error != NULL) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
444 PyErr_SetString(RegexError, error);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
445 goto finally;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
446 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 }
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
448 return (PyObject *)re;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
449 finally:
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
450 Py_DECREF(re);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
451 return NULL;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
452 }
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 static PyObject *
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
455 regex_compile(self, args)
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
456 PyObject *self;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
457 PyObject *args;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
459 PyObject *pat = NULL;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
460 PyObject *tran = NULL;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
461
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
462 if (!PyArg_ParseTuple(args, "S|S:compile", &pat, &tran))
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
463 return NULL;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
464 return newregexobject(pat, tran, pat, NULL);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
465 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
466
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
467 static PyObject *
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
468 symcomp(pattern, gdict)
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
469 PyObject *pattern;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
470 PyObject *gdict;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
471 {
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
472 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
473 int group_count = 0;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
474 int sz;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
475 int escaped = 0;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
476 char name_buf[128];
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
477 PyObject *npattern;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
478 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
479
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
480 if (!(opat = PyString_AsString(pattern)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
481 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
482
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
483 if ((sz = PyString_Size(pattern)) < 0)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
484 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
485
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
486 oend = opat + sz;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
487 o = opat;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
488
3664
ad03e21fda54 Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
489 if (oend == opat) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
490 Py_INCREF(pattern);
3664
ad03e21fda54 Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
491 return pattern;
ad03e21fda54 Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
492 }
ad03e21fda54 Avoid core dump on symcomp("").
Guido van Rossum <guido@python.org>
parents: 2397
diff changeset
493
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
494 if (!(npattern = PyString_FromStringAndSize((char*)NULL, sz)) ||
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
495 !(n = PyString_AsString(npattern)))
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
496 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
497
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
498 while (o < oend) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
499 if (*o == '(' && escaped == require_escape) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
500 char *backtrack;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
501 escaped = 0;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
502 ++group_count;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
503 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
504 if (++o >= oend || *o != '<')
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
505 continue;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
506 /* *o == '<' */
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
507 if (o+1 < oend && *(o+1) == '>')
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
508 continue;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
509 backtrack = o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
510 g = name_buf;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
511 for (++o; o < oend;) {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
512 if (*o == '>') {
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
513 PyObject *group_name = NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
514 PyObject *group_index = NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
515 *g++ = '\0';
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
516 group_name = PyString_FromString(name_buf);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
517 group_index = PyInt_FromLong(group_count);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
518 if (group_name == NULL ||
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
519 group_index == NULL ||
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
520 PyDict_SetItem(gdict, group_name,
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
521 group_index) != 0)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
522 {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
523 Py_XDECREF(group_name);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
524 Py_XDECREF(group_index);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
525 Py_XDECREF(npattern);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
526 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
527 }
4917
1bcd4972da7e Nailed a couple of memory leaks, caught by Purify.
Barry Warsaw <barry@python.org>
parents: 4808
diff changeset
528 Py_DECREF(group_name);
1bcd4972da7e Nailed a couple of memory leaks, caught by Purify.
Barry Warsaw <barry@python.org>
parents: 4808
diff changeset
529 Py_DECREF(group_index);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
530 ++o; /* eat the '>' */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
531 break;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
532 }
2310
07913fb8f92f use Py_CHARMASK
Guido van Rossum <guido@python.org>
parents: 2234
diff changeset
533 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
534 o = backtrack;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
535 break;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
536 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
537 *g++ = *o++;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
538 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
539 }
4478
267ae58232d9 Fix case where you start a new group immediately after a label.
Guido van Rossum <guido@python.org>
parents: 4372
diff changeset
540 else if (*o == '[' && !escaped) {
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
541 *n++ = *o;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
542 ++o; /* eat the char following '[' */
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
543 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
544 while (o < oend && *o != ']') {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
545 ++o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
546 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
547 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
548 if (o < oend)
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 if (*o == '\\') {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
552 escaped = 1;
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 else {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
557 escaped = 0;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
558 *n++ = *o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
559 ++o;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
560 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
561 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
562
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
563 if (!(v = PyString_AsString(npattern))) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
564 Py_DECREF(npattern);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
565 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
566 }
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
567 /* _PyString_Resize() decrements npattern on failure */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
568 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
569 return npattern;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
570 else {
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
571 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
572 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
573
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
574 }
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 static PyObject *
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
577 regex_symcomp(self, args)
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
578 PyObject *self;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
579 PyObject *args;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
580 {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
581 PyObject *pattern;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
582 PyObject *tran = NULL;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
583 PyObject *gdict = NULL;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
584 PyObject *npattern;
4917
1bcd4972da7e Nailed a couple of memory leaks, caught by Purify.
Barry Warsaw <barry@python.org>
parents: 4808
diff changeset
585 PyObject *retval = NULL;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
586
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
587 if (!PyArg_ParseTuple(args, "S|S:symcomp", &pattern, &tran))
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
588 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
589
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
590 gdict = PyDict_New();
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
591 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
592 Py_DECREF(gdict);
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
593 Py_DECREF(pattern);
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
594 return NULL;
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
595 }
4917
1bcd4972da7e Nailed a couple of memory leaks, caught by Purify.
Barry Warsaw <barry@python.org>
parents: 4808
diff changeset
596 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
597 Py_DECREF(npattern);
1bcd4972da7e Nailed a couple of memory leaks, caught by Purify.
Barry Warsaw <barry@python.org>
parents: 4808
diff changeset
598 return retval;
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
599 }
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
600
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1239
diff changeset
601
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
602 static PyObject *cache_pat;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
603 static PyObject *cache_prog;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
604
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
605 static int
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 update_cache(pat)
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
607 PyObject *pat;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
608 {
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
609 PyObject *tuple = Py_BuildValue("(O)", pat);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
610 int status = 0;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
611
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
612 if (!tuple)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
613 return -1;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
614
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 if (pat != cache_pat) {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
616 Py_XDECREF(cache_pat);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
617 cache_pat = NULL;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
618 Py_XDECREF(cache_prog);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
619 cache_prog = regex_compile((PyObject *)NULL, tuple);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
620 if (cache_prog == NULL) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
621 status = -1;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
622 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
623 }
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
624 cache_pat = pat;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
625 Py_INCREF(cache_pat);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
626 }
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
627 finally:
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
628 Py_DECREF(tuple);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
629 return status;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
630 }
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 static PyObject *
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
633 regex_match(self, args)
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
634 PyObject *self;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
635 PyObject *args;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
637 PyObject *pat, *string;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
638 PyObject *tuple, *v;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
639
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
640 if (!PyArg_Parse(args, "(SS)", &pat, &string))
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
641 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
642 if (update_cache(pat) < 0)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
643 return NULL;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
644
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
645 if (!(tuple = Py_BuildValue("(S)", string)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
646 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
647 v = regobj_match((regexobject *)cache_prog, tuple);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
648 Py_DECREF(tuple);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
649 return v;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
650 }
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 static PyObject *
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
653 regex_search(self, args)
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
654 PyObject *self;
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
655 PyObject *args;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
656 {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
657 PyObject *pat, *string;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
658 PyObject *tuple, *v;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
659
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
660 if (!PyArg_Parse(args, "(SS)", &pat, &string))
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
661 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
662 if (update_cache(pat) < 0)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
663 return NULL;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
664
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
665 if (!(tuple = Py_BuildValue("(S)", string)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
666 return NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
667 v = regobj_search((regexobject *)cache_prog, tuple);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
668 Py_DECREF(tuple);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
669 return v;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
670 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
671
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
672 static PyObject *
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
673 regex_set_syntax(self, args)
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
674 PyObject *self;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
675 PyObject *args;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
676 {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
677 int syntax;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
678 if (!PyArg_Parse(args, "i", &syntax))
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
679 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
680 syntax = re_set_syntax(syntax);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
681 /* wipe the global pattern cache */
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
682 Py_XDECREF(cache_pat);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
683 cache_pat = NULL;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
684 Py_XDECREF(cache_prog);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
685 cache_prog = NULL;
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
686 return PyInt_FromLong((long)syntax);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
687 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
688
5129
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
689 static PyObject *
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
690 regex_get_syntax(self, args)
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
691 PyObject *self;
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
692 PyObject *args;
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 if (!PyArg_Parse(args, ""))
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
695 return NULL;
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
696 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
697 }
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
698
102e2912d0d1 regex_get_syntax(): New module function exported to Python.
Barry Warsaw <barry@python.org>
parents: 4917
diff changeset
699
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
700 static struct PyMethodDef regex_global_methods[] = {
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
701 {"compile", regex_compile, 1},
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
702 {"symcomp", regex_symcomp, 1},
2397
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2310
diff changeset
703 {"match", regex_match, 0},
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2310
diff changeset
704 {"search", regex_search, 0},
0736a7ec2f28 explicitly init flags in methodlists
Guido van Rossum <guido@python.org>
parents: 2310
diff changeset
705 {"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
706 {"get_syntax", regex_get_syntax, 0},
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
707 {NULL, NULL} /* sentinel */
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
708 };
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
709
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 9307
diff changeset
710 DL_EXPORT(void)
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
711 initregex()
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
712 {
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
713 PyObject *m, *d, *v;
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
714 int i;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
715 char *s;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
716
3803
2eaa00860edc Converted to new style names by Skip Montanaro
Guido van Rossum <guido@python.org>
parents: 3664
diff changeset
717 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
718 d = PyModule_GetDict(m);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
719
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
720 /* Initialize regex.error exception */
6367
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 6220
diff changeset
721 v = RegexError = PyErr_NewException("regex.error", NULL, NULL);
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
722 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
723 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
724
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
725 /* Initialize regex.casefold constant */
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
726 if (!(v = PyString_FromStringAndSize((char *)NULL, 256)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
727 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
728
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
729 if (!(s = PyString_AsString(v)))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
730 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
731
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
732 for (i = 0; i < 256; i++) {
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
733 if (isupper(i))
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
734 s[i] = tolower(i);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
735 else
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
736 s[i] = i;
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
737 }
4808
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
738 if (PyDict_SetItemString(d, "casefold", v) < 0)
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
739 goto finally;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
740 Py_DECREF(v);
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
741
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
742 if (!PyErr_Occurred())
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
743 return;
09d6c5708999 Several changes. Test program to follow.
Barry Warsaw <barry@python.org>
parents: 4600
diff changeset
744 finally:
6367
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 6220
diff changeset
745 /* Nothing */ ;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
746 }