annotate Modules/regexmodule.c @ 1590:ac2e5cc29949 legacy-trunk

Added tests for missing host to open_http and open_gopher
author Guido van Rossum <guido@python.org>
date Mon, 18 Apr 1994 09:39:56 +0000
parents 0c657db27dbf
children 1940892d2fe3
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 /***********************************************************
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
7 Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
8 Amsterdam, 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
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 Permission to use, copy, modify, and distribute this software and its
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 documentation for any purpose and without fee is hereby granted,
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
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 both that copyright notice and this permission notice appear in
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 supporting documentation, and that the names of Stichting Mathematisch
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 Centrum or CWI not be used in advertising or publicity pertaining to
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 distribution of the software without specific, written prior permission.
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 ******************************************************************/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 /* Regular expression objects */
615
40a0ec994c54 Use Tatu Ylonen's copyleft-free reimplementation of
Guido van Rossum <guido@python.org>
parents: 551
diff changeset
31 /* 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
32 GNU regular expressions */
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 #include "allobjects.h"
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 #include "modsupport.h"
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
615
40a0ec994c54 Use Tatu Ylonen's copyleft-free reimplementation of
Guido van Rossum <guido@python.org>
parents: 551
diff changeset
37 #include "regexpr.h"
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 static object *RegexError; /* Exception */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 typedef struct {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 OB_HEAD
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 struct re_pattern_buffer re_patbuf; /* The compiled expression */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 struct re_registers re_regs; /* The registers from the last match */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 char re_fastmap[256]; /* Storage for fastmap */
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
46 object *re_translate; /* String object for translate table */
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
47 object *re_lastok; /* String object last matched/searched */
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 } regexobject;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 /* Regex object methods */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 static void
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 reg_dealloc(re)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 regexobject *re;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 {
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
56 XDECREF(re->re_translate);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
57 XDECREF(re->re_lastok);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 XDEL(re->re_patbuf.buffer);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 XDEL(re->re_patbuf.translate);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 DEL(re);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 }
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 object *
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 makeresult(regs)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 struct re_registers *regs;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 object *v = newtupleobject(RE_NREGS);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 if (v != NULL) {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 int i;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 for (i = 0; i < RE_NREGS; i++) {
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
71 object *w;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
72 w = mkvalue("(ii)", regs->start[i], regs->end[i]);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
73 if (w == NULL) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
74 XDECREF(v);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
75 v = NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
76 break;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 settupleitem(v, i, w);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 return v;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 static object *
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 reg_match(re, args)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 regexobject *re;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 object *args;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 {
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
89 object *argstring;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 char *buffer;
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
91 int size;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 int offset;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 int result;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
94 if (getargs(args, "S", &argstring)) {
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 offset = 0;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 }
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
97 else {
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
98 err_clear();
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
99 if (!getargs(args, "(Si)", &argstring, &offset))
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
100 return NULL;
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
101 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
102 buffer = getstringvalue(argstring);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
103 size = getstringsize(argstring);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
104 if (offset < 0 || offset > size) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
105 err_setstr(RegexError, "match offset out of range");
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
106 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
107 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
108 XDECREF(re->re_lastok);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
109 re->re_lastok = NULL;
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
110 result = re_match(&re->re_patbuf, buffer, size, offset, &re->re_regs);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 if (result < -1) {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 /* Failure like stack overflow */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 err_setstr(RegexError, "match failure");
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
116 if (result >= 0) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
117 INCREF(argstring);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
118 re->re_lastok = argstring;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
119 }
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 return newintobject((long)result); /* Length of the match or -1 */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 }
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
122
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 static object *
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 reg_search(re, args)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 regexobject *re;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 object *args;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 {
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
128 object *argstring;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 char *buffer;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 int size;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 int offset;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 int range;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 int result;
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
134
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
135 if (getargs(args, "S", &argstring)) {
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 offset = 0;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 }
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
138 else {
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
139 err_clear();
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
140 if (!getargs(args, "(Si)", &argstring, &offset))
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
141 return NULL;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
142 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
143 buffer = getstringvalue(argstring);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
144 size = getstringsize(argstring);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
145 if (offset < 0 || offset > size) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
146 err_setstr(RegexError, "search offset out of range");
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
147 return NULL;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 }
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
149 /* 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
150 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
151 |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
152 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
153 range = size - offset;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
154 XDECREF(re->re_lastok);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
155 re->re_lastok = NULL;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 result = re_search(&re->re_patbuf, buffer, size, offset, range,
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 &re->re_regs);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 if (result < -1) {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 /* Failure like stack overflow */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 err_setstr(RegexError, "match failure");
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
163 if (result >= 0) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
164 INCREF(argstring);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
165 re->re_lastok = argstring;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
166 }
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 return newintobject((long)result); /* Position of the match or -1 */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
170 static object *
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
171 reg_group(re, args)
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
172 regexobject *re;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
173 object *args;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
174 {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
175 int i, a, b;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
176 if (args != NULL && is_tupleobject(args)) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
177 int n = gettuplesize(args);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
178 object *res = newtupleobject(n);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
179 if (res == NULL)
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
180 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
181 for (i = 0; i < n; i++) {
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
182 object *v = reg_group(re, gettupleitem(args, i));
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
183 if (v == NULL) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
184 DECREF(res);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
185 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
186 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
187 settupleitem(res, i, v);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
188 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
189 return res;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
190 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
191 if (!getargs(args, "i", &i))
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
192 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
193 if (i < 0 || i >= RE_NREGS) {
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
194 err_setstr(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
195 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
196 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
197 if (re->re_lastok == NULL) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
198 err_setstr(RegexError,
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
199 "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
200 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
201 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
202 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
203 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
204 if (a < 0 || b < 0) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
205 INCREF(None);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
206 return None;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
207 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
208 return newsizedstringobject(getstringvalue(re->re_lastok)+a, b-a);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
209 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
210
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 static struct methodlist reg_methods[] = {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 {"match", reg_match},
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 {"search", reg_search},
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
214 {"group", reg_group},
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 {NULL, NULL} /* sentinel */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 };
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
217
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
218 static object *
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 reg_getattr(re, name)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 regexobject *re;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 char *name;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
222 {
551
5a61f827f47c 'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents: 536
diff changeset
223 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
224 if (re->re_lastok == NULL) {
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
225 INCREF(None);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
226 return None;
551
5a61f827f47c 'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents: 536
diff changeset
227 }
5a61f827f47c 'regs' is a read-only data member, not a function.
Guido van Rossum <guido@python.org>
parents: 536
diff changeset
228 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
229 }
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
230 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
231 if (re->re_lastok == NULL) {
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
232 INCREF(None);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
233 return None;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
234 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
235 INCREF(re->re_lastok);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
236 return re->re_lastok;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
237 }
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
238 if (strcmp(name, "translate") == 0) {
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
239 if (re->re_translate == NULL) {
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
240 INCREF(None);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
241 return None;
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
242 }
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
243 INCREF(re->re_translate);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
244 return re->re_translate;
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
245 }
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
246 if (strcmp(name, "__members__") == 0) {
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
247 object *list = newlistobject(3);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
248 if (list) {
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
249 setlistitem(list, 0, newstringobject("last"));
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
250 setlistitem(list, 1, newstringobject("regs"));
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
251 setlistitem(list, 2, newstringobject("translate"));
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
252 if (err_occurred()) {
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
253 DECREF(list);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
254 list = NULL;
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
255 }
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
256 }
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
257 return list;
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
258 }
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 return findmethod(reg_methods, (object *)re, name);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
261
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 static typeobject Regextype = {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 OB_HEAD_INIT(&Typetype)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 0, /*ob_size*/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 "regex", /*tp_name*/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 sizeof(regexobject), /*tp_size*/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 0, /*tp_itemsize*/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 /* methods */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 reg_dealloc, /*tp_dealloc*/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 0, /*tp_print*/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 reg_getattr, /*tp_getattr*/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 0, /*tp_setattr*/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 0, /*tp_compare*/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 0, /*tp_repr*/
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 };
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
276
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
277 static object *
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
278 newregexobject(pat, size, translate)
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
279 char *pat;
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
280 int size;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
281 object *translate;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 regexobject *re;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
284 if (translate != NULL && getstringsize(translate) != 256) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
285 err_setstr(RegexError,
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
286 "translation table must be 256 bytes");
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
287 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
288 }
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 re = NEWOBJ(regexobject, &Regextype);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
290 if (re != NULL) {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
291 char *error;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 re->re_patbuf.buffer = NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 re->re_patbuf.allocated = 0;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
294 re->re_patbuf.fastmap = re->re_fastmap;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
295 if (translate)
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
296 re->re_patbuf.translate = getstringvalue(translate);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
297 else
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
298 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
299 XINCREF(translate);
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
300 re->re_translate = translate;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
301 re->re_lastok = NULL;
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
302 error = re_compile_pattern(pat, size, &re->re_patbuf);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 if (error != NULL) {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 err_setstr(RegexError, error);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 DECREF(re);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 re = NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 }
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
309 return (object *)re;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
310 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
311
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 static object *
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 regex_compile(self, args)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 object *self;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 object *args;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 {
636
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
317 char *pat;
d5bb8fe43df2 Fix range error (doc and impl of re_search disagreed!)
Guido van Rossum <guido@python.org>
parents: 628
diff changeset
318 int size;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
319 object *tran = NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
320 if (!getargs(args, "s#", &pat, &size)) {
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
321 err_clear();
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
322 if (!getargs(args, "(s#S)", &pat, &size, &tran))
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
323 return NULL;
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
324 }
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
325 return newregexobject(pat, size, tran);
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
327
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 static object *cache_pat;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 static object *cache_prog;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
330
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 static int
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 update_cache(pat)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 object *pat;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 if (pat != cache_pat) {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 XDECREF(cache_pat);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 cache_pat = NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 XDECREF(cache_prog);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 cache_prog = regex_compile((object *)NULL, pat);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 if (cache_prog == NULL)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 return -1;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 cache_pat = pat;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 INCREF(cache_pat);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 return 0;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
347
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 static object *
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 regex_match(self, args)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 object *self;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 object *args;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 object *pat, *string;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
354 if (!getargs(args, "(SS)", &pat, &string))
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 if (update_cache(pat) < 0)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 return reg_match((regexobject *)cache_prog, string);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
360
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 static object *
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
362 regex_search(self, args)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
363 object *self;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 object *args;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 object *pat, *string;
1237
05580e00f41e * regexmodule.c: added use of translation table, substring() method.
Guido van Rossum <guido@python.org>
parents: 1019
diff changeset
367 if (!getargs(args, "(SS)", &pat, &string))
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
369 if (update_cache(pat) < 0)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 return reg_search((regexobject *)cache_prog, string);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
373
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 static object *
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 regex_set_syntax(self, args)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 object *self, *args;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 int syntax;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 if (!getintarg(args, &syntax))
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 return NULL;
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 syntax = re_set_syntax(syntax);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 return newintobject((long)syntax);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 }
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
384
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 static struct methodlist regex_global_methods[] = {
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 {"compile", regex_compile},
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 {"match", regex_match},
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 {"search", regex_search},
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 {"set_syntax", regex_set_syntax},
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 {NULL, NULL} /* sentinel */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 };
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
392
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 initregex()
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 {
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
395 object *m, *d, *v;
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
396
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 m = initmodule("regex", regex_global_methods);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 d = getmoduledict(m);
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
399
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 /* Initialize regex.error exception */
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 RegexError = newstringobject("regex.error");
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 if (RegexError == NULL || dictinsert(d, "error", RegexError) != 0)
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 fatal("can't define regex.error");
1239
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
404
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
405 /* Initialize regex.casefold constant */
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
406 v = newsizedstringobject((char *)NULL, 256);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
407 if (v != NULL) {
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
408 int i;
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
409 char *s = getstringvalue(v);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
410 for (i = 0; i < 256; i++) {
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
411 if (isupper(i))
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
412 s[i] = tolower(i);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
413 else
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
414 s[i] = i;
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
415 }
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
416 dictinsert(d, "casefold", v);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
417 DECREF(v);
0c657db27dbf Extensive changes to regex module (group(), casefold, etc.)
Guido van Rossum <guido@python.org>
parents: 1237
diff changeset
418 }
536
255728a841de Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 }