Mercurial > cpython-lru_cache-weakref
annotate Modules/reopmodule.c @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | 31cae374f786 |
| children | 0965d9fd1862 |
| rev | line source |
|---|---|
|
5755
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 /*********************************************************** |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 The Netherlands. |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 All Rights Reserved |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 provided that the above copyright notice appear in all copies and that |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 supporting documentation, and that the names of Stichting Mathematisch |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 distribution of the software without specific, written prior |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 permission. |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
20 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 ******************************************************************/ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 /* $Id$ */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
33 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
34 /* Regular expression objects */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
35 /* This uses Tatu Ylonen's copyleft-free reimplementation of |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
36 GNU regular expressions */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
38 #include "Python.h" |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
39 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
40 #include <ctype.h> |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
42 #include "regexpr.h" |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
43 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 static PyObject *ReopError; /* Exception */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
46 static PyObject * |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
47 makeresult(regs, num_regs) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 struct re_registers *regs; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
49 int num_regs; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
50 { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 PyObject *v; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 int i; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
53 static PyObject *filler = NULL; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
54 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
55 if (filler == NULL) { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 filler = Py_BuildValue("(ii)", -1, -1); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
57 if (filler == NULL) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 return NULL; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
59 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
60 v = PyTuple_New(num_regs); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
61 if (v == NULL) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 return NULL; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
63 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
64 for (i = 0; i < num_regs; i++) { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
65 int lo = regs->start[i]; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
66 int hi = regs->end[i]; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
67 PyObject *w; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 if (lo == -1 && hi == -1) { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 w = filler; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 Py_INCREF(w); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 else |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
73 w = Py_BuildValue("(ii)", lo, hi); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
74 if (w == NULL || PyTuple_SetItem(v, i, w) < 0) { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
75 Py_DECREF(v); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
76 return NULL; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
79 return v; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
80 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 static PyObject * |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 reop_match(self, args) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 PyObject *self; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 PyObject *args; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
87 char *string; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
88 int fastmaplen, stringlen; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 int can_be_null, anchor, i; |
| 5788 | 90 int flags, pos, result; |
|
5755
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
91 struct re_pattern_buffer bufp; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
92 struct re_registers re_regs; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
93 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 if (!PyArg_Parse(args, "(s#iiis#is#i)", |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 &(bufp.buffer), &(bufp.allocated), |
| 5788 | 96 &(bufp.num_registers), &flags, &can_be_null, |
|
5755
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
97 &(bufp.fastmap), &fastmaplen, |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
98 &anchor, |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 &string, &stringlen, |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
100 &pos)) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
101 return NULL; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
102 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
103 /* XXX sanity-check the input data */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
104 bufp.used=bufp.allocated; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
105 bufp.translate=NULL; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
106 bufp.fastmap_accurate=1; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
107 bufp.can_be_null=can_be_null; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 bufp.uses_registers=1; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
109 bufp.anchor=anchor; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
110 |
| 5788 | 111 for(i=0; i<bufp.num_registers; i++) {re_regs.start[i]=-1; re_regs.end[i]=-1;} |
|
5755
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
112 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
113 result = re_match(&bufp, |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
114 string, stringlen, pos, |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
115 &re_regs); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
116 if (result < -1) { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 /* Failure like stack overflow */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
118 PyErr_SetString(ReopError, "match failure"); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 return NULL; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 } |
|
5763
fd4e9f293c77
New version from Jeffrey after I complained about some glaring bugs.
Guido van Rossum <guido@python.org>
parents:
5755
diff
changeset
|
121 if (result == -1) { |
|
fd4e9f293c77
New version from Jeffrey after I complained about some glaring bugs.
Guido van Rossum <guido@python.org>
parents:
5755
diff
changeset
|
122 Py_INCREF(Py_None); |
|
fd4e9f293c77
New version from Jeffrey after I complained about some glaring bugs.
Guido van Rossum <guido@python.org>
parents:
5755
diff
changeset
|
123 return Py_None; |
|
fd4e9f293c77
New version from Jeffrey after I complained about some glaring bugs.
Guido van Rossum <guido@python.org>
parents:
5755
diff
changeset
|
124 } |
| 5788 | 125 return makeresult(&re_regs, bufp.num_registers); |
|
5755
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
126 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
128 static PyObject * |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
129 reop_search(self, args) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
130 PyObject *self; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
131 PyObject *args; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
132 { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
133 char *string; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 int fastmaplen, stringlen; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
135 int can_be_null, anchor, i; |
| 5788 | 136 int flags, pos, result; |
|
5755
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
137 struct re_pattern_buffer bufp; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
138 struct re_registers re_regs; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
139 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
140 if (!PyArg_Parse(args, "(s#iiis#is#i)", |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
141 &(bufp.buffer), &(bufp.allocated), |
| 5788 | 142 &(bufp.num_registers), &flags, &can_be_null, |
|
5755
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
143 &(bufp.fastmap), &fastmaplen, |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 &anchor, |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 &string, &stringlen, |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
146 &pos)) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
147 return NULL; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
148 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
149 /* XXX sanity-check the input data */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
150 bufp.used=bufp.allocated; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
151 bufp.translate=NULL; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
152 bufp.fastmap_accurate=1; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
153 bufp.can_be_null=can_be_null; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
154 bufp.uses_registers=1; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
155 bufp.anchor=anchor; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
156 |
| 5788 | 157 for(i=0; i<bufp.num_registers; i++) {re_regs.start[i]=-1; re_regs.end[i]=-1;} |
|
5755
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
158 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
159 result = re_search(&bufp, |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
160 string, stringlen, pos, stringlen-pos, |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
161 &re_regs); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
162 if (result < -1) { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
163 /* Failure like stack overflow */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
164 PyErr_SetString(ReopError, "match failure"); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
165 return NULL; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
166 } |
|
5763
fd4e9f293c77
New version from Jeffrey after I complained about some glaring bugs.
Guido van Rossum <guido@python.org>
parents:
5755
diff
changeset
|
167 if (result == -1) { |
|
fd4e9f293c77
New version from Jeffrey after I complained about some glaring bugs.
Guido van Rossum <guido@python.org>
parents:
5755
diff
changeset
|
168 Py_INCREF(Py_None); |
|
fd4e9f293c77
New version from Jeffrey after I complained about some glaring bugs.
Guido van Rossum <guido@python.org>
parents:
5755
diff
changeset
|
169 return Py_None; |
|
fd4e9f293c77
New version from Jeffrey after I complained about some glaring bugs.
Guido van Rossum <guido@python.org>
parents:
5755
diff
changeset
|
170 } |
| 5788 | 171 return makeresult(&re_regs, bufp.num_registers); |
|
5755
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
172 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
173 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
174 #if 0 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
175 /* Functions originally in the regsub module. |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
176 Added June 1, 1997. |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
177 */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
178 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
179 /* A cache of previously used patterns is maintained. Notice that if |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
180 you change the reop syntax flag, entries in the cache are |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 invalidated. |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
182 XXX Solution: use (syntax flag, pattern) as keys? Clear the cache |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
183 every so often, or once it gets past a certain size? |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
184 */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
185 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
186 static PyObject *cache_dict=NULL; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
187 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
188 /* Accept an object; if it's a reop pattern, Py_INCREF it and return |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 it. If it's a string, a reop object is compiled and cached. |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
192 static reopobject * |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
193 cached_compile(pattern) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
194 PyObject *pattern; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
195 { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
196 reopobject *p2; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
197 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
198 if (!PyString_Check(pattern)) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
199 { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
200 /* It's not a string, so assume it's a compiled reop object */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
201 /* XXX check that! */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
202 Py_INCREF(pattern); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
203 return (reopobject*)pattern; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
204 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
205 if (cache_dict==NULL) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
206 { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
207 cache_dict=PyDict_New(); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
208 if (cache_dict==NULL) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
209 { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
210 return (reopobject*)NULL; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
211 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
212 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
213 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
214 /* See if the pattern has already been cached; if so, return that |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
215 reop object */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
216 p2=(reopobject*)PyDict_GetItem(cache_dict, pattern); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
217 if (p2) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
218 { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
219 Py_INCREF(p2); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
220 return (reopobject*)p2; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
221 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
223 /* Compile the pattern and cache it */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
224 p2=(reopobject*)newreopobject(pattern, NULL, pattern, NULL); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 if (!p2) return p2; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
226 PyDict_SetItem(cache_dict, pattern, (PyObject*)p2); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
227 return p2; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
228 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
230 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
231 static PyObject * |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
232 internal_split(args, retain) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
233 PyObject *args; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
234 int retain; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
235 { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
236 PyObject *newlist, *s; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
237 reopobject *pattern; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
238 int maxsplit=0, count=0, length, next=0, result; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
239 int match_end=0; /* match_start is defined below */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
240 char *start; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
241 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
242 if (!PyArg_ParseTuple(args, "s#Oi", &start, &length, &pattern, |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
243 &maxsplit)) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
244 { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
245 PyErr_Clear(); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
246 if (!PyArg_ParseTuple(args, "s#O", &start, &length, &pattern)) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
247 return NULL; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
248 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
249 pattern=cached_compile((PyObject *)pattern); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
250 if (!pattern) return NULL; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
251 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
252 newlist=PyList_New(0); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 if (!newlist) return NULL; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
254 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
255 do |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
257 result = re_search(&pattern->re_patbuf, |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
258 start, length, next, length-next, |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
259 &pattern->re_regs); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
260 if (result < -1) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
261 { /* Erk... an error happened during the reop search */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
262 Py_DECREF(newlist); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
263 PyErr_SetString(ReopError, "match failure"); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
264 return NULL; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
265 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
266 if (next<=result) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
267 { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
268 int match_start=pattern->re_regs.start[0]; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
269 int oldmatch_end=match_end; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
270 match_end=pattern->re_regs.end[0]; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
271 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
272 if (match_start==match_end) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
273 { /* A zero-length match; increment to the next position */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
274 next=result+1; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
275 match_end=oldmatch_end; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
276 continue; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
277 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
278 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
279 /* Append the string up to the start of the match */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
280 s=PyString_FromStringAndSize(start+oldmatch_end, match_start-oldmatch_end); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
281 if (!s) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
282 { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
283 Py_DECREF(newlist); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
284 return NULL; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
285 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
286 PyList_Append(newlist, s); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
287 Py_DECREF(s); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
288 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
289 if (retain) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
290 { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
291 /* Append a string containing whatever matched */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
292 s=PyString_FromStringAndSize(start+match_start, match_end-match_start); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
293 if (!s) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
294 { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
295 Py_DECREF(newlist); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
296 return NULL; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
297 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
298 PyList_Append(newlist, s); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
299 Py_DECREF(s); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
300 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
301 /* Update the pointer, and increment the count of splits */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
302 next=match_end; count++; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
303 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
304 } while (result!=-1 && !(maxsplit && maxsplit==count) && |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
305 next<length); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
306 s=PyString_FromStringAndSize(start+match_end, length-match_end); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
307 if (!s) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
308 { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
309 Py_DECREF(newlist); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
310 return NULL; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
311 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
312 PyList_Append(newlist, s); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
313 Py_DECREF(s); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
314 Py_DECREF(pattern); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
315 return newlist; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
317 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
318 static PyObject * |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
319 reop_split(self, args) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
320 PyObject *self; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 PyObject *args; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
322 { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
323 return internal_split(args, 0); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
324 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
325 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
326 static PyObject * |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
327 reop_splitx(self, args) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
328 PyObject *self; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
329 PyObject *args; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
330 { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
331 return internal_split(args, 1); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
332 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
333 #endif |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
334 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
335 static struct PyMethodDef reop_global_methods[] = { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
336 {"match", reop_match, 0}, |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
337 {"search", reop_search, 0}, |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
338 #if 0 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
339 {"split", reop_split, 0}, |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
340 {"splitx", reop_splitx, 0}, |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
341 #endif |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
342 {NULL, NULL} /* sentinel */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
343 }; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
344 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
345 void |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
346 initreop() |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
347 { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
348 PyObject *m, *d, *v; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
349 int i; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
350 char *s; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
351 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
352 m = Py_InitModule("reop", reop_global_methods); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
353 d = PyModule_GetDict(m); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
354 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
355 /* Initialize reop.error exception */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
356 v = ReopError = PyString_FromString("reop.error"); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
357 if (v == NULL || PyDict_SetItemString(d, "error", v) != 0) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
358 goto finally; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
359 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
360 /* Initialize reop.casefold constant */ |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
361 if (!(v = PyString_FromStringAndSize((char *)NULL, 256))) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
362 goto finally; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
363 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
364 if (!(s = PyString_AsString(v))) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
365 goto finally; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
366 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
367 for (i = 0; i < 256; i++) { |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
368 if (isupper(i)) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
369 s[i] = tolower(i); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
370 else |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
371 s[i] = i; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
372 } |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
373 if (PyDict_SetItemString(d, "casefold", v) < 0) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
374 goto finally; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
375 Py_DECREF(v); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
376 |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
377 if (!PyErr_Occurred()) |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
378 return; |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
379 finally: |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
380 Py_FatalError("can't initialize reop module"); |
|
18a639db048c
New versions straight from Jeffrey Ollie's web site
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
381 } |
