blob: 128bf9b6b67722f3eef496b126fbd03f2a261448 [file] [log] [blame]
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001/*
Guido van Rossumb700df92000-03-31 14:59:30 +00002 * Secret Labs' Regular Expression Engine
Guido van Rossumb700df92000-03-31 14:59:30 +00003 *
Fredrik Lundh6c68dc72000-06-29 10:34:56 +00004 * regular expression matching engine
Guido van Rossumb700df92000-03-31 14:59:30 +00005 *
6 * partial history:
Fredrik Lundh5644b7f2000-09-21 17:03:25 +00007 * 1999-10-24 fl created (based on existing template matcher code)
Fredrik Lundhebc37b22000-10-28 19:30:41 +00008 * 2000-03-06 fl first alpha, sort of
Fredrik Lundhebc37b22000-10-28 19:30:41 +00009 * 2000-08-01 fl fixes for 1.6b1
Fredrik Lundh5644b7f2000-09-21 17:03:25 +000010 * 2000-08-07 fl use PyOS_CheckStack() if available
Fredrik Lundh5644b7f2000-09-21 17:03:25 +000011 * 2000-09-20 fl added expand method
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +000012 * 2001-03-20 fl lots of fixes for 2.1b2
Fredrik Lundh9c7eab82001-04-15 19:00:58 +000013 * 2001-04-15 fl export copyright as Python attribute, not global
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +000014 * 2001-04-28 fl added __copy__ methods (work in progress)
Fredrik Lundh09705f02002-11-22 12:46:35 +000015 * 2001-05-14 fl fixes for 1.5.2 compatibility
Fredrik Lundhf71ae462001-07-02 17:04:48 +000016 * 2001-07-01 fl added BIGCHARSET support (from Martin von Loewis)
Fredrik Lundh397a6542001-10-18 19:30:16 +000017 * 2001-10-18 fl fixed group reset issue (from Matthew Mueller)
Fredrik Lundh971e78b2001-10-20 17:48:46 +000018 * 2001-10-20 fl added split primitive; reenable unicode for 1.6/2.0/2.1
Fredrik Lundhbec95b92001-10-21 16:47:57 +000019 * 2001-10-21 fl added sub/subn primitive
Fredrik Lundh703ce812001-10-24 22:16:30 +000020 * 2001-10-24 fl added finditer primitive (for 2.2 only)
Fredrik Lundh82b23072001-12-09 16:13:15 +000021 * 2001-12-07 fl fixed memory leak in sub/subn (Guido van Rossum)
Fredrik Lundh09705f02002-11-22 12:46:35 +000022 * 2002-11-09 fl fixed empty sub/subn return type
Martin v. Löwis78e2f062003-04-19 12:56:08 +000023 * 2003-04-18 mvl fully support 4-byte codes
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +000024 * 2003-10-17 gn implemented non recursive scheme
Guido van Rossumb700df92000-03-31 14:59:30 +000025 *
Fredrik Lundh770617b2001-01-14 15:06:11 +000026 * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
Guido van Rossumb700df92000-03-31 14:59:30 +000027 *
Fredrik Lundh29c4ba92000-08-01 18:20:07 +000028 * This version of the SRE library can be redistributed under CNRI's
29 * Python 1.6 license. For any other use, please contact Secret Labs
30 * AB (info@pythonware.com).
31 *
Guido van Rossumb700df92000-03-31 14:59:30 +000032 * Portions of this engine have been developed in cooperation with
Fredrik Lundh29c4ba92000-08-01 18:20:07 +000033 * CNRI. Hewlett-Packard provided funding for 1.6 integration and
Guido van Rossumb700df92000-03-31 14:59:30 +000034 * other compatibility work.
35 */
36
37#ifndef SRE_RECURSIVE
38
Fredrik Lundh9c7eab82001-04-15 19:00:58 +000039static char copyright[] =
Fredrik Lundh09705f02002-11-22 12:46:35 +000040 " SRE 2.2.2 Copyright (c) 1997-2002 by Secret Labs AB ";
Guido van Rossumb700df92000-03-31 14:59:30 +000041
Neal Norwitza6d80fa2006-06-12 03:05:40 +000042#define PY_SSIZE_T_CLEAN
43
Guido van Rossumb700df92000-03-31 14:59:30 +000044#include "Python.h"
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +000045#include "structmember.h" /* offsetof */
Guido van Rossumb700df92000-03-31 14:59:30 +000046
47#include "sre.h"
48
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000049#include <ctype.h>
Guido van Rossumb700df92000-03-31 14:59:30 +000050
Fredrik Lundh436c3d52000-06-29 08:58:44 +000051/* name of this module, minus the leading underscore */
Fredrik Lundh1c5aa692001-01-16 07:37:30 +000052#if !defined(SRE_MODULE)
53#define SRE_MODULE "sre"
54#endif
Fredrik Lundh436c3d52000-06-29 08:58:44 +000055
Neal Norwitz94a9c092006-03-16 06:30:02 +000056#define SRE_PY_MODULE "re"
57
Guido van Rossumb700df92000-03-31 14:59:30 +000058/* defining this one enables tracing */
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000059#undef VERBOSE
Guido van Rossumb700df92000-03-31 14:59:30 +000060
Fredrik Lundh971e78b2001-10-20 17:48:46 +000061#if PY_VERSION_HEX >= 0x01060000
62#if PY_VERSION_HEX < 0x02020000 || defined(Py_USING_UNICODE)
Fredrik Lundh22d25462000-07-01 17:50:59 +000063/* defining this enables unicode support (default under 1.6a1 and later) */
Fredrik Lundh436c3d52000-06-29 08:58:44 +000064#define HAVE_UNICODE
65#endif
Fredrik Lundh971e78b2001-10-20 17:48:46 +000066#endif
Fredrik Lundh436c3d52000-06-29 08:58:44 +000067
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000068/* -------------------------------------------------------------------- */
Fredrik Lundh29c08be2000-06-29 23:33:12 +000069/* optional features */
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000070
71/* enables fast searching */
Fredrik Lundh29c08be2000-06-29 23:33:12 +000072#define USE_FAST_SEARCH
73
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000074/* enables aggressive inlining (always on for Visual C) */
Fredrik Lundh29c4ba92000-08-01 18:20:07 +000075#undef USE_INLINE
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000076
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +000077/* enables copy/deepcopy handling (work in progress) */
78#undef USE_BUILTIN_COPY
79
Fredrik Lundh1c5aa692001-01-16 07:37:30 +000080#if PY_VERSION_HEX < 0x01060000
81#define PyObject_DEL(op) PyMem_DEL((op))
82#endif
83
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000084/* -------------------------------------------------------------------- */
85
Fredrik Lundh80946112000-06-29 18:03:25 +000086#if defined(_MSC_VER)
Guido van Rossumb700df92000-03-31 14:59:30 +000087#pragma optimize("agtw", on) /* doesn't seem to make much difference... */
Fredrik Lundh28552902000-07-05 21:14:16 +000088#pragma warning(disable: 4710) /* who cares if functions are not inlined ;-) */
Guido van Rossumb700df92000-03-31 14:59:30 +000089/* fastest possible local call under MSVC */
90#define LOCAL(type) static __inline type __fastcall
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000091#elif defined(USE_INLINE)
Fredrik Lundh29c08be2000-06-29 23:33:12 +000092#define LOCAL(type) static inline type
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000093#else
94#define LOCAL(type) static type
Guido van Rossumb700df92000-03-31 14:59:30 +000095#endif
96
97/* error codes */
98#define SRE_ERROR_ILLEGAL -1 /* illegal opcode */
Fredrik Lundh29c4ba92000-08-01 18:20:07 +000099#define SRE_ERROR_STATE -2 /* illegal state */
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000100#define SRE_ERROR_RECURSION_LIMIT -3 /* runaway recursion */
Guido van Rossumb700df92000-03-31 14:59:30 +0000101#define SRE_ERROR_MEMORY -9 /* out of memory */
102
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000103#if defined(VERBOSE)
Guido van Rossumb700df92000-03-31 14:59:30 +0000104#define TRACE(v) printf v
Guido van Rossumb700df92000-03-31 14:59:30 +0000105#else
106#define TRACE(v)
107#endif
108
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000109/* -------------------------------------------------------------------- */
110/* search engine state */
Guido van Rossumb700df92000-03-31 14:59:30 +0000111
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000112/* default character predicates (run sre_chars.py to regenerate tables) */
113
114#define SRE_DIGIT_MASK 1
115#define SRE_SPACE_MASK 2
116#define SRE_LINEBREAK_MASK 4
117#define SRE_ALNUM_MASK 8
118#define SRE_WORD_MASK 16
119
Fredrik Lundh21009b92001-09-18 18:47:09 +0000120/* FIXME: this assumes ASCII. create tables in init_sre() instead */
121
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000122static char sre_char_info[128] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 6, 2,
1232, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0,
1240, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 25, 25, 25, 25, 25, 25, 25,
12525, 25, 0, 0, 0, 0, 0, 0, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
12624, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 0, 0,
1270, 0, 16, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
12824, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 0, 0, 0, 0, 0 };
129
Fredrik Lundhb389df32000-06-29 12:48:37 +0000130static char sre_char_lower[128] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
Fredrik Lundh436c3d52000-06-29 08:58:44 +000013110, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
13227, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
13344, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
13461, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
135108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
136122, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
137106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
138120, 121, 122, 123, 124, 125, 126, 127 };
139
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000140#define SRE_IS_DIGIT(ch)\
141 ((ch) < 128 ? (sre_char_info[(ch)] & SRE_DIGIT_MASK) : 0)
142#define SRE_IS_SPACE(ch)\
143 ((ch) < 128 ? (sre_char_info[(ch)] & SRE_SPACE_MASK) : 0)
144#define SRE_IS_LINEBREAK(ch)\
145 ((ch) < 128 ? (sre_char_info[(ch)] & SRE_LINEBREAK_MASK) : 0)
146#define SRE_IS_ALNUM(ch)\
147 ((ch) < 128 ? (sre_char_info[(ch)] & SRE_ALNUM_MASK) : 0)
148#define SRE_IS_WORD(ch)\
149 ((ch) < 128 ? (sre_char_info[(ch)] & SRE_WORD_MASK) : 0)
Guido van Rossumb700df92000-03-31 14:59:30 +0000150
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000151static unsigned int sre_lower(unsigned int ch)
152{
Gustavo Niemeyer601b9632004-02-14 00:31:13 +0000153 return ((ch) < 128 ? (unsigned int)sre_char_lower[ch] : ch);
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000154}
155
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000156/* locale-specific character predicates */
Gustavo Niemeyer601b9632004-02-14 00:31:13 +0000157/* !(c & ~N) == (c < N+1) for any unsigned c, this avoids
158 * warnings when c's type supports only numbers < N+1 */
159#define SRE_LOC_IS_DIGIT(ch) (!((ch) & ~255) ? isdigit((ch)) : 0)
160#define SRE_LOC_IS_SPACE(ch) (!((ch) & ~255) ? isspace((ch)) : 0)
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000161#define SRE_LOC_IS_LINEBREAK(ch) ((ch) == '\n')
Gustavo Niemeyer601b9632004-02-14 00:31:13 +0000162#define SRE_LOC_IS_ALNUM(ch) (!((ch) & ~255) ? isalnum((ch)) : 0)
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000163#define SRE_LOC_IS_WORD(ch) (SRE_LOC_IS_ALNUM((ch)) || (ch) == '_')
164
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000165static unsigned int sre_lower_locale(unsigned int ch)
166{
Gustavo Niemeyer601b9632004-02-14 00:31:13 +0000167 return ((ch) < 256 ? (unsigned int)tolower((ch)) : ch);
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000168}
169
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000170/* unicode-specific character predicates */
171
172#if defined(HAVE_UNICODE)
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000173
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000174#define SRE_UNI_IS_DIGIT(ch) Py_UNICODE_ISDIGIT((Py_UNICODE)(ch))
175#define SRE_UNI_IS_SPACE(ch) Py_UNICODE_ISSPACE((Py_UNICODE)(ch))
176#define SRE_UNI_IS_LINEBREAK(ch) Py_UNICODE_ISLINEBREAK((Py_UNICODE)(ch))
Fredrik Lundh22d25462000-07-01 17:50:59 +0000177#define SRE_UNI_IS_ALNUM(ch) Py_UNICODE_ISALNUM((Py_UNICODE)(ch))
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000178#define SRE_UNI_IS_WORD(ch) (SRE_UNI_IS_ALNUM((ch)) || (ch) == '_')
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000179
180static unsigned int sre_lower_unicode(unsigned int ch)
181{
182 return (unsigned int) Py_UNICODE_TOLOWER((Py_UNICODE)(ch));
183}
184
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000185#endif
186
Guido van Rossumb700df92000-03-31 14:59:30 +0000187LOCAL(int)
188sre_category(SRE_CODE category, unsigned int ch)
189{
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000190 switch (category) {
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000191
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000192 case SRE_CATEGORY_DIGIT:
193 return SRE_IS_DIGIT(ch);
194 case SRE_CATEGORY_NOT_DIGIT:
195 return !SRE_IS_DIGIT(ch);
196 case SRE_CATEGORY_SPACE:
197 return SRE_IS_SPACE(ch);
198 case SRE_CATEGORY_NOT_SPACE:
199 return !SRE_IS_SPACE(ch);
200 case SRE_CATEGORY_WORD:
201 return SRE_IS_WORD(ch);
202 case SRE_CATEGORY_NOT_WORD:
203 return !SRE_IS_WORD(ch);
204 case SRE_CATEGORY_LINEBREAK:
205 return SRE_IS_LINEBREAK(ch);
206 case SRE_CATEGORY_NOT_LINEBREAK:
207 return !SRE_IS_LINEBREAK(ch);
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000208
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000209 case SRE_CATEGORY_LOC_WORD:
210 return SRE_LOC_IS_WORD(ch);
211 case SRE_CATEGORY_LOC_NOT_WORD:
212 return !SRE_LOC_IS_WORD(ch);
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000213
214#if defined(HAVE_UNICODE)
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000215 case SRE_CATEGORY_UNI_DIGIT:
216 return SRE_UNI_IS_DIGIT(ch);
217 case SRE_CATEGORY_UNI_NOT_DIGIT:
218 return !SRE_UNI_IS_DIGIT(ch);
219 case SRE_CATEGORY_UNI_SPACE:
220 return SRE_UNI_IS_SPACE(ch);
221 case SRE_CATEGORY_UNI_NOT_SPACE:
222 return !SRE_UNI_IS_SPACE(ch);
223 case SRE_CATEGORY_UNI_WORD:
224 return SRE_UNI_IS_WORD(ch);
225 case SRE_CATEGORY_UNI_NOT_WORD:
226 return !SRE_UNI_IS_WORD(ch);
227 case SRE_CATEGORY_UNI_LINEBREAK:
228 return SRE_UNI_IS_LINEBREAK(ch);
229 case SRE_CATEGORY_UNI_NOT_LINEBREAK:
230 return !SRE_UNI_IS_LINEBREAK(ch);
Fredrik Lundh1c5aa692001-01-16 07:37:30 +0000231#else
232 case SRE_CATEGORY_UNI_DIGIT:
233 return SRE_IS_DIGIT(ch);
234 case SRE_CATEGORY_UNI_NOT_DIGIT:
235 return !SRE_IS_DIGIT(ch);
236 case SRE_CATEGORY_UNI_SPACE:
237 return SRE_IS_SPACE(ch);
238 case SRE_CATEGORY_UNI_NOT_SPACE:
239 return !SRE_IS_SPACE(ch);
240 case SRE_CATEGORY_UNI_WORD:
241 return SRE_LOC_IS_WORD(ch);
242 case SRE_CATEGORY_UNI_NOT_WORD:
243 return !SRE_LOC_IS_WORD(ch);
244 case SRE_CATEGORY_UNI_LINEBREAK:
245 return SRE_IS_LINEBREAK(ch);
246 case SRE_CATEGORY_UNI_NOT_LINEBREAK:
247 return !SRE_IS_LINEBREAK(ch);
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000248#endif
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000249 }
250 return 0;
Guido van Rossumb700df92000-03-31 14:59:30 +0000251}
252
253/* helpers */
254
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000255static void
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000256data_stack_dealloc(SRE_STATE* state)
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000257{
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000258 if (state->data_stack) {
Jack Diederich2d400772006-05-27 15:44:34 +0000259 PyMem_FREE(state->data_stack);
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000260 state->data_stack = NULL;
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000261 }
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000262 state->data_stack_size = state->data_stack_base = 0;
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000263}
264
265static int
Neal Norwitza6d80fa2006-06-12 03:05:40 +0000266data_stack_grow(SRE_STATE* state, Py_ssize_t size)
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000267{
Neal Norwitza6d80fa2006-06-12 03:05:40 +0000268 Py_ssize_t minsize, cursize;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000269 minsize = state->data_stack_base+size;
270 cursize = state->data_stack_size;
271 if (cursize < minsize) {
272 void* stack;
273 cursize = minsize+minsize/4+1024;
274 TRACE(("allocate/grow stack %d\n", cursize));
Jack Diederich2d400772006-05-27 15:44:34 +0000275 stack = PyMem_REALLOC(state->data_stack, cursize);
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000276 if (!stack) {
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000277 data_stack_dealloc(state);
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000278 return SRE_ERROR_MEMORY;
279 }
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000280 state->data_stack = (char *)stack;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000281 state->data_stack_size = cursize;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000282 }
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000283 return 0;
Guido van Rossumb700df92000-03-31 14:59:30 +0000284}
285
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000286/* generate 8-bit version */
Guido van Rossumb700df92000-03-31 14:59:30 +0000287
288#define SRE_CHAR unsigned char
289#define SRE_AT sre_at
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000290#define SRE_COUNT sre_count
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000291#define SRE_CHARSET sre_charset
292#define SRE_INFO sre_info
Guido van Rossumb700df92000-03-31 14:59:30 +0000293#define SRE_MATCH sre_match
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000294#define SRE_MATCH_CONTEXT sre_match_context
Guido van Rossumb700df92000-03-31 14:59:30 +0000295#define SRE_SEARCH sre_search
Fredrik Lundh6de22ef2001-10-22 21:18:08 +0000296#define SRE_LITERAL_TEMPLATE sre_literal_template
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000297
298#if defined(HAVE_UNICODE)
299
Guido van Rossumb700df92000-03-31 14:59:30 +0000300#define SRE_RECURSIVE
Guido van Rossumb700df92000-03-31 14:59:30 +0000301#include "_sre.c"
Guido van Rossumb700df92000-03-31 14:59:30 +0000302#undef SRE_RECURSIVE
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000303
Fredrik Lundh6de22ef2001-10-22 21:18:08 +0000304#undef SRE_LITERAL_TEMPLATE
Guido van Rossumb700df92000-03-31 14:59:30 +0000305#undef SRE_SEARCH
306#undef SRE_MATCH
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000307#undef SRE_MATCH_CONTEXT
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000308#undef SRE_INFO
309#undef SRE_CHARSET
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000310#undef SRE_COUNT
Guido van Rossumb700df92000-03-31 14:59:30 +0000311#undef SRE_AT
312#undef SRE_CHAR
313
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000314/* generate 16-bit unicode version */
Guido van Rossumb700df92000-03-31 14:59:30 +0000315
316#define SRE_CHAR Py_UNICODE
317#define SRE_AT sre_uat
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000318#define SRE_COUNT sre_ucount
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000319#define SRE_CHARSET sre_ucharset
320#define SRE_INFO sre_uinfo
Guido van Rossumb700df92000-03-31 14:59:30 +0000321#define SRE_MATCH sre_umatch
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000322#define SRE_MATCH_CONTEXT sre_umatch_context
Guido van Rossumb700df92000-03-31 14:59:30 +0000323#define SRE_SEARCH sre_usearch
Fredrik Lundh6de22ef2001-10-22 21:18:08 +0000324#define SRE_LITERAL_TEMPLATE sre_uliteral_template
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000325#endif
Guido van Rossumb700df92000-03-31 14:59:30 +0000326
327#endif /* SRE_RECURSIVE */
328
329/* -------------------------------------------------------------------- */
330/* String matching engine */
331
332/* the following section is compiled twice, with different character
333 settings */
334
335LOCAL(int)
336SRE_AT(SRE_STATE* state, SRE_CHAR* ptr, SRE_CODE at)
337{
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000338 /* check if pointer is at given position */
Guido van Rossumb700df92000-03-31 14:59:30 +0000339
Neal Norwitza6d80fa2006-06-12 03:05:40 +0000340 Py_ssize_t thisp, thatp;
Guido van Rossumb700df92000-03-31 14:59:30 +0000341
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000342 switch (at) {
Fredrik Lundh80946112000-06-29 18:03:25 +0000343
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000344 case SRE_AT_BEGINNING:
Fredrik Lundh770617b2001-01-14 15:06:11 +0000345 case SRE_AT_BEGINNING_STRING:
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000346 return ((void*) ptr == state->beginning);
Fredrik Lundh80946112000-06-29 18:03:25 +0000347
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000348 case SRE_AT_BEGINNING_LINE:
349 return ((void*) ptr == state->beginning ||
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000350 SRE_IS_LINEBREAK((int) ptr[-1]));
Fredrik Lundh80946112000-06-29 18:03:25 +0000351
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000352 case SRE_AT_END:
Fredrik Lundhef34bd22000-06-30 21:40:20 +0000353 return (((void*) (ptr+1) == state->end &&
354 SRE_IS_LINEBREAK((int) ptr[0])) ||
355 ((void*) ptr == state->end));
Fredrik Lundh80946112000-06-29 18:03:25 +0000356
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000357 case SRE_AT_END_LINE:
358 return ((void*) ptr == state->end ||
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000359 SRE_IS_LINEBREAK((int) ptr[0]));
Fredrik Lundh80946112000-06-29 18:03:25 +0000360
Fredrik Lundh770617b2001-01-14 15:06:11 +0000361 case SRE_AT_END_STRING:
362 return ((void*) ptr == state->end);
363
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000364 case SRE_AT_BOUNDARY:
365 if (state->beginning == state->end)
366 return 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000367 thatp = ((void*) ptr > state->beginning) ?
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000368 SRE_IS_WORD((int) ptr[-1]) : 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000369 thisp = ((void*) ptr < state->end) ?
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000370 SRE_IS_WORD((int) ptr[0]) : 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000371 return thisp != thatp;
Fredrik Lundh80946112000-06-29 18:03:25 +0000372
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000373 case SRE_AT_NON_BOUNDARY:
374 if (state->beginning == state->end)
375 return 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000376 thatp = ((void*) ptr > state->beginning) ?
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000377 SRE_IS_WORD((int) ptr[-1]) : 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000378 thisp = ((void*) ptr < state->end) ?
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000379 SRE_IS_WORD((int) ptr[0]) : 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000380 return thisp == thatp;
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000381
382 case SRE_AT_LOC_BOUNDARY:
383 if (state->beginning == state->end)
384 return 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000385 thatp = ((void*) ptr > state->beginning) ?
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000386 SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000387 thisp = ((void*) ptr < state->end) ?
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000388 SRE_LOC_IS_WORD((int) ptr[0]) : 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000389 return thisp != thatp;
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000390
391 case SRE_AT_LOC_NON_BOUNDARY:
392 if (state->beginning == state->end)
393 return 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000394 thatp = ((void*) ptr > state->beginning) ?
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000395 SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000396 thisp = ((void*) ptr < state->end) ?
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000397 SRE_LOC_IS_WORD((int) ptr[0]) : 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000398 return thisp == thatp;
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000399
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +0000400#if defined(HAVE_UNICODE)
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000401 case SRE_AT_UNI_BOUNDARY:
402 if (state->beginning == state->end)
403 return 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000404 thatp = ((void*) ptr > state->beginning) ?
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000405 SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000406 thisp = ((void*) ptr < state->end) ?
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000407 SRE_UNI_IS_WORD((int) ptr[0]) : 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000408 return thisp != thatp;
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000409
410 case SRE_AT_UNI_NON_BOUNDARY:
411 if (state->beginning == state->end)
412 return 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000413 thatp = ((void*) ptr > state->beginning) ?
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000414 SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000415 thisp = ((void*) ptr < state->end) ?
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000416 SRE_UNI_IS_WORD((int) ptr[0]) : 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000417 return thisp == thatp;
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +0000418#endif
419
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000420 }
Guido van Rossumb700df92000-03-31 14:59:30 +0000421
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000422 return 0;
Guido van Rossumb700df92000-03-31 14:59:30 +0000423}
424
425LOCAL(int)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000426SRE_CHARSET(SRE_CODE* set, SRE_CODE ch)
Guido van Rossumb700df92000-03-31 14:59:30 +0000427{
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000428 /* check if character is a member of the given set */
Guido van Rossumb700df92000-03-31 14:59:30 +0000429
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000430 int ok = 1;
Guido van Rossumb700df92000-03-31 14:59:30 +0000431
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000432 for (;;) {
433 switch (*set++) {
Guido van Rossumb700df92000-03-31 14:59:30 +0000434
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000435 case SRE_OP_FAILURE:
436 return !ok;
437
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000438 case SRE_OP_LITERAL:
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000439 /* <LITERAL> <code> */
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000440 if (ch == set[0])
441 return ok;
442 set++;
443 break;
Guido van Rossumb700df92000-03-31 14:59:30 +0000444
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000445 case SRE_OP_CATEGORY:
446 /* <CATEGORY> <code> */
447 if (sre_category(set[0], (int) ch))
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000448 return ok;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000449 set += 1;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000450 break;
Guido van Rossumb700df92000-03-31 14:59:30 +0000451
Fredrik Lundh3562f112000-07-02 12:00:07 +0000452 case SRE_OP_CHARSET:
Martin v. Löwis78e2f062003-04-19 12:56:08 +0000453 if (sizeof(SRE_CODE) == 2) {
454 /* <CHARSET> <bitmap> (16 bits per code word) */
455 if (ch < 256 && (set[ch >> 4] & (1 << (ch & 15))))
456 return ok;
457 set += 16;
Tim Peters3d563502006-01-21 02:47:53 +0000458 }
Martin v. Löwis78e2f062003-04-19 12:56:08 +0000459 else {
460 /* <CHARSET> <bitmap> (32 bits per code word) */
461 if (ch < 256 && (set[ch >> 5] & (1 << (ch & 31))))
462 return ok;
463 set += 8;
464 }
Fredrik Lundh3562f112000-07-02 12:00:07 +0000465 break;
466
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000467 case SRE_OP_RANGE:
468 /* <RANGE> <lower> <upper> */
469 if (set[0] <= ch && ch <= set[1])
470 return ok;
471 set += 2;
472 break;
473
474 case SRE_OP_NEGATE:
475 ok = !ok;
476 break;
477
Fredrik Lundhf71ae462001-07-02 17:04:48 +0000478 case SRE_OP_BIGCHARSET:
479 /* <BIGCHARSET> <blockcount> <256 blockindices> <blocks> */
480 {
Neal Norwitza6d80fa2006-06-12 03:05:40 +0000481 Py_ssize_t count, block;
Fredrik Lundhf71ae462001-07-02 17:04:48 +0000482 count = *(set++);
Martin v. Löwis78e2f062003-04-19 12:56:08 +0000483
484 if (sizeof(SRE_CODE) == 2) {
485 block = ((unsigned char*)set)[ch >> 8];
486 set += 128;
487 if (set[block*16 + ((ch & 255)>>4)] & (1 << (ch & 15)))
488 return ok;
489 set += count*16;
490 }
491 else {
Gustavo Niemeyer601b9632004-02-14 00:31:13 +0000492 /* !(c & ~N) == (c < N+1) for any unsigned c, this avoids
493 * warnings when c's type supports only numbers < N+1 */
494 if (!(ch & ~65535))
Martin v. Löwis78e2f062003-04-19 12:56:08 +0000495 block = ((unsigned char*)set)[ch >> 8];
496 else
497 block = -1;
498 set += 64;
Tim Peters3d563502006-01-21 02:47:53 +0000499 if (block >=0 &&
Martin v. Löwis78e2f062003-04-19 12:56:08 +0000500 (set[block*8 + ((ch & 255)>>5)] & (1 << (ch & 31))))
501 return ok;
502 set += count*8;
503 }
Fredrik Lundhf71ae462001-07-02 17:04:48 +0000504 break;
505 }
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000506
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000507 default:
508 /* internal error -- there's not much we can do about it
Fredrik Lundh80946112000-06-29 18:03:25 +0000509 here, so let's just pretend it didn't match... */
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000510 return 0;
511 }
512 }
Guido van Rossumb700df92000-03-31 14:59:30 +0000513}
514
Neal Norwitza6d80fa2006-06-12 03:05:40 +0000515LOCAL(Py_ssize_t) SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern);
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000516
Neal Norwitza6d80fa2006-06-12 03:05:40 +0000517LOCAL(Py_ssize_t)
518SRE_COUNT(SRE_STATE* state, SRE_CODE* pattern, Py_ssize_t maxcount)
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000519{
520 SRE_CODE chr;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000521 SRE_CHAR* ptr = (SRE_CHAR *)state->ptr;
522 SRE_CHAR* end = (SRE_CHAR *)state->end;
Neal Norwitza6d80fa2006-06-12 03:05:40 +0000523 Py_ssize_t i;
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000524
525 /* adjust end */
526 if (maxcount < end - ptr && maxcount != 65535)
527 end = ptr + maxcount;
528
529 switch (pattern[0]) {
530
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000531 case SRE_OP_IN:
532 /* repeated set */
533 TRACE(("|%p|%p|COUNT IN\n", pattern, ptr));
534 while (ptr < end && SRE_CHARSET(pattern + 2, *ptr))
535 ptr++;
536 break;
537
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000538 case SRE_OP_ANY:
539 /* repeated dot wildcard. */
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000540 TRACE(("|%p|%p|COUNT ANY\n", pattern, ptr));
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000541 while (ptr < end && !SRE_IS_LINEBREAK(*ptr))
542 ptr++;
543 break;
544
545 case SRE_OP_ANY_ALL:
Gustavo Niemeyer0506c642004-09-03 18:11:59 +0000546 /* repeated dot wildcard. skip to the end of the target
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000547 string, and backtrack from there */
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000548 TRACE(("|%p|%p|COUNT ANY_ALL\n", pattern, ptr));
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000549 ptr = end;
550 break;
551
552 case SRE_OP_LITERAL:
553 /* repeated literal */
554 chr = pattern[1];
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000555 TRACE(("|%p|%p|COUNT LITERAL %d\n", pattern, ptr, chr));
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000556 while (ptr < end && (SRE_CODE) *ptr == chr)
557 ptr++;
558 break;
559
560 case SRE_OP_LITERAL_IGNORE:
561 /* repeated literal */
562 chr = pattern[1];
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000563 TRACE(("|%p|%p|COUNT LITERAL_IGNORE %d\n", pattern, ptr, chr));
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000564 while (ptr < end && (SRE_CODE) state->lower(*ptr) == chr)
565 ptr++;
566 break;
567
568 case SRE_OP_NOT_LITERAL:
569 /* repeated non-literal */
570 chr = pattern[1];
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000571 TRACE(("|%p|%p|COUNT NOT_LITERAL %d\n", pattern, ptr, chr));
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000572 while (ptr < end && (SRE_CODE) *ptr != chr)
573 ptr++;
574 break;
Tim Peters3d563502006-01-21 02:47:53 +0000575
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000576 case SRE_OP_NOT_LITERAL_IGNORE:
577 /* repeated non-literal */
578 chr = pattern[1];
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000579 TRACE(("|%p|%p|COUNT NOT_LITERAL_IGNORE %d\n", pattern, ptr, chr));
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000580 while (ptr < end && (SRE_CODE) state->lower(*ptr) != chr)
581 ptr++;
582 break;
583
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000584 default:
585 /* repeated single character pattern */
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000586 TRACE(("|%p|%p|COUNT SUBPATTERN\n", pattern, ptr));
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000587 while ((SRE_CHAR*) state->ptr < end) {
Gustavo Niemeyer2cbdc2a2003-12-13 20:32:08 +0000588 i = SRE_MATCH(state, pattern);
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000589 if (i < 0)
590 return i;
591 if (!i)
592 break;
593 }
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000594 TRACE(("|%p|%p|COUNT %d\n", pattern, ptr,
595 (SRE_CHAR*) state->ptr - ptr));
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000596 return (SRE_CHAR*) state->ptr - ptr;
597 }
598
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000599 TRACE(("|%p|%p|COUNT %d\n", pattern, ptr, ptr - (SRE_CHAR*) state->ptr));
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000600 return ptr - (SRE_CHAR*) state->ptr;
601}
602
Fredrik Lundh33accc12000-08-27 20:59:47 +0000603#if 0 /* not used in this release */
Guido van Rossumb700df92000-03-31 14:59:30 +0000604LOCAL(int)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000605SRE_INFO(SRE_STATE* state, SRE_CODE* pattern)
606{
607 /* check if an SRE_OP_INFO block matches at the current position.
608 returns the number of SRE_CODE objects to skip if successful, 0
609 if no match */
610
611 SRE_CHAR* end = state->end;
612 SRE_CHAR* ptr = state->ptr;
Neal Norwitza6d80fa2006-06-12 03:05:40 +0000613 Py_ssize_t i;
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000614
615 /* check minimal length */
616 if (pattern[3] && (end - ptr) < pattern[3])
617 return 0;
618
619 /* check known prefix */
620 if (pattern[2] & SRE_INFO_PREFIX && pattern[5] > 1) {
621 /* <length> <skip> <prefix data> <overlap data> */
622 for (i = 0; i < pattern[5]; i++)
623 if ((SRE_CODE) ptr[i] != pattern[7 + i])
624 return 0;
625 return pattern[0] + 2 * pattern[6];
626 }
627 return pattern[0];
628}
Fredrik Lundh33accc12000-08-27 20:59:47 +0000629#endif
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000630
Gustavo Niemeyercaf1c9d2003-04-27 14:42:54 +0000631/* The macros below should be used to protect recursive SRE_MATCH()
632 * calls that *failed* and do *not* return immediately (IOW, those
633 * that will backtrack). Explaining:
634 *
635 * - Recursive SRE_MATCH() returned true: that's usually a success
636 * (besides atypical cases like ASSERT_NOT), therefore there's no
637 * reason to restore lastmark;
638 *
639 * - Recursive SRE_MATCH() returned false but the current SRE_MATCH()
640 * is returning to the caller: If the current SRE_MATCH() is the
641 * top function of the recursion, returning false will be a matching
642 * failure, and it doesn't matter where lastmark is pointing to.
643 * If it's *not* the top function, it will be a recursive SRE_MATCH()
644 * failure by itself, and the calling SRE_MATCH() will have to deal
645 * with the failure by the same rules explained here (it will restore
646 * lastmark by itself if necessary);
647 *
648 * - Recursive SRE_MATCH() returned false, and will continue the
649 * outside 'for' loop: must be protected when breaking, since the next
650 * OP could potentially depend on lastmark;
Tim Peters3d563502006-01-21 02:47:53 +0000651 *
Gustavo Niemeyercaf1c9d2003-04-27 14:42:54 +0000652 * - Recursive SRE_MATCH() returned false, and will be called again
653 * inside a local for/while loop: must be protected between each
654 * loop iteration, since the recursive SRE_MATCH() could do anything,
655 * and could potentially depend on lastmark.
656 *
657 * For more information, check the discussion at SF patch #712900.
658 */
Gustavo Niemeyerbe733ee2003-04-20 07:35:44 +0000659#define LASTMARK_SAVE() \
660 do { \
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000661 ctx->lastmark = state->lastmark; \
662 ctx->lastindex = state->lastindex; \
Gustavo Niemeyerbe733ee2003-04-20 07:35:44 +0000663 } while (0)
664#define LASTMARK_RESTORE() \
665 do { \
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000666 state->lastmark = ctx->lastmark; \
667 state->lastindex = ctx->lastindex; \
Gustavo Niemeyerbe733ee2003-04-20 07:35:44 +0000668 } while (0)
669
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000670#define RETURN_ERROR(i) do { return i; } while(0)
671#define RETURN_FAILURE do { ret = 0; goto exit; } while(0)
672#define RETURN_SUCCESS do { ret = 1; goto exit; } while(0)
673
674#define RETURN_ON_ERROR(i) \
675 do { if (i < 0) RETURN_ERROR(i); } while (0)
676#define RETURN_ON_SUCCESS(i) \
677 do { RETURN_ON_ERROR(i); if (i > 0) RETURN_SUCCESS; } while (0)
678#define RETURN_ON_FAILURE(i) \
679 do { RETURN_ON_ERROR(i); if (i == 0) RETURN_FAILURE; } while (0)
680
681#define SFY(x) #x
682
683#define DATA_STACK_ALLOC(state, type, ptr) \
684do { \
685 alloc_pos = state->data_stack_base; \
686 TRACE(("allocating %s in %d (%d)\n", \
687 SFY(type), alloc_pos, sizeof(type))); \
688 if (state->data_stack_size < alloc_pos+sizeof(type)) { \
689 int j = data_stack_grow(state, sizeof(type)); \
690 if (j < 0) return j; \
691 if (ctx_pos != -1) \
692 DATA_STACK_LOOKUP_AT(state, SRE_MATCH_CONTEXT, ctx, ctx_pos); \
693 } \
694 ptr = (type*)(state->data_stack+alloc_pos); \
695 state->data_stack_base += sizeof(type); \
696} while (0)
697
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000698#define DATA_STACK_LOOKUP_AT(state, type, ptr, pos) \
699do { \
700 TRACE(("looking up %s at %d\n", SFY(type), pos)); \
701 ptr = (type*)(state->data_stack+pos); \
702} while (0)
703
704#define DATA_STACK_PUSH(state, data, size) \
705do { \
706 TRACE(("copy data in %p to %d (%d)\n", \
707 data, state->data_stack_base, size)); \
708 if (state->data_stack_size < state->data_stack_base+size) { \
709 int j = data_stack_grow(state, size); \
710 if (j < 0) return j; \
711 if (ctx_pos != -1) \
712 DATA_STACK_LOOKUP_AT(state, SRE_MATCH_CONTEXT, ctx, ctx_pos); \
713 } \
714 memcpy(state->data_stack+state->data_stack_base, data, size); \
715 state->data_stack_base += size; \
716} while (0)
717
718#define DATA_STACK_POP(state, data, size, discard) \
719do { \
720 TRACE(("copy data to %p from %d (%d)\n", \
721 data, state->data_stack_base-size, size)); \
722 memcpy(data, state->data_stack+state->data_stack_base-size, size); \
723 if (discard) \
724 state->data_stack_base -= size; \
725} while (0)
726
727#define DATA_STACK_POP_DISCARD(state, size) \
728do { \
729 TRACE(("discard data from %d (%d)\n", \
730 state->data_stack_base-size, size)); \
731 state->data_stack_base -= size; \
732} while(0)
733
734#define DATA_PUSH(x) \
735 DATA_STACK_PUSH(state, (x), sizeof(*(x)))
736#define DATA_POP(x) \
737 DATA_STACK_POP(state, (x), sizeof(*(x)), 1)
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000738#define DATA_POP_DISCARD(x) \
739 DATA_STACK_POP_DISCARD(state, sizeof(*(x)))
740#define DATA_ALLOC(t,p) \
741 DATA_STACK_ALLOC(state, t, p)
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000742#define DATA_LOOKUP_AT(t,p,pos) \
743 DATA_STACK_LOOKUP_AT(state,t,p,pos)
744
745#define MARK_PUSH(lastmark) \
746 do if (lastmark > 0) { \
747 i = lastmark; /* ctx->lastmark may change if reallocated */ \
748 DATA_STACK_PUSH(state, state->mark, (i+1)*sizeof(void*)); \
749 } while (0)
750#define MARK_POP(lastmark) \
751 do if (lastmark > 0) { \
752 DATA_STACK_POP(state, state->mark, (lastmark+1)*sizeof(void*), 1); \
753 } while (0)
754#define MARK_POP_KEEP(lastmark) \
755 do if (lastmark > 0) { \
756 DATA_STACK_POP(state, state->mark, (lastmark+1)*sizeof(void*), 0); \
757 } while (0)
758#define MARK_POP_DISCARD(lastmark) \
759 do if (lastmark > 0) { \
760 DATA_STACK_POP_DISCARD(state, (lastmark+1)*sizeof(void*)); \
761 } while (0)
762
763#define JUMP_NONE 0
764#define JUMP_MAX_UNTIL_1 1
765#define JUMP_MAX_UNTIL_2 2
766#define JUMP_MAX_UNTIL_3 3
767#define JUMP_MIN_UNTIL_1 4
768#define JUMP_MIN_UNTIL_2 5
769#define JUMP_MIN_UNTIL_3 6
770#define JUMP_REPEAT 7
771#define JUMP_REPEAT_ONE_1 8
772#define JUMP_REPEAT_ONE_2 9
773#define JUMP_MIN_REPEAT_ONE 10
774#define JUMP_BRANCH 11
775#define JUMP_ASSERT 12
776#define JUMP_ASSERT_NOT 13
777
778#define DO_JUMP(jumpvalue, jumplabel, nextpattern) \
779 DATA_ALLOC(SRE_MATCH_CONTEXT, nextctx); \
780 nextctx->last_ctx_pos = ctx_pos; \
781 nextctx->jump = jumpvalue; \
782 nextctx->pattern = nextpattern; \
783 ctx_pos = alloc_pos; \
784 ctx = nextctx; \
785 goto entrance; \
786 jumplabel: \
787 while (0) /* gcc doesn't like labels at end of scopes */ \
788
789typedef struct {
Neal Norwitza6d80fa2006-06-12 03:05:40 +0000790 Py_ssize_t last_ctx_pos;
791 Py_ssize_t jump;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000792 SRE_CHAR* ptr;
793 SRE_CODE* pattern;
Neal Norwitza6d80fa2006-06-12 03:05:40 +0000794 Py_ssize_t count;
795 Py_ssize_t lastmark;
796 Py_ssize_t lastindex;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000797 union {
798 SRE_CODE chr;
799 SRE_REPEAT* rep;
800 } u;
801} SRE_MATCH_CONTEXT;
802
803/* check if string matches the given pattern. returns <0 for
804 error, 0 for failure, and 1 for success */
Neal Norwitza6d80fa2006-06-12 03:05:40 +0000805LOCAL(Py_ssize_t)
Gustavo Niemeyer2cbdc2a2003-12-13 20:32:08 +0000806SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern)
Guido van Rossumb700df92000-03-31 14:59:30 +0000807{
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000808 SRE_CHAR* end = (SRE_CHAR *)state->end;
Neal Norwitza6d80fa2006-06-12 03:05:40 +0000809 Py_ssize_t alloc_pos, ctx_pos = -1;
810 Py_ssize_t i, ret = 0;
811 Py_ssize_t jump;
Guido van Rossumb700df92000-03-31 14:59:30 +0000812
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000813 SRE_MATCH_CONTEXT* ctx;
814 SRE_MATCH_CONTEXT* nextctx;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000815
Gustavo Niemeyer2cbdc2a2003-12-13 20:32:08 +0000816 TRACE(("|%p|%p|ENTER\n", pattern, state->ptr));
Fredrik Lundh96ab4652000-08-03 16:29:50 +0000817
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000818 DATA_ALLOC(SRE_MATCH_CONTEXT, ctx);
819 ctx->last_ctx_pos = -1;
820 ctx->jump = JUMP_NONE;
821 ctx->pattern = pattern;
822 ctx_pos = alloc_pos;
823
824entrance:
825
Anthony Baxteraefd8ca2006-04-12 04:26:11 +0000826 ctx->ptr = (SRE_CHAR *)state->ptr;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000827
828 if (ctx->pattern[0] == SRE_OP_INFO) {
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000829 /* optimization info block */
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000830 /* <INFO> <1=skip> <2=flags> <3=min> ... */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000831 if (ctx->pattern[3] && (end - ctx->ptr) < ctx->pattern[3]) {
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000832 TRACE(("reject (got %d chars, need %d)\n",
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000833 (end - ctx->ptr), ctx->pattern[3]));
834 RETURN_FAILURE;
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000835 }
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000836 ctx->pattern += ctx->pattern[1] + 1;
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000837 }
838
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000839 for (;;) {
Guido van Rossumb700df92000-03-31 14:59:30 +0000840
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000841 switch (*ctx->pattern++) {
Guido van Rossumb700df92000-03-31 14:59:30 +0000842
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000843 case SRE_OP_MARK:
844 /* set mark */
845 /* <MARK> <gid> */
846 TRACE(("|%p|%p|MARK %d\n", ctx->pattern,
847 ctx->ptr, ctx->pattern[0]));
848 i = ctx->pattern[0];
849 if (i & 1)
850 state->lastindex = i/2 + 1;
851 if (i > state->lastmark) {
852 /* state->lastmark is the highest valid index in the
853 state->mark array. If it is increased by more than 1,
854 the intervening marks must be set to NULL to signal
Tim Peters3d563502006-01-21 02:47:53 +0000855 that these marks have not been encountered. */
Neal Norwitza6d80fa2006-06-12 03:05:40 +0000856 Py_ssize_t j = state->lastmark + 1;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000857 while (j < i)
858 state->mark[j++] = NULL;
859 state->lastmark = i;
860 }
861 state->mark[i] = ctx->ptr;
862 ctx->pattern++;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000863 break;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000864
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000865 case SRE_OP_LITERAL:
866 /* match literal string */
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000867 /* <LITERAL> <code> */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000868 TRACE(("|%p|%p|LITERAL %d\n", ctx->pattern,
869 ctx->ptr, *ctx->pattern));
870 if (ctx->ptr >= end || (SRE_CODE) ctx->ptr[0] != ctx->pattern[0])
871 RETURN_FAILURE;
872 ctx->pattern++;
873 ctx->ptr++;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000874 break;
Guido van Rossumb700df92000-03-31 14:59:30 +0000875
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000876 case SRE_OP_NOT_LITERAL:
877 /* match anything that is not literal character */
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000878 /* <NOT_LITERAL> <code> */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000879 TRACE(("|%p|%p|NOT_LITERAL %d\n", ctx->pattern,
880 ctx->ptr, *ctx->pattern));
881 if (ctx->ptr >= end || (SRE_CODE) ctx->ptr[0] == ctx->pattern[0])
882 RETURN_FAILURE;
883 ctx->pattern++;
884 ctx->ptr++;
885 break;
886
887 case SRE_OP_SUCCESS:
888 /* end of pattern */
889 TRACE(("|%p|%p|SUCCESS\n", ctx->pattern, ctx->ptr));
890 state->ptr = ctx->ptr;
891 RETURN_SUCCESS;
892
893 case SRE_OP_AT:
894 /* match at given position */
895 /* <AT> <code> */
896 TRACE(("|%p|%p|AT %d\n", ctx->pattern, ctx->ptr, *ctx->pattern));
897 if (!SRE_AT(state, ctx->ptr, *ctx->pattern))
898 RETURN_FAILURE;
899 ctx->pattern++;
900 break;
901
902 case SRE_OP_CATEGORY:
903 /* match at given category */
904 /* <CATEGORY> <code> */
905 TRACE(("|%p|%p|CATEGORY %d\n", ctx->pattern,
906 ctx->ptr, *ctx->pattern));
907 if (ctx->ptr >= end || !sre_category(ctx->pattern[0], ctx->ptr[0]))
908 RETURN_FAILURE;
909 ctx->pattern++;
910 ctx->ptr++;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000911 break;
Guido van Rossumb700df92000-03-31 14:59:30 +0000912
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000913 case SRE_OP_ANY:
Fredrik Lundhe1869832000-08-01 22:47:49 +0000914 /* match anything (except a newline) */
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000915 /* <ANY> */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000916 TRACE(("|%p|%p|ANY\n", ctx->pattern, ctx->ptr));
917 if (ctx->ptr >= end || SRE_IS_LINEBREAK(ctx->ptr[0]))
918 RETURN_FAILURE;
919 ctx->ptr++;
Fredrik Lundhe1869832000-08-01 22:47:49 +0000920 break;
921
922 case SRE_OP_ANY_ALL:
923 /* match anything */
924 /* <ANY_ALL> */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000925 TRACE(("|%p|%p|ANY_ALL\n", ctx->pattern, ctx->ptr));
926 if (ctx->ptr >= end)
927 RETURN_FAILURE;
928 ctx->ptr++;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000929 break;
Guido van Rossumb700df92000-03-31 14:59:30 +0000930
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000931 case SRE_OP_IN:
932 /* match set member (or non_member) */
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000933 /* <IN> <skip> <set> */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000934 TRACE(("|%p|%p|IN\n", ctx->pattern, ctx->ptr));
935 if (ctx->ptr >= end || !SRE_CHARSET(ctx->pattern + 1, *ctx->ptr))
936 RETURN_FAILURE;
937 ctx->pattern += ctx->pattern[0];
938 ctx->ptr++;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000939 break;
Guido van Rossumb700df92000-03-31 14:59:30 +0000940
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000941 case SRE_OP_LITERAL_IGNORE:
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000942 TRACE(("|%p|%p|LITERAL_IGNORE %d\n",
943 ctx->pattern, ctx->ptr, ctx->pattern[0]));
944 if (ctx->ptr >= end ||
945 state->lower(*ctx->ptr) != state->lower(*ctx->pattern))
946 RETURN_FAILURE;
947 ctx->pattern++;
948 ctx->ptr++;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000949 break;
Guido van Rossumb700df92000-03-31 14:59:30 +0000950
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000951 case SRE_OP_NOT_LITERAL_IGNORE:
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000952 TRACE(("|%p|%p|NOT_LITERAL_IGNORE %d\n",
953 ctx->pattern, ctx->ptr, *ctx->pattern));
954 if (ctx->ptr >= end ||
955 state->lower(*ctx->ptr) == state->lower(*ctx->pattern))
956 RETURN_FAILURE;
957 ctx->pattern++;
958 ctx->ptr++;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000959 break;
Guido van Rossumb700df92000-03-31 14:59:30 +0000960
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000961 case SRE_OP_IN_IGNORE:
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000962 TRACE(("|%p|%p|IN_IGNORE\n", ctx->pattern, ctx->ptr));
963 if (ctx->ptr >= end
964 || !SRE_CHARSET(ctx->pattern+1,
965 (SRE_CODE)state->lower(*ctx->ptr)))
966 RETURN_FAILURE;
967 ctx->pattern += ctx->pattern[0];
968 ctx->ptr++;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000969 break;
Fredrik Lundh7cafe4d2000-07-02 17:33:27 +0000970
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000971 case SRE_OP_JUMP:
972 case SRE_OP_INFO:
973 /* jump forward */
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000974 /* <JUMP> <offset> */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000975 TRACE(("|%p|%p|JUMP %d\n", ctx->pattern,
976 ctx->ptr, ctx->pattern[0]));
977 ctx->pattern += ctx->pattern[0];
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000978 break;
979
980 case SRE_OP_BRANCH:
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000981 /* alternation */
982 /* <BRANCH> <0=skip> code <JUMP> ... <NULL> */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000983 TRACE(("|%p|%p|BRANCH\n", ctx->pattern, ctx->ptr));
Gustavo Niemeyerbe733ee2003-04-20 07:35:44 +0000984 LASTMARK_SAVE();
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000985 ctx->u.rep = state->repeat;
986 if (ctx->u.rep)
987 MARK_PUSH(ctx->lastmark);
988 for (; ctx->pattern[0]; ctx->pattern += ctx->pattern[0]) {
989 if (ctx->pattern[1] == SRE_OP_LITERAL &&
990 (ctx->ptr >= end ||
991 (SRE_CODE) *ctx->ptr != ctx->pattern[2]))
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000992 continue;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000993 if (ctx->pattern[1] == SRE_OP_IN &&
994 (ctx->ptr >= end ||
995 !SRE_CHARSET(ctx->pattern + 3, (SRE_CODE) *ctx->ptr)))
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000996 continue;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000997 state->ptr = ctx->ptr;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000998 DO_JUMP(JUMP_BRANCH, jump_branch, ctx->pattern+1);
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000999 if (ret) {
1000 if (ctx->u.rep)
1001 MARK_POP_DISCARD(ctx->lastmark);
1002 RETURN_ON_ERROR(ret);
1003 RETURN_SUCCESS;
Gustavo Niemeyerc34f2552003-04-27 12:34:14 +00001004 }
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001005 if (ctx->u.rep)
1006 MARK_POP_KEEP(ctx->lastmark);
Gustavo Niemeyerbe733ee2003-04-20 07:35:44 +00001007 LASTMARK_RESTORE();
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001008 }
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001009 if (ctx->u.rep)
1010 MARK_POP_DISCARD(ctx->lastmark);
1011 RETURN_FAILURE;
Guido van Rossumb700df92000-03-31 14:59:30 +00001012
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +00001013 case SRE_OP_REPEAT_ONE:
1014 /* match repeated sequence (maximizing regexp) */
1015
1016 /* this operator only works if the repeated item is
1017 exactly one character wide, and we're not already
1018 collecting backtracking points. for other cases,
Fredrik Lundh770617b2001-01-14 15:06:11 +00001019 use the MAX_REPEAT operator */
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +00001020
1021 /* <REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */
1022
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001023 TRACE(("|%p|%p|REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr,
1024 ctx->pattern[1], ctx->pattern[2]));
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +00001025
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001026 if (ctx->ptr + ctx->pattern[1] > end)
1027 RETURN_FAILURE; /* cannot match */
Fredrik Lundhe1869832000-08-01 22:47:49 +00001028
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001029 state->ptr = ctx->ptr;
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +00001030
Gustavo Niemeyer166878f2004-12-02 16:15:39 +00001031 ret = SRE_COUNT(state, ctx->pattern+3, ctx->pattern[2]);
1032 RETURN_ON_ERROR(ret);
1033 DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos);
1034 ctx->count = ret;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001035 ctx->ptr += ctx->count;
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +00001036
1037 /* when we arrive here, count contains the number of
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001038 matches, and ctx->ptr points to the tail of the target
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +00001039 string. check if the rest of the pattern matches,
1040 and backtrack if not. */
1041
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001042 if (ctx->count < (Py_ssize_t) ctx->pattern[1])
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001043 RETURN_FAILURE;
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +00001044
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001045 if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS) {
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +00001046 /* tail is empty. we're finished */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001047 state->ptr = ctx->ptr;
1048 RETURN_SUCCESS;
Gustavo Niemeyerbe733ee2003-04-20 07:35:44 +00001049 }
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +00001050
Gustavo Niemeyerbe733ee2003-04-20 07:35:44 +00001051 LASTMARK_SAVE();
1052
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001053 if (ctx->pattern[ctx->pattern[0]] == SRE_OP_LITERAL) {
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +00001054 /* tail starts with a literal. skip positions where
1055 the rest of the pattern cannot possibly match */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001056 ctx->u.chr = ctx->pattern[ctx->pattern[0]+1];
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +00001057 for (;;) {
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001058 while (ctx->count >= (Py_ssize_t) ctx->pattern[1] &&
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001059 (ctx->ptr >= end || *ctx->ptr != ctx->u.chr)) {
1060 ctx->ptr--;
1061 ctx->count--;
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +00001062 }
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001063 if (ctx->count < (Py_ssize_t) ctx->pattern[1])
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +00001064 break;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001065 state->ptr = ctx->ptr;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001066 DO_JUMP(JUMP_REPEAT_ONE_1, jump_repeat_one_1,
1067 ctx->pattern+ctx->pattern[0]);
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001068 if (ret) {
1069 RETURN_ON_ERROR(ret);
1070 RETURN_SUCCESS;
1071 }
Tim Peters3d563502006-01-21 02:47:53 +00001072
Gustavo Niemeyerbe733ee2003-04-20 07:35:44 +00001073 LASTMARK_RESTORE();
Tim Peters3d563502006-01-21 02:47:53 +00001074
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001075 ctx->ptr--;
1076 ctx->count--;
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +00001077 }
1078
1079 } else {
1080 /* general case */
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001081 while (ctx->count >= (Py_ssize_t) ctx->pattern[1]) {
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001082 state->ptr = ctx->ptr;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001083 DO_JUMP(JUMP_REPEAT_ONE_2, jump_repeat_one_2,
1084 ctx->pattern+ctx->pattern[0]);
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001085 if (ret) {
1086 RETURN_ON_ERROR(ret);
1087 RETURN_SUCCESS;
1088 }
1089 ctx->ptr--;
1090 ctx->count--;
Gustavo Niemeyerbe733ee2003-04-20 07:35:44 +00001091 LASTMARK_RESTORE();
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +00001092 }
1093 }
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001094 RETURN_FAILURE;
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +00001095
Guido van Rossum41c99e72003-04-14 17:59:34 +00001096 case SRE_OP_MIN_REPEAT_ONE:
1097 /* match repeated sequence (minimizing regexp) */
1098
1099 /* this operator only works if the repeated item is
1100 exactly one character wide, and we're not already
1101 collecting backtracking points. for other cases,
1102 use the MIN_REPEAT operator */
1103
1104 /* <MIN_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */
1105
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001106 TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr,
1107 ctx->pattern[1], ctx->pattern[2]));
Guido van Rossum41c99e72003-04-14 17:59:34 +00001108
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001109 if (ctx->ptr + ctx->pattern[1] > end)
1110 RETURN_FAILURE; /* cannot match */
Guido van Rossum41c99e72003-04-14 17:59:34 +00001111
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001112 state->ptr = ctx->ptr;
Guido van Rossum41c99e72003-04-14 17:59:34 +00001113
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001114 if (ctx->pattern[1] == 0)
1115 ctx->count = 0;
Guido van Rossum41c99e72003-04-14 17:59:34 +00001116 else {
1117 /* count using pattern min as the maximum */
Gustavo Niemeyer166878f2004-12-02 16:15:39 +00001118 ret = SRE_COUNT(state, ctx->pattern+3, ctx->pattern[1]);
1119 RETURN_ON_ERROR(ret);
1120 DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos);
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001121 if (ret < (Py_ssize_t) ctx->pattern[1])
Tim Peters3d563502006-01-21 02:47:53 +00001122 /* didn't match minimum number of times */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001123 RETURN_FAILURE;
1124 /* advance past minimum matches of repeat */
Gustavo Niemeyer166878f2004-12-02 16:15:39 +00001125 ctx->count = ret;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001126 ctx->ptr += ctx->count;
Guido van Rossum41c99e72003-04-14 17:59:34 +00001127 }
1128
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001129 if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS) {
Guido van Rossum41c99e72003-04-14 17:59:34 +00001130 /* tail is empty. we're finished */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001131 state->ptr = ctx->ptr;
1132 RETURN_SUCCESS;
Guido van Rossum41c99e72003-04-14 17:59:34 +00001133
1134 } else {
1135 /* general case */
Gustavo Niemeyerbe733ee2003-04-20 07:35:44 +00001136 LASTMARK_SAVE();
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001137 while ((Py_ssize_t)ctx->pattern[2] == 65535
1138 || ctx->count <= (Py_ssize_t)ctx->pattern[2]) {
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001139 state->ptr = ctx->ptr;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001140 DO_JUMP(JUMP_MIN_REPEAT_ONE,jump_min_repeat_one,
1141 ctx->pattern+ctx->pattern[0]);
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001142 if (ret) {
1143 RETURN_ON_ERROR(ret);
1144 RETURN_SUCCESS;
1145 }
1146 state->ptr = ctx->ptr;
Gustavo Niemeyer2cbdc2a2003-12-13 20:32:08 +00001147 ret = SRE_COUNT(state, ctx->pattern+3, 1);
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001148 RETURN_ON_ERROR(ret);
Gustavo Niemeyer166878f2004-12-02 16:15:39 +00001149 DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos);
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001150 if (ret == 0)
Guido van Rossum41c99e72003-04-14 17:59:34 +00001151 break;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001152 assert(ret == 1);
1153 ctx->ptr++;
1154 ctx->count++;
Gustavo Niemeyercaf1c9d2003-04-27 14:42:54 +00001155 LASTMARK_RESTORE();
Guido van Rossum41c99e72003-04-14 17:59:34 +00001156 }
Guido van Rossum41c99e72003-04-14 17:59:34 +00001157 }
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001158 RETURN_FAILURE;
Guido van Rossum41c99e72003-04-14 17:59:34 +00001159
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001160 case SRE_OP_REPEAT:
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001161 /* create repeat context. all the hard work is done
Fredrik Lundh770617b2001-01-14 15:06:11 +00001162 by the UNTIL operator (MAX_UNTIL, MIN_UNTIL) */
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001163 /* <REPEAT> <skip> <1=min> <2=max> item <UNTIL> tail */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001164 TRACE(("|%p|%p|REPEAT %d %d\n", ctx->pattern, ctx->ptr,
1165 ctx->pattern[1], ctx->pattern[2]));
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001166
1167 /* install new repeat context */
Jack Diederich2d400772006-05-27 15:44:34 +00001168 ctx->u.rep = (SRE_REPEAT*) PyObject_MALLOC(sizeof(*ctx->u.rep));
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001169 ctx->u.rep->count = -1;
1170 ctx->u.rep->pattern = ctx->pattern;
1171 ctx->u.rep->prev = state->repeat;
1172 ctx->u.rep->last_ptr = NULL;
1173 state->repeat = ctx->u.rep;
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001174
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001175 state->ptr = ctx->ptr;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001176 DO_JUMP(JUMP_REPEAT, jump_repeat, ctx->pattern+ctx->pattern[0]);
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001177 state->repeat = ctx->u.rep->prev;
Jack Diederich2d400772006-05-27 15:44:34 +00001178 PyObject_FREE(ctx->u.rep);
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001179
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001180 if (ret) {
1181 RETURN_ON_ERROR(ret);
1182 RETURN_SUCCESS;
1183 }
1184 RETURN_FAILURE;
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001185
1186 case SRE_OP_MAX_UNTIL:
1187 /* maximizing repeat */
1188 /* <REPEAT> <skip> <1=min> <2=max> item <MAX_UNTIL> tail */
1189
1190 /* FIXME: we probably need to deal with zero-width
1191 matches in here... */
1192
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001193 ctx->u.rep = state->repeat;
1194 if (!ctx->u.rep)
1195 RETURN_ERROR(SRE_ERROR_STATE);
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001196
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001197 state->ptr = ctx->ptr;
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001198
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001199 ctx->count = ctx->u.rep->count+1;
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001200
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001201 TRACE(("|%p|%p|MAX_UNTIL %d\n", ctx->pattern,
1202 ctx->ptr, ctx->count));
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001203
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001204 if (ctx->count < ctx->u.rep->pattern[1]) {
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001205 /* not enough matches */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001206 ctx->u.rep->count = ctx->count;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001207 DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1,
1208 ctx->u.rep->pattern+3);
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001209 if (ret) {
1210 RETURN_ON_ERROR(ret);
1211 RETURN_SUCCESS;
1212 }
1213 ctx->u.rep->count = ctx->count-1;
1214 state->ptr = ctx->ptr;
1215 RETURN_FAILURE;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001216 }
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001217
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001218 if ((ctx->count < ctx->u.rep->pattern[2] ||
1219 ctx->u.rep->pattern[2] == 65535) &&
1220 state->ptr != ctx->u.rep->last_ptr) {
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001221 /* we may have enough matches, but if we can
1222 match another item, do so */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001223 ctx->u.rep->count = ctx->count;
Gustavo Niemeyercaf1c9d2003-04-27 14:42:54 +00001224 LASTMARK_SAVE();
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001225 MARK_PUSH(ctx->lastmark);
1226 /* zero-width match protection */
1227 DATA_PUSH(&ctx->u.rep->last_ptr);
1228 ctx->u.rep->last_ptr = state->ptr;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001229 DO_JUMP(JUMP_MAX_UNTIL_2, jump_max_until_2,
1230 ctx->u.rep->pattern+3);
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001231 DATA_POP(&ctx->u.rep->last_ptr);
1232 if (ret) {
1233 MARK_POP_DISCARD(ctx->lastmark);
1234 RETURN_ON_ERROR(ret);
1235 RETURN_SUCCESS;
1236 }
1237 MARK_POP(ctx->lastmark);
Gustavo Niemeyercaf1c9d2003-04-27 14:42:54 +00001238 LASTMARK_RESTORE();
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001239 ctx->u.rep->count = ctx->count-1;
1240 state->ptr = ctx->ptr;
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001241 }
1242
1243 /* cannot match more repeated items here. make sure the
1244 tail matches */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001245 state->repeat = ctx->u.rep->prev;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001246 DO_JUMP(JUMP_MAX_UNTIL_3, jump_max_until_3, ctx->pattern);
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001247 RETURN_ON_SUCCESS(ret);
1248 state->repeat = ctx->u.rep;
1249 state->ptr = ctx->ptr;
1250 RETURN_FAILURE;
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001251
1252 case SRE_OP_MIN_UNTIL:
1253 /* minimizing repeat */
1254 /* <REPEAT> <skip> <1=min> <2=max> item <MIN_UNTIL> tail */
1255
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001256 ctx->u.rep = state->repeat;
1257 if (!ctx->u.rep)
1258 RETURN_ERROR(SRE_ERROR_STATE);
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001259
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001260 state->ptr = ctx->ptr;
Gustavo Niemeyer3c9068b2003-04-22 15:39:09 +00001261
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001262 ctx->count = ctx->u.rep->count+1;
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001263
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001264 TRACE(("|%p|%p|MIN_UNTIL %d %p\n", ctx->pattern,
1265 ctx->ptr, ctx->count, ctx->u.rep->pattern));
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001266
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001267 if (ctx->count < ctx->u.rep->pattern[1]) {
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001268 /* not enough matches */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001269 ctx->u.rep->count = ctx->count;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001270 DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1,
1271 ctx->u.rep->pattern+3);
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001272 if (ret) {
1273 RETURN_ON_ERROR(ret);
1274 RETURN_SUCCESS;
1275 }
1276 ctx->u.rep->count = ctx->count-1;
1277 state->ptr = ctx->ptr;
1278 RETURN_FAILURE;
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001279 }
1280
Gustavo Niemeyercaf1c9d2003-04-27 14:42:54 +00001281 LASTMARK_SAVE();
1282
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001283 /* see if the tail matches */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001284 state->repeat = ctx->u.rep->prev;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001285 DO_JUMP(JUMP_MIN_UNTIL_2, jump_min_until_2, ctx->pattern);
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001286 if (ret) {
1287 RETURN_ON_ERROR(ret);
1288 RETURN_SUCCESS;
1289 }
Fredrik Lundhfa25a7d2001-01-14 23:55:55 +00001290
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001291 state->repeat = ctx->u.rep;
1292 state->ptr = ctx->ptr;
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001293
Gustavo Niemeyercaf1c9d2003-04-27 14:42:54 +00001294 LASTMARK_RESTORE();
1295
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001296 if (ctx->count >= ctx->u.rep->pattern[2]
1297 && ctx->u.rep->pattern[2] != 65535)
1298 RETURN_FAILURE;
Gustavo Niemeyercaf1c9d2003-04-27 14:42:54 +00001299
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001300 ctx->u.rep->count = ctx->count;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001301 DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3,
1302 ctx->u.rep->pattern+3);
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001303 if (ret) {
1304 RETURN_ON_ERROR(ret);
1305 RETURN_SUCCESS;
1306 }
1307 ctx->u.rep->count = ctx->count-1;
1308 state->ptr = ctx->ptr;
1309 RETURN_FAILURE;
1310
1311 case SRE_OP_GROUPREF:
1312 /* match backreference */
1313 TRACE(("|%p|%p|GROUPREF %d\n", ctx->pattern,
1314 ctx->ptr, ctx->pattern[0]));
1315 i = ctx->pattern[0];
1316 {
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001317 Py_ssize_t groupref = i+i;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001318 if (groupref >= state->lastmark) {
1319 RETURN_FAILURE;
1320 } else {
1321 SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1322 SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1323 if (!p || !e || e < p)
1324 RETURN_FAILURE;
1325 while (p < e) {
1326 if (ctx->ptr >= end || *ctx->ptr != *p)
1327 RETURN_FAILURE;
1328 p++; ctx->ptr++;
1329 }
1330 }
1331 }
1332 ctx->pattern++;
1333 break;
1334
1335 case SRE_OP_GROUPREF_IGNORE:
1336 /* match backreference */
1337 TRACE(("|%p|%p|GROUPREF_IGNORE %d\n", ctx->pattern,
1338 ctx->ptr, ctx->pattern[0]));
1339 i = ctx->pattern[0];
1340 {
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001341 Py_ssize_t groupref = i+i;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001342 if (groupref >= state->lastmark) {
1343 RETURN_FAILURE;
1344 } else {
1345 SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1346 SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1347 if (!p || !e || e < p)
1348 RETURN_FAILURE;
1349 while (p < e) {
1350 if (ctx->ptr >= end ||
1351 state->lower(*ctx->ptr) != state->lower(*p))
1352 RETURN_FAILURE;
1353 p++; ctx->ptr++;
1354 }
1355 }
1356 }
1357 ctx->pattern++;
1358 break;
1359
1360 case SRE_OP_GROUPREF_EXISTS:
1361 TRACE(("|%p|%p|GROUPREF_EXISTS %d\n", ctx->pattern,
1362 ctx->ptr, ctx->pattern[0]));
1363 /* <GROUPREF_EXISTS> <group> <skip> codeyes <JUMP> codeno ... */
1364 i = ctx->pattern[0];
1365 {
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001366 Py_ssize_t groupref = i+i;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001367 if (groupref >= state->lastmark) {
1368 ctx->pattern += ctx->pattern[1];
1369 break;
1370 } else {
1371 SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
1372 SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
1373 if (!p || !e || e < p) {
1374 ctx->pattern += ctx->pattern[1];
1375 break;
1376 }
1377 }
1378 }
1379 ctx->pattern += 2;
1380 break;
1381
1382 case SRE_OP_ASSERT:
1383 /* assert subpattern */
1384 /* <ASSERT> <skip> <back> <pattern> */
1385 TRACE(("|%p|%p|ASSERT %d\n", ctx->pattern,
1386 ctx->ptr, ctx->pattern[1]));
1387 state->ptr = ctx->ptr - ctx->pattern[1];
1388 if (state->ptr < state->beginning)
1389 RETURN_FAILURE;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001390 DO_JUMP(JUMP_ASSERT, jump_assert, ctx->pattern+2);
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001391 RETURN_ON_FAILURE(ret);
1392 ctx->pattern += ctx->pattern[0];
1393 break;
1394
1395 case SRE_OP_ASSERT_NOT:
1396 /* assert not subpattern */
1397 /* <ASSERT_NOT> <skip> <back> <pattern> */
1398 TRACE(("|%p|%p|ASSERT_NOT %d\n", ctx->pattern,
1399 ctx->ptr, ctx->pattern[1]));
1400 state->ptr = ctx->ptr - ctx->pattern[1];
1401 if (state->ptr >= state->beginning) {
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001402 DO_JUMP(JUMP_ASSERT_NOT, jump_assert_not, ctx->pattern+2);
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001403 if (ret) {
1404 RETURN_ON_ERROR(ret);
1405 RETURN_FAILURE;
1406 }
1407 }
1408 ctx->pattern += ctx->pattern[0];
1409 break;
1410
1411 case SRE_OP_FAILURE:
1412 /* immediate failure */
1413 TRACE(("|%p|%p|FAILURE\n", ctx->pattern, ctx->ptr));
1414 RETURN_FAILURE;
Guido van Rossumb700df92000-03-31 14:59:30 +00001415
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00001416 default:
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001417 TRACE(("|%p|%p|UNKNOWN %d\n", ctx->pattern, ctx->ptr,
1418 ctx->pattern[-1]));
1419 RETURN_ERROR(SRE_ERROR_ILLEGAL);
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001420 }
1421 }
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00001422
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001423exit:
1424 ctx_pos = ctx->last_ctx_pos;
1425 jump = ctx->jump;
1426 DATA_POP_DISCARD(ctx);
1427 if (ctx_pos == -1)
1428 return ret;
1429 DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos);
1430
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001431 switch (jump) {
1432 case JUMP_MAX_UNTIL_2:
1433 TRACE(("|%p|%p|JUMP_MAX_UNTIL_2\n", ctx->pattern, ctx->ptr));
1434 goto jump_max_until_2;
1435 case JUMP_MAX_UNTIL_3:
1436 TRACE(("|%p|%p|JUMP_MAX_UNTIL_3\n", ctx->pattern, ctx->ptr));
1437 goto jump_max_until_3;
1438 case JUMP_MIN_UNTIL_2:
1439 TRACE(("|%p|%p|JUMP_MIN_UNTIL_2\n", ctx->pattern, ctx->ptr));
1440 goto jump_min_until_2;
1441 case JUMP_MIN_UNTIL_3:
1442 TRACE(("|%p|%p|JUMP_MIN_UNTIL_3\n", ctx->pattern, ctx->ptr));
1443 goto jump_min_until_3;
1444 case JUMP_BRANCH:
1445 TRACE(("|%p|%p|JUMP_BRANCH\n", ctx->pattern, ctx->ptr));
1446 goto jump_branch;
1447 case JUMP_MAX_UNTIL_1:
1448 TRACE(("|%p|%p|JUMP_MAX_UNTIL_1\n", ctx->pattern, ctx->ptr));
1449 goto jump_max_until_1;
1450 case JUMP_MIN_UNTIL_1:
1451 TRACE(("|%p|%p|JUMP_MIN_UNTIL_1\n", ctx->pattern, ctx->ptr));
1452 goto jump_min_until_1;
1453 case JUMP_REPEAT:
1454 TRACE(("|%p|%p|JUMP_REPEAT\n", ctx->pattern, ctx->ptr));
1455 goto jump_repeat;
1456 case JUMP_REPEAT_ONE_1:
1457 TRACE(("|%p|%p|JUMP_REPEAT_ONE_1\n", ctx->pattern, ctx->ptr));
1458 goto jump_repeat_one_1;
1459 case JUMP_REPEAT_ONE_2:
1460 TRACE(("|%p|%p|JUMP_REPEAT_ONE_2\n", ctx->pattern, ctx->ptr));
1461 goto jump_repeat_one_2;
1462 case JUMP_MIN_REPEAT_ONE:
1463 TRACE(("|%p|%p|JUMP_MIN_REPEAT_ONE\n", ctx->pattern, ctx->ptr));
1464 goto jump_min_repeat_one;
1465 case JUMP_ASSERT:
1466 TRACE(("|%p|%p|JUMP_ASSERT\n", ctx->pattern, ctx->ptr));
1467 goto jump_assert;
1468 case JUMP_ASSERT_NOT:
1469 TRACE(("|%p|%p|JUMP_ASSERT_NOT\n", ctx->pattern, ctx->ptr));
1470 goto jump_assert_not;
1471 case JUMP_NONE:
1472 TRACE(("|%p|%p|RETURN %d\n", ctx->pattern, ctx->ptr, ret));
1473 break;
1474 }
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001475
1476 return ret; /* should never get here */
Guido van Rossumb700df92000-03-31 14:59:30 +00001477}
1478
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001479LOCAL(Py_ssize_t)
Guido van Rossumb700df92000-03-31 14:59:30 +00001480SRE_SEARCH(SRE_STATE* state, SRE_CODE* pattern)
1481{
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00001482 SRE_CHAR* ptr = (SRE_CHAR *)state->start;
1483 SRE_CHAR* end = (SRE_CHAR *)state->end;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001484 Py_ssize_t status = 0;
1485 Py_ssize_t prefix_len = 0;
1486 Py_ssize_t prefix_skip = 0;
Fredrik Lundh3562f112000-07-02 12:00:07 +00001487 SRE_CODE* prefix = NULL;
1488 SRE_CODE* charset = NULL;
1489 SRE_CODE* overlap = NULL;
1490 int flags = 0;
Guido van Rossumb700df92000-03-31 14:59:30 +00001491
Fredrik Lundh436c3d52000-06-29 08:58:44 +00001492 if (pattern[0] == SRE_OP_INFO) {
Fredrik Lundh29c08be2000-06-29 23:33:12 +00001493 /* optimization info block */
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001494 /* <INFO> <1=skip> <2=flags> <3=min> <4=max> <5=prefix info> */
Fredrik Lundh3562f112000-07-02 12:00:07 +00001495
1496 flags = pattern[2];
Fredrik Lundh29c08be2000-06-29 23:33:12 +00001497
Gustavo Niemeyer28b5bb32003-06-26 14:41:08 +00001498 if (pattern[3] > 1) {
Fredrik Lundh29c08be2000-06-29 23:33:12 +00001499 /* adjust end point (but make sure we leave at least one
Fredrik Lundh3562f112000-07-02 12:00:07 +00001500 character in there, so literal search will work) */
Fredrik Lundh29c08be2000-06-29 23:33:12 +00001501 end -= pattern[3]-1;
1502 if (end <= ptr)
1503 end = ptr+1;
1504 }
1505
Fredrik Lundh3562f112000-07-02 12:00:07 +00001506 if (flags & SRE_INFO_PREFIX) {
Fredrik Lundh7cafe4d2000-07-02 17:33:27 +00001507 /* pattern starts with a known prefix */
Fredrik Lundh7898c3e2000-08-07 20:59:04 +00001508 /* <length> <skip> <prefix data> <overlap data> */
Fredrik Lundh3562f112000-07-02 12:00:07 +00001509 prefix_len = pattern[5];
Fredrik Lundh7898c3e2000-08-07 20:59:04 +00001510 prefix_skip = pattern[6];
1511 prefix = pattern + 7;
Fredrik Lundh3562f112000-07-02 12:00:07 +00001512 overlap = prefix + prefix_len - 1;
1513 } else if (flags & SRE_INFO_CHARSET)
Fredrik Lundh7cafe4d2000-07-02 17:33:27 +00001514 /* pattern starts with a character from a known set */
Fredrik Lundh7898c3e2000-08-07 20:59:04 +00001515 /* <charset> */
Fredrik Lundh3562f112000-07-02 12:00:07 +00001516 charset = pattern + 5;
Fredrik Lundh29c08be2000-06-29 23:33:12 +00001517
1518 pattern += 1 + pattern[1];
Fredrik Lundh436c3d52000-06-29 08:58:44 +00001519 }
Guido van Rossumb700df92000-03-31 14:59:30 +00001520
Fredrik Lundh7898c3e2000-08-07 20:59:04 +00001521 TRACE(("prefix = %p %d %d\n", prefix, prefix_len, prefix_skip));
1522 TRACE(("charset = %p\n", charset));
1523
Fredrik Lundh29c08be2000-06-29 23:33:12 +00001524#if defined(USE_FAST_SEARCH)
Fredrik Lundh28552902000-07-05 21:14:16 +00001525 if (prefix_len > 1) {
Fredrik Lundh29c08be2000-06-29 23:33:12 +00001526 /* pattern starts with a known prefix. use the overlap
1527 table to skip forward as fast as we possibly can */
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001528 Py_ssize_t i = 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00001529 end = (SRE_CHAR *)state->end;
Fredrik Lundh29c08be2000-06-29 23:33:12 +00001530 while (ptr < end) {
1531 for (;;) {
Fredrik Lundh0640e112000-06-30 13:55:15 +00001532 if ((SRE_CODE) ptr[0] != prefix[i]) {
Fredrik Lundh29c08be2000-06-29 23:33:12 +00001533 if (!i)
1534 break;
1535 else
1536 i = overlap[i];
1537 } else {
1538 if (++i == prefix_len) {
1539 /* found a potential match */
Fredrik Lundh7898c3e2000-08-07 20:59:04 +00001540 TRACE(("|%p|%p|SEARCH SCAN\n", pattern, ptr));
1541 state->start = ptr + 1 - prefix_len;
1542 state->ptr = ptr + 1 - prefix_len + prefix_skip;
Fredrik Lundh3562f112000-07-02 12:00:07 +00001543 if (flags & SRE_INFO_LITERAL)
1544 return 1; /* we got all of it */
Gustavo Niemeyer2cbdc2a2003-12-13 20:32:08 +00001545 status = SRE_MATCH(state, pattern + 2*prefix_skip);
Fredrik Lundh29c08be2000-06-29 23:33:12 +00001546 if (status != 0)
1547 return status;
1548 /* close but no cigar -- try again */
1549 i = overlap[i];
1550 }
1551 break;
1552 }
Fredrik Lundh29c08be2000-06-29 23:33:12 +00001553 }
1554 ptr++;
1555 }
1556 return 0;
1557 }
1558#endif
Fredrik Lundh80946112000-06-29 18:03:25 +00001559
Fredrik Lundh3562f112000-07-02 12:00:07 +00001560 if (pattern[0] == SRE_OP_LITERAL) {
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001561 /* pattern starts with a literal character. this is used
Fredrik Lundh3562f112000-07-02 12:00:07 +00001562 for short prefixes, and if fast search is disabled */
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001563 SRE_CODE chr = pattern[1];
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00001564 end = (SRE_CHAR *)state->end;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001565 for (;;) {
1566 while (ptr < end && (SRE_CODE) ptr[0] != chr)
1567 ptr++;
Gustavo Niemeyerc523b042002-11-07 03:28:56 +00001568 if (ptr >= end)
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001569 return 0;
Fredrik Lundh7898c3e2000-08-07 20:59:04 +00001570 TRACE(("|%p|%p|SEARCH LITERAL\n", pattern, ptr));
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001571 state->start = ptr;
1572 state->ptr = ++ptr;
Fredrik Lundhdac58492001-10-21 21:48:30 +00001573 if (flags & SRE_INFO_LITERAL)
1574 return 1; /* we got all of it */
Gustavo Niemeyer2cbdc2a2003-12-13 20:32:08 +00001575 status = SRE_MATCH(state, pattern + 2);
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001576 if (status != 0)
1577 break;
Fredrik Lundh3562f112000-07-02 12:00:07 +00001578 }
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001579 } else if (charset) {
1580 /* pattern starts with a character from a known set */
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00001581 end = (SRE_CHAR *)state->end;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001582 for (;;) {
Fredrik Lundh7898c3e2000-08-07 20:59:04 +00001583 while (ptr < end && !SRE_CHARSET(charset, ptr[0]))
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001584 ptr++;
Gustavo Niemeyerc523b042002-11-07 03:28:56 +00001585 if (ptr >= end)
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001586 return 0;
Fredrik Lundh7898c3e2000-08-07 20:59:04 +00001587 TRACE(("|%p|%p|SEARCH CHARSET\n", pattern, ptr));
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001588 state->start = ptr;
1589 state->ptr = ptr;
Gustavo Niemeyer2cbdc2a2003-12-13 20:32:08 +00001590 status = SRE_MATCH(state, pattern);
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001591 if (status != 0)
1592 break;
Fredrik Lundh7898c3e2000-08-07 20:59:04 +00001593 ptr++;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001594 }
1595 } else
1596 /* general case */
1597 while (ptr <= end) {
Fredrik Lundh7898c3e2000-08-07 20:59:04 +00001598 TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001599 state->start = state->ptr = ptr++;
Gustavo Niemeyer2cbdc2a2003-12-13 20:32:08 +00001600 status = SRE_MATCH(state, pattern);
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001601 if (status != 0)
1602 break;
1603 }
Guido van Rossumb700df92000-03-31 14:59:30 +00001604
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001605 return status;
Guido van Rossumb700df92000-03-31 14:59:30 +00001606}
Tim Peters3d563502006-01-21 02:47:53 +00001607
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00001608LOCAL(int)
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001609SRE_LITERAL_TEMPLATE(SRE_CHAR* ptr, Py_ssize_t len)
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00001610{
1611 /* check if given string is a literal template (i.e. no escapes) */
1612 while (len-- > 0)
1613 if (*ptr++ == '\\')
1614 return 0;
1615 return 1;
1616}
Guido van Rossumb700df92000-03-31 14:59:30 +00001617
Fredrik Lundh436c3d52000-06-29 08:58:44 +00001618#if !defined(SRE_RECURSIVE)
Guido van Rossumb700df92000-03-31 14:59:30 +00001619
1620/* -------------------------------------------------------------------- */
1621/* factories and destructors */
1622
1623/* see sre.h for object declarations */
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00001624static PyObject*pattern_new_match(PatternObject*, SRE_STATE*, int);
1625static PyObject*pattern_scanner(PatternObject*, PyObject*);
Guido van Rossumb700df92000-03-31 14:59:30 +00001626
1627static PyObject *
Georg Brandl964f5972006-05-28 22:38:57 +00001628sre_codesize(PyObject* self, PyObject *unused)
Guido van Rossumb700df92000-03-31 14:59:30 +00001629{
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001630 return Py_BuildValue("l", sizeof(SRE_CODE));
Guido van Rossumb700df92000-03-31 14:59:30 +00001631}
1632
Fredrik Lundh436c3d52000-06-29 08:58:44 +00001633static PyObject *
Fredrik Lundhb389df32000-06-29 12:48:37 +00001634sre_getlower(PyObject* self, PyObject* args)
Fredrik Lundh436c3d52000-06-29 08:58:44 +00001635{
1636 int character, flags;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001637 if (!PyArg_ParseTuple(args, "ii", &character, &flags))
Fredrik Lundh436c3d52000-06-29 08:58:44 +00001638 return NULL;
1639 if (flags & SRE_FLAG_LOCALE)
Fredrik Lundhb389df32000-06-29 12:48:37 +00001640 return Py_BuildValue("i", sre_lower_locale(character));
Fredrik Lundh436c3d52000-06-29 08:58:44 +00001641 if (flags & SRE_FLAG_UNICODE)
Fredrik Lundh1c5aa692001-01-16 07:37:30 +00001642#if defined(HAVE_UNICODE)
Fredrik Lundhb389df32000-06-29 12:48:37 +00001643 return Py_BuildValue("i", sre_lower_unicode(character));
Fredrik Lundh1c5aa692001-01-16 07:37:30 +00001644#else
1645 return Py_BuildValue("i", sre_lower_locale(character));
Fredrik Lundh436c3d52000-06-29 08:58:44 +00001646#endif
Fredrik Lundhb389df32000-06-29 12:48:37 +00001647 return Py_BuildValue("i", sre_lower(character));
Fredrik Lundh436c3d52000-06-29 08:58:44 +00001648}
1649
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001650LOCAL(void)
1651state_reset(SRE_STATE* state)
1652{
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001653 /* FIXME: dynamic! */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001654 /*memset(state->mark, 0, sizeof(*state->mark) * SRE_MARK_SIZE);*/
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001655
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001656 state->lastmark = -1;
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001657 state->lastindex = -1;
1658
1659 state->repeat = NULL;
1660
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001661 data_stack_dealloc(state);
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001662}
1663
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00001664static void*
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001665getstring(PyObject* string, Py_ssize_t* p_length, int* p_charsize)
Guido van Rossumb700df92000-03-31 14:59:30 +00001666{
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00001667 /* given a python object, return a data pointer, a length (in
1668 characters), and a character size. return NULL if the object
1669 is not a string (or not compatible) */
Tim Peters3d563502006-01-21 02:47:53 +00001670
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001671 PyBufferProcs *buffer;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001672 Py_ssize_t size, bytes;
1673 int charsize;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001674 void* ptr;
Guido van Rossumb700df92000-03-31 14:59:30 +00001675
Fredrik Lundh5644b7f2000-09-21 17:03:25 +00001676#if defined(HAVE_UNICODE)
1677 if (PyUnicode_Check(string)) {
1678 /* unicode strings doesn't always support the buffer interface */
1679 ptr = (void*) PyUnicode_AS_DATA(string);
1680 bytes = PyUnicode_GET_DATA_SIZE(string);
1681 size = PyUnicode_GET_SIZE(string);
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00001682 charsize = sizeof(Py_UNICODE);
Fredrik Lundh5644b7f2000-09-21 17:03:25 +00001683
1684 } else {
1685#endif
1686
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001687 /* get pointer to string buffer */
1688 buffer = string->ob_type->tp_as_buffer;
1689 if (!buffer || !buffer->bf_getreadbuffer || !buffer->bf_getsegcount ||
1690 buffer->bf_getsegcount(string, NULL) != 1) {
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001691 PyErr_SetString(PyExc_TypeError, "expected string or buffer");
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001692 return NULL;
1693 }
Guido van Rossumb700df92000-03-31 14:59:30 +00001694
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001695 /* determine buffer size */
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001696 bytes = buffer->bf_getreadbuffer(string, 0, &ptr);
1697 if (bytes < 0) {
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001698 PyErr_SetString(PyExc_TypeError, "buffer has negative size");
1699 return NULL;
1700 }
Guido van Rossumb700df92000-03-31 14:59:30 +00001701
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001702 /* determine character size */
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001703#if PY_VERSION_HEX >= 0x01060000
1704 size = PyObject_Size(string);
Fredrik Lundh436c3d52000-06-29 08:58:44 +00001705#else
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001706 size = PyObject_Length(string);
Fredrik Lundh436c3d52000-06-29 08:58:44 +00001707#endif
Guido van Rossumb700df92000-03-31 14:59:30 +00001708
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001709 if (PyString_Check(string) || bytes == size)
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00001710 charsize = 1;
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001711#if defined(HAVE_UNICODE)
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001712 else if (bytes == (Py_ssize_t) (size * sizeof(Py_UNICODE)))
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00001713 charsize = sizeof(Py_UNICODE);
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00001714#endif
1715 else {
1716 PyErr_SetString(PyExc_TypeError, "buffer size mismatch");
1717 return NULL;
1718 }
Guido van Rossumb700df92000-03-31 14:59:30 +00001719
Fredrik Lundh5644b7f2000-09-21 17:03:25 +00001720#if defined(HAVE_UNICODE)
1721 }
1722#endif
1723
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00001724 *p_length = size;
1725 *p_charsize = charsize;
1726
1727 return ptr;
1728}
1729
1730LOCAL(PyObject*)
1731state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001732 Py_ssize_t start, Py_ssize_t end)
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00001733{
1734 /* prepare state object */
1735
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001736 Py_ssize_t length;
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00001737 int charsize;
1738 void* ptr;
1739
1740 memset(state, 0, sizeof(SRE_STATE));
1741
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001742 state->lastmark = -1;
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00001743 state->lastindex = -1;
1744
1745 ptr = getstring(string, &length, &charsize);
1746 if (!ptr)
1747 return NULL;
1748
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001749 /* adjust boundaries */
1750 if (start < 0)
1751 start = 0;
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00001752 else if (start > length)
1753 start = length;
Guido van Rossumb700df92000-03-31 14:59:30 +00001754
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001755 if (end < 0)
1756 end = 0;
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00001757 else if (end > length)
1758 end = length;
1759
1760 state->charsize = charsize;
Guido van Rossumb700df92000-03-31 14:59:30 +00001761
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001762 state->beginning = ptr;
Guido van Rossumb700df92000-03-31 14:59:30 +00001763
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001764 state->start = (void*) ((char*) ptr + start * state->charsize);
1765 state->end = (void*) ((char*) ptr + end * state->charsize);
1766
1767 Py_INCREF(string);
1768 state->string = string;
1769 state->pos = start;
1770 state->endpos = end;
Guido van Rossumb700df92000-03-31 14:59:30 +00001771
Fredrik Lundh436c3d52000-06-29 08:58:44 +00001772 if (pattern->flags & SRE_FLAG_LOCALE)
Fredrik Lundhb389df32000-06-29 12:48:37 +00001773 state->lower = sre_lower_locale;
Fredrik Lundh436c3d52000-06-29 08:58:44 +00001774 else if (pattern->flags & SRE_FLAG_UNICODE)
Fredrik Lundh1c5aa692001-01-16 07:37:30 +00001775#if defined(HAVE_UNICODE)
Fredrik Lundhb389df32000-06-29 12:48:37 +00001776 state->lower = sre_lower_unicode;
Fredrik Lundh1c5aa692001-01-16 07:37:30 +00001777#else
1778 state->lower = sre_lower_locale;
Fredrik Lundh436c3d52000-06-29 08:58:44 +00001779#endif
1780 else
Fredrik Lundhb389df32000-06-29 12:48:37 +00001781 state->lower = sre_lower;
Fredrik Lundh436c3d52000-06-29 08:58:44 +00001782
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001783 return string;
Guido van Rossumb700df92000-03-31 14:59:30 +00001784}
1785
Fredrik Lundh75f2d672000-06-29 11:34:28 +00001786LOCAL(void)
1787state_fini(SRE_STATE* state)
1788{
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001789 Py_XDECREF(state->string);
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001790 data_stack_dealloc(state);
Fredrik Lundh75f2d672000-06-29 11:34:28 +00001791}
1792
Fredrik Lundhbec95b92001-10-21 16:47:57 +00001793/* calculate offset from start of string */
1794#define STATE_OFFSET(state, member)\
1795 (((char*)(member) - (char*)(state)->beginning) / (state)->charsize)
1796
Fredrik Lundh75f2d672000-06-29 11:34:28 +00001797LOCAL(PyObject*)
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001798state_getslice(SRE_STATE* state, Py_ssize_t index, PyObject* string, int empty)
Fredrik Lundh75f2d672000-06-29 11:34:28 +00001799{
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001800 Py_ssize_t i, j;
Fredrik Lundh58100642000-08-09 09:14:35 +00001801
Fredrik Lundh75f2d672000-06-29 11:34:28 +00001802 index = (index - 1) * 2;
1803
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +00001804 if (string == Py_None || index >= state->lastmark || !state->mark[index] || !state->mark[index+1]) {
Fredrik Lundh971e78b2001-10-20 17:48:46 +00001805 if (empty)
1806 /* want empty string */
1807 i = j = 0;
1808 else {
1809 Py_INCREF(Py_None);
1810 return Py_None;
1811 }
Fredrik Lundh58100642000-08-09 09:14:35 +00001812 } else {
Fredrik Lundhbec95b92001-10-21 16:47:57 +00001813 i = STATE_OFFSET(state, state->mark[index]);
1814 j = STATE_OFFSET(state, state->mark[index+1]);
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001815 }
Fredrik Lundh75f2d672000-06-29 11:34:28 +00001816
Fredrik Lundh58100642000-08-09 09:14:35 +00001817 return PySequence_GetSlice(string, i, j);
Fredrik Lundh75f2d672000-06-29 11:34:28 +00001818}
1819
Fredrik Lundh96ab4652000-08-03 16:29:50 +00001820static void
1821pattern_error(int status)
1822{
1823 switch (status) {
1824 case SRE_ERROR_RECURSION_LIMIT:
1825 PyErr_SetString(
1826 PyExc_RuntimeError,
1827 "maximum recursion limit exceeded"
1828 );
1829 break;
1830 case SRE_ERROR_MEMORY:
1831 PyErr_NoMemory();
1832 break;
1833 default:
1834 /* other error codes indicate compiler/engine bugs */
1835 PyErr_SetString(
1836 PyExc_RuntimeError,
1837 "internal error in regular expression engine"
1838 );
1839 }
1840}
1841
Guido van Rossumb700df92000-03-31 14:59:30 +00001842static void
Fredrik Lundh75f2d672000-06-29 11:34:28 +00001843pattern_dealloc(PatternObject* self)
Guido van Rossumb700df92000-03-31 14:59:30 +00001844{
Raymond Hettinger027bb632004-05-31 03:09:25 +00001845 if (self->weakreflist != NULL)
1846 PyObject_ClearWeakRefs((PyObject *) self);
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001847 Py_XDECREF(self->pattern);
1848 Py_XDECREF(self->groupindex);
Fredrik Lundh6f5cba62001-01-16 07:05:29 +00001849 Py_XDECREF(self->indexgroup);
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001850 PyObject_DEL(self);
Guido van Rossumb700df92000-03-31 14:59:30 +00001851}
1852
1853static PyObject*
Fredrik Lundh562586e2000-10-03 20:43:34 +00001854pattern_match(PatternObject* self, PyObject* args, PyObject* kw)
Guido van Rossumb700df92000-03-31 14:59:30 +00001855{
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001856 SRE_STATE state;
1857 int status;
Guido van Rossumb700df92000-03-31 14:59:30 +00001858
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001859 PyObject* string;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001860 Py_ssize_t start = 0;
1861 Py_ssize_t end = PY_SSIZE_T_MAX;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001862 static char* kwlist[] = { "pattern", "pos", "endpos", NULL };
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001863 if (!PyArg_ParseTupleAndKeywords(args, kw, "O|nn:match", kwlist,
Fredrik Lundh562586e2000-10-03 20:43:34 +00001864 &string, &start, &end))
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001865 return NULL;
Guido van Rossumb700df92000-03-31 14:59:30 +00001866
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001867 string = state_init(&state, self, string, start, end);
1868 if (!string)
1869 return NULL;
Guido van Rossumb700df92000-03-31 14:59:30 +00001870
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001871 state.ptr = state.start;
1872
Fredrik Lundh7898c3e2000-08-07 20:59:04 +00001873 TRACE(("|%p|%p|MATCH\n", PatternObject_GetCode(self), state.ptr));
1874
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001875 if (state.charsize == 1) {
Gustavo Niemeyer2cbdc2a2003-12-13 20:32:08 +00001876 status = sre_match(&state, PatternObject_GetCode(self));
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001877 } else {
Fredrik Lundh436c3d52000-06-29 08:58:44 +00001878#if defined(HAVE_UNICODE)
Gustavo Niemeyer2cbdc2a2003-12-13 20:32:08 +00001879 status = sre_umatch(&state, PatternObject_GetCode(self));
Fredrik Lundh436c3d52000-06-29 08:58:44 +00001880#endif
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001881 }
Guido van Rossumb700df92000-03-31 14:59:30 +00001882
Fredrik Lundh7898c3e2000-08-07 20:59:04 +00001883 TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr));
1884
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001885 state_fini(&state);
Guido van Rossumb700df92000-03-31 14:59:30 +00001886
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001887 return pattern_new_match(self, &state, status);
Guido van Rossumb700df92000-03-31 14:59:30 +00001888}
1889
1890static PyObject*
Fredrik Lundh562586e2000-10-03 20:43:34 +00001891pattern_search(PatternObject* self, PyObject* args, PyObject* kw)
Guido van Rossumb700df92000-03-31 14:59:30 +00001892{
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001893 SRE_STATE state;
1894 int status;
Guido van Rossumb700df92000-03-31 14:59:30 +00001895
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001896 PyObject* string;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001897 Py_ssize_t start = 0;
1898 Py_ssize_t end = PY_SSIZE_T_MAX;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001899 static char* kwlist[] = { "pattern", "pos", "endpos", NULL };
Neal Norwitza6d80fa2006-06-12 03:05:40 +00001900 if (!PyArg_ParseTupleAndKeywords(args, kw, "O|nn:search", kwlist,
Fredrik Lundh562586e2000-10-03 20:43:34 +00001901 &string, &start, &end))
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001902 return NULL;
Guido van Rossumb700df92000-03-31 14:59:30 +00001903
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001904 string = state_init(&state, self, string, start, end);
1905 if (!string)
1906 return NULL;
1907
Fredrik Lundh7898c3e2000-08-07 20:59:04 +00001908 TRACE(("|%p|%p|SEARCH\n", PatternObject_GetCode(self), state.ptr));
1909
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001910 if (state.charsize == 1) {
1911 status = sre_search(&state, PatternObject_GetCode(self));
1912 } else {
Fredrik Lundh436c3d52000-06-29 08:58:44 +00001913#if defined(HAVE_UNICODE)
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001914 status = sre_usearch(&state, PatternObject_GetCode(self));
Fredrik Lundh436c3d52000-06-29 08:58:44 +00001915#endif
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001916 }
Guido van Rossumb700df92000-03-31 14:59:30 +00001917
Fredrik Lundh7898c3e2000-08-07 20:59:04 +00001918 TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr));
1919
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001920 state_fini(&state);
Guido van Rossumb700df92000-03-31 14:59:30 +00001921
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00001922 return pattern_new_match(self, &state, status);
Guido van Rossumb700df92000-03-31 14:59:30 +00001923}
1924
1925static PyObject*
Fredrik Lundhd89a2e72001-07-03 20:32:36 +00001926call(char* module, char* function, PyObject* args)
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00001927{
1928 PyObject* name;
Fredrik Lundhd89a2e72001-07-03 20:32:36 +00001929 PyObject* mod;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00001930 PyObject* func;
1931 PyObject* result;
1932
Fredrik Lundhbec95b92001-10-21 16:47:57 +00001933 if (!args)
1934 return NULL;
Fredrik Lundhd89a2e72001-07-03 20:32:36 +00001935 name = PyString_FromString(module);
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00001936 if (!name)
1937 return NULL;
Fredrik Lundhd89a2e72001-07-03 20:32:36 +00001938 mod = PyImport_Import(name);
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00001939 Py_DECREF(name);
Fredrik Lundhd89a2e72001-07-03 20:32:36 +00001940 if (!mod)
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00001941 return NULL;
Fredrik Lundhd89a2e72001-07-03 20:32:36 +00001942 func = PyObject_GetAttrString(mod, function);
1943 Py_DECREF(mod);
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00001944 if (!func)
1945 return NULL;
1946 result = PyObject_CallObject(func, args);
1947 Py_DECREF(func);
1948 Py_DECREF(args);
1949 return result;
1950}
1951
Fredrik Lundhd89a2e72001-07-03 20:32:36 +00001952#ifdef USE_BUILTIN_COPY
1953static int
1954deepcopy(PyObject** object, PyObject* memo)
1955{
1956 PyObject* copy;
1957
1958 copy = call(
1959 "copy", "deepcopy",
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001960 PyTuple_Pack(2, *object, memo)
Fredrik Lundhd89a2e72001-07-03 20:32:36 +00001961 );
1962 if (!copy)
1963 return 0;
1964
1965 Py_DECREF(*object);
1966 *object = copy;
1967
1968 return 1; /* success */
1969}
1970#endif
1971
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00001972static PyObject*
Michael W. Hudsonb6a45052002-07-31 09:54:24 +00001973join_list(PyObject* list, PyObject* pattern)
Fredrik Lundhbec95b92001-10-21 16:47:57 +00001974{
1975 /* join list elements */
1976
1977 PyObject* joiner;
1978#if PY_VERSION_HEX >= 0x01060000
1979 PyObject* function;
1980 PyObject* args;
1981#endif
1982 PyObject* result;
1983
1984 switch (PyList_GET_SIZE(list)) {
1985 case 0:
1986 Py_DECREF(list);
Fredrik Lundh09705f02002-11-22 12:46:35 +00001987 return PySequence_GetSlice(pattern, 0, 0);
Fredrik Lundhbec95b92001-10-21 16:47:57 +00001988 case 1:
1989 result = PyList_GET_ITEM(list, 0);
1990 Py_INCREF(result);
1991 Py_DECREF(list);
1992 return result;
1993 }
1994
1995 /* two or more elements: slice out a suitable separator from the
1996 first member, and use that to join the entire list */
1997
1998 joiner = PySequence_GetSlice(pattern, 0, 0);
1999 if (!joiner)
2000 return NULL;
2001
2002#if PY_VERSION_HEX >= 0x01060000
2003 function = PyObject_GetAttrString(joiner, "join");
2004 if (!function) {
2005 Py_DECREF(joiner);
2006 return NULL;
2007 }
2008 args = PyTuple_New(1);
Fredrik Lundh1296a8d2001-10-21 18:04:11 +00002009 if (!args) {
2010 Py_DECREF(function);
2011 Py_DECREF(joiner);
2012 return NULL;
2013 }
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002014 PyTuple_SET_ITEM(args, 0, list);
2015 result = PyObject_CallObject(function, args);
2016 Py_DECREF(args); /* also removes list */
2017 Py_DECREF(function);
2018#else
2019 result = call(
2020 "string", "join",
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002021 PyTuple_Pack(2, list, joiner)
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002022 );
2023#endif
2024 Py_DECREF(joiner);
2025
2026 return result;
2027}
2028
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00002029static PyObject*
Fredrik Lundh562586e2000-10-03 20:43:34 +00002030pattern_findall(PatternObject* self, PyObject* args, PyObject* kw)
Guido van Rossumb700df92000-03-31 14:59:30 +00002031{
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002032 SRE_STATE state;
2033 PyObject* list;
2034 int status;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002035 Py_ssize_t i, b, e;
Guido van Rossumb700df92000-03-31 14:59:30 +00002036
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002037 PyObject* string;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002038 Py_ssize_t start = 0;
2039 Py_ssize_t end = PY_SSIZE_T_MAX;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002040 static char* kwlist[] = { "source", "pos", "endpos", NULL };
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002041 if (!PyArg_ParseTupleAndKeywords(args, kw, "O|nn:findall", kwlist,
Fredrik Lundh562586e2000-10-03 20:43:34 +00002042 &string, &start, &end))
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002043 return NULL;
Guido van Rossumb700df92000-03-31 14:59:30 +00002044
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002045 string = state_init(&state, self, string, start, end);
2046 if (!string)
2047 return NULL;
Guido van Rossumb700df92000-03-31 14:59:30 +00002048
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002049 list = PyList_New(0);
Fredrik Lundh1296a8d2001-10-21 18:04:11 +00002050 if (!list) {
2051 state_fini(&state);
2052 return NULL;
2053 }
Guido van Rossumb700df92000-03-31 14:59:30 +00002054
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002055 while (state.start <= state.end) {
Guido van Rossumb700df92000-03-31 14:59:30 +00002056
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002057 PyObject* item;
Tim Peters3d563502006-01-21 02:47:53 +00002058
Fredrik Lundhebc37b22000-10-28 19:30:41 +00002059 state_reset(&state);
2060
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002061 state.ptr = state.start;
2062
2063 if (state.charsize == 1) {
2064 status = sre_search(&state, PatternObject_GetCode(self));
2065 } else {
Fredrik Lundh436c3d52000-06-29 08:58:44 +00002066#if defined(HAVE_UNICODE)
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002067 status = sre_usearch(&state, PatternObject_GetCode(self));
Fredrik Lundh436c3d52000-06-29 08:58:44 +00002068#endif
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002069 }
Guido van Rossumb700df92000-03-31 14:59:30 +00002070
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002071 if (status <= 0) {
Fredrik Lundh436c3d52000-06-29 08:58:44 +00002072 if (status == 0)
2073 break;
Fredrik Lundh96ab4652000-08-03 16:29:50 +00002074 pattern_error(status);
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002075 goto error;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002076 }
Tim Peters3d563502006-01-21 02:47:53 +00002077
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002078 /* don't bother to build a match object */
2079 switch (self->groups) {
2080 case 0:
2081 b = STATE_OFFSET(&state, state.start);
2082 e = STATE_OFFSET(&state, state.ptr);
2083 item = PySequence_GetSlice(string, b, e);
2084 if (!item)
2085 goto error;
2086 break;
2087 case 1:
2088 item = state_getslice(&state, 1, string, 1);
2089 if (!item)
2090 goto error;
2091 break;
2092 default:
2093 item = PyTuple_New(self->groups);
2094 if (!item)
2095 goto error;
2096 for (i = 0; i < self->groups; i++) {
2097 PyObject* o = state_getslice(&state, i+1, string, 1);
2098 if (!o) {
2099 Py_DECREF(item);
2100 goto error;
2101 }
2102 PyTuple_SET_ITEM(item, i, o);
2103 }
2104 break;
2105 }
2106
2107 status = PyList_Append(list, item);
2108 Py_DECREF(item);
2109 if (status < 0)
2110 goto error;
2111
2112 if (state.ptr == state.start)
2113 state.start = (void*) ((char*) state.ptr + state.charsize);
2114 else
2115 state.start = state.ptr;
2116
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002117 }
Guido van Rossumb700df92000-03-31 14:59:30 +00002118
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002119 state_fini(&state);
2120 return list;
Guido van Rossumb700df92000-03-31 14:59:30 +00002121
2122error:
Fredrik Lundh75f2d672000-06-29 11:34:28 +00002123 Py_DECREF(list);
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002124 state_fini(&state);
2125 return NULL;
Tim Peters3d563502006-01-21 02:47:53 +00002126
Guido van Rossumb700df92000-03-31 14:59:30 +00002127}
2128
Fredrik Lundh703ce812001-10-24 22:16:30 +00002129#if PY_VERSION_HEX >= 0x02020000
2130static PyObject*
2131pattern_finditer(PatternObject* pattern, PyObject* args)
2132{
2133 PyObject* scanner;
2134 PyObject* search;
2135 PyObject* iterator;
2136
2137 scanner = pattern_scanner(pattern, args);
2138 if (!scanner)
2139 return NULL;
2140
2141 search = PyObject_GetAttrString(scanner, "search");
2142 Py_DECREF(scanner);
2143 if (!search)
2144 return NULL;
2145
2146 iterator = PyCallIter_New(search, Py_None);
2147 Py_DECREF(search);
2148
2149 return iterator;
2150}
2151#endif
2152
Fredrik Lundh971e78b2001-10-20 17:48:46 +00002153static PyObject*
2154pattern_split(PatternObject* self, PyObject* args, PyObject* kw)
2155{
2156 SRE_STATE state;
2157 PyObject* list;
2158 PyObject* item;
2159 int status;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002160 Py_ssize_t n;
2161 Py_ssize_t i;
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002162 void* last;
Fredrik Lundh971e78b2001-10-20 17:48:46 +00002163
2164 PyObject* string;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002165 Py_ssize_t maxsplit = 0;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002166 static char* kwlist[] = { "source", "maxsplit", NULL };
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002167 if (!PyArg_ParseTupleAndKeywords(args, kw, "O|n:split", kwlist,
Fredrik Lundh971e78b2001-10-20 17:48:46 +00002168 &string, &maxsplit))
2169 return NULL;
2170
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002171 string = state_init(&state, self, string, 0, PY_SSIZE_T_MAX);
Fredrik Lundh971e78b2001-10-20 17:48:46 +00002172 if (!string)
2173 return NULL;
2174
2175 list = PyList_New(0);
Fredrik Lundh1296a8d2001-10-21 18:04:11 +00002176 if (!list) {
2177 state_fini(&state);
2178 return NULL;
2179 }
Fredrik Lundh971e78b2001-10-20 17:48:46 +00002180
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002181 n = 0;
2182 last = state.start;
Fredrik Lundh971e78b2001-10-20 17:48:46 +00002183
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002184 while (!maxsplit || n < maxsplit) {
Fredrik Lundh971e78b2001-10-20 17:48:46 +00002185
2186 state_reset(&state);
2187
2188 state.ptr = state.start;
2189
2190 if (state.charsize == 1) {
2191 status = sre_search(&state, PatternObject_GetCode(self));
2192 } else {
2193#if defined(HAVE_UNICODE)
2194 status = sre_usearch(&state, PatternObject_GetCode(self));
2195#endif
2196 }
2197
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002198 if (status <= 0) {
2199 if (status == 0)
2200 break;
2201 pattern_error(status);
2202 goto error;
2203 }
Tim Peters3d563502006-01-21 02:47:53 +00002204
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002205 if (state.start == state.ptr) {
2206 if (last == state.end)
2207 break;
2208 /* skip one character */
2209 state.start = (void*) ((char*) state.ptr + state.charsize);
2210 continue;
2211 }
Fredrik Lundh971e78b2001-10-20 17:48:46 +00002212
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002213 /* get segment before this match */
2214 item = PySequence_GetSlice(
2215 string, STATE_OFFSET(&state, last),
2216 STATE_OFFSET(&state, state.start)
2217 );
2218 if (!item)
2219 goto error;
2220 status = PyList_Append(list, item);
2221 Py_DECREF(item);
2222 if (status < 0)
2223 goto error;
Fredrik Lundh971e78b2001-10-20 17:48:46 +00002224
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002225 /* add groups (if any) */
2226 for (i = 0; i < self->groups; i++) {
2227 item = state_getslice(&state, i+1, string, 0);
Fredrik Lundh971e78b2001-10-20 17:48:46 +00002228 if (!item)
2229 goto error;
2230 status = PyList_Append(list, item);
2231 Py_DECREF(item);
2232 if (status < 0)
2233 goto error;
Fredrik Lundh971e78b2001-10-20 17:48:46 +00002234 }
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002235
2236 n = n + 1;
2237
2238 last = state.start = state.ptr;
2239
Fredrik Lundh971e78b2001-10-20 17:48:46 +00002240 }
2241
Fredrik Lundhf864aa82001-10-22 06:01:56 +00002242 /* get segment following last match (even if empty) */
2243 item = PySequence_GetSlice(
2244 string, STATE_OFFSET(&state, last), state.endpos
2245 );
2246 if (!item)
2247 goto error;
2248 status = PyList_Append(list, item);
2249 Py_DECREF(item);
2250 if (status < 0)
2251 goto error;
Fredrik Lundh971e78b2001-10-20 17:48:46 +00002252
2253 state_fini(&state);
2254 return list;
2255
2256error:
2257 Py_DECREF(list);
2258 state_fini(&state);
2259 return NULL;
Tim Peters3d563502006-01-21 02:47:53 +00002260
Fredrik Lundh971e78b2001-10-20 17:48:46 +00002261}
Fredrik Lundh971e78b2001-10-20 17:48:46 +00002262
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002263static PyObject*
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00002264pattern_subx(PatternObject* self, PyObject* ptemplate, PyObject* string,
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002265 Py_ssize_t count, Py_ssize_t subn)
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002266{
2267 SRE_STATE state;
2268 PyObject* list;
2269 PyObject* item;
2270 PyObject* filter;
2271 PyObject* args;
2272 PyObject* match;
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00002273 void* ptr;
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002274 int status;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002275 Py_ssize_t n;
2276 Py_ssize_t i, b, e;
2277 int bint;
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002278 int filter_is_callable;
2279
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00002280 if (PyCallable_Check(ptemplate)) {
Fredrik Lundhdac58492001-10-21 21:48:30 +00002281 /* sub/subn takes either a function or a template */
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00002282 filter = ptemplate;
Fredrik Lundhdac58492001-10-21 21:48:30 +00002283 Py_INCREF(filter);
2284 filter_is_callable = 1;
2285 } else {
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00002286 /* if not callable, check if it's a literal string */
2287 int literal;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002288 ptr = getstring(ptemplate, &n, &bint);
2289 b = bint;
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00002290 if (ptr) {
2291 if (b == 1) {
Skip Montanaro816a1622006-04-18 11:53:09 +00002292 literal = sre_literal_template((unsigned char *)ptr, n);
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00002293 } else {
2294#if defined(HAVE_UNICODE)
Skip Montanaro816a1622006-04-18 11:53:09 +00002295 literal = sre_uliteral_template((Py_UNICODE *)ptr, n);
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00002296#endif
2297 }
2298 } else {
2299 PyErr_Clear();
2300 literal = 0;
2301 }
2302 if (literal) {
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00002303 filter = ptemplate;
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00002304 Py_INCREF(filter);
2305 filter_is_callable = 0;
2306 } else {
2307 /* not a literal; hand it over to the template compiler */
2308 filter = call(
Neal Norwitz94a9c092006-03-16 06:30:02 +00002309 SRE_PY_MODULE, "_subx",
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00002310 PyTuple_Pack(2, self, ptemplate)
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00002311 );
2312 if (!filter)
2313 return NULL;
2314 filter_is_callable = PyCallable_Check(filter);
2315 }
Fredrik Lundhdac58492001-10-21 21:48:30 +00002316 }
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002317
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002318 string = state_init(&state, self, string, 0, PY_SSIZE_T_MAX);
Fredrik Lundh82b23072001-12-09 16:13:15 +00002319 if (!string) {
2320 Py_DECREF(filter);
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002321 return NULL;
Fredrik Lundh82b23072001-12-09 16:13:15 +00002322 }
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002323
2324 list = PyList_New(0);
Fredrik Lundh1296a8d2001-10-21 18:04:11 +00002325 if (!list) {
Fredrik Lundh82b23072001-12-09 16:13:15 +00002326 Py_DECREF(filter);
Fredrik Lundh1296a8d2001-10-21 18:04:11 +00002327 state_fini(&state);
2328 return NULL;
2329 }
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002330
2331 n = i = 0;
2332
2333 while (!count || n < count) {
2334
2335 state_reset(&state);
2336
2337 state.ptr = state.start;
2338
2339 if (state.charsize == 1) {
2340 status = sre_search(&state, PatternObject_GetCode(self));
2341 } else {
2342#if defined(HAVE_UNICODE)
2343 status = sre_usearch(&state, PatternObject_GetCode(self));
2344#endif
2345 }
2346
2347 if (status <= 0) {
2348 if (status == 0)
2349 break;
2350 pattern_error(status);
2351 goto error;
2352 }
Tim Peters3d563502006-01-21 02:47:53 +00002353
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002354 b = STATE_OFFSET(&state, state.start);
2355 e = STATE_OFFSET(&state, state.ptr);
2356
2357 if (i < b) {
2358 /* get segment before this match */
2359 item = PySequence_GetSlice(string, i, b);
2360 if (!item)
2361 goto error;
2362 status = PyList_Append(list, item);
2363 Py_DECREF(item);
2364 if (status < 0)
2365 goto error;
2366
2367 } else if (i == b && i == e && n > 0)
2368 /* ignore empty match on latest position */
2369 goto next;
2370
2371 if (filter_is_callable) {
Fredrik Lundhdac58492001-10-21 21:48:30 +00002372 /* pass match object through filter */
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002373 match = pattern_new_match(self, &state, 1);
2374 if (!match)
2375 goto error;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002376 args = PyTuple_Pack(1, match);
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002377 if (!args) {
Guido van Rossum4e173842001-12-07 04:25:10 +00002378 Py_DECREF(match);
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002379 goto error;
2380 }
2381 item = PyObject_CallObject(filter, args);
2382 Py_DECREF(args);
2383 Py_DECREF(match);
2384 if (!item)
2385 goto error;
2386 } else {
2387 /* filter is literal string */
2388 item = filter;
Fredrik Lundhdac58492001-10-21 21:48:30 +00002389 Py_INCREF(item);
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002390 }
2391
2392 /* add to list */
Fredrik Lundh6de22ef2001-10-22 21:18:08 +00002393 if (item != Py_None) {
2394 status = PyList_Append(list, item);
2395 Py_DECREF(item);
2396 if (status < 0)
2397 goto error;
2398 }
Tim Peters3d563502006-01-21 02:47:53 +00002399
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002400 i = e;
2401 n = n + 1;
2402
2403next:
2404 /* move on */
2405 if (state.ptr == state.start)
2406 state.start = (void*) ((char*) state.ptr + state.charsize);
2407 else
2408 state.start = state.ptr;
2409
2410 }
2411
2412 /* get segment following last match */
Fredrik Lundhdac58492001-10-21 21:48:30 +00002413 if (i < state.endpos) {
2414 item = PySequence_GetSlice(string, i, state.endpos);
2415 if (!item)
2416 goto error;
2417 status = PyList_Append(list, item);
2418 Py_DECREF(item);
2419 if (status < 0)
2420 goto error;
2421 }
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002422
2423 state_fini(&state);
2424
Guido van Rossum4e173842001-12-07 04:25:10 +00002425 Py_DECREF(filter);
2426
Fredrik Lundhdac58492001-10-21 21:48:30 +00002427 /* convert list to single string (also removes list) */
Michael W. Hudsonb6a45052002-07-31 09:54:24 +00002428 item = join_list(list, self->pattern);
Fredrik Lundhdac58492001-10-21 21:48:30 +00002429
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002430 if (!item)
2431 return NULL;
2432
2433 if (subn)
2434 return Py_BuildValue("Ni", item, n);
2435
2436 return item;
2437
2438error:
2439 Py_DECREF(list);
2440 state_fini(&state);
Fredrik Lundh82b23072001-12-09 16:13:15 +00002441 Py_DECREF(filter);
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002442 return NULL;
Tim Peters3d563502006-01-21 02:47:53 +00002443
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002444}
2445
2446static PyObject*
2447pattern_sub(PatternObject* self, PyObject* args, PyObject* kw)
2448{
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00002449 PyObject* ptemplate;
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002450 PyObject* string;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002451 Py_ssize_t count = 0;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002452 static char* kwlist[] = { "repl", "string", "count", NULL };
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002453 if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|n:sub", kwlist,
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00002454 &ptemplate, &string, &count))
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002455 return NULL;
2456
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00002457 return pattern_subx(self, ptemplate, string, count, 0);
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002458}
2459
2460static PyObject*
2461pattern_subn(PatternObject* self, PyObject* args, PyObject* kw)
2462{
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00002463 PyObject* ptemplate;
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002464 PyObject* string;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002465 Py_ssize_t count = 0;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002466 static char* kwlist[] = { "repl", "string", "count", NULL };
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002467 if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|n:subn", kwlist,
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00002468 &ptemplate, &string, &count))
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002469 return NULL;
2470
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00002471 return pattern_subx(self, ptemplate, string, count, 1);
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002472}
Fredrik Lundhbec95b92001-10-21 16:47:57 +00002473
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +00002474static PyObject*
Georg Brandl964f5972006-05-28 22:38:57 +00002475pattern_copy(PatternObject* self, PyObject *unused)
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +00002476{
Fredrik Lundhd89a2e72001-07-03 20:32:36 +00002477#ifdef USE_BUILTIN_COPY
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +00002478 PatternObject* copy;
2479 int offset;
2480
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +00002481 copy = PyObject_NEW_VAR(PatternObject, &Pattern_Type, self->codesize);
2482 if (!copy)
2483 return NULL;
2484
2485 offset = offsetof(PatternObject, groups);
2486
2487 Py_XINCREF(self->groupindex);
2488 Py_XINCREF(self->indexgroup);
2489 Py_XINCREF(self->pattern);
2490
2491 memcpy((char*) copy + offset, (char*) self + offset,
2492 sizeof(PatternObject) + self->codesize * sizeof(SRE_CODE) - offset);
Raymond Hettinger027bb632004-05-31 03:09:25 +00002493 copy->weakreflist = NULL;
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +00002494
2495 return (PyObject*) copy;
2496#else
2497 PyErr_SetString(PyExc_TypeError, "cannot copy this pattern object");
2498 return NULL;
2499#endif
2500}
2501
2502static PyObject*
Georg Brandlfbef5882006-05-28 22:14:04 +00002503pattern_deepcopy(PatternObject* self, PyObject* memo)
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +00002504{
Fredrik Lundhd89a2e72001-07-03 20:32:36 +00002505#ifdef USE_BUILTIN_COPY
2506 PatternObject* copy;
Tim Peters3d563502006-01-21 02:47:53 +00002507
Georg Brandlfbef5882006-05-28 22:14:04 +00002508 copy = (PatternObject*) pattern_copy(self);
Fredrik Lundhd89a2e72001-07-03 20:32:36 +00002509 if (!copy)
2510 return NULL;
2511
2512 if (!deepcopy(&copy->groupindex, memo) ||
2513 !deepcopy(&copy->indexgroup, memo) ||
2514 !deepcopy(&copy->pattern, memo)) {
2515 Py_DECREF(copy);
2516 return NULL;
2517 }
2518
2519#else
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +00002520 PyErr_SetString(PyExc_TypeError, "cannot deepcopy this pattern object");
2521 return NULL;
Fredrik Lundhd89a2e72001-07-03 20:32:36 +00002522#endif
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +00002523}
2524
Raymond Hettinger94478742004-09-24 04:31:19 +00002525PyDoc_STRVAR(pattern_match_doc,
2526"match(string[, pos[, endpos]]) --> match object or None.\n\
2527 Matches zero or more characters at the beginning of the string");
2528
2529PyDoc_STRVAR(pattern_search_doc,
2530"search(string[, pos[, endpos]]) --> match object or None.\n\
2531 Scan through string looking for a match, and return a corresponding\n\
2532 MatchObject instance. Return None if no position in the string matches.");
2533
2534PyDoc_STRVAR(pattern_split_doc,
2535"split(string[, maxsplit = 0]) --> list.\n\
2536 Split string by the occurrences of pattern.");
2537
2538PyDoc_STRVAR(pattern_findall_doc,
2539"findall(string[, pos[, endpos]]) --> list.\n\
2540 Return a list of all non-overlapping matches of pattern in string.");
2541
2542PyDoc_STRVAR(pattern_finditer_doc,
2543"finditer(string[, pos[, endpos]]) --> iterator.\n\
2544 Return an iterator over all non-overlapping matches for the \n\
2545 RE pattern in string. For each match, the iterator returns a\n\
2546 match object.");
2547
2548PyDoc_STRVAR(pattern_sub_doc,
2549"sub(repl, string[, count = 0]) --> newstring\n\
2550 Return the string obtained by replacing the leftmost non-overlapping\n\
Tim Peters3d563502006-01-21 02:47:53 +00002551 occurrences of pattern in string by the replacement repl.");
Raymond Hettinger94478742004-09-24 04:31:19 +00002552
2553PyDoc_STRVAR(pattern_subn_doc,
2554"subn(repl, string[, count = 0]) --> (newstring, number of subs)\n\
2555 Return the tuple (new_string, number_of_subs_made) found by replacing\n\
2556 the leftmost non-overlapping occurrences of pattern with the\n\
Tim Peters3d563502006-01-21 02:47:53 +00002557 replacement repl.");
Raymond Hettinger94478742004-09-24 04:31:19 +00002558
2559PyDoc_STRVAR(pattern_doc, "Compiled regular expression objects");
2560
Fredrik Lundh75f2d672000-06-29 11:34:28 +00002561static PyMethodDef pattern_methods[] = {
Tim Peters3d563502006-01-21 02:47:53 +00002562 {"match", (PyCFunction) pattern_match, METH_VARARGS|METH_KEYWORDS,
Raymond Hettinger94478742004-09-24 04:31:19 +00002563 pattern_match_doc},
Tim Peters3d563502006-01-21 02:47:53 +00002564 {"search", (PyCFunction) pattern_search, METH_VARARGS|METH_KEYWORDS,
Raymond Hettinger94478742004-09-24 04:31:19 +00002565 pattern_search_doc},
2566 {"sub", (PyCFunction) pattern_sub, METH_VARARGS|METH_KEYWORDS,
2567 pattern_sub_doc},
2568 {"subn", (PyCFunction) pattern_subn, METH_VARARGS|METH_KEYWORDS,
2569 pattern_subn_doc},
Tim Peters3d563502006-01-21 02:47:53 +00002570 {"split", (PyCFunction) pattern_split, METH_VARARGS|METH_KEYWORDS,
Raymond Hettinger94478742004-09-24 04:31:19 +00002571 pattern_split_doc},
Tim Peters3d563502006-01-21 02:47:53 +00002572 {"findall", (PyCFunction) pattern_findall, METH_VARARGS|METH_KEYWORDS,
Raymond Hettinger94478742004-09-24 04:31:19 +00002573 pattern_findall_doc},
Fredrik Lundh703ce812001-10-24 22:16:30 +00002574#if PY_VERSION_HEX >= 0x02020000
Raymond Hettinger94478742004-09-24 04:31:19 +00002575 {"finditer", (PyCFunction) pattern_finditer, METH_VARARGS,
2576 pattern_finditer_doc},
Fredrik Lundh703ce812001-10-24 22:16:30 +00002577#endif
Fredrik Lundh562586e2000-10-03 20:43:34 +00002578 {"scanner", (PyCFunction) pattern_scanner, METH_VARARGS},
Georg Brandlfbef5882006-05-28 22:14:04 +00002579 {"__copy__", (PyCFunction) pattern_copy, METH_NOARGS},
2580 {"__deepcopy__", (PyCFunction) pattern_deepcopy, METH_O},
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002581 {NULL, NULL}
Guido van Rossumb700df92000-03-31 14:59:30 +00002582};
2583
Tim Peters3d563502006-01-21 02:47:53 +00002584static PyObject*
Fredrik Lundh75f2d672000-06-29 11:34:28 +00002585pattern_getattr(PatternObject* self, char* name)
Guido van Rossumb700df92000-03-31 14:59:30 +00002586{
2587 PyObject* res;
2588
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002589 res = Py_FindMethod(pattern_methods, (PyObject*) self, name);
Guido van Rossumb700df92000-03-31 14:59:30 +00002590
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002591 if (res)
2592 return res;
Guido van Rossumb700df92000-03-31 14:59:30 +00002593
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002594 PyErr_Clear();
Guido van Rossumb700df92000-03-31 14:59:30 +00002595
2596 /* attributes */
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002597 if (!strcmp(name, "pattern")) {
Guido van Rossumb700df92000-03-31 14:59:30 +00002598 Py_INCREF(self->pattern);
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002599 return self->pattern;
Guido van Rossumb700df92000-03-31 14:59:30 +00002600 }
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00002601
2602 if (!strcmp(name, "flags"))
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002603 return Py_BuildValue("i", self->flags);
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00002604
Fredrik Lundh01016fe2000-06-30 00:27:46 +00002605 if (!strcmp(name, "groups"))
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002606 return Py_BuildValue("i", self->groups);
Fredrik Lundh01016fe2000-06-30 00:27:46 +00002607
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002608 if (!strcmp(name, "groupindex") && self->groupindex) {
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00002609 Py_INCREF(self->groupindex);
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002610 return self->groupindex;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00002611 }
2612
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002613 PyErr_SetString(PyExc_AttributeError, name);
2614 return NULL;
Guido van Rossumb700df92000-03-31 14:59:30 +00002615}
2616
2617statichere PyTypeObject Pattern_Type = {
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002618 PyObject_HEAD_INIT(NULL)
Fredrik Lundh82b23072001-12-09 16:13:15 +00002619 0, "_" SRE_MODULE ".SRE_Pattern",
Fredrik Lundh6f013982000-07-03 18:44:21 +00002620 sizeof(PatternObject), sizeof(SRE_CODE),
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002621 (destructor)pattern_dealloc, /*tp_dealloc*/
2622 0, /*tp_print*/
Raymond Hettinger027bb632004-05-31 03:09:25 +00002623 (getattrfunc)pattern_getattr, /*tp_getattr*/
2624 0, /* tp_setattr */
2625 0, /* tp_compare */
2626 0, /* tp_repr */
2627 0, /* tp_as_number */
2628 0, /* tp_as_sequence */
2629 0, /* tp_as_mapping */
2630 0, /* tp_hash */
2631 0, /* tp_call */
2632 0, /* tp_str */
2633 0, /* tp_getattro */
2634 0, /* tp_setattro */
2635 0, /* tp_as_buffer */
2636 Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
Raymond Hettinger94478742004-09-24 04:31:19 +00002637 pattern_doc, /* tp_doc */
Raymond Hettinger027bb632004-05-31 03:09:25 +00002638 0, /* tp_traverse */
2639 0, /* tp_clear */
2640 0, /* tp_richcompare */
2641 offsetof(PatternObject, weakreflist), /* tp_weaklistoffset */
Guido van Rossumb700df92000-03-31 14:59:30 +00002642};
2643
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00002644static PyObject *
2645_compile(PyObject* self_, PyObject* args)
2646{
2647 /* "compile" pattern descriptor to pattern object */
2648
2649 PatternObject* self;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002650 Py_ssize_t i, n;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00002651
2652 PyObject* pattern;
2653 int flags = 0;
2654 PyObject* code;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002655 Py_ssize_t groups = 0;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00002656 PyObject* groupindex = NULL;
2657 PyObject* indexgroup = NULL;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002658 if (!PyArg_ParseTuple(args, "OiO!|nOO", &pattern, &flags,
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00002659 &PyList_Type, &code, &groups,
2660 &groupindex, &indexgroup))
2661 return NULL;
2662
2663 n = PyList_GET_SIZE(code);
2664
2665 self = PyObject_NEW_VAR(PatternObject, &Pattern_Type, n);
2666 if (!self)
2667 return NULL;
2668
2669 self->codesize = n;
2670
2671 for (i = 0; i < n; i++) {
2672 PyObject *o = PyList_GET_ITEM(code, i);
2673 unsigned long value = PyInt_Check(o) ? (unsigned long)PyInt_AsLong(o)
2674 : PyLong_AsUnsignedLong(o);
2675 self->code[i] = (SRE_CODE) value;
2676 if ((unsigned long) self->code[i] != value) {
2677 PyErr_SetString(PyExc_OverflowError,
2678 "regular expression code size limit exceeded");
2679 break;
2680 }
2681 }
2682
2683 if (PyErr_Occurred()) {
2684 PyObject_DEL(self);
2685 return NULL;
2686 }
2687
2688 Py_INCREF(pattern);
2689 self->pattern = pattern;
2690
2691 self->flags = flags;
2692
2693 self->groups = groups;
2694
2695 Py_XINCREF(groupindex);
2696 self->groupindex = groupindex;
2697
2698 Py_XINCREF(indexgroup);
2699 self->indexgroup = indexgroup;
2700
2701 self->weakreflist = NULL;
2702
2703 return (PyObject*) self;
2704}
2705
Guido van Rossumb700df92000-03-31 14:59:30 +00002706/* -------------------------------------------------------------------- */
2707/* match methods */
2708
2709static void
Fredrik Lundh75f2d672000-06-29 11:34:28 +00002710match_dealloc(MatchObject* self)
Guido van Rossumb700df92000-03-31 14:59:30 +00002711{
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002712 Py_XDECREF(self->regs);
2713 Py_XDECREF(self->string);
2714 Py_DECREF(self->pattern);
2715 PyObject_DEL(self);
Guido van Rossumb700df92000-03-31 14:59:30 +00002716}
2717
2718static PyObject*
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002719match_getslice_by_index(MatchObject* self, Py_ssize_t index, PyObject* def)
Guido van Rossumb700df92000-03-31 14:59:30 +00002720{
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002721 if (index < 0 || index >= self->groups) {
2722 /* raise IndexError if we were given a bad group number */
2723 PyErr_SetString(
2724 PyExc_IndexError,
2725 "no such group"
2726 );
2727 return NULL;
2728 }
Guido van Rossumb700df92000-03-31 14:59:30 +00002729
Fredrik Lundh6f013982000-07-03 18:44:21 +00002730 index *= 2;
2731
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002732 if (self->string == Py_None || self->mark[index] < 0) {
2733 /* return default value if the string or group is undefined */
2734 Py_INCREF(def);
2735 return def;
2736 }
Guido van Rossumb700df92000-03-31 14:59:30 +00002737
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002738 return PySequence_GetSlice(
2739 self->string, self->mark[index], self->mark[index+1]
2740 );
Guido van Rossumb700df92000-03-31 14:59:30 +00002741}
2742
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002743static Py_ssize_t
Fredrik Lundh75f2d672000-06-29 11:34:28 +00002744match_getindex(MatchObject* self, PyObject* index)
Guido van Rossumb700df92000-03-31 14:59:30 +00002745{
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002746 Py_ssize_t i;
Guido van Rossumb700df92000-03-31 14:59:30 +00002747
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002748 if (PyInt_Check(index))
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002749 return PyInt_AsSsize_t(index);
Guido van Rossumb700df92000-03-31 14:59:30 +00002750
Fredrik Lundh6f013982000-07-03 18:44:21 +00002751 i = -1;
2752
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002753 if (self->pattern->groupindex) {
2754 index = PyObject_GetItem(self->pattern->groupindex, index);
2755 if (index) {
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002756 if (PyInt_Check(index) || PyLong_Check(index))
2757 i = PyInt_AsSsize_t(index);
Fredrik Lundh6f013982000-07-03 18:44:21 +00002758 Py_DECREF(index);
2759 } else
2760 PyErr_Clear();
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002761 }
Fredrik Lundh6f013982000-07-03 18:44:21 +00002762
2763 return i;
Fredrik Lundh436c3d52000-06-29 08:58:44 +00002764}
2765
2766static PyObject*
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +00002767match_getslice(MatchObject* self, PyObject* index, PyObject* def)
Fredrik Lundh436c3d52000-06-29 08:58:44 +00002768{
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002769 return match_getslice_by_index(self, match_getindex(self, index), def);
Guido van Rossumb700df92000-03-31 14:59:30 +00002770}
2771
2772static PyObject*
Georg Brandlfbef5882006-05-28 22:14:04 +00002773match_expand(MatchObject* self, PyObject* ptemplate)
Fredrik Lundh5644b7f2000-09-21 17:03:25 +00002774{
Fredrik Lundh5644b7f2000-09-21 17:03:25 +00002775 /* delegate to Python code */
2776 return call(
Neal Norwitz94a9c092006-03-16 06:30:02 +00002777 SRE_PY_MODULE, "_expand",
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00002778 PyTuple_Pack(3, self->pattern, self, ptemplate)
Fredrik Lundh5644b7f2000-09-21 17:03:25 +00002779 );
2780}
2781
2782static PyObject*
Fredrik Lundh75f2d672000-06-29 11:34:28 +00002783match_group(MatchObject* self, PyObject* args)
Guido van Rossumb700df92000-03-31 14:59:30 +00002784{
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002785 PyObject* result;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002786 Py_ssize_t i, size;
Guido van Rossumb700df92000-03-31 14:59:30 +00002787
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002788 size = PyTuple_GET_SIZE(args);
Guido van Rossumb700df92000-03-31 14:59:30 +00002789
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002790 switch (size) {
2791 case 0:
2792 result = match_getslice(self, Py_False, Py_None);
2793 break;
2794 case 1:
2795 result = match_getslice(self, PyTuple_GET_ITEM(args, 0), Py_None);
2796 break;
2797 default:
2798 /* fetch multiple items */
2799 result = PyTuple_New(size);
2800 if (!result)
2801 return NULL;
2802 for (i = 0; i < size; i++) {
2803 PyObject* item = match_getslice(
Fredrik Lundhdf02d0b2000-06-30 07:08:20 +00002804 self, PyTuple_GET_ITEM(args, i), Py_None
2805 );
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002806 if (!item) {
2807 Py_DECREF(result);
2808 return NULL;
2809 }
2810 PyTuple_SET_ITEM(result, i, item);
2811 }
2812 break;
2813 }
2814 return result;
Guido van Rossumb700df92000-03-31 14:59:30 +00002815}
2816
2817static PyObject*
Fredrik Lundh562586e2000-10-03 20:43:34 +00002818match_groups(MatchObject* self, PyObject* args, PyObject* kw)
Guido van Rossumb700df92000-03-31 14:59:30 +00002819{
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002820 PyObject* result;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002821 Py_ssize_t index;
Guido van Rossumb700df92000-03-31 14:59:30 +00002822
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002823 PyObject* def = Py_None;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002824 static char* kwlist[] = { "default", NULL };
Fredrik Lundh562586e2000-10-03 20:43:34 +00002825 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:groups", kwlist, &def))
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002826 return NULL;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00002827
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002828 result = PyTuple_New(self->groups-1);
2829 if (!result)
2830 return NULL;
Guido van Rossumb700df92000-03-31 14:59:30 +00002831
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002832 for (index = 1; index < self->groups; index++) {
2833 PyObject* item;
2834 item = match_getslice_by_index(self, index, def);
2835 if (!item) {
2836 Py_DECREF(result);
2837 return NULL;
2838 }
2839 PyTuple_SET_ITEM(result, index-1, item);
2840 }
Guido van Rossumb700df92000-03-31 14:59:30 +00002841
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002842 return result;
Guido van Rossumb700df92000-03-31 14:59:30 +00002843}
2844
2845static PyObject*
Fredrik Lundh562586e2000-10-03 20:43:34 +00002846match_groupdict(MatchObject* self, PyObject* args, PyObject* kw)
Guido van Rossumb700df92000-03-31 14:59:30 +00002847{
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002848 PyObject* result;
2849 PyObject* keys;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002850 Py_ssize_t index;
Guido van Rossumb700df92000-03-31 14:59:30 +00002851
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002852 PyObject* def = Py_None;
Martin v. Löwis15e62742006-02-27 16:46:16 +00002853 static char* kwlist[] = { "default", NULL };
Fredrik Lundh770617b2001-01-14 15:06:11 +00002854 if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:groupdict", kwlist, &def))
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002855 return NULL;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00002856
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002857 result = PyDict_New();
2858 if (!result || !self->pattern->groupindex)
2859 return result;
Guido van Rossumb700df92000-03-31 14:59:30 +00002860
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002861 keys = PyMapping_Keys(self->pattern->groupindex);
Fredrik Lundh770617b2001-01-14 15:06:11 +00002862 if (!keys)
2863 goto failed;
Guido van Rossumb700df92000-03-31 14:59:30 +00002864
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002865 for (index = 0; index < PyList_GET_SIZE(keys); index++) {
Fredrik Lundh770617b2001-01-14 15:06:11 +00002866 int status;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002867 PyObject* key;
Fredrik Lundh770617b2001-01-14 15:06:11 +00002868 PyObject* value;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002869 key = PyList_GET_ITEM(keys, index);
Fredrik Lundh770617b2001-01-14 15:06:11 +00002870 if (!key)
2871 goto failed;
2872 value = match_getslice(self, key, def);
2873 if (!value) {
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002874 Py_DECREF(key);
Fredrik Lundh770617b2001-01-14 15:06:11 +00002875 goto failed;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002876 }
Fredrik Lundh770617b2001-01-14 15:06:11 +00002877 status = PyDict_SetItem(result, key, value);
2878 Py_DECREF(value);
2879 if (status < 0)
2880 goto failed;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002881 }
Guido van Rossumb700df92000-03-31 14:59:30 +00002882
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002883 Py_DECREF(keys);
Guido van Rossumb700df92000-03-31 14:59:30 +00002884
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002885 return result;
Fredrik Lundh770617b2001-01-14 15:06:11 +00002886
2887failed:
Neal Norwitz60da3162006-03-07 04:48:24 +00002888 Py_XDECREF(keys);
Fredrik Lundh770617b2001-01-14 15:06:11 +00002889 Py_DECREF(result);
2890 return NULL;
Guido van Rossumb700df92000-03-31 14:59:30 +00002891}
2892
2893static PyObject*
Fredrik Lundh75f2d672000-06-29 11:34:28 +00002894match_start(MatchObject* self, PyObject* args)
Guido van Rossumb700df92000-03-31 14:59:30 +00002895{
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002896 Py_ssize_t index;
Fredrik Lundh436c3d52000-06-29 08:58:44 +00002897
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002898 PyObject* index_ = Py_False; /* zero */
Georg Brandl96a8c392006-05-29 21:04:52 +00002899 if (!PyArg_UnpackTuple(args, "start", 0, 1, &index_))
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002900 return NULL;
Guido van Rossumb700df92000-03-31 14:59:30 +00002901
Fredrik Lundh75f2d672000-06-29 11:34:28 +00002902 index = match_getindex(self, index_);
Fredrik Lundh436c3d52000-06-29 08:58:44 +00002903
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002904 if (index < 0 || index >= self->groups) {
2905 PyErr_SetString(
2906 PyExc_IndexError,
2907 "no such group"
2908 );
2909 return NULL;
2910 }
Guido van Rossumb700df92000-03-31 14:59:30 +00002911
Fredrik Lundh510c97b2000-09-02 16:36:57 +00002912 /* mark is -1 if group is undefined */
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002913 return Py_BuildValue("i", self->mark[index*2]);
Guido van Rossumb700df92000-03-31 14:59:30 +00002914}
2915
2916static PyObject*
Fredrik Lundh75f2d672000-06-29 11:34:28 +00002917match_end(MatchObject* self, PyObject* args)
Guido van Rossumb700df92000-03-31 14:59:30 +00002918{
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002919 Py_ssize_t index;
Fredrik Lundh436c3d52000-06-29 08:58:44 +00002920
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002921 PyObject* index_ = Py_False; /* zero */
Georg Brandl96a8c392006-05-29 21:04:52 +00002922 if (!PyArg_UnpackTuple(args, "end", 0, 1, &index_))
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002923 return NULL;
Guido van Rossumb700df92000-03-31 14:59:30 +00002924
Fredrik Lundh75f2d672000-06-29 11:34:28 +00002925 index = match_getindex(self, index_);
Fredrik Lundh436c3d52000-06-29 08:58:44 +00002926
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002927 if (index < 0 || index >= self->groups) {
2928 PyErr_SetString(
2929 PyExc_IndexError,
2930 "no such group"
2931 );
2932 return NULL;
2933 }
Guido van Rossumb700df92000-03-31 14:59:30 +00002934
Fredrik Lundh510c97b2000-09-02 16:36:57 +00002935 /* mark is -1 if group is undefined */
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002936 return Py_BuildValue("i", self->mark[index*2+1]);
2937}
2938
2939LOCAL(PyObject*)
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002940_pair(Py_ssize_t i1, Py_ssize_t i2)
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002941{
2942 PyObject* pair;
2943 PyObject* item;
2944
2945 pair = PyTuple_New(2);
2946 if (!pair)
2947 return NULL;
2948
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002949 item = PyInt_FromSsize_t(i1);
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002950 if (!item)
2951 goto error;
2952 PyTuple_SET_ITEM(pair, 0, item);
2953
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002954 item = PyInt_FromSsize_t(i2);
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002955 if (!item)
2956 goto error;
2957 PyTuple_SET_ITEM(pair, 1, item);
2958
2959 return pair;
2960
2961 error:
2962 Py_DECREF(pair);
2963 return NULL;
Guido van Rossumb700df92000-03-31 14:59:30 +00002964}
2965
2966static PyObject*
Fredrik Lundh75f2d672000-06-29 11:34:28 +00002967match_span(MatchObject* self, PyObject* args)
Guido van Rossumb700df92000-03-31 14:59:30 +00002968{
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002969 Py_ssize_t index;
Fredrik Lundh436c3d52000-06-29 08:58:44 +00002970
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002971 PyObject* index_ = Py_False; /* zero */
Georg Brandl96a8c392006-05-29 21:04:52 +00002972 if (!PyArg_UnpackTuple(args, "span", 0, 1, &index_))
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002973 return NULL;
Guido van Rossumb700df92000-03-31 14:59:30 +00002974
Fredrik Lundh75f2d672000-06-29 11:34:28 +00002975 index = match_getindex(self, index_);
Fredrik Lundh436c3d52000-06-29 08:58:44 +00002976
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002977 if (index < 0 || index >= self->groups) {
2978 PyErr_SetString(
2979 PyExc_IndexError,
2980 "no such group"
2981 );
2982 return NULL;
2983 }
Guido van Rossumb700df92000-03-31 14:59:30 +00002984
Fredrik Lundh510c97b2000-09-02 16:36:57 +00002985 /* marks are -1 if group is undefined */
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002986 return _pair(self->mark[index*2], self->mark[index*2+1]);
2987}
2988
2989static PyObject*
2990match_regs(MatchObject* self)
2991{
2992 PyObject* regs;
2993 PyObject* item;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00002994 Py_ssize_t index;
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00002995
2996 regs = PyTuple_New(self->groups);
2997 if (!regs)
2998 return NULL;
2999
3000 for (index = 0; index < self->groups; index++) {
3001 item = _pair(self->mark[index*2], self->mark[index*2+1]);
3002 if (!item) {
3003 Py_DECREF(regs);
3004 return NULL;
3005 }
3006 PyTuple_SET_ITEM(regs, index, item);
3007 }
3008
3009 Py_INCREF(regs);
3010 self->regs = regs;
3011
3012 return regs;
Guido van Rossumb700df92000-03-31 14:59:30 +00003013}
3014
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +00003015static PyObject*
Georg Brandl964f5972006-05-28 22:38:57 +00003016match_copy(MatchObject* self, PyObject *unused)
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +00003017{
Fredrik Lundhd89a2e72001-07-03 20:32:36 +00003018#ifdef USE_BUILTIN_COPY
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +00003019 MatchObject* copy;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00003020 Py_ssize_t slots, offset;
Tim Peters3d563502006-01-21 02:47:53 +00003021
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +00003022 slots = 2 * (self->pattern->groups+1);
3023
3024 copy = PyObject_NEW_VAR(MatchObject, &Match_Type, slots);
3025 if (!copy)
3026 return NULL;
3027
3028 /* this value a constant, but any compiler should be able to
3029 figure that out all by itself */
3030 offset = offsetof(MatchObject, string);
3031
3032 Py_XINCREF(self->pattern);
3033 Py_XINCREF(self->string);
3034 Py_XINCREF(self->regs);
3035
3036 memcpy((char*) copy + offset, (char*) self + offset,
Neal Norwitza6d80fa2006-06-12 03:05:40 +00003037 sizeof(MatchObject) + slots * sizeof(Py_ssize_t) - offset);
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +00003038
3039 return (PyObject*) copy;
3040#else
Fredrik Lundhd89a2e72001-07-03 20:32:36 +00003041 PyErr_SetString(PyExc_TypeError, "cannot copy this match object");
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +00003042 return NULL;
3043#endif
3044}
3045
3046static PyObject*
Georg Brandlfbef5882006-05-28 22:14:04 +00003047match_deepcopy(MatchObject* self, PyObject* memo)
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +00003048{
Fredrik Lundhd89a2e72001-07-03 20:32:36 +00003049#ifdef USE_BUILTIN_COPY
3050 MatchObject* copy;
Tim Peters3d563502006-01-21 02:47:53 +00003051
Georg Brandlfbef5882006-05-28 22:14:04 +00003052 copy = (MatchObject*) match_copy(self);
Fredrik Lundhd89a2e72001-07-03 20:32:36 +00003053 if (!copy)
3054 return NULL;
3055
3056 if (!deepcopy((PyObject**) &copy->pattern, memo) ||
3057 !deepcopy(&copy->string, memo) ||
3058 !deepcopy(&copy->regs, memo)) {
3059 Py_DECREF(copy);
3060 return NULL;
3061 }
3062
3063#else
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +00003064 PyErr_SetString(PyExc_TypeError, "cannot deepcopy this match object");
3065 return NULL;
Fredrik Lundhd89a2e72001-07-03 20:32:36 +00003066#endif
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +00003067}
3068
Fredrik Lundh75f2d672000-06-29 11:34:28 +00003069static PyMethodDef match_methods[] = {
Fredrik Lundh562586e2000-10-03 20:43:34 +00003070 {"group", (PyCFunction) match_group, METH_VARARGS},
3071 {"start", (PyCFunction) match_start, METH_VARARGS},
3072 {"end", (PyCFunction) match_end, METH_VARARGS},
3073 {"span", (PyCFunction) match_span, METH_VARARGS},
3074 {"groups", (PyCFunction) match_groups, METH_VARARGS|METH_KEYWORDS},
3075 {"groupdict", (PyCFunction) match_groupdict, METH_VARARGS|METH_KEYWORDS},
Georg Brandlfbef5882006-05-28 22:14:04 +00003076 {"expand", (PyCFunction) match_expand, METH_O},
3077 {"__copy__", (PyCFunction) match_copy, METH_NOARGS},
3078 {"__deepcopy__", (PyCFunction) match_deepcopy, METH_O},
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003079 {NULL, NULL}
Guido van Rossumb700df92000-03-31 14:59:30 +00003080};
3081
Tim Peters3d563502006-01-21 02:47:53 +00003082static PyObject*
Fredrik Lundh75f2d672000-06-29 11:34:28 +00003083match_getattr(MatchObject* self, char* name)
Guido van Rossumb700df92000-03-31 14:59:30 +00003084{
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003085 PyObject* res;
Guido van Rossumb700df92000-03-31 14:59:30 +00003086
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003087 res = Py_FindMethod(match_methods, (PyObject*) self, name);
3088 if (res)
3089 return res;
Guido van Rossumb700df92000-03-31 14:59:30 +00003090
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003091 PyErr_Clear();
Guido van Rossumb700df92000-03-31 14:59:30 +00003092
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003093 if (!strcmp(name, "lastindex")) {
Fredrik Lundh6f013982000-07-03 18:44:21 +00003094 if (self->lastindex >= 0)
3095 return Py_BuildValue("i", self->lastindex);
Fredrik Lundhc2301732000-07-02 22:25:39 +00003096 Py_INCREF(Py_None);
3097 return Py_None;
3098 }
3099
3100 if (!strcmp(name, "lastgroup")) {
Fredrik Lundh6f013982000-07-03 18:44:21 +00003101 if (self->pattern->indexgroup && self->lastindex >= 0) {
Fredrik Lundhc2301732000-07-02 22:25:39 +00003102 PyObject* result = PySequence_GetItem(
Fredrik Lundh6f013982000-07-03 18:44:21 +00003103 self->pattern->indexgroup, self->lastindex
Fredrik Lundhc2301732000-07-02 22:25:39 +00003104 );
3105 if (result)
3106 return result;
3107 PyErr_Clear();
3108 }
3109 Py_INCREF(Py_None);
3110 return Py_None;
3111 }
3112
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003113 if (!strcmp(name, "string")) {
3114 if (self->string) {
3115 Py_INCREF(self->string);
3116 return self->string;
3117 } else {
3118 Py_INCREF(Py_None);
3119 return Py_None;
3120 }
Guido van Rossumb700df92000-03-31 14:59:30 +00003121 }
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003122
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003123 if (!strcmp(name, "regs")) {
3124 if (self->regs) {
3125 Py_INCREF(self->regs);
3126 return self->regs;
3127 } else
3128 return match_regs(self);
3129 }
3130
3131 if (!strcmp(name, "re")) {
Guido van Rossumb700df92000-03-31 14:59:30 +00003132 Py_INCREF(self->pattern);
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003133 return (PyObject*) self->pattern;
Guido van Rossumb700df92000-03-31 14:59:30 +00003134 }
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003135
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003136 if (!strcmp(name, "pos"))
3137 return Py_BuildValue("i", self->pos);
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003138
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003139 if (!strcmp(name, "endpos"))
3140 return Py_BuildValue("i", self->endpos);
Guido van Rossumb700df92000-03-31 14:59:30 +00003141
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003142 PyErr_SetString(PyExc_AttributeError, name);
3143 return NULL;
Guido van Rossumb700df92000-03-31 14:59:30 +00003144}
3145
3146/* FIXME: implement setattr("string", None) as a special case (to
3147 detach the associated string, if any */
3148
3149statichere PyTypeObject Match_Type = {
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003150 PyObject_HEAD_INIT(NULL)
Fredrik Lundh82b23072001-12-09 16:13:15 +00003151 0, "_" SRE_MODULE ".SRE_Match",
Neal Norwitza6d80fa2006-06-12 03:05:40 +00003152 sizeof(MatchObject), sizeof(Py_ssize_t),
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003153 (destructor)match_dealloc, /*tp_dealloc*/
3154 0, /*tp_print*/
3155 (getattrfunc)match_getattr /*tp_getattr*/
Guido van Rossumb700df92000-03-31 14:59:30 +00003156};
3157
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00003158static PyObject*
3159pattern_new_match(PatternObject* pattern, SRE_STATE* state, int status)
3160{
3161 /* create match object (from state object) */
3162
3163 MatchObject* match;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00003164 Py_ssize_t i, j;
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00003165 char* base;
3166 int n;
3167
3168 if (status > 0) {
3169
3170 /* create match object (with room for extra group marks) */
3171 match = PyObject_NEW_VAR(MatchObject, &Match_Type,
3172 2*(pattern->groups+1));
3173 if (!match)
3174 return NULL;
3175
3176 Py_INCREF(pattern);
3177 match->pattern = pattern;
3178
3179 Py_INCREF(state->string);
3180 match->string = state->string;
3181
3182 match->regs = NULL;
3183 match->groups = pattern->groups+1;
3184
3185 /* fill in group slices */
3186
3187 base = (char*) state->beginning;
3188 n = state->charsize;
3189
3190 match->mark[0] = ((char*) state->start - base) / n;
3191 match->mark[1] = ((char*) state->ptr - base) / n;
3192
3193 for (i = j = 0; i < pattern->groups; i++, j+=2)
3194 if (j+1 <= state->lastmark && state->mark[j] && state->mark[j+1]) {
3195 match->mark[j+2] = ((char*) state->mark[j] - base) / n;
3196 match->mark[j+3] = ((char*) state->mark[j+1] - base) / n;
3197 } else
3198 match->mark[j+2] = match->mark[j+3] = -1; /* undefined */
3199
3200 match->pos = state->pos;
3201 match->endpos = state->endpos;
3202
3203 match->lastindex = state->lastindex;
3204
3205 return (PyObject*) match;
3206
3207 } else if (status == 0) {
3208
3209 /* no match */
3210 Py_INCREF(Py_None);
3211 return Py_None;
3212
3213 }
3214
3215 /* internal error */
3216 pattern_error(status);
3217 return NULL;
3218}
3219
3220
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003221/* -------------------------------------------------------------------- */
Fredrik Lundhbe2211e2000-06-29 16:57:40 +00003222/* scanner methods (experimental) */
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003223
3224static void
Fredrik Lundhbe2211e2000-06-29 16:57:40 +00003225scanner_dealloc(ScannerObject* self)
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003226{
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003227 state_fini(&self->state);
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003228 Py_DECREF(self->pattern);
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003229 PyObject_DEL(self);
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003230}
3231
3232static PyObject*
Georg Brandl964f5972006-05-28 22:38:57 +00003233scanner_match(ScannerObject* self, PyObject *unused)
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003234{
3235 SRE_STATE* state = &self->state;
3236 PyObject* match;
3237 int status;
3238
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00003239 state_reset(state);
3240
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003241 state->ptr = state->start;
3242
3243 if (state->charsize == 1) {
Gustavo Niemeyer2cbdc2a2003-12-13 20:32:08 +00003244 status = sre_match(state, PatternObject_GetCode(self->pattern));
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003245 } else {
Fredrik Lundh436c3d52000-06-29 08:58:44 +00003246#if defined(HAVE_UNICODE)
Gustavo Niemeyer2cbdc2a2003-12-13 20:32:08 +00003247 status = sre_umatch(state, PatternObject_GetCode(self->pattern));
Fredrik Lundh436c3d52000-06-29 08:58:44 +00003248#endif
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003249 }
3250
Fredrik Lundh75f2d672000-06-29 11:34:28 +00003251 match = pattern_new_match((PatternObject*) self->pattern,
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003252 state, status);
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003253
Gustavo Niemeyer0506c642004-09-03 18:11:59 +00003254 if (status == 0 || state->ptr == state->start)
Fredrik Lundh436c3d52000-06-29 08:58:44 +00003255 state->start = (void*) ((char*) state->ptr + state->charsize);
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003256 else
Fredrik Lundh436c3d52000-06-29 08:58:44 +00003257 state->start = state->ptr;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003258
3259 return match;
3260}
3261
3262
3263static PyObject*
Georg Brandl964f5972006-05-28 22:38:57 +00003264scanner_search(ScannerObject* self, PyObject *unused)
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003265{
3266 SRE_STATE* state = &self->state;
3267 PyObject* match;
3268 int status;
3269
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00003270 state_reset(state);
3271
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003272 state->ptr = state->start;
3273
3274 if (state->charsize == 1) {
3275 status = sre_search(state, PatternObject_GetCode(self->pattern));
3276 } else {
Fredrik Lundh436c3d52000-06-29 08:58:44 +00003277#if defined(HAVE_UNICODE)
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003278 status = sre_usearch(state, PatternObject_GetCode(self->pattern));
Fredrik Lundh436c3d52000-06-29 08:58:44 +00003279#endif
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003280 }
3281
Fredrik Lundh75f2d672000-06-29 11:34:28 +00003282 match = pattern_new_match((PatternObject*) self->pattern,
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003283 state, status);
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003284
Gustavo Niemeyer0506c642004-09-03 18:11:59 +00003285 if (status == 0 || state->ptr == state->start)
Fredrik Lundhbe2211e2000-06-29 16:57:40 +00003286 state->start = (void*) ((char*) state->ptr + state->charsize);
3287 else
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003288 state->start = state->ptr;
3289
3290 return match;
3291}
3292
Fredrik Lundhbe2211e2000-06-29 16:57:40 +00003293static PyMethodDef scanner_methods[] = {
Georg Brandlfbef5882006-05-28 22:14:04 +00003294 {"match", (PyCFunction) scanner_match, METH_NOARGS},
3295 {"search", (PyCFunction) scanner_search, METH_NOARGS},
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003296 {NULL, NULL}
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003297};
3298
Tim Peters3d563502006-01-21 02:47:53 +00003299static PyObject*
Fredrik Lundhbe2211e2000-06-29 16:57:40 +00003300scanner_getattr(ScannerObject* self, char* name)
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003301{
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003302 PyObject* res;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003303
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003304 res = Py_FindMethod(scanner_methods, (PyObject*) self, name);
3305 if (res)
3306 return res;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003307
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003308 PyErr_Clear();
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003309
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003310 /* attributes */
3311 if (!strcmp(name, "pattern")) {
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003312 Py_INCREF(self->pattern);
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003313 return self->pattern;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003314 }
3315
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003316 PyErr_SetString(PyExc_AttributeError, name);
3317 return NULL;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003318}
3319
Fredrik Lundhbe2211e2000-06-29 16:57:40 +00003320statichere PyTypeObject Scanner_Type = {
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003321 PyObject_HEAD_INIT(NULL)
Fredrik Lundh82b23072001-12-09 16:13:15 +00003322 0, "_" SRE_MODULE ".SRE_Scanner",
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003323 sizeof(ScannerObject), 0,
3324 (destructor)scanner_dealloc, /*tp_dealloc*/
3325 0, /*tp_print*/
3326 (getattrfunc)scanner_getattr, /*tp_getattr*/
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +00003327};
3328
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00003329static PyObject*
3330pattern_scanner(PatternObject* pattern, PyObject* args)
3331{
3332 /* create search state object */
3333
3334 ScannerObject* self;
3335
3336 PyObject* string;
Neal Norwitza6d80fa2006-06-12 03:05:40 +00003337 Py_ssize_t start = 0;
3338 Py_ssize_t end = PY_SSIZE_T_MAX;
3339 if (!PyArg_ParseTuple(args, "O|nn:scanner", &string, &start, &end))
Anthony Baxteraefd8ca2006-04-12 04:26:11 +00003340 return NULL;
3341
3342 /* create scanner object */
3343 self = PyObject_NEW(ScannerObject, &Scanner_Type);
3344 if (!self)
3345 return NULL;
3346
3347 string = state_init(&self->state, pattern, string, start, end);
3348 if (!string) {
3349 PyObject_DEL(self);
3350 return NULL;
3351 }
3352
3353 Py_INCREF(pattern);
3354 self->pattern = (PyObject*) pattern;
3355
3356 return (PyObject*) self;
3357}
3358
Guido van Rossumb700df92000-03-31 14:59:30 +00003359static PyMethodDef _functions[] = {
Neal Norwitzb0493252002-03-31 14:44:22 +00003360 {"compile", _compile, METH_VARARGS},
Georg Brandlfbef5882006-05-28 22:14:04 +00003361 {"getcodesize", sre_codesize, METH_NOARGS},
Neal Norwitzb0493252002-03-31 14:44:22 +00003362 {"getlower", sre_getlower, METH_VARARGS},
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003363 {NULL, NULL}
Guido van Rossumb700df92000-03-31 14:59:30 +00003364};
3365
Tim Peters3d563502006-01-21 02:47:53 +00003366#if PY_VERSION_HEX < 0x02030000
Andrew M. Kuchlingc24fe362003-04-30 13:09:08 +00003367DL_EXPORT(void) init_sre(void)
3368#else
Mark Hammond8235ea12002-07-19 06:55:41 +00003369PyMODINIT_FUNC init_sre(void)
Andrew M. Kuchlingc24fe362003-04-30 13:09:08 +00003370#endif
Guido van Rossumb700df92000-03-31 14:59:30 +00003371{
Fredrik Lundhb35ffc02001-01-15 12:46:09 +00003372 PyObject* m;
3373 PyObject* d;
Barry Warsaw214a0b132001-08-16 20:33:48 +00003374 PyObject* x;
Fredrik Lundhb35ffc02001-01-15 12:46:09 +00003375
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +00003376 /* Patch object types */
3377 Pattern_Type.ob_type = Match_Type.ob_type =
Fredrik Lundhbe2211e2000-06-29 16:57:40 +00003378 Scanner_Type.ob_type = &PyType_Type;
Guido van Rossumb700df92000-03-31 14:59:30 +00003379
Fredrik Lundh1c5aa692001-01-16 07:37:30 +00003380 m = Py_InitModule("_" SRE_MODULE, _functions);
Neal Norwitz1ac754f2006-01-19 06:09:39 +00003381 if (m == NULL)
3382 return;
Fredrik Lundhb35ffc02001-01-15 12:46:09 +00003383 d = PyModule_GetDict(m);
3384
Fredrik Lundh21009b92001-09-18 18:47:09 +00003385 x = PyInt_FromLong(SRE_MAGIC);
3386 if (x) {
3387 PyDict_SetItemString(d, "MAGIC", x);
3388 Py_DECREF(x);
3389 }
Fredrik Lundh9c7eab82001-04-15 19:00:58 +00003390
Martin v. Löwis78e2f062003-04-19 12:56:08 +00003391 x = PyInt_FromLong(sizeof(SRE_CODE));
3392 if (x) {
3393 PyDict_SetItemString(d, "CODESIZE", x);
3394 Py_DECREF(x);
3395 }
3396
Fredrik Lundh21009b92001-09-18 18:47:09 +00003397 x = PyString_FromString(copyright);
3398 if (x) {
3399 PyDict_SetItemString(d, "copyright", x);
3400 Py_DECREF(x);
3401 }
Guido van Rossumb700df92000-03-31 14:59:30 +00003402}
3403
Fredrik Lundh436c3d52000-06-29 08:58:44 +00003404#endif /* !defined(SRE_RECURSIVE) */
Gustavo Niemeyerbe733ee2003-04-20 07:35:44 +00003405
3406/* vim:ts=4:sw=4:et
3407*/