2000-07-23 18:46:17 -03:00
|
|
|
/*
|
2000-03-31 10:59:30 -04:00
|
|
|
* Secret Labs' Regular Expression Engine
|
|
|
|
*
|
2000-06-29 07:34:56 -03:00
|
|
|
* regular expression matching engine
|
2000-03-31 10:59:30 -04:00
|
|
|
*
|
|
|
|
* partial history:
|
2000-09-21 14:03:25 -03:00
|
|
|
* 1999-10-24 fl created (based on existing template matcher code)
|
2000-10-28 16:30:41 -03:00
|
|
|
* 2000-03-06 fl first alpha, sort of
|
|
|
|
* 2000-08-01 fl fixes for 1.6b1
|
2000-09-21 14:03:25 -03:00
|
|
|
* 2000-08-07 fl use PyOS_CheckStack() if available
|
|
|
|
* 2000-09-20 fl added expand method
|
2001-03-22 11:50:10 -04:00
|
|
|
* 2001-03-20 fl lots of fixes for 2.1b2
|
2001-04-15 16:00:58 -03:00
|
|
|
* 2001-04-15 fl export copyright as Python attribute, not global
|
2001-07-02 13:42:49 -03:00
|
|
|
* 2001-04-28 fl added __copy__ methods (work in progress)
|
2002-11-22 08:46:35 -04:00
|
|
|
* 2001-05-14 fl fixes for 1.5.2 compatibility
|
2001-07-02 14:04:48 -03:00
|
|
|
* 2001-07-01 fl added BIGCHARSET support (from Martin von Loewis)
|
2001-10-18 16:30:16 -03:00
|
|
|
* 2001-10-18 fl fixed group reset issue (from Matthew Mueller)
|
2001-10-20 14:48:46 -03:00
|
|
|
* 2001-10-20 fl added split primitive; reenable unicode for 1.6/2.0/2.1
|
2001-10-21 13:47:57 -03:00
|
|
|
* 2001-10-21 fl added sub/subn primitive
|
2001-10-24 19:16:30 -03:00
|
|
|
* 2001-10-24 fl added finditer primitive (for 2.2 only)
|
2001-12-09 12:13:15 -04:00
|
|
|
* 2001-12-07 fl fixed memory leak in sub/subn (Guido van Rossum)
|
2002-11-22 08:46:35 -04:00
|
|
|
* 2002-11-09 fl fixed empty sub/subn return type
|
2003-04-19 09:56:08 -03:00
|
|
|
* 2003-04-18 mvl fully support 4-byte codes
|
2003-10-17 19:13:16 -03:00
|
|
|
* 2003-10-17 gn implemented non recursive scheme
|
2000-03-31 10:59:30 -04:00
|
|
|
*
|
2001-01-14 11:06:11 -04:00
|
|
|
* Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
|
2000-03-31 10:59:30 -04:00
|
|
|
*
|
2000-08-01 15:20:07 -03:00
|
|
|
* This version of the SRE library can be redistributed under CNRI's
|
|
|
|
* Python 1.6 license. For any other use, please contact Secret Labs
|
|
|
|
* AB (info@pythonware.com).
|
|
|
|
*
|
2000-03-31 10:59:30 -04:00
|
|
|
* Portions of this engine have been developed in cooperation with
|
2000-08-01 15:20:07 -03:00
|
|
|
* CNRI. Hewlett-Packard provided funding for 1.6 integration and
|
2000-03-31 10:59:30 -04:00
|
|
|
* other compatibility work.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SRE_RECURSIVE
|
|
|
|
|
2001-04-15 16:00:58 -03:00
|
|
|
static char copyright[] =
|
2002-11-22 08:46:35 -04:00
|
|
|
" SRE 2.2.2 Copyright (c) 1997-2002 by Secret Labs AB ";
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2006-06-12 00:05:40 -03:00
|
|
|
#define PY_SSIZE_T_CLEAN
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
#include "Python.h"
|
2001-07-02 13:42:49 -03:00
|
|
|
#include "structmember.h" /* offsetof */
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
#include "sre.h"
|
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
#include <ctype.h>
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
/* name of this module, minus the leading underscore */
|
2001-01-16 03:37:30 -04:00
|
|
|
#if !defined(SRE_MODULE)
|
|
|
|
#define SRE_MODULE "sre"
|
|
|
|
#endif
|
2000-06-29 05:58:44 -03:00
|
|
|
|
2006-03-16 02:30:02 -04:00
|
|
|
#define SRE_PY_MODULE "re"
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
/* defining this one enables tracing */
|
2000-07-23 18:46:17 -03:00
|
|
|
#undef VERBOSE
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2001-10-20 14:48:46 -03:00
|
|
|
#if PY_VERSION_HEX >= 0x01060000
|
|
|
|
#if PY_VERSION_HEX < 0x02020000 || defined(Py_USING_UNICODE)
|
2000-07-01 14:50:59 -03:00
|
|
|
/* defining this enables unicode support (default under 1.6a1 and later) */
|
2000-06-29 05:58:44 -03:00
|
|
|
#define HAVE_UNICODE
|
|
|
|
#endif
|
2001-10-20 14:48:46 -03:00
|
|
|
#endif
|
2000-06-29 05:58:44 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
/* -------------------------------------------------------------------- */
|
2000-06-29 20:33:12 -03:00
|
|
|
/* optional features */
|
2000-07-23 18:46:17 -03:00
|
|
|
|
|
|
|
/* enables fast searching */
|
2000-06-29 20:33:12 -03:00
|
|
|
#define USE_FAST_SEARCH
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
/* enables aggressive inlining (always on for Visual C) */
|
2000-08-01 15:20:07 -03:00
|
|
|
#undef USE_INLINE
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2001-07-02 13:42:49 -03:00
|
|
|
/* enables copy/deepcopy handling (work in progress) */
|
|
|
|
#undef USE_BUILTIN_COPY
|
|
|
|
|
2001-01-16 03:37:30 -04:00
|
|
|
#if PY_VERSION_HEX < 0x01060000
|
|
|
|
#define PyObject_DEL(op) PyMem_DEL((op))
|
|
|
|
#endif
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
|
2000-06-29 15:03:25 -03:00
|
|
|
#if defined(_MSC_VER)
|
2000-03-31 10:59:30 -04:00
|
|
|
#pragma optimize("agtw", on) /* doesn't seem to make much difference... */
|
2000-07-05 18:14:16 -03:00
|
|
|
#pragma warning(disable: 4710) /* who cares if functions are not inlined ;-) */
|
2000-03-31 10:59:30 -04:00
|
|
|
/* fastest possible local call under MSVC */
|
|
|
|
#define LOCAL(type) static __inline type __fastcall
|
2000-07-23 18:46:17 -03:00
|
|
|
#elif defined(USE_INLINE)
|
2000-06-29 20:33:12 -03:00
|
|
|
#define LOCAL(type) static inline type
|
2000-07-23 18:46:17 -03:00
|
|
|
#else
|
|
|
|
#define LOCAL(type) static type
|
2000-03-31 10:59:30 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* error codes */
|
|
|
|
#define SRE_ERROR_ILLEGAL -1 /* illegal opcode */
|
2000-08-01 15:20:07 -03:00
|
|
|
#define SRE_ERROR_STATE -2 /* illegal state */
|
2000-08-03 13:29:50 -03:00
|
|
|
#define SRE_ERROR_RECURSION_LIMIT -3 /* runaway recursion */
|
2000-03-31 10:59:30 -04:00
|
|
|
#define SRE_ERROR_MEMORY -9 /* out of memory */
|
2008-01-08 17:10:12 -04:00
|
|
|
#define SRE_ERROR_INTERRUPTED -10 /* signal handler raised exception */
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
#if defined(VERBOSE)
|
2000-03-31 10:59:30 -04:00
|
|
|
#define TRACE(v) printf v
|
|
|
|
#else
|
|
|
|
#define TRACE(v)
|
|
|
|
#endif
|
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* search engine state */
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
/* default character predicates (run sre_chars.py to regenerate tables) */
|
|
|
|
|
|
|
|
#define SRE_DIGIT_MASK 1
|
|
|
|
#define SRE_SPACE_MASK 2
|
|
|
|
#define SRE_LINEBREAK_MASK 4
|
|
|
|
#define SRE_ALNUM_MASK 8
|
|
|
|
#define SRE_WORD_MASK 16
|
|
|
|
|
2001-09-18 15:47:09 -03:00
|
|
|
/* FIXME: this assumes ASCII. create tables in init_sre() instead */
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
static char sre_char_info[128] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 6, 2,
|
|
|
|
2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 25, 25, 25, 25, 25, 25, 25,
|
|
|
|
25, 25, 0, 0, 0, 0, 0, 0, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
|
|
|
|
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 0, 0,
|
|
|
|
0, 0, 16, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24,
|
|
|
|
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 0, 0, 0, 0, 0 };
|
|
|
|
|
2000-06-29 09:48:37 -03:00
|
|
|
static char sre_char_lower[128] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
2000-06-29 05:58:44 -03:00
|
|
|
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
|
|
|
|
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
|
|
|
|
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
|
|
|
|
61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
|
|
|
|
108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
|
|
|
|
122, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
|
|
|
|
106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
|
|
|
|
120, 121, 122, 123, 124, 125, 126, 127 };
|
|
|
|
|
|
|
|
#define SRE_IS_DIGIT(ch)\
|
|
|
|
((ch) < 128 ? (sre_char_info[(ch)] & SRE_DIGIT_MASK) : 0)
|
|
|
|
#define SRE_IS_SPACE(ch)\
|
|
|
|
((ch) < 128 ? (sre_char_info[(ch)] & SRE_SPACE_MASK) : 0)
|
|
|
|
#define SRE_IS_LINEBREAK(ch)\
|
|
|
|
((ch) < 128 ? (sre_char_info[(ch)] & SRE_LINEBREAK_MASK) : 0)
|
|
|
|
#define SRE_IS_ALNUM(ch)\
|
|
|
|
((ch) < 128 ? (sre_char_info[(ch)] & SRE_ALNUM_MASK) : 0)
|
|
|
|
#define SRE_IS_WORD(ch)\
|
|
|
|
((ch) < 128 ? (sre_char_info[(ch)] & SRE_WORD_MASK) : 0)
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2001-03-22 11:50:10 -04:00
|
|
|
static unsigned int sre_lower(unsigned int ch)
|
2000-06-29 05:58:44 -03:00
|
|
|
{
|
2004-02-13 20:31:13 -04:00
|
|
|
return ((ch) < 128 ? (unsigned int)sre_char_lower[ch] : ch);
|
2000-06-29 05:58:44 -03:00
|
|
|
}
|
2001-03-22 11:50:10 -04:00
|
|
|
|
|
|
|
/* locale-specific character predicates */
|
2004-02-13 20:31:13 -04:00
|
|
|
/* !(c & ~N) == (c < N+1) for any unsigned c, this avoids
|
|
|
|
* warnings when c's type supports only numbers < N+1 */
|
|
|
|
#define SRE_LOC_IS_DIGIT(ch) (!((ch) & ~255) ? isdigit((ch)) : 0)
|
|
|
|
#define SRE_LOC_IS_SPACE(ch) (!((ch) & ~255) ? isspace((ch)) : 0)
|
2000-06-01 14:39:12 -03:00
|
|
|
#define SRE_LOC_IS_LINEBREAK(ch) ((ch) == '\n')
|
2004-02-13 20:31:13 -04:00
|
|
|
#define SRE_LOC_IS_ALNUM(ch) (!((ch) & ~255) ? isalnum((ch)) : 0)
|
2000-06-01 14:39:12 -03:00
|
|
|
#define SRE_LOC_IS_WORD(ch) (SRE_LOC_IS_ALNUM((ch)) || (ch) == '_')
|
|
|
|
|
2001-03-22 11:50:10 -04:00
|
|
|
static unsigned int sre_lower_locale(unsigned int ch)
|
|
|
|
{
|
2004-02-13 20:31:13 -04:00
|
|
|
return ((ch) < 256 ? (unsigned int)tolower((ch)) : ch);
|
2001-03-22 11:50:10 -04:00
|
|
|
}
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
/* unicode-specific character predicates */
|
|
|
|
|
|
|
|
#if defined(HAVE_UNICODE)
|
2001-03-22 11:50:10 -04:00
|
|
|
|
2009-07-28 17:35:03 -03:00
|
|
|
#define SRE_UNI_IS_DIGIT(ch) Py_UNICODE_ISDECIMAL((Py_UNICODE)(ch))
|
2000-06-29 05:58:44 -03:00
|
|
|
#define SRE_UNI_IS_SPACE(ch) Py_UNICODE_ISSPACE((Py_UNICODE)(ch))
|
|
|
|
#define SRE_UNI_IS_LINEBREAK(ch) Py_UNICODE_ISLINEBREAK((Py_UNICODE)(ch))
|
2000-07-01 14:50:59 -03:00
|
|
|
#define SRE_UNI_IS_ALNUM(ch) Py_UNICODE_ISALNUM((Py_UNICODE)(ch))
|
2000-07-23 18:46:17 -03:00
|
|
|
#define SRE_UNI_IS_WORD(ch) (SRE_UNI_IS_ALNUM((ch)) || (ch) == '_')
|
2001-03-22 11:50:10 -04:00
|
|
|
|
|
|
|
static unsigned int sre_lower_unicode(unsigned int ch)
|
|
|
|
{
|
|
|
|
return (unsigned int) Py_UNICODE_TOLOWER((Py_UNICODE)(ch));
|
|
|
|
}
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
#endif
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
LOCAL(int)
|
|
|
|
sre_category(SRE_CODE category, unsigned int ch)
|
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
switch (category) {
|
|
|
|
|
|
|
|
case SRE_CATEGORY_DIGIT:
|
|
|
|
return SRE_IS_DIGIT(ch);
|
|
|
|
case SRE_CATEGORY_NOT_DIGIT:
|
|
|
|
return !SRE_IS_DIGIT(ch);
|
|
|
|
case SRE_CATEGORY_SPACE:
|
|
|
|
return SRE_IS_SPACE(ch);
|
|
|
|
case SRE_CATEGORY_NOT_SPACE:
|
|
|
|
return !SRE_IS_SPACE(ch);
|
|
|
|
case SRE_CATEGORY_WORD:
|
|
|
|
return SRE_IS_WORD(ch);
|
|
|
|
case SRE_CATEGORY_NOT_WORD:
|
|
|
|
return !SRE_IS_WORD(ch);
|
|
|
|
case SRE_CATEGORY_LINEBREAK:
|
|
|
|
return SRE_IS_LINEBREAK(ch);
|
|
|
|
case SRE_CATEGORY_NOT_LINEBREAK:
|
|
|
|
return !SRE_IS_LINEBREAK(ch);
|
|
|
|
|
|
|
|
case SRE_CATEGORY_LOC_WORD:
|
|
|
|
return SRE_LOC_IS_WORD(ch);
|
|
|
|
case SRE_CATEGORY_LOC_NOT_WORD:
|
|
|
|
return !SRE_LOC_IS_WORD(ch);
|
2000-06-29 05:58:44 -03:00
|
|
|
|
|
|
|
#if defined(HAVE_UNICODE)
|
2000-07-23 18:46:17 -03:00
|
|
|
case SRE_CATEGORY_UNI_DIGIT:
|
|
|
|
return SRE_UNI_IS_DIGIT(ch);
|
|
|
|
case SRE_CATEGORY_UNI_NOT_DIGIT:
|
|
|
|
return !SRE_UNI_IS_DIGIT(ch);
|
|
|
|
case SRE_CATEGORY_UNI_SPACE:
|
|
|
|
return SRE_UNI_IS_SPACE(ch);
|
|
|
|
case SRE_CATEGORY_UNI_NOT_SPACE:
|
|
|
|
return !SRE_UNI_IS_SPACE(ch);
|
|
|
|
case SRE_CATEGORY_UNI_WORD:
|
|
|
|
return SRE_UNI_IS_WORD(ch);
|
|
|
|
case SRE_CATEGORY_UNI_NOT_WORD:
|
|
|
|
return !SRE_UNI_IS_WORD(ch);
|
|
|
|
case SRE_CATEGORY_UNI_LINEBREAK:
|
|
|
|
return SRE_UNI_IS_LINEBREAK(ch);
|
|
|
|
case SRE_CATEGORY_UNI_NOT_LINEBREAK:
|
|
|
|
return !SRE_UNI_IS_LINEBREAK(ch);
|
2001-01-16 03:37:30 -04:00
|
|
|
#else
|
|
|
|
case SRE_CATEGORY_UNI_DIGIT:
|
|
|
|
return SRE_IS_DIGIT(ch);
|
|
|
|
case SRE_CATEGORY_UNI_NOT_DIGIT:
|
|
|
|
return !SRE_IS_DIGIT(ch);
|
|
|
|
case SRE_CATEGORY_UNI_SPACE:
|
|
|
|
return SRE_IS_SPACE(ch);
|
|
|
|
case SRE_CATEGORY_UNI_NOT_SPACE:
|
|
|
|
return !SRE_IS_SPACE(ch);
|
|
|
|
case SRE_CATEGORY_UNI_WORD:
|
|
|
|
return SRE_LOC_IS_WORD(ch);
|
|
|
|
case SRE_CATEGORY_UNI_NOT_WORD:
|
|
|
|
return !SRE_LOC_IS_WORD(ch);
|
|
|
|
case SRE_CATEGORY_UNI_LINEBREAK:
|
|
|
|
return SRE_IS_LINEBREAK(ch);
|
|
|
|
case SRE_CATEGORY_UNI_NOT_LINEBREAK:
|
|
|
|
return !SRE_IS_LINEBREAK(ch);
|
2000-06-29 05:58:44 -03:00
|
|
|
#endif
|
2000-07-23 18:46:17 -03:00
|
|
|
}
|
|
|
|
return 0;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* helpers */
|
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
static void
|
2003-10-17 19:13:16 -03:00
|
|
|
data_stack_dealloc(SRE_STATE* state)
|
2000-08-01 15:20:07 -03:00
|
|
|
{
|
2003-10-17 19:13:16 -03:00
|
|
|
if (state->data_stack) {
|
2006-05-27 12:44:34 -03:00
|
|
|
PyMem_FREE(state->data_stack);
|
2003-10-17 19:13:16 -03:00
|
|
|
state->data_stack = NULL;
|
2000-08-03 13:29:50 -03:00
|
|
|
}
|
2003-10-17 19:13:16 -03:00
|
|
|
state->data_stack_size = state->data_stack_base = 0;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
static int
|
2006-06-12 00:05:40 -03:00
|
|
|
data_stack_grow(SRE_STATE* state, Py_ssize_t size)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t minsize, cursize;
|
2003-10-17 19:13:16 -03:00
|
|
|
minsize = state->data_stack_base+size;
|
|
|
|
cursize = state->data_stack_size;
|
|
|
|
if (cursize < minsize) {
|
|
|
|
void* stack;
|
|
|
|
cursize = minsize+minsize/4+1024;
|
2013-09-05 12:02:57 -03:00
|
|
|
TRACE(("allocate/grow stack %" PY_FORMAT_SIZE_T "d\n", cursize));
|
2006-05-27 12:44:34 -03:00
|
|
|
stack = PyMem_REALLOC(state->data_stack, cursize);
|
2000-08-01 15:20:07 -03:00
|
|
|
if (!stack) {
|
2003-10-17 19:13:16 -03:00
|
|
|
data_stack_dealloc(state);
|
2000-08-01 15:20:07 -03:00
|
|
|
return SRE_ERROR_MEMORY;
|
|
|
|
}
|
2006-04-12 01:26:11 -03:00
|
|
|
state->data_stack = (char *)stack;
|
2003-10-17 19:13:16 -03:00
|
|
|
state->data_stack_size = cursize;
|
2000-07-23 18:46:17 -03:00
|
|
|
}
|
|
|
|
return 0;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
/* generate 8-bit version */
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
#define SRE_CHAR unsigned char
|
|
|
|
#define SRE_AT sre_at
|
2000-08-03 13:29:50 -03:00
|
|
|
#define SRE_COUNT sre_count
|
2000-08-07 17:59:04 -03:00
|
|
|
#define SRE_CHARSET sre_charset
|
|
|
|
#define SRE_INFO sre_info
|
2000-03-31 10:59:30 -04:00
|
|
|
#define SRE_MATCH sre_match
|
2003-10-17 19:13:16 -03:00
|
|
|
#define SRE_MATCH_CONTEXT sre_match_context
|
2000-03-31 10:59:30 -04:00
|
|
|
#define SRE_SEARCH sre_search
|
2001-10-22 18:18:08 -03:00
|
|
|
#define SRE_LITERAL_TEMPLATE sre_literal_template
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
#if defined(HAVE_UNICODE)
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
#define SRE_RECURSIVE
|
|
|
|
#include "_sre.c"
|
2000-03-31 10:59:30 -04:00
|
|
|
#undef SRE_RECURSIVE
|
2000-06-29 05:58:44 -03:00
|
|
|
|
2001-10-22 18:18:08 -03:00
|
|
|
#undef SRE_LITERAL_TEMPLATE
|
2000-03-31 10:59:30 -04:00
|
|
|
#undef SRE_SEARCH
|
|
|
|
#undef SRE_MATCH
|
2003-10-17 19:13:16 -03:00
|
|
|
#undef SRE_MATCH_CONTEXT
|
2000-08-07 17:59:04 -03:00
|
|
|
#undef SRE_INFO
|
|
|
|
#undef SRE_CHARSET
|
2000-08-03 13:29:50 -03:00
|
|
|
#undef SRE_COUNT
|
2000-03-31 10:59:30 -04:00
|
|
|
#undef SRE_AT
|
|
|
|
#undef SRE_CHAR
|
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
/* generate 16-bit unicode version */
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
#define SRE_CHAR Py_UNICODE
|
|
|
|
#define SRE_AT sre_uat
|
2000-08-03 13:29:50 -03:00
|
|
|
#define SRE_COUNT sre_ucount
|
2000-08-07 17:59:04 -03:00
|
|
|
#define SRE_CHARSET sre_ucharset
|
|
|
|
#define SRE_INFO sre_uinfo
|
2000-03-31 10:59:30 -04:00
|
|
|
#define SRE_MATCH sre_umatch
|
2003-10-17 19:13:16 -03:00
|
|
|
#define SRE_MATCH_CONTEXT sre_umatch_context
|
2000-03-31 10:59:30 -04:00
|
|
|
#define SRE_SEARCH sre_usearch
|
2001-10-22 18:18:08 -03:00
|
|
|
#define SRE_LITERAL_TEMPLATE sre_uliteral_template
|
2000-06-29 05:58:44 -03:00
|
|
|
#endif
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
#endif /* SRE_RECURSIVE */
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* String matching engine */
|
|
|
|
|
|
|
|
/* the following section is compiled twice, with different character
|
|
|
|
settings */
|
|
|
|
|
|
|
|
LOCAL(int)
|
|
|
|
SRE_AT(SRE_STATE* state, SRE_CHAR* ptr, SRE_CODE at)
|
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
/* check if pointer is at given position */
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t thisp, thatp;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
switch (at) {
|
2000-06-29 15:03:25 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
case SRE_AT_BEGINNING:
|
2001-01-14 11:06:11 -04:00
|
|
|
case SRE_AT_BEGINNING_STRING:
|
2000-07-23 18:46:17 -03:00
|
|
|
return ((void*) ptr == state->beginning);
|
2000-06-29 15:03:25 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
case SRE_AT_BEGINNING_LINE:
|
|
|
|
return ((void*) ptr == state->beginning ||
|
2000-06-01 14:39:12 -03:00
|
|
|
SRE_IS_LINEBREAK((int) ptr[-1]));
|
2000-06-29 15:03:25 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
case SRE_AT_END:
|
2015-07-06 07:58:24 -03:00
|
|
|
return (((SRE_CHAR *)state->end - ptr == 1 &&
|
2000-06-30 18:40:20 -03:00
|
|
|
SRE_IS_LINEBREAK((int) ptr[0])) ||
|
|
|
|
((void*) ptr == state->end));
|
2000-06-29 15:03:25 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
case SRE_AT_END_LINE:
|
|
|
|
return ((void*) ptr == state->end ||
|
2000-06-01 14:39:12 -03:00
|
|
|
SRE_IS_LINEBREAK((int) ptr[0]));
|
2000-06-29 15:03:25 -03:00
|
|
|
|
2001-01-14 11:06:11 -04:00
|
|
|
case SRE_AT_END_STRING:
|
|
|
|
return ((void*) ptr == state->end);
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
case SRE_AT_BOUNDARY:
|
|
|
|
if (state->beginning == state->end)
|
|
|
|
return 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
thatp = ((void*) ptr > state->beginning) ?
|
2000-07-23 18:46:17 -03:00
|
|
|
SRE_IS_WORD((int) ptr[-1]) : 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
thisp = ((void*) ptr < state->end) ?
|
2000-07-23 18:46:17 -03:00
|
|
|
SRE_IS_WORD((int) ptr[0]) : 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
return thisp != thatp;
|
2000-07-23 18:46:17 -03:00
|
|
|
|
|
|
|
case SRE_AT_NON_BOUNDARY:
|
|
|
|
if (state->beginning == state->end)
|
|
|
|
return 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
thatp = ((void*) ptr > state->beginning) ?
|
2000-07-23 18:46:17 -03:00
|
|
|
SRE_IS_WORD((int) ptr[-1]) : 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
thisp = ((void*) ptr < state->end) ?
|
2000-07-23 18:46:17 -03:00
|
|
|
SRE_IS_WORD((int) ptr[0]) : 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
return thisp == thatp;
|
2001-03-22 11:50:10 -04:00
|
|
|
|
|
|
|
case SRE_AT_LOC_BOUNDARY:
|
|
|
|
if (state->beginning == state->end)
|
|
|
|
return 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
thatp = ((void*) ptr > state->beginning) ?
|
2001-03-22 11:50:10 -04:00
|
|
|
SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
thisp = ((void*) ptr < state->end) ?
|
2001-03-22 11:50:10 -04:00
|
|
|
SRE_LOC_IS_WORD((int) ptr[0]) : 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
return thisp != thatp;
|
2001-03-22 11:50:10 -04:00
|
|
|
|
|
|
|
case SRE_AT_LOC_NON_BOUNDARY:
|
|
|
|
if (state->beginning == state->end)
|
|
|
|
return 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
thatp = ((void*) ptr > state->beginning) ?
|
2001-03-22 11:50:10 -04:00
|
|
|
SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
thisp = ((void*) ptr < state->end) ?
|
2001-03-22 11:50:10 -04:00
|
|
|
SRE_LOC_IS_WORD((int) ptr[0]) : 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
return thisp == thatp;
|
2001-03-22 11:50:10 -04:00
|
|
|
|
2001-07-02 13:42:49 -03:00
|
|
|
#if defined(HAVE_UNICODE)
|
2001-03-22 11:50:10 -04:00
|
|
|
case SRE_AT_UNI_BOUNDARY:
|
|
|
|
if (state->beginning == state->end)
|
|
|
|
return 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
thatp = ((void*) ptr > state->beginning) ?
|
2001-03-22 11:50:10 -04:00
|
|
|
SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
thisp = ((void*) ptr < state->end) ?
|
2001-03-22 11:50:10 -04:00
|
|
|
SRE_UNI_IS_WORD((int) ptr[0]) : 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
return thisp != thatp;
|
2001-03-22 11:50:10 -04:00
|
|
|
|
|
|
|
case SRE_AT_UNI_NON_BOUNDARY:
|
|
|
|
if (state->beginning == state->end)
|
|
|
|
return 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
thatp = ((void*) ptr > state->beginning) ?
|
2001-03-22 11:50:10 -04:00
|
|
|
SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
thisp = ((void*) ptr < state->end) ?
|
2001-03-22 11:50:10 -04:00
|
|
|
SRE_UNI_IS_WORD((int) ptr[0]) : 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
return thisp == thatp;
|
2001-07-02 13:42:49 -03:00
|
|
|
#endif
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
LOCAL(int)
|
2000-08-07 17:59:04 -03:00
|
|
|
SRE_CHARSET(SRE_CODE* set, SRE_CODE ch)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
/* check if character is a member of the given set */
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
int ok = 1;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
for (;;) {
|
|
|
|
switch (*set++) {
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
case SRE_OP_FAILURE:
|
|
|
|
return !ok;
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
case SRE_OP_LITERAL:
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <LITERAL> <code> */
|
2000-07-23 18:46:17 -03:00
|
|
|
if (ch == set[0])
|
|
|
|
return ok;
|
|
|
|
set++;
|
|
|
|
break;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
case SRE_OP_CATEGORY:
|
|
|
|
/* <CATEGORY> <code> */
|
|
|
|
if (sre_category(set[0], (int) ch))
|
2000-07-23 18:46:17 -03:00
|
|
|
return ok;
|
2003-10-17 19:13:16 -03:00
|
|
|
set += 1;
|
2000-07-23 18:46:17 -03:00
|
|
|
break;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-02 09:00:07 -03:00
|
|
|
case SRE_OP_CHARSET:
|
2003-04-19 09:56:08 -03:00
|
|
|
if (sizeof(SRE_CODE) == 2) {
|
|
|
|
/* <CHARSET> <bitmap> (16 bits per code word) */
|
|
|
|
if (ch < 256 && (set[ch >> 4] & (1 << (ch & 15))))
|
|
|
|
return ok;
|
|
|
|
set += 16;
|
2006-01-20 22:47:53 -04:00
|
|
|
}
|
2003-04-19 09:56:08 -03:00
|
|
|
else {
|
|
|
|
/* <CHARSET> <bitmap> (32 bits per code word) */
|
2012-12-10 21:45:54 -04:00
|
|
|
if (ch < 256 && (set[ch >> 5] & (1u << (ch & 31))))
|
2003-04-19 09:56:08 -03:00
|
|
|
return ok;
|
|
|
|
set += 8;
|
|
|
|
}
|
2000-07-02 09:00:07 -03:00
|
|
|
break;
|
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
case SRE_OP_RANGE:
|
|
|
|
/* <RANGE> <lower> <upper> */
|
|
|
|
if (set[0] <= ch && ch <= set[1])
|
|
|
|
return ok;
|
|
|
|
set += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_NEGATE:
|
|
|
|
ok = !ok;
|
|
|
|
break;
|
|
|
|
|
2001-07-02 14:04:48 -03:00
|
|
|
case SRE_OP_BIGCHARSET:
|
|
|
|
/* <BIGCHARSET> <blockcount> <256 blockindices> <blocks> */
|
|
|
|
{
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t count, block;
|
2001-07-02 14:04:48 -03:00
|
|
|
count = *(set++);
|
2003-04-19 09:56:08 -03:00
|
|
|
|
|
|
|
if (sizeof(SRE_CODE) == 2) {
|
|
|
|
block = ((unsigned char*)set)[ch >> 8];
|
|
|
|
set += 128;
|
|
|
|
if (set[block*16 + ((ch & 255)>>4)] & (1 << (ch & 15)))
|
|
|
|
return ok;
|
|
|
|
set += count*16;
|
|
|
|
}
|
|
|
|
else {
|
2004-02-13 20:31:13 -04:00
|
|
|
/* !(c & ~N) == (c < N+1) for any unsigned c, this avoids
|
|
|
|
* warnings when c's type supports only numbers < N+1 */
|
|
|
|
if (!(ch & ~65535))
|
2003-04-19 09:56:08 -03:00
|
|
|
block = ((unsigned char*)set)[ch >> 8];
|
|
|
|
else
|
|
|
|
block = -1;
|
|
|
|
set += 64;
|
2006-01-20 22:47:53 -04:00
|
|
|
if (block >=0 &&
|
2012-12-10 21:45:54 -04:00
|
|
|
(set[block*8 + ((ch & 255)>>5)] & (1u << (ch & 31))))
|
2003-04-19 09:56:08 -03:00
|
|
|
return ok;
|
|
|
|
set += count*8;
|
|
|
|
}
|
2001-07-02 14:04:48 -03:00
|
|
|
break;
|
|
|
|
}
|
2001-07-02 13:58:38 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
default:
|
|
|
|
/* internal error -- there's not much we can do about it
|
2000-06-29 15:03:25 -03:00
|
|
|
here, so let's just pretend it didn't match... */
|
2000-07-23 18:46:17 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
2006-06-12 00:05:40 -03:00
|
|
|
LOCAL(Py_ssize_t) SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern);
|
2000-08-03 13:29:50 -03:00
|
|
|
|
2006-06-12 00:05:40 -03:00
|
|
|
LOCAL(Py_ssize_t)
|
|
|
|
SRE_COUNT(SRE_STATE* state, SRE_CODE* pattern, Py_ssize_t maxcount)
|
2000-08-03 13:29:50 -03:00
|
|
|
{
|
|
|
|
SRE_CODE chr;
|
2006-04-12 01:26:11 -03:00
|
|
|
SRE_CHAR* ptr = (SRE_CHAR *)state->ptr;
|
|
|
|
SRE_CHAR* end = (SRE_CHAR *)state->end;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t i;
|
2000-08-03 13:29:50 -03:00
|
|
|
|
|
|
|
/* adjust end */
|
2013-02-16 10:47:15 -04:00
|
|
|
if (maxcount < end - ptr && maxcount != SRE_MAXREPEAT)
|
2000-08-03 13:29:50 -03:00
|
|
|
end = ptr + maxcount;
|
|
|
|
|
|
|
|
switch (pattern[0]) {
|
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
case SRE_OP_IN:
|
|
|
|
/* repeated set */
|
|
|
|
TRACE(("|%p|%p|COUNT IN\n", pattern, ptr));
|
|
|
|
while (ptr < end && SRE_CHARSET(pattern + 2, *ptr))
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
2000-08-03 13:29:50 -03:00
|
|
|
case SRE_OP_ANY:
|
|
|
|
/* repeated dot wildcard. */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|COUNT ANY\n", pattern, ptr));
|
2000-08-03 13:29:50 -03:00
|
|
|
while (ptr < end && !SRE_IS_LINEBREAK(*ptr))
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_ANY_ALL:
|
2004-09-03 15:11:59 -03:00
|
|
|
/* repeated dot wildcard. skip to the end of the target
|
2000-08-03 13:29:50 -03:00
|
|
|
string, and backtrack from there */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|COUNT ANY_ALL\n", pattern, ptr));
|
2000-08-03 13:29:50 -03:00
|
|
|
ptr = end;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_LITERAL:
|
|
|
|
/* repeated literal */
|
|
|
|
chr = pattern[1];
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|COUNT LITERAL %d\n", pattern, ptr, chr));
|
2000-08-03 13:29:50 -03:00
|
|
|
while (ptr < end && (SRE_CODE) *ptr == chr)
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_LITERAL_IGNORE:
|
|
|
|
/* repeated literal */
|
|
|
|
chr = pattern[1];
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|COUNT LITERAL_IGNORE %d\n", pattern, ptr, chr));
|
2000-08-03 13:29:50 -03:00
|
|
|
while (ptr < end && (SRE_CODE) state->lower(*ptr) == chr)
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_NOT_LITERAL:
|
|
|
|
/* repeated non-literal */
|
|
|
|
chr = pattern[1];
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|COUNT NOT_LITERAL %d\n", pattern, ptr, chr));
|
2000-08-03 13:29:50 -03:00
|
|
|
while (ptr < end && (SRE_CODE) *ptr != chr)
|
|
|
|
ptr++;
|
|
|
|
break;
|
2006-01-20 22:47:53 -04:00
|
|
|
|
2000-08-03 13:29:50 -03:00
|
|
|
case SRE_OP_NOT_LITERAL_IGNORE:
|
|
|
|
/* repeated non-literal */
|
|
|
|
chr = pattern[1];
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|COUNT NOT_LITERAL_IGNORE %d\n", pattern, ptr, chr));
|
2000-08-03 13:29:50 -03:00
|
|
|
while (ptr < end && (SRE_CODE) state->lower(*ptr) != chr)
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* repeated single character pattern */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|COUNT SUBPATTERN\n", pattern, ptr));
|
2000-08-03 13:29:50 -03:00
|
|
|
while ((SRE_CHAR*) state->ptr < end) {
|
2003-12-13 16:32:08 -04:00
|
|
|
i = SRE_MATCH(state, pattern);
|
2000-08-03 13:29:50 -03:00
|
|
|
if (i < 0)
|
|
|
|
return i;
|
|
|
|
if (!i)
|
|
|
|
break;
|
|
|
|
}
|
2013-09-05 12:02:57 -03:00
|
|
|
TRACE(("|%p|%p|COUNT %" PY_FORMAT_SIZE_T "d\n", pattern, ptr,
|
2000-08-07 17:59:04 -03:00
|
|
|
(SRE_CHAR*) state->ptr - ptr));
|
2000-08-03 13:29:50 -03:00
|
|
|
return (SRE_CHAR*) state->ptr - ptr;
|
|
|
|
}
|
|
|
|
|
2013-09-05 12:02:57 -03:00
|
|
|
TRACE(("|%p|%p|COUNT %" PY_FORMAT_SIZE_T "d\n", pattern, ptr,
|
|
|
|
ptr - (SRE_CHAR*) state->ptr));
|
2000-08-03 13:29:50 -03:00
|
|
|
return ptr - (SRE_CHAR*) state->ptr;
|
|
|
|
}
|
|
|
|
|
2000-08-27 17:59:47 -03:00
|
|
|
#if 0 /* not used in this release */
|
2000-08-07 17:59:04 -03:00
|
|
|
LOCAL(int)
|
|
|
|
SRE_INFO(SRE_STATE* state, SRE_CODE* pattern)
|
|
|
|
{
|
|
|
|
/* check if an SRE_OP_INFO block matches at the current position.
|
|
|
|
returns the number of SRE_CODE objects to skip if successful, 0
|
|
|
|
if no match */
|
|
|
|
|
|
|
|
SRE_CHAR* end = state->end;
|
|
|
|
SRE_CHAR* ptr = state->ptr;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t i;
|
2000-08-07 17:59:04 -03:00
|
|
|
|
|
|
|
/* check minimal length */
|
|
|
|
if (pattern[3] && (end - ptr) < pattern[3])
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* check known prefix */
|
|
|
|
if (pattern[2] & SRE_INFO_PREFIX && pattern[5] > 1) {
|
|
|
|
/* <length> <skip> <prefix data> <overlap data> */
|
|
|
|
for (i = 0; i < pattern[5]; i++)
|
|
|
|
if ((SRE_CODE) ptr[i] != pattern[7 + i])
|
|
|
|
return 0;
|
|
|
|
return pattern[0] + 2 * pattern[6];
|
|
|
|
}
|
|
|
|
return pattern[0];
|
|
|
|
}
|
2000-08-27 17:59:47 -03:00
|
|
|
#endif
|
2000-08-07 17:59:04 -03:00
|
|
|
|
2003-04-27 11:42:54 -03:00
|
|
|
/* The macros below should be used to protect recursive SRE_MATCH()
|
|
|
|
* calls that *failed* and do *not* return immediately (IOW, those
|
|
|
|
* that will backtrack). Explaining:
|
|
|
|
*
|
|
|
|
* - Recursive SRE_MATCH() returned true: that's usually a success
|
|
|
|
* (besides atypical cases like ASSERT_NOT), therefore there's no
|
|
|
|
* reason to restore lastmark;
|
|
|
|
*
|
|
|
|
* - Recursive SRE_MATCH() returned false but the current SRE_MATCH()
|
|
|
|
* is returning to the caller: If the current SRE_MATCH() is the
|
|
|
|
* top function of the recursion, returning false will be a matching
|
|
|
|
* failure, and it doesn't matter where lastmark is pointing to.
|
|
|
|
* If it's *not* the top function, it will be a recursive SRE_MATCH()
|
|
|
|
* failure by itself, and the calling SRE_MATCH() will have to deal
|
|
|
|
* with the failure by the same rules explained here (it will restore
|
|
|
|
* lastmark by itself if necessary);
|
|
|
|
*
|
|
|
|
* - Recursive SRE_MATCH() returned false, and will continue the
|
|
|
|
* outside 'for' loop: must be protected when breaking, since the next
|
|
|
|
* OP could potentially depend on lastmark;
|
2006-01-20 22:47:53 -04:00
|
|
|
*
|
2003-04-27 11:42:54 -03:00
|
|
|
* - Recursive SRE_MATCH() returned false, and will be called again
|
|
|
|
* inside a local for/while loop: must be protected between each
|
|
|
|
* loop iteration, since the recursive SRE_MATCH() could do anything,
|
|
|
|
* and could potentially depend on lastmark.
|
|
|
|
*
|
|
|
|
* For more information, check the discussion at SF patch #712900.
|
|
|
|
*/
|
2003-04-20 04:35:44 -03:00
|
|
|
#define LASTMARK_SAVE() \
|
|
|
|
do { \
|
2003-10-17 19:13:16 -03:00
|
|
|
ctx->lastmark = state->lastmark; \
|
|
|
|
ctx->lastindex = state->lastindex; \
|
2003-04-20 04:35:44 -03:00
|
|
|
} while (0)
|
|
|
|
#define LASTMARK_RESTORE() \
|
|
|
|
do { \
|
2003-10-17 19:13:16 -03:00
|
|
|
state->lastmark = ctx->lastmark; \
|
|
|
|
state->lastindex = ctx->lastindex; \
|
2003-04-20 04:35:44 -03:00
|
|
|
} while (0)
|
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
#define RETURN_ERROR(i) do { return i; } while(0)
|
|
|
|
#define RETURN_FAILURE do { ret = 0; goto exit; } while(0)
|
|
|
|
#define RETURN_SUCCESS do { ret = 1; goto exit; } while(0)
|
|
|
|
|
|
|
|
#define RETURN_ON_ERROR(i) \
|
|
|
|
do { if (i < 0) RETURN_ERROR(i); } while (0)
|
|
|
|
#define RETURN_ON_SUCCESS(i) \
|
|
|
|
do { RETURN_ON_ERROR(i); if (i > 0) RETURN_SUCCESS; } while (0)
|
|
|
|
#define RETURN_ON_FAILURE(i) \
|
|
|
|
do { RETURN_ON_ERROR(i); if (i == 0) RETURN_FAILURE; } while (0)
|
|
|
|
|
|
|
|
#define SFY(x) #x
|
|
|
|
|
|
|
|
#define DATA_STACK_ALLOC(state, type, ptr) \
|
|
|
|
do { \
|
|
|
|
alloc_pos = state->data_stack_base; \
|
2013-09-05 12:02:57 -03:00
|
|
|
TRACE(("allocating %s in %" PY_FORMAT_SIZE_T "d " \
|
|
|
|
"(%" PY_FORMAT_SIZE_T "d)\n", \
|
2003-10-17 19:13:16 -03:00
|
|
|
SFY(type), alloc_pos, sizeof(type))); \
|
2013-04-13 15:15:10 -03:00
|
|
|
if (sizeof(type) > state->data_stack_size - alloc_pos) { \
|
2003-10-17 19:13:16 -03:00
|
|
|
int j = data_stack_grow(state, sizeof(type)); \
|
|
|
|
if (j < 0) return j; \
|
|
|
|
if (ctx_pos != -1) \
|
|
|
|
DATA_STACK_LOOKUP_AT(state, SRE_MATCH_CONTEXT, ctx, ctx_pos); \
|
|
|
|
} \
|
|
|
|
ptr = (type*)(state->data_stack+alloc_pos); \
|
|
|
|
state->data_stack_base += sizeof(type); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define DATA_STACK_LOOKUP_AT(state, type, ptr, pos) \
|
|
|
|
do { \
|
2013-09-05 12:02:57 -03:00
|
|
|
TRACE(("looking up %s at %" PY_FORMAT_SIZE_T "d\n", SFY(type), pos)); \
|
2003-10-17 19:13:16 -03:00
|
|
|
ptr = (type*)(state->data_stack+pos); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define DATA_STACK_PUSH(state, data, size) \
|
|
|
|
do { \
|
2013-09-05 12:02:57 -03:00
|
|
|
TRACE(("copy data in %p to %" PY_FORMAT_SIZE_T "d " \
|
|
|
|
"(%" PY_FORMAT_SIZE_T "d)\n", \
|
2003-10-17 19:13:16 -03:00
|
|
|
data, state->data_stack_base, size)); \
|
2013-04-13 15:15:10 -03:00
|
|
|
if (size > state->data_stack_size - state->data_stack_base) { \
|
2003-10-17 19:13:16 -03:00
|
|
|
int j = data_stack_grow(state, size); \
|
|
|
|
if (j < 0) return j; \
|
|
|
|
if (ctx_pos != -1) \
|
|
|
|
DATA_STACK_LOOKUP_AT(state, SRE_MATCH_CONTEXT, ctx, ctx_pos); \
|
|
|
|
} \
|
|
|
|
memcpy(state->data_stack+state->data_stack_base, data, size); \
|
|
|
|
state->data_stack_base += size; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define DATA_STACK_POP(state, data, size, discard) \
|
|
|
|
do { \
|
2013-09-05 12:02:57 -03:00
|
|
|
TRACE(("copy data to %p from %" PY_FORMAT_SIZE_T "d " \
|
|
|
|
"(%" PY_FORMAT_SIZE_T "d)\n", \
|
2003-10-17 19:13:16 -03:00
|
|
|
data, state->data_stack_base-size, size)); \
|
|
|
|
memcpy(data, state->data_stack+state->data_stack_base-size, size); \
|
|
|
|
if (discard) \
|
|
|
|
state->data_stack_base -= size; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define DATA_STACK_POP_DISCARD(state, size) \
|
|
|
|
do { \
|
2013-09-05 12:02:57 -03:00
|
|
|
TRACE(("discard data from %" PY_FORMAT_SIZE_T "d " \
|
|
|
|
"(%" PY_FORMAT_SIZE_T "d)\n", \
|
2003-10-17 19:13:16 -03:00
|
|
|
state->data_stack_base-size, size)); \
|
|
|
|
state->data_stack_base -= size; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define DATA_PUSH(x) \
|
|
|
|
DATA_STACK_PUSH(state, (x), sizeof(*(x)))
|
|
|
|
#define DATA_POP(x) \
|
|
|
|
DATA_STACK_POP(state, (x), sizeof(*(x)), 1)
|
|
|
|
#define DATA_POP_DISCARD(x) \
|
|
|
|
DATA_STACK_POP_DISCARD(state, sizeof(*(x)))
|
|
|
|
#define DATA_ALLOC(t,p) \
|
|
|
|
DATA_STACK_ALLOC(state, t, p)
|
|
|
|
#define DATA_LOOKUP_AT(t,p,pos) \
|
|
|
|
DATA_STACK_LOOKUP_AT(state,t,p,pos)
|
|
|
|
|
|
|
|
#define MARK_PUSH(lastmark) \
|
|
|
|
do if (lastmark > 0) { \
|
|
|
|
i = lastmark; /* ctx->lastmark may change if reallocated */ \
|
|
|
|
DATA_STACK_PUSH(state, state->mark, (i+1)*sizeof(void*)); \
|
|
|
|
} while (0)
|
|
|
|
#define MARK_POP(lastmark) \
|
|
|
|
do if (lastmark > 0) { \
|
|
|
|
DATA_STACK_POP(state, state->mark, (lastmark+1)*sizeof(void*), 1); \
|
|
|
|
} while (0)
|
|
|
|
#define MARK_POP_KEEP(lastmark) \
|
|
|
|
do if (lastmark > 0) { \
|
|
|
|
DATA_STACK_POP(state, state->mark, (lastmark+1)*sizeof(void*), 0); \
|
|
|
|
} while (0)
|
|
|
|
#define MARK_POP_DISCARD(lastmark) \
|
|
|
|
do if (lastmark > 0) { \
|
|
|
|
DATA_STACK_POP_DISCARD(state, (lastmark+1)*sizeof(void*)); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define JUMP_NONE 0
|
|
|
|
#define JUMP_MAX_UNTIL_1 1
|
|
|
|
#define JUMP_MAX_UNTIL_2 2
|
|
|
|
#define JUMP_MAX_UNTIL_3 3
|
|
|
|
#define JUMP_MIN_UNTIL_1 4
|
|
|
|
#define JUMP_MIN_UNTIL_2 5
|
|
|
|
#define JUMP_MIN_UNTIL_3 6
|
|
|
|
#define JUMP_REPEAT 7
|
|
|
|
#define JUMP_REPEAT_ONE_1 8
|
|
|
|
#define JUMP_REPEAT_ONE_2 9
|
|
|
|
#define JUMP_MIN_REPEAT_ONE 10
|
|
|
|
#define JUMP_BRANCH 11
|
|
|
|
#define JUMP_ASSERT 12
|
|
|
|
#define JUMP_ASSERT_NOT 13
|
|
|
|
|
|
|
|
#define DO_JUMP(jumpvalue, jumplabel, nextpattern) \
|
|
|
|
DATA_ALLOC(SRE_MATCH_CONTEXT, nextctx); \
|
|
|
|
nextctx->last_ctx_pos = ctx_pos; \
|
|
|
|
nextctx->jump = jumpvalue; \
|
|
|
|
nextctx->pattern = nextpattern; \
|
|
|
|
ctx_pos = alloc_pos; \
|
|
|
|
ctx = nextctx; \
|
|
|
|
goto entrance; \
|
|
|
|
jumplabel: \
|
|
|
|
while (0) /* gcc doesn't like labels at end of scopes */ \
|
|
|
|
|
|
|
|
typedef struct {
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t last_ctx_pos;
|
|
|
|
Py_ssize_t jump;
|
2003-10-17 19:13:16 -03:00
|
|
|
SRE_CHAR* ptr;
|
|
|
|
SRE_CODE* pattern;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t count;
|
|
|
|
Py_ssize_t lastmark;
|
|
|
|
Py_ssize_t lastindex;
|
2003-10-17 19:13:16 -03:00
|
|
|
union {
|
|
|
|
SRE_CODE chr;
|
|
|
|
SRE_REPEAT* rep;
|
|
|
|
} u;
|
|
|
|
} SRE_MATCH_CONTEXT;
|
|
|
|
|
|
|
|
/* check if string matches the given pattern. returns <0 for
|
|
|
|
error, 0 for failure, and 1 for success */
|
2006-06-12 00:05:40 -03:00
|
|
|
LOCAL(Py_ssize_t)
|
2003-12-13 16:32:08 -04:00
|
|
|
SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2006-04-12 01:26:11 -03:00
|
|
|
SRE_CHAR* end = (SRE_CHAR *)state->end;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t alloc_pos, ctx_pos = -1;
|
|
|
|
Py_ssize_t i, ret = 0;
|
|
|
|
Py_ssize_t jump;
|
2008-01-08 17:10:12 -04:00
|
|
|
unsigned int sigcount=0;
|
2000-08-01 15:20:07 -03:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
SRE_MATCH_CONTEXT* ctx;
|
|
|
|
SRE_MATCH_CONTEXT* nextctx;
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2003-12-13 16:32:08 -04:00
|
|
|
TRACE(("|%p|%p|ENTER\n", pattern, state->ptr));
|
2000-08-03 13:29:50 -03:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
DATA_ALLOC(SRE_MATCH_CONTEXT, ctx);
|
|
|
|
ctx->last_ctx_pos = -1;
|
|
|
|
ctx->jump = JUMP_NONE;
|
|
|
|
ctx->pattern = pattern;
|
|
|
|
ctx_pos = alloc_pos;
|
|
|
|
|
|
|
|
entrance:
|
|
|
|
|
2006-04-12 01:26:11 -03:00
|
|
|
ctx->ptr = (SRE_CHAR *)state->ptr;
|
2003-10-17 19:13:16 -03:00
|
|
|
|
|
|
|
if (ctx->pattern[0] == SRE_OP_INFO) {
|
2000-06-29 20:33:12 -03:00
|
|
|
/* optimization info block */
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <INFO> <1=skip> <2=flags> <3=min> ... */
|
2003-10-17 19:13:16 -03:00
|
|
|
if (ctx->pattern[3] && (end - ctx->ptr) < ctx->pattern[3]) {
|
2013-09-05 12:02:57 -03:00
|
|
|
TRACE(("reject (got %" PY_FORMAT_SIZE_T "d chars, "
|
|
|
|
"need %" PY_FORMAT_SIZE_T "d)\n",
|
|
|
|
(end - ctx->ptr), (Py_ssize_t) ctx->pattern[3]));
|
2003-10-17 19:13:16 -03:00
|
|
|
RETURN_FAILURE;
|
2000-06-29 20:33:12 -03:00
|
|
|
}
|
2003-10-17 19:13:16 -03:00
|
|
|
ctx->pattern += ctx->pattern[1] + 1;
|
2000-06-29 20:33:12 -03:00
|
|
|
}
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
for (;;) {
|
2008-01-08 17:10:12 -04:00
|
|
|
++sigcount;
|
|
|
|
if ((0 == (sigcount & 0xfff)) && PyErr_CheckSignals())
|
|
|
|
RETURN_ERROR(SRE_ERROR_INTERRUPTED);
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
switch (*ctx->pattern++) {
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
case SRE_OP_MARK:
|
|
|
|
/* set mark */
|
|
|
|
/* <MARK> <gid> */
|
|
|
|
TRACE(("|%p|%p|MARK %d\n", ctx->pattern,
|
|
|
|
ctx->ptr, ctx->pattern[0]));
|
|
|
|
i = ctx->pattern[0];
|
|
|
|
if (i & 1)
|
|
|
|
state->lastindex = i/2 + 1;
|
|
|
|
if (i > state->lastmark) {
|
|
|
|
/* state->lastmark is the highest valid index in the
|
|
|
|
state->mark array. If it is increased by more than 1,
|
|
|
|
the intervening marks must be set to NULL to signal
|
2006-01-20 22:47:53 -04:00
|
|
|
that these marks have not been encountered. */
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t j = state->lastmark + 1;
|
2003-10-17 19:13:16 -03:00
|
|
|
while (j < i)
|
|
|
|
state->mark[j++] = NULL;
|
|
|
|
state->lastmark = i;
|
|
|
|
}
|
|
|
|
state->mark[i] = ctx->ptr;
|
|
|
|
ctx->pattern++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_LITERAL:
|
|
|
|
/* match literal string */
|
|
|
|
/* <LITERAL> <code> */
|
|
|
|
TRACE(("|%p|%p|LITERAL %d\n", ctx->pattern,
|
|
|
|
ctx->ptr, *ctx->pattern));
|
|
|
|
if (ctx->ptr >= end || (SRE_CODE) ctx->ptr[0] != ctx->pattern[0])
|
|
|
|
RETURN_FAILURE;
|
|
|
|
ctx->pattern++;
|
|
|
|
ctx->ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_NOT_LITERAL:
|
|
|
|
/* match anything that is not literal character */
|
|
|
|
/* <NOT_LITERAL> <code> */
|
|
|
|
TRACE(("|%p|%p|NOT_LITERAL %d\n", ctx->pattern,
|
|
|
|
ctx->ptr, *ctx->pattern));
|
|
|
|
if (ctx->ptr >= end || (SRE_CODE) ctx->ptr[0] == ctx->pattern[0])
|
|
|
|
RETURN_FAILURE;
|
|
|
|
ctx->pattern++;
|
|
|
|
ctx->ptr++;
|
|
|
|
break;
|
2000-07-23 18:46:17 -03:00
|
|
|
|
|
|
|
case SRE_OP_SUCCESS:
|
|
|
|
/* end of pattern */
|
2003-10-17 19:13:16 -03:00
|
|
|
TRACE(("|%p|%p|SUCCESS\n", ctx->pattern, ctx->ptr));
|
|
|
|
state->ptr = ctx->ptr;
|
|
|
|
RETURN_SUCCESS;
|
2000-07-23 18:46:17 -03:00
|
|
|
|
|
|
|
case SRE_OP_AT:
|
|
|
|
/* match at given position */
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <AT> <code> */
|
2003-10-17 19:13:16 -03:00
|
|
|
TRACE(("|%p|%p|AT %d\n", ctx->pattern, ctx->ptr, *ctx->pattern));
|
|
|
|
if (!SRE_AT(state, ctx->ptr, *ctx->pattern))
|
|
|
|
RETURN_FAILURE;
|
|
|
|
ctx->pattern++;
|
2000-07-23 18:46:17 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_CATEGORY:
|
|
|
|
/* match at given category */
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <CATEGORY> <code> */
|
2003-10-17 19:13:16 -03:00
|
|
|
TRACE(("|%p|%p|CATEGORY %d\n", ctx->pattern,
|
|
|
|
ctx->ptr, *ctx->pattern));
|
|
|
|
if (ctx->ptr >= end || !sre_category(ctx->pattern[0], ctx->ptr[0]))
|
|
|
|
RETURN_FAILURE;
|
|
|
|
ctx->pattern++;
|
|
|
|
ctx->ptr++;
|
2000-07-23 18:46:17 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_ANY:
|
2000-08-01 19:47:49 -03:00
|
|
|
/* match anything (except a newline) */
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <ANY> */
|
2003-10-17 19:13:16 -03:00
|
|
|
TRACE(("|%p|%p|ANY\n", ctx->pattern, ctx->ptr));
|
|
|
|
if (ctx->ptr >= end || SRE_IS_LINEBREAK(ctx->ptr[0]))
|
|
|
|
RETURN_FAILURE;
|
|
|
|
ctx->ptr++;
|
2000-08-01 19:47:49 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_ANY_ALL:
|
|
|
|
/* match anything */
|
|
|
|
/* <ANY_ALL> */
|
2003-10-17 19:13:16 -03:00
|
|
|
TRACE(("|%p|%p|ANY_ALL\n", ctx->pattern, ctx->ptr));
|
|
|
|
if (ctx->ptr >= end)
|
|
|
|
RETURN_FAILURE;
|
|
|
|
ctx->ptr++;
|
2000-07-23 18:46:17 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_IN:
|
|
|
|
/* match set member (or non_member) */
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <IN> <skip> <set> */
|
2003-10-17 19:13:16 -03:00
|
|
|
TRACE(("|%p|%p|IN\n", ctx->pattern, ctx->ptr));
|
|
|
|
if (ctx->ptr >= end || !SRE_CHARSET(ctx->pattern + 1, *ctx->ptr))
|
|
|
|
RETURN_FAILURE;
|
|
|
|
ctx->pattern += ctx->pattern[0];
|
|
|
|
ctx->ptr++;
|
2000-07-23 18:46:17 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_LITERAL_IGNORE:
|
2003-10-17 19:13:16 -03:00
|
|
|
TRACE(("|%p|%p|LITERAL_IGNORE %d\n",
|
|
|
|
ctx->pattern, ctx->ptr, ctx->pattern[0]));
|
|
|
|
if (ctx->ptr >= end ||
|
|
|
|
state->lower(*ctx->ptr) != state->lower(*ctx->pattern))
|
|
|
|
RETURN_FAILURE;
|
|
|
|
ctx->pattern++;
|
|
|
|
ctx->ptr++;
|
2000-07-23 18:46:17 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_NOT_LITERAL_IGNORE:
|
2003-10-17 19:13:16 -03:00
|
|
|
TRACE(("|%p|%p|NOT_LITERAL_IGNORE %d\n",
|
|
|
|
ctx->pattern, ctx->ptr, *ctx->pattern));
|
|
|
|
if (ctx->ptr >= end ||
|
|
|
|
state->lower(*ctx->ptr) == state->lower(*ctx->pattern))
|
|
|
|
RETURN_FAILURE;
|
|
|
|
ctx->pattern++;
|
|
|
|
ctx->ptr++;
|
2000-07-23 18:46:17 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_IN_IGNORE:
|
2003-10-17 19:13:16 -03:00
|
|
|
TRACE(("|%p|%p|IN_IGNORE\n", ctx->pattern, ctx->ptr));
|
|
|
|
if (ctx->ptr >= end
|
|
|
|
|| !SRE_CHARSET(ctx->pattern+1,
|
|
|
|
(SRE_CODE)state->lower(*ctx->ptr)))
|
|
|
|
RETURN_FAILURE;
|
|
|
|
ctx->pattern += ctx->pattern[0];
|
|
|
|
ctx->ptr++;
|
2000-07-23 18:46:17 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_JUMP:
|
|
|
|
case SRE_OP_INFO:
|
|
|
|
/* jump forward */
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <JUMP> <offset> */
|
2003-10-17 19:13:16 -03:00
|
|
|
TRACE(("|%p|%p|JUMP %d\n", ctx->pattern,
|
|
|
|
ctx->ptr, ctx->pattern[0]));
|
|
|
|
ctx->pattern += ctx->pattern[0];
|
2000-07-23 18:46:17 -03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_BRANCH:
|
2000-08-01 15:20:07 -03:00
|
|
|
/* alternation */
|
|
|
|
/* <BRANCH> <0=skip> code <JUMP> ... <NULL> */
|
2003-10-17 19:13:16 -03:00
|
|
|
TRACE(("|%p|%p|BRANCH\n", ctx->pattern, ctx->ptr));
|
2003-04-20 04:35:44 -03:00
|
|
|
LASTMARK_SAVE();
|
2003-10-17 19:13:16 -03:00
|
|
|
ctx->u.rep = state->repeat;
|
|
|
|
if (ctx->u.rep)
|
|
|
|
MARK_PUSH(ctx->lastmark);
|
|
|
|
for (; ctx->pattern[0]; ctx->pattern += ctx->pattern[0]) {
|
|
|
|
if (ctx->pattern[1] == SRE_OP_LITERAL &&
|
|
|
|
(ctx->ptr >= end ||
|
|
|
|
(SRE_CODE) *ctx->ptr != ctx->pattern[2]))
|
2000-08-07 17:59:04 -03:00
|
|
|
continue;
|
2003-10-17 19:13:16 -03:00
|
|
|
if (ctx->pattern[1] == SRE_OP_IN &&
|
|
|
|
(ctx->ptr >= end ||
|
|
|
|
!SRE_CHARSET(ctx->pattern + 3, (SRE_CODE) *ctx->ptr)))
|
2000-08-07 17:59:04 -03:00
|
|
|
continue;
|
2003-10-17 19:13:16 -03:00
|
|
|
state->ptr = ctx->ptr;
|
|
|
|
DO_JUMP(JUMP_BRANCH, jump_branch, ctx->pattern+1);
|
|
|
|
if (ret) {
|
|
|
|
if (ctx->u.rep)
|
|
|
|
MARK_POP_DISCARD(ctx->lastmark);
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
2003-04-27 09:34:14 -03:00
|
|
|
}
|
2003-10-17 19:13:16 -03:00
|
|
|
if (ctx->u.rep)
|
|
|
|
MARK_POP_KEEP(ctx->lastmark);
|
2003-04-20 04:35:44 -03:00
|
|
|
LASTMARK_RESTORE();
|
2000-08-01 15:20:07 -03:00
|
|
|
}
|
2003-10-17 19:13:16 -03:00
|
|
|
if (ctx->u.rep)
|
|
|
|
MARK_POP_DISCARD(ctx->lastmark);
|
|
|
|
RETURN_FAILURE;
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2000-08-01 18:05:41 -03:00
|
|
|
case SRE_OP_REPEAT_ONE:
|
|
|
|
/* match repeated sequence (maximizing regexp) */
|
|
|
|
|
|
|
|
/* this operator only works if the repeated item is
|
|
|
|
exactly one character wide, and we're not already
|
|
|
|
collecting backtracking points. for other cases,
|
2001-01-14 11:06:11 -04:00
|
|
|
use the MAX_REPEAT operator */
|
2000-08-01 18:05:41 -03:00
|
|
|
|
|
|
|
/* <REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */
|
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
TRACE(("|%p|%p|REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr,
|
|
|
|
ctx->pattern[1], ctx->pattern[2]));
|
2000-08-01 18:05:41 -03:00
|
|
|
|
2013-08-03 13:26:33 -03:00
|
|
|
if ((Py_ssize_t) ctx->pattern[1] > end - ctx->ptr)
|
2003-10-17 19:13:16 -03:00
|
|
|
RETURN_FAILURE; /* cannot match */
|
2000-08-01 19:47:49 -03:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
state->ptr = ctx->ptr;
|
2000-08-01 18:05:41 -03:00
|
|
|
|
2004-12-02 12:15:39 -04:00
|
|
|
ret = SRE_COUNT(state, ctx->pattern+3, ctx->pattern[2]);
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos);
|
|
|
|
ctx->count = ret;
|
2003-10-17 19:13:16 -03:00
|
|
|
ctx->ptr += ctx->count;
|
2000-08-01 18:05:41 -03:00
|
|
|
|
|
|
|
/* when we arrive here, count contains the number of
|
2003-10-17 19:13:16 -03:00
|
|
|
matches, and ctx->ptr points to the tail of the target
|
2000-08-01 18:05:41 -03:00
|
|
|
string. check if the rest of the pattern matches,
|
|
|
|
and backtrack if not. */
|
|
|
|
|
2006-06-12 00:05:40 -03:00
|
|
|
if (ctx->count < (Py_ssize_t) ctx->pattern[1])
|
2003-10-17 19:13:16 -03:00
|
|
|
RETURN_FAILURE;
|
2000-08-01 18:05:41 -03:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS) {
|
2000-08-01 18:05:41 -03:00
|
|
|
/* tail is empty. we're finished */
|
2003-10-17 19:13:16 -03:00
|
|
|
state->ptr = ctx->ptr;
|
|
|
|
RETURN_SUCCESS;
|
2003-04-20 04:35:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
LASTMARK_SAVE();
|
2000-08-01 18:05:41 -03:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
if (ctx->pattern[ctx->pattern[0]] == SRE_OP_LITERAL) {
|
2000-08-01 18:05:41 -03:00
|
|
|
/* tail starts with a literal. skip positions where
|
|
|
|
the rest of the pattern cannot possibly match */
|
2003-10-17 19:13:16 -03:00
|
|
|
ctx->u.chr = ctx->pattern[ctx->pattern[0]+1];
|
2000-08-01 18:05:41 -03:00
|
|
|
for (;;) {
|
2006-06-12 00:05:40 -03:00
|
|
|
while (ctx->count >= (Py_ssize_t) ctx->pattern[1] &&
|
2003-10-17 19:13:16 -03:00
|
|
|
(ctx->ptr >= end || *ctx->ptr != ctx->u.chr)) {
|
|
|
|
ctx->ptr--;
|
|
|
|
ctx->count--;
|
2000-08-01 18:05:41 -03:00
|
|
|
}
|
2006-06-12 00:05:40 -03:00
|
|
|
if (ctx->count < (Py_ssize_t) ctx->pattern[1])
|
2000-08-01 18:05:41 -03:00
|
|
|
break;
|
2003-10-17 19:13:16 -03:00
|
|
|
state->ptr = ctx->ptr;
|
|
|
|
DO_JUMP(JUMP_REPEAT_ONE_1, jump_repeat_one_1,
|
|
|
|
ctx->pattern+ctx->pattern[0]);
|
|
|
|
if (ret) {
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
2006-01-20 22:47:53 -04:00
|
|
|
|
2003-04-20 04:35:44 -03:00
|
|
|
LASTMARK_RESTORE();
|
2006-01-20 22:47:53 -04:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
ctx->ptr--;
|
|
|
|
ctx->count--;
|
2000-08-01 18:05:41 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* general case */
|
2006-06-12 00:05:40 -03:00
|
|
|
while (ctx->count >= (Py_ssize_t) ctx->pattern[1]) {
|
2003-10-17 19:13:16 -03:00
|
|
|
state->ptr = ctx->ptr;
|
|
|
|
DO_JUMP(JUMP_REPEAT_ONE_2, jump_repeat_one_2,
|
|
|
|
ctx->pattern+ctx->pattern[0]);
|
|
|
|
if (ret) {
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
|
|
|
ctx->ptr--;
|
|
|
|
ctx->count--;
|
2003-04-20 04:35:44 -03:00
|
|
|
LASTMARK_RESTORE();
|
2000-08-01 18:05:41 -03:00
|
|
|
}
|
|
|
|
}
|
2003-10-17 19:13:16 -03:00
|
|
|
RETURN_FAILURE;
|
2000-08-01 18:05:41 -03:00
|
|
|
|
2003-04-14 14:59:34 -03:00
|
|
|
case SRE_OP_MIN_REPEAT_ONE:
|
|
|
|
/* match repeated sequence (minimizing regexp) */
|
|
|
|
|
|
|
|
/* this operator only works if the repeated item is
|
|
|
|
exactly one character wide, and we're not already
|
|
|
|
collecting backtracking points. for other cases,
|
|
|
|
use the MIN_REPEAT operator */
|
|
|
|
|
|
|
|
/* <MIN_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */
|
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr,
|
|
|
|
ctx->pattern[1], ctx->pattern[2]));
|
2003-04-14 14:59:34 -03:00
|
|
|
|
2013-08-03 13:26:33 -03:00
|
|
|
if ((Py_ssize_t) ctx->pattern[1] > end - ctx->ptr)
|
2003-10-17 19:13:16 -03:00
|
|
|
RETURN_FAILURE; /* cannot match */
|
2003-04-14 14:59:34 -03:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
state->ptr = ctx->ptr;
|
2003-04-14 14:59:34 -03:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
if (ctx->pattern[1] == 0)
|
|
|
|
ctx->count = 0;
|
2003-04-14 14:59:34 -03:00
|
|
|
else {
|
|
|
|
/* count using pattern min as the maximum */
|
2004-12-02 12:15:39 -04:00
|
|
|
ret = SRE_COUNT(state, ctx->pattern+3, ctx->pattern[1]);
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos);
|
2006-06-12 00:05:40 -03:00
|
|
|
if (ret < (Py_ssize_t) ctx->pattern[1])
|
2006-01-20 22:47:53 -04:00
|
|
|
/* didn't match minimum number of times */
|
2003-10-17 19:13:16 -03:00
|
|
|
RETURN_FAILURE;
|
|
|
|
/* advance past minimum matches of repeat */
|
2004-12-02 12:15:39 -04:00
|
|
|
ctx->count = ret;
|
2003-10-17 19:13:16 -03:00
|
|
|
ctx->ptr += ctx->count;
|
2003-04-14 14:59:34 -03:00
|
|
|
}
|
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS) {
|
2003-04-14 14:59:34 -03:00
|
|
|
/* tail is empty. we're finished */
|
2003-10-17 19:13:16 -03:00
|
|
|
state->ptr = ctx->ptr;
|
|
|
|
RETURN_SUCCESS;
|
2003-04-14 14:59:34 -03:00
|
|
|
|
|
|
|
} else {
|
|
|
|
/* general case */
|
2003-04-20 04:35:44 -03:00
|
|
|
LASTMARK_SAVE();
|
2013-02-16 10:47:15 -04:00
|
|
|
while ((Py_ssize_t)ctx->pattern[2] == SRE_MAXREPEAT
|
2006-06-12 00:05:40 -03:00
|
|
|
|| ctx->count <= (Py_ssize_t)ctx->pattern[2]) {
|
2003-10-17 19:13:16 -03:00
|
|
|
state->ptr = ctx->ptr;
|
|
|
|
DO_JUMP(JUMP_MIN_REPEAT_ONE,jump_min_repeat_one,
|
|
|
|
ctx->pattern+ctx->pattern[0]);
|
|
|
|
if (ret) {
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
|
|
|
state->ptr = ctx->ptr;
|
2003-12-13 16:32:08 -04:00
|
|
|
ret = SRE_COUNT(state, ctx->pattern+3, 1);
|
2003-10-17 19:13:16 -03:00
|
|
|
RETURN_ON_ERROR(ret);
|
2004-12-02 12:15:39 -04:00
|
|
|
DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos);
|
2003-10-17 19:13:16 -03:00
|
|
|
if (ret == 0)
|
2003-04-14 14:59:34 -03:00
|
|
|
break;
|
2003-10-17 19:13:16 -03:00
|
|
|
assert(ret == 1);
|
|
|
|
ctx->ptr++;
|
|
|
|
ctx->count++;
|
2003-04-27 11:42:54 -03:00
|
|
|
LASTMARK_RESTORE();
|
2003-04-14 14:59:34 -03:00
|
|
|
}
|
|
|
|
}
|
2003-10-17 19:13:16 -03:00
|
|
|
RETURN_FAILURE;
|
2003-04-14 14:59:34 -03:00
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
case SRE_OP_REPEAT:
|
|
|
|
/* create repeat context. all the hard work is done
|
2001-01-14 11:06:11 -04:00
|
|
|
by the UNTIL operator (MAX_UNTIL, MIN_UNTIL) */
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <REPEAT> <skip> <1=min> <2=max> item <UNTIL> tail */
|
2003-10-17 19:13:16 -03:00
|
|
|
TRACE(("|%p|%p|REPEAT %d %d\n", ctx->pattern, ctx->ptr,
|
|
|
|
ctx->pattern[1], ctx->pattern[2]));
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
/* install new repeat context */
|
2006-05-27 12:44:34 -03:00
|
|
|
ctx->u.rep = (SRE_REPEAT*) PyObject_MALLOC(sizeof(*ctx->u.rep));
|
2006-10-04 10:42:43 -03:00
|
|
|
if (!ctx->u.rep) {
|
|
|
|
PyErr_NoMemory();
|
2006-08-11 22:53:28 -03:00
|
|
|
RETURN_FAILURE;
|
2006-10-04 10:42:43 -03:00
|
|
|
}
|
2003-10-17 19:13:16 -03:00
|
|
|
ctx->u.rep->count = -1;
|
|
|
|
ctx->u.rep->pattern = ctx->pattern;
|
|
|
|
ctx->u.rep->prev = state->repeat;
|
|
|
|
ctx->u.rep->last_ptr = NULL;
|
|
|
|
state->repeat = ctx->u.rep;
|
|
|
|
|
|
|
|
state->ptr = ctx->ptr;
|
|
|
|
DO_JUMP(JUMP_REPEAT, jump_repeat, ctx->pattern+ctx->pattern[0]);
|
|
|
|
state->repeat = ctx->u.rep->prev;
|
2006-05-27 12:44:34 -03:00
|
|
|
PyObject_FREE(ctx->u.rep);
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
if (ret) {
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
|
|
|
RETURN_FAILURE;
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
case SRE_OP_MAX_UNTIL:
|
|
|
|
/* maximizing repeat */
|
|
|
|
/* <REPEAT> <skip> <1=min> <2=max> item <MAX_UNTIL> tail */
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
/* FIXME: we probably need to deal with zero-width
|
|
|
|
matches in here... */
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
ctx->u.rep = state->repeat;
|
|
|
|
if (!ctx->u.rep)
|
|
|
|
RETURN_ERROR(SRE_ERROR_STATE);
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
state->ptr = ctx->ptr;
|
2000-06-29 05:58:44 -03:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
ctx->count = ctx->u.rep->count+1;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2013-09-05 12:02:57 -03:00
|
|
|
TRACE(("|%p|%p|MAX_UNTIL %" PY_FORMAT_SIZE_T "d\n", ctx->pattern,
|
2003-10-17 19:13:16 -03:00
|
|
|
ctx->ptr, ctx->count));
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2013-08-03 13:26:33 -03:00
|
|
|
if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
|
2000-08-01 15:20:07 -03:00
|
|
|
/* not enough matches */
|
2003-10-17 19:13:16 -03:00
|
|
|
ctx->u.rep->count = ctx->count;
|
|
|
|
DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1,
|
|
|
|
ctx->u.rep->pattern+3);
|
|
|
|
if (ret) {
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
|
|
|
ctx->u.rep->count = ctx->count-1;
|
|
|
|
state->ptr = ctx->ptr;
|
|
|
|
RETURN_FAILURE;
|
2000-06-29 05:58:44 -03:00
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2013-08-03 13:26:33 -03:00
|
|
|
if ((ctx->count < (Py_ssize_t) ctx->u.rep->pattern[2] ||
|
2013-02-16 10:47:15 -04:00
|
|
|
ctx->u.rep->pattern[2] == SRE_MAXREPEAT) &&
|
2003-10-17 19:13:16 -03:00
|
|
|
state->ptr != ctx->u.rep->last_ptr) {
|
2000-08-01 15:20:07 -03:00
|
|
|
/* we may have enough matches, but if we can
|
|
|
|
match another item, do so */
|
2003-10-17 19:13:16 -03:00
|
|
|
ctx->u.rep->count = ctx->count;
|
2003-04-27 11:42:54 -03:00
|
|
|
LASTMARK_SAVE();
|
2003-10-17 19:13:16 -03:00
|
|
|
MARK_PUSH(ctx->lastmark);
|
|
|
|
/* zero-width match protection */
|
|
|
|
DATA_PUSH(&ctx->u.rep->last_ptr);
|
|
|
|
ctx->u.rep->last_ptr = state->ptr;
|
|
|
|
DO_JUMP(JUMP_MAX_UNTIL_2, jump_max_until_2,
|
|
|
|
ctx->u.rep->pattern+3);
|
|
|
|
DATA_POP(&ctx->u.rep->last_ptr);
|
|
|
|
if (ret) {
|
|
|
|
MARK_POP_DISCARD(ctx->lastmark);
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
|
|
|
MARK_POP(ctx->lastmark);
|
2003-04-27 11:42:54 -03:00
|
|
|
LASTMARK_RESTORE();
|
2003-10-17 19:13:16 -03:00
|
|
|
ctx->u.rep->count = ctx->count-1;
|
|
|
|
state->ptr = ctx->ptr;
|
2000-07-23 18:46:17 -03:00
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
/* cannot match more repeated items here. make sure the
|
|
|
|
tail matches */
|
2003-10-17 19:13:16 -03:00
|
|
|
state->repeat = ctx->u.rep->prev;
|
|
|
|
DO_JUMP(JUMP_MAX_UNTIL_3, jump_max_until_3, ctx->pattern);
|
|
|
|
RETURN_ON_SUCCESS(ret);
|
|
|
|
state->repeat = ctx->u.rep;
|
|
|
|
state->ptr = ctx->ptr;
|
|
|
|
RETURN_FAILURE;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
case SRE_OP_MIN_UNTIL:
|
|
|
|
/* minimizing repeat */
|
|
|
|
/* <REPEAT> <skip> <1=min> <2=max> item <MIN_UNTIL> tail */
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
ctx->u.rep = state->repeat;
|
|
|
|
if (!ctx->u.rep)
|
|
|
|
RETURN_ERROR(SRE_ERROR_STATE);
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
state->ptr = ctx->ptr;
|
2003-04-22 12:39:09 -03:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
ctx->count = ctx->u.rep->count+1;
|
2000-08-01 15:20:07 -03:00
|
|
|
|
2013-09-05 12:02:57 -03:00
|
|
|
TRACE(("|%p|%p|MIN_UNTIL %" PY_FORMAT_SIZE_T "d %p\n", ctx->pattern,
|
2003-10-17 19:13:16 -03:00
|
|
|
ctx->ptr, ctx->count, ctx->u.rep->pattern));
|
2000-07-03 18:31:48 -03:00
|
|
|
|
2013-08-03 13:26:33 -03:00
|
|
|
if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) {
|
2000-08-01 15:20:07 -03:00
|
|
|
/* not enough matches */
|
2003-10-17 19:13:16 -03:00
|
|
|
ctx->u.rep->count = ctx->count;
|
|
|
|
DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1,
|
|
|
|
ctx->u.rep->pattern+3);
|
|
|
|
if (ret) {
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
|
|
|
ctx->u.rep->count = ctx->count-1;
|
|
|
|
state->ptr = ctx->ptr;
|
|
|
|
RETURN_FAILURE;
|
2000-07-23 18:46:17 -03:00
|
|
|
}
|
|
|
|
|
2003-04-27 11:42:54 -03:00
|
|
|
LASTMARK_SAVE();
|
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
/* see if the tail matches */
|
2003-10-17 19:13:16 -03:00
|
|
|
state->repeat = ctx->u.rep->prev;
|
|
|
|
DO_JUMP(JUMP_MIN_UNTIL_2, jump_min_until_2, ctx->pattern);
|
|
|
|
if (ret) {
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
2000-08-01 15:20:07 -03:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
state->repeat = ctx->u.rep;
|
|
|
|
state->ptr = ctx->ptr;
|
2000-08-01 15:20:07 -03:00
|
|
|
|
2003-04-27 11:42:54 -03:00
|
|
|
LASTMARK_RESTORE();
|
|
|
|
|
2013-08-03 13:26:33 -03:00
|
|
|
if ((ctx->count >= (Py_ssize_t) ctx->u.rep->pattern[2]
|
2013-02-16 15:23:01 -04:00
|
|
|
&& ctx->u.rep->pattern[2] != SRE_MAXREPEAT) ||
|
|
|
|
state->ptr == ctx->u.rep->last_ptr)
|
2003-10-17 19:13:16 -03:00
|
|
|
RETURN_FAILURE;
|
|
|
|
|
|
|
|
ctx->u.rep->count = ctx->count;
|
2013-02-16 15:23:01 -04:00
|
|
|
/* zero-width match protection */
|
|
|
|
DATA_PUSH(&ctx->u.rep->last_ptr);
|
|
|
|
ctx->u.rep->last_ptr = state->ptr;
|
2003-10-17 19:13:16 -03:00
|
|
|
DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3,
|
|
|
|
ctx->u.rep->pattern+3);
|
2013-02-16 15:23:01 -04:00
|
|
|
DATA_POP(&ctx->u.rep->last_ptr);
|
2003-10-17 19:13:16 -03:00
|
|
|
if (ret) {
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_SUCCESS;
|
|
|
|
}
|
|
|
|
ctx->u.rep->count = ctx->count-1;
|
|
|
|
state->ptr = ctx->ptr;
|
|
|
|
RETURN_FAILURE;
|
2003-04-27 11:42:54 -03:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
case SRE_OP_GROUPREF:
|
|
|
|
/* match backreference */
|
|
|
|
TRACE(("|%p|%p|GROUPREF %d\n", ctx->pattern,
|
|
|
|
ctx->ptr, ctx->pattern[0]));
|
|
|
|
i = ctx->pattern[0];
|
|
|
|
{
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t groupref = i+i;
|
2003-10-17 19:13:16 -03:00
|
|
|
if (groupref >= state->lastmark) {
|
|
|
|
RETURN_FAILURE;
|
|
|
|
} else {
|
|
|
|
SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
|
|
|
|
SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
|
|
|
|
if (!p || !e || e < p)
|
|
|
|
RETURN_FAILURE;
|
|
|
|
while (p < e) {
|
|
|
|
if (ctx->ptr >= end || *ctx->ptr != *p)
|
|
|
|
RETURN_FAILURE;
|
|
|
|
p++; ctx->ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx->pattern++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_GROUPREF_IGNORE:
|
|
|
|
/* match backreference */
|
|
|
|
TRACE(("|%p|%p|GROUPREF_IGNORE %d\n", ctx->pattern,
|
|
|
|
ctx->ptr, ctx->pattern[0]));
|
|
|
|
i = ctx->pattern[0];
|
|
|
|
{
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t groupref = i+i;
|
2003-10-17 19:13:16 -03:00
|
|
|
if (groupref >= state->lastmark) {
|
|
|
|
RETURN_FAILURE;
|
|
|
|
} else {
|
|
|
|
SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
|
|
|
|
SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
|
|
|
|
if (!p || !e || e < p)
|
|
|
|
RETURN_FAILURE;
|
|
|
|
while (p < e) {
|
|
|
|
if (ctx->ptr >= end ||
|
|
|
|
state->lower(*ctx->ptr) != state->lower(*p))
|
|
|
|
RETURN_FAILURE;
|
|
|
|
p++; ctx->ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx->pattern++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_GROUPREF_EXISTS:
|
|
|
|
TRACE(("|%p|%p|GROUPREF_EXISTS %d\n", ctx->pattern,
|
|
|
|
ctx->ptr, ctx->pattern[0]));
|
|
|
|
/* <GROUPREF_EXISTS> <group> <skip> codeyes <JUMP> codeno ... */
|
|
|
|
i = ctx->pattern[0];
|
|
|
|
{
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t groupref = i+i;
|
2003-10-17 19:13:16 -03:00
|
|
|
if (groupref >= state->lastmark) {
|
|
|
|
ctx->pattern += ctx->pattern[1];
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref];
|
|
|
|
SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1];
|
|
|
|
if (!p || !e || e < p) {
|
|
|
|
ctx->pattern += ctx->pattern[1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx->pattern += 2;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_ASSERT:
|
|
|
|
/* assert subpattern */
|
|
|
|
/* <ASSERT> <skip> <back> <pattern> */
|
|
|
|
TRACE(("|%p|%p|ASSERT %d\n", ctx->pattern,
|
|
|
|
ctx->ptr, ctx->pattern[1]));
|
2015-07-06 07:58:24 -03:00
|
|
|
if (ctx->ptr - (SRE_CHAR *)state->beginning < (Py_ssize_t)ctx->pattern[1])
|
2003-10-17 19:13:16 -03:00
|
|
|
RETURN_FAILURE;
|
2015-07-06 07:58:24 -03:00
|
|
|
state->ptr = ctx->ptr - ctx->pattern[1];
|
2003-10-17 19:13:16 -03:00
|
|
|
DO_JUMP(JUMP_ASSERT, jump_assert, ctx->pattern+2);
|
|
|
|
RETURN_ON_FAILURE(ret);
|
|
|
|
ctx->pattern += ctx->pattern[0];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_ASSERT_NOT:
|
|
|
|
/* assert not subpattern */
|
|
|
|
/* <ASSERT_NOT> <skip> <back> <pattern> */
|
|
|
|
TRACE(("|%p|%p|ASSERT_NOT %d\n", ctx->pattern,
|
|
|
|
ctx->ptr, ctx->pattern[1]));
|
2015-07-06 07:58:24 -03:00
|
|
|
if (ctx->ptr - (SRE_CHAR *)state->beginning >= (Py_ssize_t)ctx->pattern[1]) {
|
|
|
|
state->ptr = ctx->ptr - ctx->pattern[1];
|
2003-10-17 19:13:16 -03:00
|
|
|
DO_JUMP(JUMP_ASSERT_NOT, jump_assert_not, ctx->pattern+2);
|
|
|
|
if (ret) {
|
|
|
|
RETURN_ON_ERROR(ret);
|
|
|
|
RETURN_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ctx->pattern += ctx->pattern[0];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_FAILURE:
|
|
|
|
/* immediate failure */
|
|
|
|
TRACE(("|%p|%p|FAILURE\n", ctx->pattern, ctx->ptr));
|
|
|
|
RETURN_FAILURE;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
default:
|
2003-10-17 19:13:16 -03:00
|
|
|
TRACE(("|%p|%p|UNKNOWN %d\n", ctx->pattern, ctx->ptr,
|
|
|
|
ctx->pattern[-1]));
|
|
|
|
RETURN_ERROR(SRE_ERROR_ILLEGAL);
|
2000-07-23 18:46:17 -03:00
|
|
|
}
|
|
|
|
}
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
exit:
|
|
|
|
ctx_pos = ctx->last_ctx_pos;
|
|
|
|
jump = ctx->jump;
|
|
|
|
DATA_POP_DISCARD(ctx);
|
|
|
|
if (ctx_pos == -1)
|
|
|
|
return ret;
|
|
|
|
DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos);
|
|
|
|
|
|
|
|
switch (jump) {
|
|
|
|
case JUMP_MAX_UNTIL_2:
|
|
|
|
TRACE(("|%p|%p|JUMP_MAX_UNTIL_2\n", ctx->pattern, ctx->ptr));
|
|
|
|
goto jump_max_until_2;
|
|
|
|
case JUMP_MAX_UNTIL_3:
|
|
|
|
TRACE(("|%p|%p|JUMP_MAX_UNTIL_3\n", ctx->pattern, ctx->ptr));
|
|
|
|
goto jump_max_until_3;
|
|
|
|
case JUMP_MIN_UNTIL_2:
|
|
|
|
TRACE(("|%p|%p|JUMP_MIN_UNTIL_2\n", ctx->pattern, ctx->ptr));
|
|
|
|
goto jump_min_until_2;
|
|
|
|
case JUMP_MIN_UNTIL_3:
|
|
|
|
TRACE(("|%p|%p|JUMP_MIN_UNTIL_3\n", ctx->pattern, ctx->ptr));
|
|
|
|
goto jump_min_until_3;
|
|
|
|
case JUMP_BRANCH:
|
|
|
|
TRACE(("|%p|%p|JUMP_BRANCH\n", ctx->pattern, ctx->ptr));
|
|
|
|
goto jump_branch;
|
|
|
|
case JUMP_MAX_UNTIL_1:
|
|
|
|
TRACE(("|%p|%p|JUMP_MAX_UNTIL_1\n", ctx->pattern, ctx->ptr));
|
|
|
|
goto jump_max_until_1;
|
|
|
|
case JUMP_MIN_UNTIL_1:
|
|
|
|
TRACE(("|%p|%p|JUMP_MIN_UNTIL_1\n", ctx->pattern, ctx->ptr));
|
|
|
|
goto jump_min_until_1;
|
|
|
|
case JUMP_REPEAT:
|
|
|
|
TRACE(("|%p|%p|JUMP_REPEAT\n", ctx->pattern, ctx->ptr));
|
|
|
|
goto jump_repeat;
|
|
|
|
case JUMP_REPEAT_ONE_1:
|
|
|
|
TRACE(("|%p|%p|JUMP_REPEAT_ONE_1\n", ctx->pattern, ctx->ptr));
|
|
|
|
goto jump_repeat_one_1;
|
|
|
|
case JUMP_REPEAT_ONE_2:
|
|
|
|
TRACE(("|%p|%p|JUMP_REPEAT_ONE_2\n", ctx->pattern, ctx->ptr));
|
|
|
|
goto jump_repeat_one_2;
|
|
|
|
case JUMP_MIN_REPEAT_ONE:
|
|
|
|
TRACE(("|%p|%p|JUMP_MIN_REPEAT_ONE\n", ctx->pattern, ctx->ptr));
|
|
|
|
goto jump_min_repeat_one;
|
|
|
|
case JUMP_ASSERT:
|
|
|
|
TRACE(("|%p|%p|JUMP_ASSERT\n", ctx->pattern, ctx->ptr));
|
|
|
|
goto jump_assert;
|
|
|
|
case JUMP_ASSERT_NOT:
|
|
|
|
TRACE(("|%p|%p|JUMP_ASSERT_NOT\n", ctx->pattern, ctx->ptr));
|
|
|
|
goto jump_assert_not;
|
|
|
|
case JUMP_NONE:
|
2013-09-05 12:02:57 -03:00
|
|
|
TRACE(("|%p|%p|RETURN %" PY_FORMAT_SIZE_T "d\n", ctx->pattern,
|
|
|
|
ctx->ptr, ret));
|
2003-10-17 19:13:16 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret; /* should never get here */
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
2006-06-12 00:05:40 -03:00
|
|
|
LOCAL(Py_ssize_t)
|
2000-03-31 10:59:30 -04:00
|
|
|
SRE_SEARCH(SRE_STATE* state, SRE_CODE* pattern)
|
|
|
|
{
|
2006-04-12 01:26:11 -03:00
|
|
|
SRE_CHAR* ptr = (SRE_CHAR *)state->start;
|
|
|
|
SRE_CHAR* end = (SRE_CHAR *)state->end;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t status = 0;
|
|
|
|
Py_ssize_t prefix_len = 0;
|
|
|
|
Py_ssize_t prefix_skip = 0;
|
2000-07-02 09:00:07 -03:00
|
|
|
SRE_CODE* prefix = NULL;
|
|
|
|
SRE_CODE* charset = NULL;
|
|
|
|
SRE_CODE* overlap = NULL;
|
|
|
|
int flags = 0;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2015-07-06 07:58:24 -03:00
|
|
|
if (ptr > end)
|
|
|
|
return 0;
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
if (pattern[0] == SRE_OP_INFO) {
|
2000-06-29 20:33:12 -03:00
|
|
|
/* optimization info block */
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <INFO> <1=skip> <2=flags> <3=min> <4=max> <5=prefix info> */
|
2000-07-02 09:00:07 -03:00
|
|
|
|
|
|
|
flags = pattern[2];
|
2000-06-29 20:33:12 -03:00
|
|
|
|
2015-07-06 07:58:24 -03:00
|
|
|
if (pattern[3] && end - ptr < (Py_ssize_t)pattern[3]) {
|
|
|
|
TRACE(("reject (got %u chars, need %u)\n",
|
|
|
|
(unsigned int)(end - ptr), pattern[3]));
|
|
|
|
return 0;
|
|
|
|
}
|
2003-06-26 11:41:08 -03:00
|
|
|
if (pattern[3] > 1) {
|
2000-06-29 20:33:12 -03:00
|
|
|
/* adjust end point (but make sure we leave at least one
|
2000-07-02 09:00:07 -03:00
|
|
|
character in there, so literal search will work) */
|
2000-06-29 20:33:12 -03:00
|
|
|
end -= pattern[3]-1;
|
|
|
|
if (end <= ptr)
|
|
|
|
end = ptr+1;
|
|
|
|
}
|
|
|
|
|
2000-07-02 09:00:07 -03:00
|
|
|
if (flags & SRE_INFO_PREFIX) {
|
2000-07-02 14:33:27 -03:00
|
|
|
/* pattern starts with a known prefix */
|
2000-08-07 17:59:04 -03:00
|
|
|
/* <length> <skip> <prefix data> <overlap data> */
|
2000-07-02 09:00:07 -03:00
|
|
|
prefix_len = pattern[5];
|
2000-08-07 17:59:04 -03:00
|
|
|
prefix_skip = pattern[6];
|
|
|
|
prefix = pattern + 7;
|
2000-07-02 09:00:07 -03:00
|
|
|
overlap = prefix + prefix_len - 1;
|
|
|
|
} else if (flags & SRE_INFO_CHARSET)
|
2000-07-02 14:33:27 -03:00
|
|
|
/* pattern starts with a character from a known set */
|
2000-08-07 17:59:04 -03:00
|
|
|
/* <charset> */
|
2000-07-02 09:00:07 -03:00
|
|
|
charset = pattern + 5;
|
2000-06-29 20:33:12 -03:00
|
|
|
|
|
|
|
pattern += 1 + pattern[1];
|
2000-06-29 05:58:44 -03:00
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2013-09-05 12:02:57 -03:00
|
|
|
TRACE(("prefix = %p %" PY_FORMAT_SIZE_T "d %" PY_FORMAT_SIZE_T "d\n",
|
|
|
|
prefix, prefix_len, prefix_skip));
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("charset = %p\n", charset));
|
|
|
|
|
2000-06-29 20:33:12 -03:00
|
|
|
#if defined(USE_FAST_SEARCH)
|
2000-07-05 18:14:16 -03:00
|
|
|
if (prefix_len > 1) {
|
2000-06-29 20:33:12 -03:00
|
|
|
/* pattern starts with a known prefix. use the overlap
|
|
|
|
table to skip forward as fast as we possibly can */
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t i = 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
end = (SRE_CHAR *)state->end;
|
2000-06-29 20:33:12 -03:00
|
|
|
while (ptr < end) {
|
|
|
|
for (;;) {
|
2000-06-30 10:55:15 -03:00
|
|
|
if ((SRE_CODE) ptr[0] != prefix[i]) {
|
2000-06-29 20:33:12 -03:00
|
|
|
if (!i)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
i = overlap[i];
|
|
|
|
} else {
|
|
|
|
if (++i == prefix_len) {
|
|
|
|
/* found a potential match */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|SEARCH SCAN\n", pattern, ptr));
|
|
|
|
state->start = ptr + 1 - prefix_len;
|
|
|
|
state->ptr = ptr + 1 - prefix_len + prefix_skip;
|
2000-07-02 09:00:07 -03:00
|
|
|
if (flags & SRE_INFO_LITERAL)
|
|
|
|
return 1; /* we got all of it */
|
2003-12-13 16:32:08 -04:00
|
|
|
status = SRE_MATCH(state, pattern + 2*prefix_skip);
|
2000-06-29 20:33:12 -03:00
|
|
|
if (status != 0)
|
|
|
|
return status;
|
|
|
|
/* close but no cigar -- try again */
|
|
|
|
i = overlap[i];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2000-06-29 15:03:25 -03:00
|
|
|
|
2000-07-02 09:00:07 -03:00
|
|
|
if (pattern[0] == SRE_OP_LITERAL) {
|
2000-07-23 18:46:17 -03:00
|
|
|
/* pattern starts with a literal character. this is used
|
2000-07-02 09:00:07 -03:00
|
|
|
for short prefixes, and if fast search is disabled */
|
2000-07-23 18:46:17 -03:00
|
|
|
SRE_CODE chr = pattern[1];
|
2006-04-12 01:26:11 -03:00
|
|
|
end = (SRE_CHAR *)state->end;
|
2000-07-23 18:46:17 -03:00
|
|
|
for (;;) {
|
|
|
|
while (ptr < end && (SRE_CODE) ptr[0] != chr)
|
|
|
|
ptr++;
|
2002-11-06 23:28:56 -04:00
|
|
|
if (ptr >= end)
|
2000-07-23 18:46:17 -03:00
|
|
|
return 0;
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|SEARCH LITERAL\n", pattern, ptr));
|
2000-07-23 18:46:17 -03:00
|
|
|
state->start = ptr;
|
|
|
|
state->ptr = ++ptr;
|
2001-10-21 18:48:30 -03:00
|
|
|
if (flags & SRE_INFO_LITERAL)
|
|
|
|
return 1; /* we got all of it */
|
2003-12-13 16:32:08 -04:00
|
|
|
status = SRE_MATCH(state, pattern + 2);
|
2000-07-23 18:46:17 -03:00
|
|
|
if (status != 0)
|
|
|
|
break;
|
|
|
|
}
|
2000-07-02 09:00:07 -03:00
|
|
|
} else if (charset) {
|
2000-07-23 18:46:17 -03:00
|
|
|
/* pattern starts with a character from a known set */
|
2006-04-12 01:26:11 -03:00
|
|
|
end = (SRE_CHAR *)state->end;
|
2000-07-23 18:46:17 -03:00
|
|
|
for (;;) {
|
2000-08-07 17:59:04 -03:00
|
|
|
while (ptr < end && !SRE_CHARSET(charset, ptr[0]))
|
2000-07-23 18:46:17 -03:00
|
|
|
ptr++;
|
2002-11-06 23:28:56 -04:00
|
|
|
if (ptr >= end)
|
2000-07-23 18:46:17 -03:00
|
|
|
return 0;
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|SEARCH CHARSET\n", pattern, ptr));
|
2000-07-23 18:46:17 -03:00
|
|
|
state->start = ptr;
|
|
|
|
state->ptr = ptr;
|
2003-12-13 16:32:08 -04:00
|
|
|
status = SRE_MATCH(state, pattern);
|
2000-07-23 18:46:17 -03:00
|
|
|
if (status != 0)
|
|
|
|
break;
|
2000-08-07 17:59:04 -03:00
|
|
|
ptr++;
|
2000-07-02 09:00:07 -03:00
|
|
|
}
|
2015-07-06 07:58:24 -03:00
|
|
|
} else {
|
2000-07-23 18:46:17 -03:00
|
|
|
/* general case */
|
2015-07-06 07:58:24 -03:00
|
|
|
assert(ptr <= end);
|
|
|
|
while (1) {
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
|
2015-07-06 07:58:24 -03:00
|
|
|
state->start = state->ptr = ptr;
|
2003-12-13 16:32:08 -04:00
|
|
|
status = SRE_MATCH(state, pattern);
|
2015-07-06 07:58:24 -03:00
|
|
|
if (status != 0 || ptr >= end)
|
2000-07-23 18:46:17 -03:00
|
|
|
break;
|
2015-07-06 07:58:24 -03:00
|
|
|
ptr++;
|
2000-07-23 18:46:17 -03:00
|
|
|
}
|
2015-07-06 07:58:24 -03:00
|
|
|
}
|
2000-07-23 18:46:17 -03:00
|
|
|
|
|
|
|
return status;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
2006-01-20 22:47:53 -04:00
|
|
|
|
2001-10-22 18:18:08 -03:00
|
|
|
LOCAL(int)
|
2006-06-12 00:05:40 -03:00
|
|
|
SRE_LITERAL_TEMPLATE(SRE_CHAR* ptr, Py_ssize_t len)
|
2001-10-22 18:18:08 -03:00
|
|
|
{
|
|
|
|
/* check if given string is a literal template (i.e. no escapes) */
|
|
|
|
while (len-- > 0)
|
|
|
|
if (*ptr++ == '\\')
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
#if !defined(SRE_RECURSIVE)
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* factories and destructors */
|
|
|
|
|
|
|
|
/* see sre.h for object declarations */
|
2006-04-12 01:26:11 -03:00
|
|
|
static PyObject*pattern_new_match(PatternObject*, SRE_STATE*, int);
|
|
|
|
static PyObject*pattern_scanner(PatternObject*, PyObject*);
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
static PyObject *
|
2006-05-28 19:38:57 -03:00
|
|
|
sre_codesize(PyObject* self, PyObject *unused)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2013-01-10 12:37:47 -04:00
|
|
|
return PyInt_FromSize_t(sizeof(SRE_CODE));
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
static PyObject *
|
2000-06-29 09:48:37 -03:00
|
|
|
sre_getlower(PyObject* self, PyObject* args)
|
2000-06-29 05:58:44 -03:00
|
|
|
{
|
|
|
|
int character, flags;
|
2000-07-23 18:46:17 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "ii", &character, &flags))
|
2000-06-29 05:58:44 -03:00
|
|
|
return NULL;
|
|
|
|
if (flags & SRE_FLAG_LOCALE)
|
2000-06-29 09:48:37 -03:00
|
|
|
return Py_BuildValue("i", sre_lower_locale(character));
|
2000-06-29 05:58:44 -03:00
|
|
|
if (flags & SRE_FLAG_UNICODE)
|
2001-01-16 03:37:30 -04:00
|
|
|
#if defined(HAVE_UNICODE)
|
2000-06-29 09:48:37 -03:00
|
|
|
return Py_BuildValue("i", sre_lower_unicode(character));
|
2001-01-16 03:37:30 -04:00
|
|
|
#else
|
|
|
|
return Py_BuildValue("i", sre_lower_locale(character));
|
2000-06-29 05:58:44 -03:00
|
|
|
#endif
|
2000-06-29 09:48:37 -03:00
|
|
|
return Py_BuildValue("i", sre_lower(character));
|
2000-06-29 05:58:44 -03:00
|
|
|
}
|
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
LOCAL(void)
|
|
|
|
state_reset(SRE_STATE* state)
|
|
|
|
{
|
|
|
|
/* FIXME: dynamic! */
|
2003-10-17 19:13:16 -03:00
|
|
|
/*memset(state->mark, 0, sizeof(*state->mark) * SRE_MARK_SIZE);*/
|
2000-08-01 15:20:07 -03:00
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
state->lastmark = -1;
|
2000-08-01 15:20:07 -03:00
|
|
|
state->lastindex = -1;
|
|
|
|
|
|
|
|
state->repeat = NULL;
|
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
data_stack_dealloc(state);
|
2000-08-01 15:20:07 -03:00
|
|
|
}
|
|
|
|
|
2001-10-22 18:18:08 -03:00
|
|
|
static void*
|
2006-06-12 00:05:40 -03:00
|
|
|
getstring(PyObject* string, Py_ssize_t* p_length, int* p_charsize)
|
2000-06-01 14:39:12 -03:00
|
|
|
{
|
2001-10-22 18:18:08 -03:00
|
|
|
/* given a python object, return a data pointer, a length (in
|
|
|
|
characters), and a character size. return NULL if the object
|
|
|
|
is not a string (or not compatible) */
|
2006-01-20 22:47:53 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyBufferProcs *buffer;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t size, bytes;
|
|
|
|
int charsize;
|
2000-07-23 18:46:17 -03:00
|
|
|
void* ptr;
|
|
|
|
|
2000-09-21 14:03:25 -03:00
|
|
|
#if defined(HAVE_UNICODE)
|
|
|
|
if (PyUnicode_Check(string)) {
|
|
|
|
/* unicode strings doesn't always support the buffer interface */
|
|
|
|
ptr = (void*) PyUnicode_AS_DATA(string);
|
2010-05-03 20:51:28 -03:00
|
|
|
/* bytes = PyUnicode_GET_DATA_SIZE(string); */
|
2000-09-21 14:03:25 -03:00
|
|
|
size = PyUnicode_GET_SIZE(string);
|
2001-10-22 18:18:08 -03:00
|
|
|
charsize = sizeof(Py_UNICODE);
|
2000-09-21 14:03:25 -03:00
|
|
|
|
|
|
|
} else {
|
|
|
|
#endif
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
/* get pointer to string buffer */
|
2007-12-18 22:37:44 -04:00
|
|
|
buffer = Py_TYPE(string)->tp_as_buffer;
|
2000-07-23 18:46:17 -03:00
|
|
|
if (!buffer || !buffer->bf_getreadbuffer || !buffer->bf_getsegcount ||
|
|
|
|
buffer->bf_getsegcount(string, NULL) != 1) {
|
2000-08-01 15:20:07 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, "expected string or buffer");
|
2000-07-23 18:46:17 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* determine buffer size */
|
2000-08-01 15:20:07 -03:00
|
|
|
bytes = buffer->bf_getreadbuffer(string, 0, &ptr);
|
|
|
|
if (bytes < 0) {
|
2000-07-23 18:46:17 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, "buffer has negative size");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* determine character size */
|
2000-08-01 15:20:07 -03:00
|
|
|
#if PY_VERSION_HEX >= 0x01060000
|
|
|
|
size = PyObject_Size(string);
|
2000-06-29 05:58:44 -03:00
|
|
|
#else
|
2000-08-01 15:20:07 -03:00
|
|
|
size = PyObject_Length(string);
|
2000-06-29 05:58:44 -03:00
|
|
|
#endif
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2008-06-09 01:58:54 -03:00
|
|
|
if (PyString_Check(string) || bytes == size)
|
2001-10-22 18:18:08 -03:00
|
|
|
charsize = 1;
|
2000-08-01 15:20:07 -03:00
|
|
|
#if defined(HAVE_UNICODE)
|
2006-06-12 00:05:40 -03:00
|
|
|
else if (bytes == (Py_ssize_t) (size * sizeof(Py_UNICODE)))
|
2001-10-22 18:18:08 -03:00
|
|
|
charsize = sizeof(Py_UNICODE);
|
2000-08-01 15:20:07 -03:00
|
|
|
#endif
|
|
|
|
else {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "buffer size mismatch");
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2000-09-21 14:03:25 -03:00
|
|
|
#if defined(HAVE_UNICODE)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-10-22 18:18:08 -03:00
|
|
|
*p_length = size;
|
|
|
|
*p_charsize = charsize;
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCAL(PyObject*)
|
|
|
|
state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t start, Py_ssize_t end)
|
2001-10-22 18:18:08 -03:00
|
|
|
{
|
|
|
|
/* prepare state object */
|
|
|
|
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t length;
|
2001-10-22 18:18:08 -03:00
|
|
|
int charsize;
|
|
|
|
void* ptr;
|
|
|
|
|
|
|
|
memset(state, 0, sizeof(SRE_STATE));
|
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
state->lastmark = -1;
|
2001-10-22 18:18:08 -03:00
|
|
|
state->lastindex = -1;
|
|
|
|
|
|
|
|
ptr = getstring(string, &length, &charsize);
|
|
|
|
if (!ptr)
|
|
|
|
return NULL;
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
/* adjust boundaries */
|
|
|
|
if (start < 0)
|
|
|
|
start = 0;
|
2001-10-22 18:18:08 -03:00
|
|
|
else if (start > length)
|
|
|
|
start = length;
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
if (end < 0)
|
|
|
|
end = 0;
|
2001-10-22 18:18:08 -03:00
|
|
|
else if (end > length)
|
|
|
|
end = length;
|
|
|
|
|
|
|
|
state->charsize = charsize;
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
state->beginning = ptr;
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
state->start = (void*) ((char*) ptr + start * state->charsize);
|
|
|
|
state->end = (void*) ((char*) ptr + end * state->charsize);
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
Py_INCREF(string);
|
|
|
|
state->string = string;
|
|
|
|
state->pos = start;
|
|
|
|
state->endpos = end;
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
if (pattern->flags & SRE_FLAG_LOCALE)
|
2000-06-29 09:48:37 -03:00
|
|
|
state->lower = sre_lower_locale;
|
2000-06-29 05:58:44 -03:00
|
|
|
else if (pattern->flags & SRE_FLAG_UNICODE)
|
2001-01-16 03:37:30 -04:00
|
|
|
#if defined(HAVE_UNICODE)
|
2000-06-29 09:48:37 -03:00
|
|
|
state->lower = sre_lower_unicode;
|
2001-01-16 03:37:30 -04:00
|
|
|
#else
|
|
|
|
state->lower = sre_lower_locale;
|
2000-06-29 05:58:44 -03:00
|
|
|
#endif
|
|
|
|
else
|
2000-06-29 09:48:37 -03:00
|
|
|
state->lower = sre_lower;
|
2000-06-29 05:58:44 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
return string;
|
2000-06-01 14:39:12 -03:00
|
|
|
}
|
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
LOCAL(void)
|
|
|
|
state_fini(SRE_STATE* state)
|
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
Py_XDECREF(state->string);
|
2003-10-17 19:13:16 -03:00
|
|
|
data_stack_dealloc(state);
|
2000-06-29 08:34:28 -03:00
|
|
|
}
|
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
/* calculate offset from start of string */
|
|
|
|
#define STATE_OFFSET(state, member)\
|
|
|
|
(((char*)(member) - (char*)(state)->beginning) / (state)->charsize)
|
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
LOCAL(PyObject*)
|
2006-06-12 00:05:40 -03:00
|
|
|
state_getslice(SRE_STATE* state, Py_ssize_t index, PyObject* string, int empty)
|
2000-06-29 08:34:28 -03:00
|
|
|
{
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t i, j;
|
2000-08-09 06:14:35 -03:00
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
index = (index - 1) * 2;
|
|
|
|
|
2003-10-17 19:13:16 -03:00
|
|
|
if (string == Py_None || index >= state->lastmark || !state->mark[index] || !state->mark[index+1]) {
|
2001-10-20 14:48:46 -03:00
|
|
|
if (empty)
|
|
|
|
/* want empty string */
|
|
|
|
i = j = 0;
|
|
|
|
else {
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
2000-08-09 06:14:35 -03:00
|
|
|
} else {
|
2001-10-21 13:47:57 -03:00
|
|
|
i = STATE_OFFSET(state, state->mark[index]);
|
|
|
|
j = STATE_OFFSET(state, state->mark[index+1]);
|
2000-07-23 18:46:17 -03:00
|
|
|
}
|
2000-06-29 08:34:28 -03:00
|
|
|
|
2000-08-09 06:14:35 -03:00
|
|
|
return PySequence_GetSlice(string, i, j);
|
2000-06-29 08:34:28 -03:00
|
|
|
}
|
|
|
|
|
2000-08-03 13:29:50 -03:00
|
|
|
static void
|
|
|
|
pattern_error(int status)
|
|
|
|
{
|
|
|
|
switch (status) {
|
|
|
|
case SRE_ERROR_RECURSION_LIMIT:
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_RuntimeError,
|
|
|
|
"maximum recursion limit exceeded"
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case SRE_ERROR_MEMORY:
|
|
|
|
PyErr_NoMemory();
|
|
|
|
break;
|
2008-01-08 17:10:12 -04:00
|
|
|
case SRE_ERROR_INTERRUPTED:
|
|
|
|
/* An exception has already been raised, so let it fly */
|
|
|
|
break;
|
2000-08-03 13:29:50 -03:00
|
|
|
default:
|
|
|
|
/* other error codes indicate compiler/engine bugs */
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_RuntimeError,
|
|
|
|
"internal error in regular expression engine"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
static void
|
2000-06-29 08:34:28 -03:00
|
|
|
pattern_dealloc(PatternObject* self)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2004-05-31 00:09:25 -03:00
|
|
|
if (self->weakreflist != NULL)
|
|
|
|
PyObject_ClearWeakRefs((PyObject *) self);
|
2000-07-23 18:46:17 -03:00
|
|
|
Py_XDECREF(self->pattern);
|
|
|
|
Py_XDECREF(self->groupindex);
|
2001-01-16 03:05:29 -04:00
|
|
|
Py_XDECREF(self->indexgroup);
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject_DEL(self);
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
2014-03-06 06:24:29 -04:00
|
|
|
static int
|
|
|
|
check_args_size(const char *name, PyObject* args, PyObject* kw, int n)
|
|
|
|
{
|
|
|
|
Py_ssize_t m = PyTuple_GET_SIZE(args) + (kw ? PyDict_Size(kw) : 0);
|
|
|
|
if (m <= n)
|
|
|
|
return 1;
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"%s() takes at most %d positional arguments (%zd given)",
|
|
|
|
name, n, m);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
fix_string_param(PyObject *string, PyObject *string2, const char *oldname)
|
|
|
|
{
|
|
|
|
if (string2 != NULL) {
|
|
|
|
char buf[100];
|
|
|
|
if (string != NULL) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"Argument given by name ('%s') and position (1)",
|
|
|
|
oldname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
sprintf(buf, "The '%s' keyword parameter name is deprecated. "
|
|
|
|
"Use 'string' instead.", oldname);
|
|
|
|
if (PyErr_Warn(PyExc_DeprecationWarning, buf) < 0)
|
|
|
|
return NULL;
|
|
|
|
return string2;
|
|
|
|
}
|
|
|
|
if (string == NULL) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"Required argument 'string' (pos 1) not found");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
static PyObject*
|
2000-10-03 17:43:34 -03:00
|
|
|
pattern_match(PatternObject* self, PyObject* args, PyObject* kw)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
SRE_STATE state;
|
|
|
|
int status;
|
|
|
|
|
2014-03-06 06:24:29 -04:00
|
|
|
PyObject *string = NULL, *string2 = NULL;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t start = 0;
|
|
|
|
Py_ssize_t end = PY_SSIZE_T_MAX;
|
2014-03-06 06:24:29 -04:00
|
|
|
static char* kwlist[] = { "string", "pos", "endpos", "pattern", NULL };
|
|
|
|
if (!check_args_size("match", args, kw, 3))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kw, "|OnnO:match", kwlist,
|
|
|
|
&string, &start, &end, &string2))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
string = fix_string_param(string, string2, "pattern");
|
|
|
|
if (!string)
|
2000-07-23 18:46:17 -03:00
|
|
|
return NULL;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
string = state_init(&state, self, string, start, end);
|
|
|
|
if (!string)
|
|
|
|
return NULL;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
state.ptr = state.start;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|MATCH\n", PatternObject_GetCode(self), state.ptr));
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
if (state.charsize == 1) {
|
2003-12-13 16:32:08 -04:00
|
|
|
status = sre_match(&state, PatternObject_GetCode(self));
|
2000-07-23 18:46:17 -03:00
|
|
|
} else {
|
2000-06-29 05:58:44 -03:00
|
|
|
#if defined(HAVE_UNICODE)
|
2003-12-13 16:32:08 -04:00
|
|
|
status = sre_umatch(&state, PatternObject_GetCode(self));
|
2000-06-29 05:58:44 -03:00
|
|
|
#endif
|
2000-07-23 18:46:17 -03:00
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr));
|
2006-10-04 10:42:43 -03:00
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
2000-08-07 17:59:04 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
state_fini(&state);
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
return pattern_new_match(self, &state, status);
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2000-10-03 17:43:34 -03:00
|
|
|
pattern_search(PatternObject* self, PyObject* args, PyObject* kw)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
SRE_STATE state;
|
|
|
|
int status;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2014-03-06 06:24:29 -04:00
|
|
|
PyObject *string = NULL, *string2 = NULL;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t start = 0;
|
|
|
|
Py_ssize_t end = PY_SSIZE_T_MAX;
|
2014-03-06 06:24:29 -04:00
|
|
|
static char* kwlist[] = { "string", "pos", "endpos", "pattern", NULL };
|
|
|
|
if (!check_args_size("search", args, kw, 3))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kw, "|OnnO:search", kwlist,
|
|
|
|
&string, &start, &end, &string2))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
string = fix_string_param(string, string2, "pattern");
|
|
|
|
if (!string)
|
2000-07-23 18:46:17 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
string = state_init(&state, self, string, start, end);
|
|
|
|
if (!string)
|
|
|
|
return NULL;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|SEARCH\n", PatternObject_GetCode(self), state.ptr));
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
if (state.charsize == 1) {
|
|
|
|
status = sre_search(&state, PatternObject_GetCode(self));
|
|
|
|
} else {
|
2000-06-29 05:58:44 -03:00
|
|
|
#if defined(HAVE_UNICODE)
|
2000-07-23 18:46:17 -03:00
|
|
|
status = sre_usearch(&state, PatternObject_GetCode(self));
|
2000-06-29 05:58:44 -03:00
|
|
|
#endif
|
2000-07-23 18:46:17 -03:00
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr));
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
state_fini(&state);
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2006-10-04 10:42:43 -03:00
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
return pattern_new_match(self, &state, status);
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2001-07-03 17:32:36 -03:00
|
|
|
call(char* module, char* function, PyObject* args)
|
2000-06-01 14:39:12 -03:00
|
|
|
{
|
|
|
|
PyObject* name;
|
2001-07-03 17:32:36 -03:00
|
|
|
PyObject* mod;
|
2000-06-01 14:39:12 -03:00
|
|
|
PyObject* func;
|
|
|
|
PyObject* result;
|
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
if (!args)
|
|
|
|
return NULL;
|
2008-06-09 01:58:54 -03:00
|
|
|
name = PyString_FromString(module);
|
2000-06-01 14:39:12 -03:00
|
|
|
if (!name)
|
|
|
|
return NULL;
|
2001-07-03 17:32:36 -03:00
|
|
|
mod = PyImport_Import(name);
|
2000-06-01 14:39:12 -03:00
|
|
|
Py_DECREF(name);
|
2001-07-03 17:32:36 -03:00
|
|
|
if (!mod)
|
2000-06-01 14:39:12 -03:00
|
|
|
return NULL;
|
2001-07-03 17:32:36 -03:00
|
|
|
func = PyObject_GetAttrString(mod, function);
|
|
|
|
Py_DECREF(mod);
|
2000-06-01 14:39:12 -03:00
|
|
|
if (!func)
|
|
|
|
return NULL;
|
|
|
|
result = PyObject_CallObject(func, args);
|
|
|
|
Py_DECREF(func);
|
|
|
|
Py_DECREF(args);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2001-07-03 17:32:36 -03:00
|
|
|
#ifdef USE_BUILTIN_COPY
|
|
|
|
static int
|
|
|
|
deepcopy(PyObject** object, PyObject* memo)
|
|
|
|
{
|
|
|
|
PyObject* copy;
|
|
|
|
|
|
|
|
copy = call(
|
|
|
|
"copy", "deepcopy",
|
2003-10-12 16:09:37 -03:00
|
|
|
PyTuple_Pack(2, *object, memo)
|
2001-07-03 17:32:36 -03:00
|
|
|
);
|
|
|
|
if (!copy)
|
|
|
|
return 0;
|
|
|
|
|
2016-04-10 12:05:12 -03:00
|
|
|
Py_SETREF(*object, copy);
|
2001-07-03 17:32:36 -03:00
|
|
|
|
|
|
|
return 1; /* success */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
static PyObject*
|
2007-09-10 19:02:25 -03:00
|
|
|
join_list(PyObject* list, PyObject* string)
|
2001-10-21 13:47:57 -03:00
|
|
|
{
|
|
|
|
/* join list elements */
|
|
|
|
|
|
|
|
PyObject* joiner;
|
|
|
|
#if PY_VERSION_HEX >= 0x01060000
|
|
|
|
PyObject* function;
|
|
|
|
PyObject* args;
|
|
|
|
#endif
|
|
|
|
PyObject* result;
|
|
|
|
|
2007-09-10 19:02:25 -03:00
|
|
|
joiner = PySequence_GetSlice(string, 0, 0);
|
2001-10-21 13:47:57 -03:00
|
|
|
if (!joiner)
|
|
|
|
return NULL;
|
|
|
|
|
2007-09-10 19:02:25 -03:00
|
|
|
if (PyList_GET_SIZE(list) == 0) {
|
|
|
|
Py_DECREF(list);
|
|
|
|
return joiner;
|
|
|
|
}
|
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
#if PY_VERSION_HEX >= 0x01060000
|
|
|
|
function = PyObject_GetAttrString(joiner, "join");
|
|
|
|
if (!function) {
|
|
|
|
Py_DECREF(joiner);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
args = PyTuple_New(1);
|
2001-10-21 15:04:11 -03:00
|
|
|
if (!args) {
|
|
|
|
Py_DECREF(function);
|
|
|
|
Py_DECREF(joiner);
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-10-21 13:47:57 -03:00
|
|
|
PyTuple_SET_ITEM(args, 0, list);
|
|
|
|
result = PyObject_CallObject(function, args);
|
|
|
|
Py_DECREF(args); /* also removes list */
|
|
|
|
Py_DECREF(function);
|
|
|
|
#else
|
|
|
|
result = call(
|
|
|
|
"string", "join",
|
2003-10-12 16:09:37 -03:00
|
|
|
PyTuple_Pack(2, list, joiner)
|
2001-10-21 13:47:57 -03:00
|
|
|
);
|
|
|
|
#endif
|
|
|
|
Py_DECREF(joiner);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
static PyObject*
|
2000-10-03 17:43:34 -03:00
|
|
|
pattern_findall(PatternObject* self, PyObject* args, PyObject* kw)
|
2000-06-01 14:39:12 -03:00
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
SRE_STATE state;
|
|
|
|
PyObject* list;
|
|
|
|
int status;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t i, b, e;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2014-03-06 06:24:29 -04:00
|
|
|
PyObject *string = NULL, *string2 = NULL;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t start = 0;
|
|
|
|
Py_ssize_t end = PY_SSIZE_T_MAX;
|
2014-03-06 06:24:29 -04:00
|
|
|
static char* kwlist[] = { "string", "pos", "endpos", "source", NULL };
|
|
|
|
if (!check_args_size("findall", args, kw, 3))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kw, "|OnnO:findall", kwlist,
|
|
|
|
&string, &start, &end, &string2))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
string = fix_string_param(string, string2, "source");
|
|
|
|
if (!string)
|
2000-07-23 18:46:17 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
string = state_init(&state, self, string, start, end);
|
|
|
|
if (!string)
|
|
|
|
return NULL;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
list = PyList_New(0);
|
2001-10-21 15:04:11 -03:00
|
|
|
if (!list) {
|
|
|
|
state_fini(&state);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
while (state.start <= state.end) {
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject* item;
|
2006-01-20 22:47:53 -04:00
|
|
|
|
2000-10-28 16:30:41 -03:00
|
|
|
state_reset(&state);
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
state.ptr = state.start;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
if (state.charsize == 1) {
|
|
|
|
status = sre_search(&state, PatternObject_GetCode(self));
|
|
|
|
} else {
|
2000-06-29 05:58:44 -03:00
|
|
|
#if defined(HAVE_UNICODE)
|
2000-07-23 18:46:17 -03:00
|
|
|
status = sre_usearch(&state, PatternObject_GetCode(self));
|
2000-06-29 05:58:44 -03:00
|
|
|
#endif
|
2000-07-23 18:46:17 -03:00
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2006-10-04 10:42:43 -03:00
|
|
|
if (PyErr_Occurred())
|
|
|
|
goto error;
|
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
if (status <= 0) {
|
|
|
|
if (status == 0)
|
2000-06-29 08:34:28 -03:00
|
|
|
break;
|
2001-10-21 13:47:57 -03:00
|
|
|
pattern_error(status);
|
|
|
|
goto error;
|
|
|
|
}
|
2006-01-20 22:47:53 -04:00
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
/* don't bother to build a match object */
|
|
|
|
switch (self->groups) {
|
|
|
|
case 0:
|
|
|
|
b = STATE_OFFSET(&state, state.start);
|
|
|
|
e = STATE_OFFSET(&state, state.ptr);
|
|
|
|
item = PySequence_GetSlice(string, b, e);
|
|
|
|
if (!item)
|
|
|
|
goto error;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
item = state_getslice(&state, 1, string, 1);
|
|
|
|
if (!item)
|
|
|
|
goto error;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
item = PyTuple_New(self->groups);
|
|
|
|
if (!item)
|
|
|
|
goto error;
|
|
|
|
for (i = 0; i < self->groups; i++) {
|
|
|
|
PyObject* o = state_getslice(&state, i+1, string, 1);
|
|
|
|
if (!o) {
|
|
|
|
Py_DECREF(item);
|
2000-06-29 08:34:28 -03:00
|
|
|
goto error;
|
|
|
|
}
|
2001-10-21 13:47:57 -03:00
|
|
|
PyTuple_SET_ITEM(item, i, o);
|
2000-06-29 08:34:28 -03:00
|
|
|
}
|
2001-10-21 13:47:57 -03:00
|
|
|
break;
|
|
|
|
}
|
2000-06-29 08:34:28 -03:00
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
status = PyList_Append(list, item);
|
|
|
|
Py_DECREF(item);
|
|
|
|
if (status < 0)
|
2000-07-23 18:46:17 -03:00
|
|
|
goto error;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
if (state.ptr == state.start)
|
|
|
|
state.start = (void*) ((char*) state.ptr + state.charsize);
|
|
|
|
else
|
|
|
|
state.start = state.ptr;
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
state_fini(&state);
|
|
|
|
return list;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
error:
|
2000-06-29 08:34:28 -03:00
|
|
|
Py_DECREF(list);
|
2000-07-23 18:46:17 -03:00
|
|
|
state_fini(&state);
|
|
|
|
return NULL;
|
2006-01-20 22:47:53 -04:00
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
2001-10-24 19:16:30 -03:00
|
|
|
#if PY_VERSION_HEX >= 0x02020000
|
|
|
|
static PyObject*
|
|
|
|
pattern_finditer(PatternObject* pattern, PyObject* args)
|
|
|
|
{
|
|
|
|
PyObject* scanner;
|
|
|
|
PyObject* search;
|
|
|
|
PyObject* iterator;
|
|
|
|
|
|
|
|
scanner = pattern_scanner(pattern, args);
|
|
|
|
if (!scanner)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
search = PyObject_GetAttrString(scanner, "search");
|
|
|
|
Py_DECREF(scanner);
|
|
|
|
if (!search)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
iterator = PyCallIter_New(search, Py_None);
|
|
|
|
Py_DECREF(search);
|
|
|
|
|
|
|
|
return iterator;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-10-20 14:48:46 -03:00
|
|
|
static PyObject*
|
|
|
|
pattern_split(PatternObject* self, PyObject* args, PyObject* kw)
|
|
|
|
{
|
|
|
|
SRE_STATE state;
|
|
|
|
PyObject* list;
|
|
|
|
PyObject* item;
|
|
|
|
int status;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t n;
|
|
|
|
Py_ssize_t i;
|
2001-10-21 13:47:57 -03:00
|
|
|
void* last;
|
2001-10-20 14:48:46 -03:00
|
|
|
|
2014-03-06 06:24:29 -04:00
|
|
|
PyObject *string = NULL, *string2 = NULL;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t maxsplit = 0;
|
2014-03-06 06:24:29 -04:00
|
|
|
static char* kwlist[] = { "string", "maxsplit", "source", NULL };
|
|
|
|
if (!check_args_size("split", args, kw, 2))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kw, "|OnO:split", kwlist,
|
|
|
|
&string, &maxsplit, &string2))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
string = fix_string_param(string, string2, "source");
|
|
|
|
if (!string)
|
2001-10-20 14:48:46 -03:00
|
|
|
return NULL;
|
|
|
|
|
2006-06-12 00:05:40 -03:00
|
|
|
string = state_init(&state, self, string, 0, PY_SSIZE_T_MAX);
|
2001-10-20 14:48:46 -03:00
|
|
|
if (!string)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
list = PyList_New(0);
|
2001-10-21 15:04:11 -03:00
|
|
|
if (!list) {
|
|
|
|
state_fini(&state);
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-10-20 14:48:46 -03:00
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
n = 0;
|
|
|
|
last = state.start;
|
2001-10-20 14:48:46 -03:00
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
while (!maxsplit || n < maxsplit) {
|
2001-10-20 14:48:46 -03:00
|
|
|
|
|
|
|
state_reset(&state);
|
|
|
|
|
|
|
|
state.ptr = state.start;
|
|
|
|
|
|
|
|
if (state.charsize == 1) {
|
|
|
|
status = sre_search(&state, PatternObject_GetCode(self));
|
|
|
|
} else {
|
|
|
|
#if defined(HAVE_UNICODE)
|
|
|
|
status = sre_usearch(&state, PatternObject_GetCode(self));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-10-04 10:42:43 -03:00
|
|
|
if (PyErr_Occurred())
|
|
|
|
goto error;
|
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
if (status <= 0) {
|
|
|
|
if (status == 0)
|
|
|
|
break;
|
|
|
|
pattern_error(status);
|
|
|
|
goto error;
|
|
|
|
}
|
2006-01-20 22:47:53 -04:00
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
if (state.start == state.ptr) {
|
2015-07-06 07:58:24 -03:00
|
|
|
if (last == state.end || state.ptr == state.end)
|
2001-10-21 13:47:57 -03:00
|
|
|
break;
|
|
|
|
/* skip one character */
|
|
|
|
state.start = (void*) ((char*) state.ptr + state.charsize);
|
|
|
|
continue;
|
|
|
|
}
|
2001-10-20 14:48:46 -03:00
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
/* get segment before this match */
|
|
|
|
item = PySequence_GetSlice(
|
|
|
|
string, STATE_OFFSET(&state, last),
|
|
|
|
STATE_OFFSET(&state, state.start)
|
|
|
|
);
|
|
|
|
if (!item)
|
|
|
|
goto error;
|
|
|
|
status = PyList_Append(list, item);
|
|
|
|
Py_DECREF(item);
|
|
|
|
if (status < 0)
|
|
|
|
goto error;
|
2001-10-20 14:48:46 -03:00
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
/* add groups (if any) */
|
|
|
|
for (i = 0; i < self->groups; i++) {
|
|
|
|
item = state_getslice(&state, i+1, string, 0);
|
2001-10-20 14:48:46 -03:00
|
|
|
if (!item)
|
|
|
|
goto error;
|
|
|
|
status = PyList_Append(list, item);
|
|
|
|
Py_DECREF(item);
|
|
|
|
if (status < 0)
|
|
|
|
goto error;
|
2001-10-21 13:47:57 -03:00
|
|
|
}
|
2001-10-20 14:48:46 -03:00
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
n = n + 1;
|
2001-10-20 14:48:46 -03:00
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
last = state.start = state.ptr;
|
2001-10-20 14:48:46 -03:00
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
}
|
|
|
|
|
2001-10-22 03:01:56 -03:00
|
|
|
/* get segment following last match (even if empty) */
|
|
|
|
item = PySequence_GetSlice(
|
|
|
|
string, STATE_OFFSET(&state, last), state.endpos
|
|
|
|
);
|
|
|
|
if (!item)
|
|
|
|
goto error;
|
|
|
|
status = PyList_Append(list, item);
|
|
|
|
Py_DECREF(item);
|
|
|
|
if (status < 0)
|
|
|
|
goto error;
|
2001-10-21 13:47:57 -03:00
|
|
|
|
|
|
|
state_fini(&state);
|
|
|
|
return list;
|
|
|
|
|
|
|
|
error:
|
|
|
|
Py_DECREF(list);
|
|
|
|
state_fini(&state);
|
|
|
|
return NULL;
|
2006-01-20 22:47:53 -04:00
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2006-04-12 01:26:11 -03:00
|
|
|
pattern_subx(PatternObject* self, PyObject* ptemplate, PyObject* string,
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t count, Py_ssize_t subn)
|
2001-10-21 13:47:57 -03:00
|
|
|
{
|
|
|
|
SRE_STATE state;
|
|
|
|
PyObject* list;
|
|
|
|
PyObject* item;
|
|
|
|
PyObject* filter;
|
|
|
|
PyObject* args;
|
|
|
|
PyObject* match;
|
2001-10-22 18:18:08 -03:00
|
|
|
void* ptr;
|
2001-10-21 13:47:57 -03:00
|
|
|
int status;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t n;
|
|
|
|
Py_ssize_t i, b, e;
|
|
|
|
int bint;
|
2001-10-21 13:47:57 -03:00
|
|
|
int filter_is_callable;
|
|
|
|
|
2006-04-12 01:26:11 -03:00
|
|
|
if (PyCallable_Check(ptemplate)) {
|
2001-10-21 18:48:30 -03:00
|
|
|
/* sub/subn takes either a function or a template */
|
2006-04-12 01:26:11 -03:00
|
|
|
filter = ptemplate;
|
2001-10-21 18:48:30 -03:00
|
|
|
Py_INCREF(filter);
|
|
|
|
filter_is_callable = 1;
|
|
|
|
} else {
|
2001-10-22 18:18:08 -03:00
|
|
|
/* if not callable, check if it's a literal string */
|
|
|
|
int literal;
|
2006-06-12 00:05:40 -03:00
|
|
|
ptr = getstring(ptemplate, &n, &bint);
|
|
|
|
b = bint;
|
2001-10-22 18:18:08 -03:00
|
|
|
if (ptr) {
|
|
|
|
if (b == 1) {
|
2006-04-18 08:53:09 -03:00
|
|
|
literal = sre_literal_template((unsigned char *)ptr, n);
|
2001-10-22 18:18:08 -03:00
|
|
|
} else {
|
|
|
|
#if defined(HAVE_UNICODE)
|
2006-04-18 08:53:09 -03:00
|
|
|
literal = sre_uliteral_template((Py_UNICODE *)ptr, n);
|
2001-10-22 18:18:08 -03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PyErr_Clear();
|
|
|
|
literal = 0;
|
|
|
|
}
|
|
|
|
if (literal) {
|
2006-04-12 01:26:11 -03:00
|
|
|
filter = ptemplate;
|
2001-10-22 18:18:08 -03:00
|
|
|
Py_INCREF(filter);
|
|
|
|
filter_is_callable = 0;
|
|
|
|
} else {
|
|
|
|
/* not a literal; hand it over to the template compiler */
|
|
|
|
filter = call(
|
2006-03-16 02:30:02 -04:00
|
|
|
SRE_PY_MODULE, "_subx",
|
2006-04-12 01:26:11 -03:00
|
|
|
PyTuple_Pack(2, self, ptemplate)
|
2001-10-22 18:18:08 -03:00
|
|
|
);
|
|
|
|
if (!filter)
|
|
|
|
return NULL;
|
|
|
|
filter_is_callable = PyCallable_Check(filter);
|
|
|
|
}
|
2001-10-21 18:48:30 -03:00
|
|
|
}
|
2001-10-21 13:47:57 -03:00
|
|
|
|
2006-06-12 00:05:40 -03:00
|
|
|
string = state_init(&state, self, string, 0, PY_SSIZE_T_MAX);
|
2001-12-09 12:13:15 -04:00
|
|
|
if (!string) {
|
|
|
|
Py_DECREF(filter);
|
2001-10-21 13:47:57 -03:00
|
|
|
return NULL;
|
2001-12-09 12:13:15 -04:00
|
|
|
}
|
2001-10-21 13:47:57 -03:00
|
|
|
|
|
|
|
list = PyList_New(0);
|
2001-10-21 15:04:11 -03:00
|
|
|
if (!list) {
|
2001-12-09 12:13:15 -04:00
|
|
|
Py_DECREF(filter);
|
2001-10-21 15:04:11 -03:00
|
|
|
state_fini(&state);
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-10-20 14:48:46 -03:00
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
n = i = 0;
|
|
|
|
|
|
|
|
while (!count || n < count) {
|
|
|
|
|
|
|
|
state_reset(&state);
|
|
|
|
|
|
|
|
state.ptr = state.start;
|
|
|
|
|
|
|
|
if (state.charsize == 1) {
|
|
|
|
status = sre_search(&state, PatternObject_GetCode(self));
|
2001-10-20 14:48:46 -03:00
|
|
|
} else {
|
2001-10-21 13:47:57 -03:00
|
|
|
#if defined(HAVE_UNICODE)
|
|
|
|
status = sre_usearch(&state, PatternObject_GetCode(self));
|
|
|
|
#endif
|
|
|
|
}
|
2001-10-20 14:48:46 -03:00
|
|
|
|
2006-10-04 10:42:43 -03:00
|
|
|
if (PyErr_Occurred())
|
|
|
|
goto error;
|
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
if (status <= 0) {
|
2001-10-20 14:48:46 -03:00
|
|
|
if (status == 0)
|
|
|
|
break;
|
|
|
|
pattern_error(status);
|
|
|
|
goto error;
|
2001-10-21 13:47:57 -03:00
|
|
|
}
|
2006-01-20 22:47:53 -04:00
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
b = STATE_OFFSET(&state, state.start);
|
|
|
|
e = STATE_OFFSET(&state, state.ptr);
|
|
|
|
|
|
|
|
if (i < b) {
|
|
|
|
/* get segment before this match */
|
|
|
|
item = PySequence_GetSlice(string, i, b);
|
|
|
|
if (!item)
|
|
|
|
goto error;
|
|
|
|
status = PyList_Append(list, item);
|
|
|
|
Py_DECREF(item);
|
|
|
|
if (status < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
} else if (i == b && i == e && n > 0)
|
|
|
|
/* ignore empty match on latest position */
|
|
|
|
goto next;
|
2001-10-20 14:48:46 -03:00
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
if (filter_is_callable) {
|
2001-10-21 18:48:30 -03:00
|
|
|
/* pass match object through filter */
|
2001-10-21 13:47:57 -03:00
|
|
|
match = pattern_new_match(self, &state, 1);
|
|
|
|
if (!match)
|
|
|
|
goto error;
|
2003-10-12 16:09:37 -03:00
|
|
|
args = PyTuple_Pack(1, match);
|
2001-10-21 13:47:57 -03:00
|
|
|
if (!args) {
|
2001-12-07 00:25:10 -04:00
|
|
|
Py_DECREF(match);
|
2001-10-21 13:47:57 -03:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
item = PyObject_CallObject(filter, args);
|
|
|
|
Py_DECREF(args);
|
|
|
|
Py_DECREF(match);
|
|
|
|
if (!item)
|
|
|
|
goto error;
|
|
|
|
} else {
|
|
|
|
/* filter is literal string */
|
|
|
|
item = filter;
|
2001-10-21 18:48:30 -03:00
|
|
|
Py_INCREF(item);
|
2001-10-20 14:48:46 -03:00
|
|
|
}
|
2001-10-21 13:47:57 -03:00
|
|
|
|
|
|
|
/* add to list */
|
2001-10-22 18:18:08 -03:00
|
|
|
if (item != Py_None) {
|
|
|
|
status = PyList_Append(list, item);
|
|
|
|
Py_DECREF(item);
|
|
|
|
if (status < 0)
|
|
|
|
goto error;
|
|
|
|
}
|
2006-01-20 22:47:53 -04:00
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
i = e;
|
|
|
|
n = n + 1;
|
|
|
|
|
|
|
|
next:
|
|
|
|
/* move on */
|
2015-07-06 07:58:24 -03:00
|
|
|
if (state.ptr == state.end)
|
|
|
|
break;
|
2001-10-21 13:47:57 -03:00
|
|
|
if (state.ptr == state.start)
|
|
|
|
state.start = (void*) ((char*) state.ptr + state.charsize);
|
|
|
|
else
|
|
|
|
state.start = state.ptr;
|
|
|
|
|
2001-10-20 14:48:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get segment following last match */
|
2001-10-21 18:48:30 -03:00
|
|
|
if (i < state.endpos) {
|
|
|
|
item = PySequence_GetSlice(string, i, state.endpos);
|
|
|
|
if (!item)
|
|
|
|
goto error;
|
|
|
|
status = PyList_Append(list, item);
|
|
|
|
Py_DECREF(item);
|
|
|
|
if (status < 0)
|
|
|
|
goto error;
|
|
|
|
}
|
2001-10-20 14:48:46 -03:00
|
|
|
|
|
|
|
state_fini(&state);
|
2001-10-21 13:47:57 -03:00
|
|
|
|
2001-12-07 00:25:10 -04:00
|
|
|
Py_DECREF(filter);
|
|
|
|
|
2001-10-21 18:48:30 -03:00
|
|
|
/* convert list to single string (also removes list) */
|
2007-09-10 19:02:25 -03:00
|
|
|
item = join_list(list, string);
|
2001-10-21 18:48:30 -03:00
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
if (!item)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (subn)
|
2012-12-02 07:52:36 -04:00
|
|
|
return Py_BuildValue("Nn", item, n);
|
2001-10-21 13:47:57 -03:00
|
|
|
|
|
|
|
return item;
|
2001-10-20 14:48:46 -03:00
|
|
|
|
|
|
|
error:
|
|
|
|
Py_DECREF(list);
|
|
|
|
state_fini(&state);
|
2001-12-09 12:13:15 -04:00
|
|
|
Py_DECREF(filter);
|
2001-10-20 14:48:46 -03:00
|
|
|
return NULL;
|
2006-01-20 22:47:53 -04:00
|
|
|
|
2001-10-20 14:48:46 -03:00
|
|
|
}
|
2001-10-21 13:47:57 -03:00
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
pattern_sub(PatternObject* self, PyObject* args, PyObject* kw)
|
|
|
|
{
|
2006-04-12 01:26:11 -03:00
|
|
|
PyObject* ptemplate;
|
2001-10-21 13:47:57 -03:00
|
|
|
PyObject* string;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t count = 0;
|
2006-02-27 12:46:16 -04:00
|
|
|
static char* kwlist[] = { "repl", "string", "count", NULL };
|
2006-06-12 00:05:40 -03:00
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|n:sub", kwlist,
|
2006-04-12 01:26:11 -03:00
|
|
|
&ptemplate, &string, &count))
|
2001-10-21 13:47:57 -03:00
|
|
|
return NULL;
|
|
|
|
|
2006-04-12 01:26:11 -03:00
|
|
|
return pattern_subx(self, ptemplate, string, count, 0);
|
2001-10-21 13:47:57 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
pattern_subn(PatternObject* self, PyObject* args, PyObject* kw)
|
|
|
|
{
|
2006-04-12 01:26:11 -03:00
|
|
|
PyObject* ptemplate;
|
2001-10-21 13:47:57 -03:00
|
|
|
PyObject* string;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t count = 0;
|
2006-02-27 12:46:16 -04:00
|
|
|
static char* kwlist[] = { "repl", "string", "count", NULL };
|
2006-06-12 00:05:40 -03:00
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|n:subn", kwlist,
|
2006-04-12 01:26:11 -03:00
|
|
|
&ptemplate, &string, &count))
|
2001-10-21 13:47:57 -03:00
|
|
|
return NULL;
|
|
|
|
|
2006-04-12 01:26:11 -03:00
|
|
|
return pattern_subx(self, ptemplate, string, count, 1);
|
2001-10-21 13:47:57 -03:00
|
|
|
}
|
2001-10-20 14:48:46 -03:00
|
|
|
|
2001-07-02 13:42:49 -03:00
|
|
|
static PyObject*
|
2006-05-28 19:38:57 -03:00
|
|
|
pattern_copy(PatternObject* self, PyObject *unused)
|
2001-07-02 13:42:49 -03:00
|
|
|
{
|
2001-07-03 17:32:36 -03:00
|
|
|
#ifdef USE_BUILTIN_COPY
|
2001-07-02 13:42:49 -03:00
|
|
|
PatternObject* copy;
|
|
|
|
int offset;
|
|
|
|
|
|
|
|
copy = PyObject_NEW_VAR(PatternObject, &Pattern_Type, self->codesize);
|
|
|
|
if (!copy)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
offset = offsetof(PatternObject, groups);
|
|
|
|
|
|
|
|
Py_XINCREF(self->groupindex);
|
|
|
|
Py_XINCREF(self->indexgroup);
|
|
|
|
Py_XINCREF(self->pattern);
|
|
|
|
|
|
|
|
memcpy((char*) copy + offset, (char*) self + offset,
|
|
|
|
sizeof(PatternObject) + self->codesize * sizeof(SRE_CODE) - offset);
|
2004-05-31 00:09:25 -03:00
|
|
|
copy->weakreflist = NULL;
|
2001-07-02 13:42:49 -03:00
|
|
|
|
|
|
|
return (PyObject*) copy;
|
|
|
|
#else
|
|
|
|
PyErr_SetString(PyExc_TypeError, "cannot copy this pattern object");
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2006-05-28 19:14:04 -03:00
|
|
|
pattern_deepcopy(PatternObject* self, PyObject* memo)
|
2001-07-02 13:42:49 -03:00
|
|
|
{
|
2001-07-03 17:32:36 -03:00
|
|
|
#ifdef USE_BUILTIN_COPY
|
|
|
|
PatternObject* copy;
|
2006-01-20 22:47:53 -04:00
|
|
|
|
2006-05-28 19:14:04 -03:00
|
|
|
copy = (PatternObject*) pattern_copy(self);
|
2001-07-03 17:32:36 -03:00
|
|
|
if (!copy)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!deepcopy(©->groupindex, memo) ||
|
|
|
|
!deepcopy(©->indexgroup, memo) ||
|
|
|
|
!deepcopy(©->pattern, memo)) {
|
|
|
|
Py_DECREF(copy);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2001-07-02 13:42:49 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, "cannot deepcopy this pattern object");
|
|
|
|
return NULL;
|
2001-07-03 17:32:36 -03:00
|
|
|
#endif
|
2001-07-02 13:42:49 -03:00
|
|
|
}
|
|
|
|
|
2004-09-24 01:31:19 -03:00
|
|
|
PyDoc_STRVAR(pattern_match_doc,
|
|
|
|
"match(string[, pos[, endpos]]) --> match object or None.\n\
|
|
|
|
Matches zero or more characters at the beginning of the string");
|
|
|
|
|
|
|
|
PyDoc_STRVAR(pattern_search_doc,
|
|
|
|
"search(string[, pos[, endpos]]) --> match object or None.\n\
|
|
|
|
Scan through string looking for a match, and return a corresponding\n\
|
2012-12-25 12:50:03 -04:00
|
|
|
match object instance. Return None if no position in the string matches.");
|
2004-09-24 01:31:19 -03:00
|
|
|
|
|
|
|
PyDoc_STRVAR(pattern_split_doc,
|
|
|
|
"split(string[, maxsplit = 0]) --> list.\n\
|
|
|
|
Split string by the occurrences of pattern.");
|
|
|
|
|
|
|
|
PyDoc_STRVAR(pattern_findall_doc,
|
|
|
|
"findall(string[, pos[, endpos]]) --> list.\n\
|
|
|
|
Return a list of all non-overlapping matches of pattern in string.");
|
|
|
|
|
|
|
|
PyDoc_STRVAR(pattern_finditer_doc,
|
|
|
|
"finditer(string[, pos[, endpos]]) --> iterator.\n\
|
|
|
|
Return an iterator over all non-overlapping matches for the \n\
|
|
|
|
RE pattern in string. For each match, the iterator returns a\n\
|
|
|
|
match object.");
|
|
|
|
|
|
|
|
PyDoc_STRVAR(pattern_sub_doc,
|
|
|
|
"sub(repl, string[, count = 0]) --> newstring\n\
|
|
|
|
Return the string obtained by replacing the leftmost non-overlapping\n\
|
2006-01-20 22:47:53 -04:00
|
|
|
occurrences of pattern in string by the replacement repl.");
|
2004-09-24 01:31:19 -03:00
|
|
|
|
|
|
|
PyDoc_STRVAR(pattern_subn_doc,
|
|
|
|
"subn(repl, string[, count = 0]) --> (newstring, number of subs)\n\
|
|
|
|
Return the tuple (new_string, number_of_subs_made) found by replacing\n\
|
|
|
|
the leftmost non-overlapping occurrences of pattern with the\n\
|
2006-01-20 22:47:53 -04:00
|
|
|
replacement repl.");
|
2004-09-24 01:31:19 -03:00
|
|
|
|
|
|
|
PyDoc_STRVAR(pattern_doc, "Compiled regular expression objects");
|
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
static PyMethodDef pattern_methods[] = {
|
2006-01-20 22:47:53 -04:00
|
|
|
{"match", (PyCFunction) pattern_match, METH_VARARGS|METH_KEYWORDS,
|
2004-09-24 01:31:19 -03:00
|
|
|
pattern_match_doc},
|
2006-01-20 22:47:53 -04:00
|
|
|
{"search", (PyCFunction) pattern_search, METH_VARARGS|METH_KEYWORDS,
|
2004-09-24 01:31:19 -03:00
|
|
|
pattern_search_doc},
|
|
|
|
{"sub", (PyCFunction) pattern_sub, METH_VARARGS|METH_KEYWORDS,
|
|
|
|
pattern_sub_doc},
|
|
|
|
{"subn", (PyCFunction) pattern_subn, METH_VARARGS|METH_KEYWORDS,
|
|
|
|
pattern_subn_doc},
|
2006-01-20 22:47:53 -04:00
|
|
|
{"split", (PyCFunction) pattern_split, METH_VARARGS|METH_KEYWORDS,
|
2004-09-24 01:31:19 -03:00
|
|
|
pattern_split_doc},
|
2006-01-20 22:47:53 -04:00
|
|
|
{"findall", (PyCFunction) pattern_findall, METH_VARARGS|METH_KEYWORDS,
|
2004-09-24 01:31:19 -03:00
|
|
|
pattern_findall_doc},
|
2001-10-24 19:16:30 -03:00
|
|
|
#if PY_VERSION_HEX >= 0x02020000
|
2004-09-24 01:31:19 -03:00
|
|
|
{"finditer", (PyCFunction) pattern_finditer, METH_VARARGS,
|
|
|
|
pattern_finditer_doc},
|
2001-10-24 19:16:30 -03:00
|
|
|
#endif
|
2000-10-03 17:43:34 -03:00
|
|
|
{"scanner", (PyCFunction) pattern_scanner, METH_VARARGS},
|
2006-05-28 19:14:04 -03:00
|
|
|
{"__copy__", (PyCFunction) pattern_copy, METH_NOARGS},
|
|
|
|
{"__deepcopy__", (PyCFunction) pattern_deepcopy, METH_O},
|
2000-07-23 18:46:17 -03:00
|
|
|
{NULL, NULL}
|
2000-03-31 10:59:30 -04:00
|
|
|
};
|
|
|
|
|
2011-05-17 20:31:20 -03:00
|
|
|
#define PAT_OFF(x) offsetof(PatternObject, x)
|
|
|
|
static PyMemberDef pattern_members[] = {
|
|
|
|
{"pattern", T_OBJECT, PAT_OFF(pattern), READONLY},
|
|
|
|
{"flags", T_INT, PAT_OFF(flags), READONLY},
|
|
|
|
{"groups", T_PYSSIZET, PAT_OFF(groups), READONLY},
|
|
|
|
{"groupindex", T_OBJECT, PAT_OFF(groupindex), READONLY},
|
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
statichere PyTypeObject Pattern_Type = {
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject_HEAD_INIT(NULL)
|
2001-12-09 12:13:15 -04:00
|
|
|
0, "_" SRE_MODULE ".SRE_Pattern",
|
2000-07-03 15:44:21 -03:00
|
|
|
sizeof(PatternObject), sizeof(SRE_CODE),
|
2000-07-23 18:46:17 -03:00
|
|
|
(destructor)pattern_dealloc, /*tp_dealloc*/
|
2011-05-17 20:31:20 -03:00
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattrn */
|
2004-05-31 00:09:25 -03:00
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_compare */
|
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
0, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
2011-05-17 20:31:20 -03:00
|
|
|
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
2004-09-24 01:31:19 -03:00
|
|
|
pattern_doc, /* tp_doc */
|
2004-05-31 00:09:25 -03:00
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
offsetof(PatternObject, weakreflist), /* tp_weaklistoffset */
|
2011-05-17 20:31:20 -03:00
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
pattern_methods, /* tp_methods */
|
|
|
|
pattern_members, /* tp_members */
|
2000-03-31 10:59:30 -04:00
|
|
|
};
|
|
|
|
|
2008-08-05 00:39:21 -03:00
|
|
|
static int _validate(PatternObject *self); /* Forward */
|
|
|
|
|
2006-04-12 01:26:11 -03:00
|
|
|
static PyObject *
|
|
|
|
_compile(PyObject* self_, PyObject* args)
|
|
|
|
{
|
|
|
|
/* "compile" pattern descriptor to pattern object */
|
|
|
|
|
|
|
|
PatternObject* self;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t i, n;
|
2006-04-12 01:26:11 -03:00
|
|
|
|
|
|
|
PyObject* pattern;
|
|
|
|
int flags = 0;
|
|
|
|
PyObject* code;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t groups = 0;
|
2006-04-12 01:26:11 -03:00
|
|
|
PyObject* groupindex = NULL;
|
|
|
|
PyObject* indexgroup = NULL;
|
2006-06-12 00:05:40 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "OiO!|nOO", &pattern, &flags,
|
2006-04-12 01:26:11 -03:00
|
|
|
&PyList_Type, &code, &groups,
|
|
|
|
&groupindex, &indexgroup))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
n = PyList_GET_SIZE(code);
|
2008-01-18 15:12:56 -04:00
|
|
|
/* coverity[ampersand_in_size] */
|
2006-04-12 01:26:11 -03:00
|
|
|
self = PyObject_NEW_VAR(PatternObject, &Pattern_Type, n);
|
|
|
|
if (!self)
|
|
|
|
return NULL;
|
2010-01-14 13:25:24 -04:00
|
|
|
self->weakreflist = NULL;
|
|
|
|
self->pattern = NULL;
|
|
|
|
self->groupindex = NULL;
|
|
|
|
self->indexgroup = NULL;
|
2006-04-12 01:26:11 -03:00
|
|
|
|
|
|
|
self->codesize = n;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
PyObject *o = PyList_GET_ITEM(code, i);
|
|
|
|
unsigned long value = PyInt_Check(o) ? (unsigned long)PyInt_AsLong(o)
|
|
|
|
: PyLong_AsUnsignedLong(o);
|
2012-11-20 17:30:42 -04:00
|
|
|
if (value == (unsigned long)-1 && PyErr_Occurred()) {
|
|
|
|
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"regular expression code size limit exceeded");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2006-04-12 01:26:11 -03:00
|
|
|
self->code[i] = (SRE_CODE) value;
|
|
|
|
if ((unsigned long) self->code[i] != value) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"regular expression code size limit exceeded");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PyErr_Occurred()) {
|
2010-01-14 13:25:24 -04:00
|
|
|
Py_DECREF(self);
|
2006-04-12 01:26:11 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(pattern);
|
|
|
|
self->pattern = pattern;
|
|
|
|
|
|
|
|
self->flags = flags;
|
|
|
|
|
|
|
|
self->groups = groups;
|
|
|
|
|
|
|
|
Py_XINCREF(groupindex);
|
|
|
|
self->groupindex = groupindex;
|
|
|
|
|
|
|
|
Py_XINCREF(indexgroup);
|
|
|
|
self->indexgroup = indexgroup;
|
|
|
|
|
|
|
|
self->weakreflist = NULL;
|
|
|
|
|
2008-08-05 00:39:21 -03:00
|
|
|
if (!_validate(self)) {
|
|
|
|
Py_DECREF(self);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-04-12 01:26:11 -03:00
|
|
|
return (PyObject*) self;
|
|
|
|
}
|
|
|
|
|
2008-08-05 00:39:21 -03:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Code validation */
|
|
|
|
|
|
|
|
/* To learn more about this code, have a look at the _compile() function in
|
|
|
|
Lib/sre_compile.py. The validation functions below checks the code array
|
|
|
|
for conformance with the code patterns generated there.
|
|
|
|
|
|
|
|
The nice thing about the generated code is that it is position-independent:
|
|
|
|
all jumps are relative jumps forward. Also, jumps don't cross each other:
|
|
|
|
the target of a later jump is always earlier than the target of an earlier
|
|
|
|
jump. IOW, this is okay:
|
|
|
|
|
|
|
|
J---------J-------T--------T
|
|
|
|
\ \_____/ /
|
|
|
|
\______________________/
|
|
|
|
|
|
|
|
but this is not:
|
|
|
|
|
|
|
|
J---------J-------T--------T
|
|
|
|
\_________\_____/ /
|
|
|
|
\____________/
|
|
|
|
|
2013-10-27 03:00:57 -03:00
|
|
|
It also helps that SRE_CODE is always an unsigned type.
|
2008-08-05 00:39:21 -03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Defining this one enables tracing of the validator */
|
|
|
|
#undef VVERBOSE
|
|
|
|
|
|
|
|
/* Trace macro for the validator */
|
|
|
|
#if defined(VVERBOSE)
|
|
|
|
#define VTRACE(v) printf v
|
|
|
|
#else
|
2011-10-19 15:13:23 -03:00
|
|
|
#define VTRACE(v) do {} while(0) /* do nothing */
|
2008-08-05 00:39:21 -03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Report failure */
|
|
|
|
#define FAIL do { VTRACE(("FAIL: %d\n", __LINE__)); return 0; } while (0)
|
|
|
|
|
|
|
|
/* Extract opcode, argument, or skip count from code array */
|
|
|
|
#define GET_OP \
|
|
|
|
do { \
|
|
|
|
VTRACE(("%p: ", code)); \
|
|
|
|
if (code >= end) FAIL; \
|
|
|
|
op = *code++; \
|
|
|
|
VTRACE(("%lu (op)\n", (unsigned long)op)); \
|
|
|
|
} while (0)
|
|
|
|
#define GET_ARG \
|
|
|
|
do { \
|
|
|
|
VTRACE(("%p= ", code)); \
|
|
|
|
if (code >= end) FAIL; \
|
|
|
|
arg = *code++; \
|
|
|
|
VTRACE(("%lu (arg)\n", (unsigned long)arg)); \
|
|
|
|
} while (0)
|
2008-09-10 11:27:00 -03:00
|
|
|
#define GET_SKIP_ADJ(adj) \
|
2008-08-05 00:39:21 -03:00
|
|
|
do { \
|
|
|
|
VTRACE(("%p= ", code)); \
|
|
|
|
if (code >= end) FAIL; \
|
|
|
|
skip = *code; \
|
|
|
|
VTRACE(("%lu (skip to %p)\n", \
|
|
|
|
(unsigned long)skip, code+skip)); \
|
2013-04-13 15:15:10 -03:00
|
|
|
if (skip-adj > end-code) \
|
2008-08-05 00:39:21 -03:00
|
|
|
FAIL; \
|
|
|
|
code++; \
|
|
|
|
} while (0)
|
2008-09-10 11:27:00 -03:00
|
|
|
#define GET_SKIP GET_SKIP_ADJ(0)
|
2008-08-05 00:39:21 -03:00
|
|
|
|
|
|
|
static int
|
|
|
|
_validate_charset(SRE_CODE *code, SRE_CODE *end)
|
|
|
|
{
|
|
|
|
/* Some variables are manipulated by the macros above */
|
|
|
|
SRE_CODE op;
|
|
|
|
SRE_CODE arg;
|
|
|
|
SRE_CODE offset;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
while (code < end) {
|
|
|
|
GET_OP;
|
|
|
|
switch (op) {
|
|
|
|
|
|
|
|
case SRE_OP_NEGATE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_LITERAL:
|
|
|
|
GET_ARG;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_RANGE:
|
|
|
|
GET_ARG;
|
|
|
|
GET_ARG;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_CHARSET:
|
|
|
|
offset = 32/sizeof(SRE_CODE); /* 32-byte bitmap */
|
2013-04-13 15:15:10 -03:00
|
|
|
if (offset > end-code)
|
2008-08-05 00:39:21 -03:00
|
|
|
FAIL;
|
|
|
|
code += offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_BIGCHARSET:
|
|
|
|
GET_ARG; /* Number of blocks */
|
|
|
|
offset = 256/sizeof(SRE_CODE); /* 256-byte table */
|
2013-04-13 15:15:10 -03:00
|
|
|
if (offset > end-code)
|
2008-08-05 00:39:21 -03:00
|
|
|
FAIL;
|
|
|
|
/* Make sure that each byte points to a valid block */
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
if (((unsigned char *)code)[i] >= arg)
|
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
code += offset;
|
|
|
|
offset = arg * 32/sizeof(SRE_CODE); /* 32-byte bitmap times arg */
|
2013-04-13 15:15:10 -03:00
|
|
|
if (offset > end-code)
|
2008-08-05 00:39:21 -03:00
|
|
|
FAIL;
|
|
|
|
code += offset;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_CATEGORY:
|
|
|
|
GET_ARG;
|
|
|
|
switch (arg) {
|
|
|
|
case SRE_CATEGORY_DIGIT:
|
|
|
|
case SRE_CATEGORY_NOT_DIGIT:
|
|
|
|
case SRE_CATEGORY_SPACE:
|
|
|
|
case SRE_CATEGORY_NOT_SPACE:
|
|
|
|
case SRE_CATEGORY_WORD:
|
|
|
|
case SRE_CATEGORY_NOT_WORD:
|
|
|
|
case SRE_CATEGORY_LINEBREAK:
|
|
|
|
case SRE_CATEGORY_NOT_LINEBREAK:
|
|
|
|
case SRE_CATEGORY_LOC_WORD:
|
|
|
|
case SRE_CATEGORY_LOC_NOT_WORD:
|
|
|
|
case SRE_CATEGORY_UNI_DIGIT:
|
|
|
|
case SRE_CATEGORY_UNI_NOT_DIGIT:
|
|
|
|
case SRE_CATEGORY_UNI_SPACE:
|
|
|
|
case SRE_CATEGORY_UNI_NOT_SPACE:
|
|
|
|
case SRE_CATEGORY_UNI_WORD:
|
|
|
|
case SRE_CATEGORY_UNI_NOT_WORD:
|
|
|
|
case SRE_CATEGORY_UNI_LINEBREAK:
|
|
|
|
case SRE_CATEGORY_UNI_NOT_LINEBREAK:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
FAIL;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_validate_inner(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups)
|
|
|
|
{
|
|
|
|
/* Some variables are manipulated by the macros above */
|
|
|
|
SRE_CODE op;
|
|
|
|
SRE_CODE arg;
|
|
|
|
SRE_CODE skip;
|
|
|
|
|
|
|
|
VTRACE(("code=%p, end=%p\n", code, end));
|
|
|
|
|
|
|
|
if (code > end)
|
|
|
|
FAIL;
|
|
|
|
|
|
|
|
while (code < end) {
|
|
|
|
GET_OP;
|
|
|
|
switch (op) {
|
|
|
|
|
|
|
|
case SRE_OP_MARK:
|
|
|
|
/* We don't check whether marks are properly nested; the
|
|
|
|
sre_match() code is robust even if they don't, and the worst
|
|
|
|
you can get is nonsensical match results. */
|
|
|
|
GET_ARG;
|
|
|
|
if (arg > 2*groups+1) {
|
|
|
|
VTRACE(("arg=%d, groups=%d\n", (int)arg, (int)groups));
|
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_LITERAL:
|
|
|
|
case SRE_OP_NOT_LITERAL:
|
|
|
|
case SRE_OP_LITERAL_IGNORE:
|
|
|
|
case SRE_OP_NOT_LITERAL_IGNORE:
|
|
|
|
GET_ARG;
|
|
|
|
/* The arg is just a character, nothing to check */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_SUCCESS:
|
|
|
|
case SRE_OP_FAILURE:
|
|
|
|
/* Nothing to check; these normally end the matching process */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_AT:
|
|
|
|
GET_ARG;
|
|
|
|
switch (arg) {
|
|
|
|
case SRE_AT_BEGINNING:
|
|
|
|
case SRE_AT_BEGINNING_STRING:
|
|
|
|
case SRE_AT_BEGINNING_LINE:
|
|
|
|
case SRE_AT_END:
|
|
|
|
case SRE_AT_END_LINE:
|
|
|
|
case SRE_AT_END_STRING:
|
|
|
|
case SRE_AT_BOUNDARY:
|
|
|
|
case SRE_AT_NON_BOUNDARY:
|
|
|
|
case SRE_AT_LOC_BOUNDARY:
|
|
|
|
case SRE_AT_LOC_NON_BOUNDARY:
|
|
|
|
case SRE_AT_UNI_BOUNDARY:
|
|
|
|
case SRE_AT_UNI_NON_BOUNDARY:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_ANY:
|
|
|
|
case SRE_OP_ANY_ALL:
|
|
|
|
/* These have no operands */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_IN:
|
|
|
|
case SRE_OP_IN_IGNORE:
|
|
|
|
GET_SKIP;
|
|
|
|
/* Stop 1 before the end; we check the FAILURE below */
|
|
|
|
if (!_validate_charset(code, code+skip-2))
|
|
|
|
FAIL;
|
|
|
|
if (code[skip-2] != SRE_OP_FAILURE)
|
|
|
|
FAIL;
|
|
|
|
code += skip-1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_INFO:
|
|
|
|
{
|
|
|
|
/* A minimal info field is
|
|
|
|
<INFO> <1=skip> <2=flags> <3=min> <4=max>;
|
|
|
|
If SRE_INFO_PREFIX or SRE_INFO_CHARSET is in the flags,
|
|
|
|
more follows. */
|
2010-05-03 20:51:28 -03:00
|
|
|
SRE_CODE flags, i;
|
2008-08-05 00:39:21 -03:00
|
|
|
SRE_CODE *newcode;
|
|
|
|
GET_SKIP;
|
|
|
|
newcode = code+skip-1;
|
|
|
|
GET_ARG; flags = arg;
|
2010-05-03 20:51:28 -03:00
|
|
|
GET_ARG; /* min */
|
|
|
|
GET_ARG; /* max */
|
2008-08-05 00:39:21 -03:00
|
|
|
/* Check that only valid flags are present */
|
|
|
|
if ((flags & ~(SRE_INFO_PREFIX |
|
|
|
|
SRE_INFO_LITERAL |
|
|
|
|
SRE_INFO_CHARSET)) != 0)
|
|
|
|
FAIL;
|
|
|
|
/* PREFIX and CHARSET are mutually exclusive */
|
|
|
|
if ((flags & SRE_INFO_PREFIX) &&
|
|
|
|
(flags & SRE_INFO_CHARSET))
|
|
|
|
FAIL;
|
|
|
|
/* LITERAL implies PREFIX */
|
|
|
|
if ((flags & SRE_INFO_LITERAL) &&
|
|
|
|
!(flags & SRE_INFO_PREFIX))
|
|
|
|
FAIL;
|
|
|
|
/* Validate the prefix */
|
|
|
|
if (flags & SRE_INFO_PREFIX) {
|
2010-05-03 20:51:28 -03:00
|
|
|
SRE_CODE prefix_len;
|
2008-08-05 00:39:21 -03:00
|
|
|
GET_ARG; prefix_len = arg;
|
2010-05-03 20:51:28 -03:00
|
|
|
GET_ARG; /* prefix skip */
|
2008-08-05 00:39:21 -03:00
|
|
|
/* Here comes the prefix string */
|
2013-04-13 15:15:10 -03:00
|
|
|
if (prefix_len > newcode-code)
|
2008-08-05 00:39:21 -03:00
|
|
|
FAIL;
|
|
|
|
code += prefix_len;
|
|
|
|
/* And here comes the overlap table */
|
2013-04-13 15:15:10 -03:00
|
|
|
if (prefix_len > newcode-code)
|
2008-08-05 00:39:21 -03:00
|
|
|
FAIL;
|
|
|
|
/* Each overlap value should be < prefix_len */
|
|
|
|
for (i = 0; i < prefix_len; i++) {
|
|
|
|
if (code[i] >= prefix_len)
|
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
code += prefix_len;
|
|
|
|
}
|
|
|
|
/* Validate the charset */
|
|
|
|
if (flags & SRE_INFO_CHARSET) {
|
|
|
|
if (!_validate_charset(code, newcode-1))
|
|
|
|
FAIL;
|
|
|
|
if (newcode[-1] != SRE_OP_FAILURE)
|
|
|
|
FAIL;
|
|
|
|
code = newcode;
|
|
|
|
}
|
|
|
|
else if (code != newcode) {
|
|
|
|
VTRACE(("code=%p, newcode=%p\n", code, newcode));
|
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_BRANCH:
|
|
|
|
{
|
|
|
|
SRE_CODE *target = NULL;
|
|
|
|
for (;;) {
|
|
|
|
GET_SKIP;
|
|
|
|
if (skip == 0)
|
|
|
|
break;
|
|
|
|
/* Stop 2 before the end; we check the JUMP below */
|
|
|
|
if (!_validate_inner(code, code+skip-3, groups))
|
|
|
|
FAIL;
|
|
|
|
code += skip-3;
|
|
|
|
/* Check that it ends with a JUMP, and that each JUMP
|
|
|
|
has the same target */
|
|
|
|
GET_OP;
|
|
|
|
if (op != SRE_OP_JUMP)
|
|
|
|
FAIL;
|
|
|
|
GET_SKIP;
|
|
|
|
if (target == NULL)
|
|
|
|
target = code+skip-1;
|
|
|
|
else if (code+skip-1 != target)
|
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_REPEAT_ONE:
|
|
|
|
case SRE_OP_MIN_REPEAT_ONE:
|
|
|
|
{
|
|
|
|
SRE_CODE min, max;
|
|
|
|
GET_SKIP;
|
|
|
|
GET_ARG; min = arg;
|
|
|
|
GET_ARG; max = arg;
|
|
|
|
if (min > max)
|
|
|
|
FAIL;
|
2013-02-16 10:47:15 -04:00
|
|
|
if (max > SRE_MAXREPEAT)
|
2008-08-05 00:39:21 -03:00
|
|
|
FAIL;
|
|
|
|
if (!_validate_inner(code, code+skip-4, groups))
|
|
|
|
FAIL;
|
|
|
|
code += skip-4;
|
|
|
|
GET_OP;
|
|
|
|
if (op != SRE_OP_SUCCESS)
|
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_REPEAT:
|
|
|
|
{
|
|
|
|
SRE_CODE min, max;
|
|
|
|
GET_SKIP;
|
|
|
|
GET_ARG; min = arg;
|
|
|
|
GET_ARG; max = arg;
|
|
|
|
if (min > max)
|
|
|
|
FAIL;
|
2013-02-16 10:47:15 -04:00
|
|
|
if (max > SRE_MAXREPEAT)
|
2008-08-05 00:39:21 -03:00
|
|
|
FAIL;
|
|
|
|
if (!_validate_inner(code, code+skip-3, groups))
|
|
|
|
FAIL;
|
|
|
|
code += skip-3;
|
|
|
|
GET_OP;
|
|
|
|
if (op != SRE_OP_MAX_UNTIL && op != SRE_OP_MIN_UNTIL)
|
|
|
|
FAIL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_GROUPREF:
|
|
|
|
case SRE_OP_GROUPREF_IGNORE:
|
|
|
|
GET_ARG;
|
|
|
|
if (arg >= groups)
|
|
|
|
FAIL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_GROUPREF_EXISTS:
|
|
|
|
/* The regex syntax for this is: '(?(group)then|else)', where
|
|
|
|
'group' is either an integer group number or a group name,
|
|
|
|
'then' and 'else' are sub-regexes, and 'else' is optional. */
|
|
|
|
GET_ARG;
|
|
|
|
if (arg >= groups)
|
|
|
|
FAIL;
|
2008-09-10 11:27:00 -03:00
|
|
|
GET_SKIP_ADJ(1);
|
2008-08-05 00:39:21 -03:00
|
|
|
code--; /* The skip is relative to the first arg! */
|
|
|
|
/* There are two possibilities here: if there is both a 'then'
|
|
|
|
part and an 'else' part, the generated code looks like:
|
|
|
|
|
|
|
|
GROUPREF_EXISTS
|
|
|
|
<group>
|
|
|
|
<skipyes>
|
|
|
|
...then part...
|
|
|
|
JUMP
|
|
|
|
<skipno>
|
|
|
|
(<skipyes> jumps here)
|
|
|
|
...else part...
|
|
|
|
(<skipno> jumps here)
|
|
|
|
|
|
|
|
If there is only a 'then' part, it looks like:
|
|
|
|
|
|
|
|
GROUPREF_EXISTS
|
|
|
|
<group>
|
|
|
|
<skip>
|
|
|
|
...then part...
|
|
|
|
(<skip> jumps here)
|
|
|
|
|
|
|
|
There is no direct way to decide which it is, and we don't want
|
|
|
|
to allow arbitrary jumps anywhere in the code; so we just look
|
|
|
|
for a JUMP opcode preceding our skip target.
|
|
|
|
*/
|
2013-04-13 15:15:10 -03:00
|
|
|
if (skip >= 3 && skip-3 < end-code &&
|
2008-08-05 00:39:21 -03:00
|
|
|
code[skip-3] == SRE_OP_JUMP)
|
|
|
|
{
|
|
|
|
VTRACE(("both then and else parts present\n"));
|
|
|
|
if (!_validate_inner(code+1, code+skip-3, groups))
|
|
|
|
FAIL;
|
|
|
|
code += skip-2; /* Position after JUMP, at <skipno> */
|
|
|
|
GET_SKIP;
|
|
|
|
if (!_validate_inner(code, code+skip-1, groups))
|
|
|
|
FAIL;
|
|
|
|
code += skip-1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
VTRACE(("only a then part present\n"));
|
|
|
|
if (!_validate_inner(code+1, code+skip-1, groups))
|
|
|
|
FAIL;
|
|
|
|
code += skip-1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_ASSERT:
|
|
|
|
case SRE_OP_ASSERT_NOT:
|
|
|
|
GET_SKIP;
|
|
|
|
GET_ARG; /* 0 for lookahead, width for lookbehind */
|
|
|
|
code--; /* Back up over arg to simplify math below */
|
|
|
|
if (arg & 0x80000000)
|
|
|
|
FAIL; /* Width too large */
|
|
|
|
/* Stop 1 before the end; we check the SUCCESS below */
|
|
|
|
if (!_validate_inner(code+1, code+skip-2, groups))
|
|
|
|
FAIL;
|
|
|
|
code += skip-2;
|
|
|
|
GET_OP;
|
|
|
|
if (op != SRE_OP_SUCCESS)
|
|
|
|
FAIL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
FAIL;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VTRACE(("okay\n"));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_validate_outer(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups)
|
|
|
|
{
|
|
|
|
if (groups < 0 || groups > 100 || code >= end || end[-1] != SRE_OP_SUCCESS)
|
|
|
|
FAIL;
|
|
|
|
if (groups == 0) /* fix for simplejson */
|
|
|
|
groups = 100; /* 100 groups should always be safe */
|
|
|
|
return _validate_inner(code, end-1, groups);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_validate(PatternObject *self)
|
|
|
|
{
|
|
|
|
if (!_validate_outer(self->code, self->code+self->codesize, self->groups))
|
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "invalid SRE code");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
VTRACE(("Success!\n"));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* match methods */
|
|
|
|
|
|
|
|
static void
|
2000-06-29 08:34:28 -03:00
|
|
|
match_dealloc(MatchObject* self)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
Py_XDECREF(self->regs);
|
|
|
|
Py_XDECREF(self->string);
|
|
|
|
Py_DECREF(self->pattern);
|
|
|
|
PyObject_DEL(self);
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2006-06-12 00:05:40 -03:00
|
|
|
match_getslice_by_index(MatchObject* self, Py_ssize_t index, PyObject* def)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
if (index < 0 || index >= self->groups) {
|
|
|
|
/* raise IndexError if we were given a bad group number */
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_IndexError,
|
|
|
|
"no such group"
|
|
|
|
);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-03 15:44:21 -03:00
|
|
|
index *= 2;
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
if (self->string == Py_None || self->mark[index] < 0) {
|
|
|
|
/* return default value if the string or group is undefined */
|
|
|
|
Py_INCREF(def);
|
|
|
|
return def;
|
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
return PySequence_GetSlice(
|
|
|
|
self->string, self->mark[index], self->mark[index+1]
|
|
|
|
);
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
2006-06-12 00:05:40 -03:00
|
|
|
static Py_ssize_t
|
2000-06-29 08:34:28 -03:00
|
|
|
match_getindex(MatchObject* self, PyObject* index)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t i;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2014-09-30 23:04:28 -03:00
|
|
|
if (PyInt_Check(index) || PyLong_Check(index))
|
2006-06-12 00:05:40 -03:00
|
|
|
return PyInt_AsSsize_t(index);
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-03 15:44:21 -03:00
|
|
|
i = -1;
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
if (self->pattern->groupindex) {
|
|
|
|
index = PyObject_GetItem(self->pattern->groupindex, index);
|
|
|
|
if (index) {
|
2006-06-12 00:05:40 -03:00
|
|
|
if (PyInt_Check(index) || PyLong_Check(index))
|
|
|
|
i = PyInt_AsSsize_t(index);
|
2000-07-03 15:44:21 -03:00
|
|
|
Py_DECREF(index);
|
|
|
|
} else
|
|
|
|
PyErr_Clear();
|
2000-07-23 18:46:17 -03:00
|
|
|
}
|
2000-07-03 15:44:21 -03:00
|
|
|
|
|
|
|
return i;
|
2000-06-29 05:58:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-30 04:08:20 -03:00
|
|
|
match_getslice(MatchObject* self, PyObject* index, PyObject* def)
|
2000-06-29 05:58:44 -03:00
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
return match_getslice_by_index(self, match_getindex(self, index), def);
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
2000-09-21 14:03:25 -03:00
|
|
|
static PyObject*
|
2006-05-28 19:14:04 -03:00
|
|
|
match_expand(MatchObject* self, PyObject* ptemplate)
|
2000-09-21 14:03:25 -03:00
|
|
|
{
|
|
|
|
/* delegate to Python code */
|
|
|
|
return call(
|
2006-03-16 02:30:02 -04:00
|
|
|
SRE_PY_MODULE, "_expand",
|
2006-04-12 01:26:11 -03:00
|
|
|
PyTuple_Pack(3, self->pattern, self, ptemplate)
|
2000-09-21 14:03:25 -03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
static PyObject*
|
2000-06-29 08:34:28 -03:00
|
|
|
match_group(MatchObject* self, PyObject* args)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject* result;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t i, size;
|
2000-07-23 18:46:17 -03:00
|
|
|
|
|
|
|
size = PyTuple_GET_SIZE(args);
|
|
|
|
|
|
|
|
switch (size) {
|
|
|
|
case 0:
|
|
|
|
result = match_getslice(self, Py_False, Py_None);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
result = match_getslice(self, PyTuple_GET_ITEM(args, 0), Py_None);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* fetch multiple items */
|
|
|
|
result = PyTuple_New(size);
|
|
|
|
if (!result)
|
|
|
|
return NULL;
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
PyObject* item = match_getslice(
|
2000-06-30 04:08:20 -03:00
|
|
|
self, PyTuple_GET_ITEM(args, i), Py_None
|
|
|
|
);
|
2000-07-23 18:46:17 -03:00
|
|
|
if (!item) {
|
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyTuple_SET_ITEM(result, i, item);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2000-10-03 17:43:34 -03:00
|
|
|
match_groups(MatchObject* self, PyObject* args, PyObject* kw)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject* result;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t index;
|
2000-07-23 18:46:17 -03:00
|
|
|
|
|
|
|
PyObject* def = Py_None;
|
2006-02-27 12:46:16 -04:00
|
|
|
static char* kwlist[] = { "default", NULL };
|
2000-10-03 17:43:34 -03:00
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:groups", kwlist, &def))
|
2000-07-23 18:46:17 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
result = PyTuple_New(self->groups-1);
|
|
|
|
if (!result)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (index = 1; index < self->groups; index++) {
|
|
|
|
PyObject* item;
|
|
|
|
item = match_getslice_by_index(self, index, def);
|
|
|
|
if (!item) {
|
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyTuple_SET_ITEM(result, index-1, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2000-10-03 17:43:34 -03:00
|
|
|
match_groupdict(MatchObject* self, PyObject* args, PyObject* kw)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject* result;
|
|
|
|
PyObject* keys;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t index;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject* def = Py_None;
|
2006-02-27 12:46:16 -04:00
|
|
|
static char* kwlist[] = { "default", NULL };
|
2001-01-14 11:06:11 -04:00
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:groupdict", kwlist, &def))
|
2000-07-23 18:46:17 -03:00
|
|
|
return NULL;
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
result = PyDict_New();
|
|
|
|
if (!result || !self->pattern->groupindex)
|
|
|
|
return result;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
keys = PyMapping_Keys(self->pattern->groupindex);
|
2001-01-14 11:06:11 -04:00
|
|
|
if (!keys)
|
|
|
|
goto failed;
|
2000-07-23 18:46:17 -03:00
|
|
|
|
|
|
|
for (index = 0; index < PyList_GET_SIZE(keys); index++) {
|
2001-01-14 11:06:11 -04:00
|
|
|
int status;
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject* key;
|
2001-01-14 11:06:11 -04:00
|
|
|
PyObject* value;
|
2000-07-23 18:46:17 -03:00
|
|
|
key = PyList_GET_ITEM(keys, index);
|
2001-01-14 11:06:11 -04:00
|
|
|
if (!key)
|
|
|
|
goto failed;
|
|
|
|
value = match_getslice(self, key, def);
|
|
|
|
if (!value) {
|
2000-07-23 18:46:17 -03:00
|
|
|
Py_DECREF(key);
|
2001-01-14 11:06:11 -04:00
|
|
|
goto failed;
|
2000-07-23 18:46:17 -03:00
|
|
|
}
|
2001-01-14 11:06:11 -04:00
|
|
|
status = PyDict_SetItem(result, key, value);
|
|
|
|
Py_DECREF(value);
|
|
|
|
if (status < 0)
|
|
|
|
goto failed;
|
2000-06-30 04:08:20 -03:00
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
Py_DECREF(keys);
|
|
|
|
|
|
|
|
return result;
|
2001-01-14 11:06:11 -04:00
|
|
|
|
|
|
|
failed:
|
2006-03-07 00:48:24 -04:00
|
|
|
Py_XDECREF(keys);
|
2001-01-14 11:06:11 -04:00
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-29 08:34:28 -03:00
|
|
|
match_start(MatchObject* self, PyObject* args)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t index;
|
2000-06-29 05:58:44 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject* index_ = Py_False; /* zero */
|
2006-05-29 18:04:52 -03:00
|
|
|
if (!PyArg_UnpackTuple(args, "start", 0, 1, &index_))
|
2000-07-23 18:46:17 -03:00
|
|
|
return NULL;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
index = match_getindex(self, index_);
|
2000-06-29 05:58:44 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
if (index < 0 || index >= self->groups) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_IndexError,
|
|
|
|
"no such group"
|
|
|
|
);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-09-02 13:36:57 -03:00
|
|
|
/* mark is -1 if group is undefined */
|
2013-01-10 12:37:47 -04:00
|
|
|
return PyInt_FromSsize_t(self->mark[index*2]);
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-29 08:34:28 -03:00
|
|
|
match_end(MatchObject* self, PyObject* args)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t index;
|
2000-06-29 05:58:44 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject* index_ = Py_False; /* zero */
|
2006-05-29 18:04:52 -03:00
|
|
|
if (!PyArg_UnpackTuple(args, "end", 0, 1, &index_))
|
2000-07-23 18:46:17 -03:00
|
|
|
return NULL;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
index = match_getindex(self, index_);
|
2000-06-29 05:58:44 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
if (index < 0 || index >= self->groups) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_IndexError,
|
|
|
|
"no such group"
|
|
|
|
);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-09-02 13:36:57 -03:00
|
|
|
/* mark is -1 if group is undefined */
|
2013-01-10 12:37:47 -04:00
|
|
|
return PyInt_FromSsize_t(self->mark[index*2+1]);
|
2000-07-23 18:46:17 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
LOCAL(PyObject*)
|
2006-06-12 00:05:40 -03:00
|
|
|
_pair(Py_ssize_t i1, Py_ssize_t i2)
|
2000-07-23 18:46:17 -03:00
|
|
|
{
|
|
|
|
PyObject* pair;
|
|
|
|
PyObject* item;
|
|
|
|
|
|
|
|
pair = PyTuple_New(2);
|
|
|
|
if (!pair)
|
|
|
|
return NULL;
|
|
|
|
|
2006-06-12 00:05:40 -03:00
|
|
|
item = PyInt_FromSsize_t(i1);
|
2000-07-23 18:46:17 -03:00
|
|
|
if (!item)
|
|
|
|
goto error;
|
|
|
|
PyTuple_SET_ITEM(pair, 0, item);
|
|
|
|
|
2006-06-12 00:05:40 -03:00
|
|
|
item = PyInt_FromSsize_t(i2);
|
2000-07-23 18:46:17 -03:00
|
|
|
if (!item)
|
|
|
|
goto error;
|
|
|
|
PyTuple_SET_ITEM(pair, 1, item);
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
return pair;
|
|
|
|
|
|
|
|
error:
|
|
|
|
Py_DECREF(pair);
|
|
|
|
return NULL;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-29 08:34:28 -03:00
|
|
|
match_span(MatchObject* self, PyObject* args)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t index;
|
2000-06-29 05:58:44 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject* index_ = Py_False; /* zero */
|
2006-05-29 18:04:52 -03:00
|
|
|
if (!PyArg_UnpackTuple(args, "span", 0, 1, &index_))
|
2000-07-23 18:46:17 -03:00
|
|
|
return NULL;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
index = match_getindex(self, index_);
|
2000-06-29 05:58:44 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
if (index < 0 || index >= self->groups) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_IndexError,
|
|
|
|
"no such group"
|
|
|
|
);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-09-02 13:36:57 -03:00
|
|
|
/* marks are -1 if group is undefined */
|
2000-07-23 18:46:17 -03:00
|
|
|
return _pair(self->mark[index*2], self->mark[index*2+1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
match_regs(MatchObject* self)
|
|
|
|
{
|
|
|
|
PyObject* regs;
|
|
|
|
PyObject* item;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t index;
|
2000-07-23 18:46:17 -03:00
|
|
|
|
|
|
|
regs = PyTuple_New(self->groups);
|
|
|
|
if (!regs)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (index = 0; index < self->groups; index++) {
|
|
|
|
item = _pair(self->mark[index*2], self->mark[index*2+1]);
|
|
|
|
if (!item) {
|
|
|
|
Py_DECREF(regs);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyTuple_SET_ITEM(regs, index, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(regs);
|
|
|
|
self->regs = regs;
|
|
|
|
|
|
|
|
return regs;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
2001-07-02 13:42:49 -03:00
|
|
|
static PyObject*
|
2006-05-28 19:38:57 -03:00
|
|
|
match_copy(MatchObject* self, PyObject *unused)
|
2001-07-02 13:42:49 -03:00
|
|
|
{
|
2001-07-03 17:32:36 -03:00
|
|
|
#ifdef USE_BUILTIN_COPY
|
2001-07-02 13:42:49 -03:00
|
|
|
MatchObject* copy;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t slots, offset;
|
2006-01-20 22:47:53 -04:00
|
|
|
|
2001-07-02 13:42:49 -03:00
|
|
|
slots = 2 * (self->pattern->groups+1);
|
|
|
|
|
|
|
|
copy = PyObject_NEW_VAR(MatchObject, &Match_Type, slots);
|
|
|
|
if (!copy)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* this value a constant, but any compiler should be able to
|
|
|
|
figure that out all by itself */
|
|
|
|
offset = offsetof(MatchObject, string);
|
|
|
|
|
|
|
|
Py_XINCREF(self->pattern);
|
|
|
|
Py_XINCREF(self->string);
|
|
|
|
Py_XINCREF(self->regs);
|
|
|
|
|
|
|
|
memcpy((char*) copy + offset, (char*) self + offset,
|
2006-06-12 00:05:40 -03:00
|
|
|
sizeof(MatchObject) + slots * sizeof(Py_ssize_t) - offset);
|
2001-07-02 13:42:49 -03:00
|
|
|
|
|
|
|
return (PyObject*) copy;
|
|
|
|
#else
|
2001-07-03 17:32:36 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, "cannot copy this match object");
|
2001-07-02 13:42:49 -03:00
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2006-05-28 19:14:04 -03:00
|
|
|
match_deepcopy(MatchObject* self, PyObject* memo)
|
2001-07-02 13:42:49 -03:00
|
|
|
{
|
2001-07-03 17:32:36 -03:00
|
|
|
#ifdef USE_BUILTIN_COPY
|
|
|
|
MatchObject* copy;
|
2006-01-20 22:47:53 -04:00
|
|
|
|
2006-05-28 19:14:04 -03:00
|
|
|
copy = (MatchObject*) match_copy(self);
|
2001-07-03 17:32:36 -03:00
|
|
|
if (!copy)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!deepcopy((PyObject**) ©->pattern, memo) ||
|
|
|
|
!deepcopy(©->string, memo) ||
|
|
|
|
!deepcopy(©->regs, memo)) {
|
|
|
|
Py_DECREF(copy);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2001-07-02 13:42:49 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, "cannot deepcopy this match object");
|
|
|
|
return NULL;
|
2001-07-03 17:32:36 -03:00
|
|
|
#endif
|
2001-07-02 13:42:49 -03:00
|
|
|
}
|
|
|
|
|
2012-12-23 14:09:01 -04:00
|
|
|
PyDoc_STRVAR(match_doc,
|
|
|
|
"The result of re.match() and re.search().\n\
|
|
|
|
Match objects always have a boolean value of True.");
|
|
|
|
|
|
|
|
PyDoc_STRVAR(match_group_doc,
|
|
|
|
"group([group1, ...]) -> str or tuple.\n\
|
|
|
|
Return subgroup(s) of the match by indices or names.\n\
|
|
|
|
For 0 returns the entire match.");
|
|
|
|
|
|
|
|
PyDoc_STRVAR(match_start_doc,
|
|
|
|
"start([group=0]) -> int.\n\
|
|
|
|
Return index of the start of the substring matched by group.");
|
|
|
|
|
|
|
|
PyDoc_STRVAR(match_end_doc,
|
|
|
|
"end([group=0]) -> int.\n\
|
|
|
|
Return index of the end of the substring matched by group.");
|
|
|
|
|
|
|
|
PyDoc_STRVAR(match_span_doc,
|
|
|
|
"span([group]) -> tuple.\n\
|
|
|
|
For MatchObject m, return the 2-tuple (m.start(group), m.end(group)).");
|
|
|
|
|
|
|
|
PyDoc_STRVAR(match_groups_doc,
|
|
|
|
"groups([default=None]) -> tuple.\n\
|
|
|
|
Return a tuple containing all the subgroups of the match, from 1.\n\
|
|
|
|
The default argument is used for groups\n\
|
|
|
|
that did not participate in the match");
|
|
|
|
|
|
|
|
PyDoc_STRVAR(match_groupdict_doc,
|
|
|
|
"groupdict([default=None]) -> dict.\n\
|
|
|
|
Return a dictionary containing all the named subgroups of the match,\n\
|
|
|
|
keyed by the subgroup name. The default argument is used for groups\n\
|
|
|
|
that did not participate in the match");
|
|
|
|
|
|
|
|
PyDoc_STRVAR(match_expand_doc,
|
|
|
|
"expand(template) -> str.\n\
|
|
|
|
Return the string obtained by doing backslash substitution\n\
|
|
|
|
on the string template, as done by the sub() method.");
|
|
|
|
|
|
|
|
static PyMethodDef match_methods[] = {
|
|
|
|
{"group", (PyCFunction) match_group, METH_VARARGS, match_group_doc},
|
|
|
|
{"start", (PyCFunction) match_start, METH_VARARGS, match_start_doc},
|
|
|
|
{"end", (PyCFunction) match_end, METH_VARARGS, match_end_doc},
|
|
|
|
{"span", (PyCFunction) match_span, METH_VARARGS, match_span_doc},
|
|
|
|
{"groups", (PyCFunction) match_groups, METH_VARARGS|METH_KEYWORDS,
|
|
|
|
match_groups_doc},
|
|
|
|
{"groupdict", (PyCFunction) match_groupdict, METH_VARARGS|METH_KEYWORDS,
|
|
|
|
match_groupdict_doc},
|
|
|
|
{"expand", (PyCFunction) match_expand, METH_O, match_expand_doc},
|
2006-05-28 19:14:04 -03:00
|
|
|
{"__copy__", (PyCFunction) match_copy, METH_NOARGS},
|
|
|
|
{"__deepcopy__", (PyCFunction) match_deepcopy, METH_O},
|
2000-07-23 18:46:17 -03:00
|
|
|
{NULL, NULL}
|
2000-03-31 10:59:30 -04:00
|
|
|
};
|
|
|
|
|
2011-05-17 20:31:20 -03:00
|
|
|
static PyObject *
|
|
|
|
match_lastindex_get(MatchObject *self)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2011-05-17 20:31:20 -03:00
|
|
|
if (self->lastindex >= 0)
|
2013-01-10 12:37:47 -04:00
|
|
|
return PyInt_FromSsize_t(self->lastindex);
|
2011-05-17 20:31:20 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2011-05-17 20:31:20 -03:00
|
|
|
static PyObject *
|
|
|
|
match_lastgroup_get(MatchObject *self)
|
|
|
|
{
|
|
|
|
if (self->pattern->indexgroup && self->lastindex >= 0) {
|
|
|
|
PyObject* result = PySequence_GetItem(
|
|
|
|
self->pattern->indexgroup, self->lastindex
|
|
|
|
);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
PyErr_Clear();
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
2011-05-17 20:31:20 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2011-05-17 20:31:20 -03:00
|
|
|
static PyObject *
|
|
|
|
match_regs_get(MatchObject *self)
|
|
|
|
{
|
|
|
|
if (self->regs) {
|
|
|
|
Py_INCREF(self->regs);
|
|
|
|
return self->regs;
|
|
|
|
} else
|
|
|
|
return match_regs(self);
|
|
|
|
}
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2011-05-17 20:31:20 -03:00
|
|
|
static PyGetSetDef match_getset[] = {
|
|
|
|
{"lastindex", (getter)match_lastindex_get, (setter)NULL},
|
|
|
|
{"lastgroup", (getter)match_lastgroup_get, (setter)NULL},
|
|
|
|
{"regs", (getter)match_regs_get, (setter)NULL},
|
|
|
|
{NULL}
|
|
|
|
};
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2011-05-17 20:31:20 -03:00
|
|
|
#define MATCH_OFF(x) offsetof(MatchObject, x)
|
|
|
|
static PyMemberDef match_members[] = {
|
|
|
|
{"string", T_OBJECT, MATCH_OFF(string), READONLY},
|
|
|
|
{"re", T_OBJECT, MATCH_OFF(pattern), READONLY},
|
|
|
|
{"pos", T_PYSSIZET, MATCH_OFF(pos), READONLY},
|
|
|
|
{"endpos", T_PYSSIZET, MATCH_OFF(endpos), READONLY},
|
|
|
|
{NULL}
|
|
|
|
};
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
|
|
|
|
/* FIXME: implement setattr("string", None) as a special case (to
|
|
|
|
detach the associated string, if any */
|
|
|
|
|
2011-05-17 20:31:20 -03:00
|
|
|
static PyTypeObject Match_Type = {
|
|
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
|
|
|
"_" SRE_MODULE ".SRE_Match",
|
2006-06-12 00:05:40 -03:00
|
|
|
sizeof(MatchObject), sizeof(Py_ssize_t),
|
2011-05-17 20:31:20 -03:00
|
|
|
(destructor)match_dealloc, /* tp_dealloc */
|
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_compare */
|
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
0, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT,
|
2012-12-23 14:09:01 -04:00
|
|
|
match_doc, /* tp_doc */
|
2011-05-17 20:31:20 -03:00
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
match_methods, /* tp_methods */
|
|
|
|
match_members, /* tp_members */
|
|
|
|
match_getset, /* tp_getset */
|
2000-03-31 10:59:30 -04:00
|
|
|
};
|
|
|
|
|
2006-04-12 01:26:11 -03:00
|
|
|
static PyObject*
|
|
|
|
pattern_new_match(PatternObject* pattern, SRE_STATE* state, int status)
|
|
|
|
{
|
|
|
|
/* create match object (from state object) */
|
|
|
|
|
|
|
|
MatchObject* match;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t i, j;
|
2006-04-12 01:26:11 -03:00
|
|
|
char* base;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (status > 0) {
|
|
|
|
|
|
|
|
/* create match object (with room for extra group marks) */
|
2008-01-18 15:12:56 -04:00
|
|
|
/* coverity[ampersand_in_size] */
|
2006-04-12 01:26:11 -03:00
|
|
|
match = PyObject_NEW_VAR(MatchObject, &Match_Type,
|
|
|
|
2*(pattern->groups+1));
|
|
|
|
if (!match)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Py_INCREF(pattern);
|
|
|
|
match->pattern = pattern;
|
|
|
|
|
|
|
|
Py_INCREF(state->string);
|
|
|
|
match->string = state->string;
|
|
|
|
|
|
|
|
match->regs = NULL;
|
|
|
|
match->groups = pattern->groups+1;
|
|
|
|
|
|
|
|
/* fill in group slices */
|
|
|
|
|
|
|
|
base = (char*) state->beginning;
|
|
|
|
n = state->charsize;
|
|
|
|
|
|
|
|
match->mark[0] = ((char*) state->start - base) / n;
|
|
|
|
match->mark[1] = ((char*) state->ptr - base) / n;
|
|
|
|
|
|
|
|
for (i = j = 0; i < pattern->groups; i++, j+=2)
|
|
|
|
if (j+1 <= state->lastmark && state->mark[j] && state->mark[j+1]) {
|
|
|
|
match->mark[j+2] = ((char*) state->mark[j] - base) / n;
|
|
|
|
match->mark[j+3] = ((char*) state->mark[j+1] - base) / n;
|
|
|
|
} else
|
|
|
|
match->mark[j+2] = match->mark[j+3] = -1; /* undefined */
|
|
|
|
|
|
|
|
match->pos = state->pos;
|
|
|
|
match->endpos = state->endpos;
|
|
|
|
|
|
|
|
match->lastindex = state->lastindex;
|
|
|
|
|
|
|
|
return (PyObject*) match;
|
|
|
|
|
|
|
|
} else if (status == 0) {
|
|
|
|
|
|
|
|
/* no match */
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* internal error */
|
|
|
|
pattern_error(status);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
/* -------------------------------------------------------------------- */
|
2000-06-29 13:57:40 -03:00
|
|
|
/* scanner methods (experimental) */
|
2000-06-01 14:39:12 -03:00
|
|
|
|
|
|
|
static void
|
2000-06-29 13:57:40 -03:00
|
|
|
scanner_dealloc(ScannerObject* self)
|
2000-06-01 14:39:12 -03:00
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
state_fini(&self->state);
|
2010-01-14 13:25:24 -04:00
|
|
|
Py_XDECREF(self->pattern);
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject_DEL(self);
|
2000-06-01 14:39:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2006-05-28 19:38:57 -03:00
|
|
|
scanner_match(ScannerObject* self, PyObject *unused)
|
2000-06-01 14:39:12 -03:00
|
|
|
{
|
|
|
|
SRE_STATE* state = &self->state;
|
|
|
|
PyObject* match;
|
|
|
|
int status;
|
|
|
|
|
2015-07-06 07:58:24 -03:00
|
|
|
if (state->start == NULL)
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
state_reset(state);
|
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
state->ptr = state->start;
|
|
|
|
|
|
|
|
if (state->charsize == 1) {
|
2003-12-13 16:32:08 -04:00
|
|
|
status = sre_match(state, PatternObject_GetCode(self->pattern));
|
2000-06-01 14:39:12 -03:00
|
|
|
} else {
|
2000-06-29 05:58:44 -03:00
|
|
|
#if defined(HAVE_UNICODE)
|
2003-12-13 16:32:08 -04:00
|
|
|
status = sre_umatch(state, PatternObject_GetCode(self->pattern));
|
2000-06-29 05:58:44 -03:00
|
|
|
#endif
|
2000-06-01 14:39:12 -03:00
|
|
|
}
|
2006-10-04 10:42:43 -03:00
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
match = pattern_new_match((PatternObject*) self->pattern,
|
2000-07-23 18:46:17 -03:00
|
|
|
state, status);
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2015-07-06 07:58:24 -03:00
|
|
|
if (status == 0)
|
|
|
|
state->start = NULL;
|
|
|
|
else if (state->ptr != state->start)
|
|
|
|
state->start = state->ptr;
|
|
|
|
else if (state->ptr != state->end)
|
2000-06-29 05:58:44 -03:00
|
|
|
state->start = (void*) ((char*) state->ptr + state->charsize);
|
2000-06-01 14:39:12 -03:00
|
|
|
else
|
2015-07-06 07:58:24 -03:00
|
|
|
state->start = NULL;
|
2000-06-01 14:39:12 -03:00
|
|
|
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject*
|
2006-05-28 19:38:57 -03:00
|
|
|
scanner_search(ScannerObject* self, PyObject *unused)
|
2000-06-01 14:39:12 -03:00
|
|
|
{
|
|
|
|
SRE_STATE* state = &self->state;
|
|
|
|
PyObject* match;
|
|
|
|
int status;
|
|
|
|
|
2015-07-06 07:58:24 -03:00
|
|
|
if (state->start == NULL)
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
state_reset(state);
|
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
state->ptr = state->start;
|
|
|
|
|
|
|
|
if (state->charsize == 1) {
|
|
|
|
status = sre_search(state, PatternObject_GetCode(self->pattern));
|
|
|
|
} else {
|
2000-06-29 05:58:44 -03:00
|
|
|
#if defined(HAVE_UNICODE)
|
2000-06-01 14:39:12 -03:00
|
|
|
status = sre_usearch(state, PatternObject_GetCode(self->pattern));
|
2000-06-29 05:58:44 -03:00
|
|
|
#endif
|
2000-06-01 14:39:12 -03:00
|
|
|
}
|
2006-10-04 10:42:43 -03:00
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
match = pattern_new_match((PatternObject*) self->pattern,
|
2000-07-23 18:46:17 -03:00
|
|
|
state, status);
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2015-07-06 07:58:24 -03:00
|
|
|
if (status == 0)
|
|
|
|
state->start = NULL;
|
|
|
|
else if (state->ptr != state->start)
|
|
|
|
state->start = state->ptr;
|
|
|
|
else if (state->ptr != state->end)
|
2000-06-29 13:57:40 -03:00
|
|
|
state->start = (void*) ((char*) state->ptr + state->charsize);
|
|
|
|
else
|
2015-07-06 07:58:24 -03:00
|
|
|
state->start = NULL;
|
2000-06-01 14:39:12 -03:00
|
|
|
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
2000-06-29 13:57:40 -03:00
|
|
|
static PyMethodDef scanner_methods[] = {
|
2006-05-28 19:14:04 -03:00
|
|
|
{"match", (PyCFunction) scanner_match, METH_NOARGS},
|
|
|
|
{"search", (PyCFunction) scanner_search, METH_NOARGS},
|
2000-07-23 18:46:17 -03:00
|
|
|
{NULL, NULL}
|
2000-06-01 14:39:12 -03:00
|
|
|
};
|
|
|
|
|
2011-05-17 20:31:20 -03:00
|
|
|
#define SCAN_OFF(x) offsetof(ScannerObject, x)
|
|
|
|
static PyMemberDef scanner_members[] = {
|
|
|
|
{"pattern", T_OBJECT, SCAN_OFF(pattern), READONLY},
|
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-06-29 13:57:40 -03:00
|
|
|
statichere PyTypeObject Scanner_Type = {
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject_HEAD_INIT(NULL)
|
2001-12-09 12:13:15 -04:00
|
|
|
0, "_" SRE_MODULE ".SRE_Scanner",
|
2000-07-23 18:46:17 -03:00
|
|
|
sizeof(ScannerObject), 0,
|
|
|
|
(destructor)scanner_dealloc, /*tp_dealloc*/
|
2011-05-17 20:31:20 -03:00
|
|
|
0, /* tp_print */
|
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_reserved */
|
|
|
|
0, /* tp_repr */
|
|
|
|
0, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
|
|
|
0, /* tp_str */
|
|
|
|
0, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
|
|
|
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
|
|
|
0, /* tp_doc */
|
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
scanner_methods, /* tp_methods */
|
|
|
|
scanner_members, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
2000-06-01 14:39:12 -03:00
|
|
|
};
|
|
|
|
|
2006-04-12 01:26:11 -03:00
|
|
|
static PyObject*
|
|
|
|
pattern_scanner(PatternObject* pattern, PyObject* args)
|
|
|
|
{
|
|
|
|
/* create search state object */
|
|
|
|
|
|
|
|
ScannerObject* self;
|
|
|
|
|
|
|
|
PyObject* string;
|
2006-06-12 00:05:40 -03:00
|
|
|
Py_ssize_t start = 0;
|
|
|
|
Py_ssize_t end = PY_SSIZE_T_MAX;
|
|
|
|
if (!PyArg_ParseTuple(args, "O|nn:scanner", &string, &start, &end))
|
2006-04-12 01:26:11 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* create scanner object */
|
|
|
|
self = PyObject_NEW(ScannerObject, &Scanner_Type);
|
|
|
|
if (!self)
|
|
|
|
return NULL;
|
2010-01-14 13:25:24 -04:00
|
|
|
self->pattern = NULL;
|
2006-04-12 01:26:11 -03:00
|
|
|
|
|
|
|
string = state_init(&self->state, pattern, string, start, end);
|
|
|
|
if (!string) {
|
2010-01-14 13:25:24 -04:00
|
|
|
Py_DECREF(self);
|
2006-04-12 01:26:11 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(pattern);
|
|
|
|
self->pattern = (PyObject*) pattern;
|
|
|
|
|
|
|
|
return (PyObject*) self;
|
|
|
|
}
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
static PyMethodDef _functions[] = {
|
2002-03-31 10:44:22 -04:00
|
|
|
{"compile", _compile, METH_VARARGS},
|
2006-05-28 19:14:04 -03:00
|
|
|
{"getcodesize", sre_codesize, METH_NOARGS},
|
2002-03-31 10:44:22 -04:00
|
|
|
{"getlower", sre_getlower, METH_VARARGS},
|
2000-07-23 18:46:17 -03:00
|
|
|
{NULL, NULL}
|
2000-03-31 10:59:30 -04:00
|
|
|
};
|
|
|
|
|
2006-01-20 22:47:53 -04:00
|
|
|
#if PY_VERSION_HEX < 0x02030000
|
2003-04-30 10:09:08 -03:00
|
|
|
DL_EXPORT(void) init_sre(void)
|
|
|
|
#else
|
2002-07-19 03:55:41 -03:00
|
|
|
PyMODINIT_FUNC init_sre(void)
|
2003-04-30 10:09:08 -03:00
|
|
|
#endif
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2001-01-15 08:46:09 -04:00
|
|
|
PyObject* m;
|
|
|
|
PyObject* d;
|
2001-08-16 17:33:48 -03:00
|
|
|
PyObject* x;
|
2001-01-15 08:46:09 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
/* Patch object types */
|
2010-04-06 00:34:09 -03:00
|
|
|
if (PyType_Ready(&Pattern_Type) || PyType_Ready(&Match_Type) ||
|
|
|
|
PyType_Ready(&Scanner_Type))
|
|
|
|
return;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2001-01-16 03:37:30 -04:00
|
|
|
m = Py_InitModule("_" SRE_MODULE, _functions);
|
2006-01-19 02:09:39 -04:00
|
|
|
if (m == NULL)
|
|
|
|
return;
|
2001-01-15 08:46:09 -04:00
|
|
|
d = PyModule_GetDict(m);
|
|
|
|
|
2001-09-18 15:47:09 -03:00
|
|
|
x = PyInt_FromLong(SRE_MAGIC);
|
|
|
|
if (x) {
|
|
|
|
PyDict_SetItemString(d, "MAGIC", x);
|
|
|
|
Py_DECREF(x);
|
|
|
|
}
|
2001-04-15 16:00:58 -03:00
|
|
|
|
2003-04-19 09:56:08 -03:00
|
|
|
x = PyInt_FromLong(sizeof(SRE_CODE));
|
|
|
|
if (x) {
|
|
|
|
PyDict_SetItemString(d, "CODESIZE", x);
|
|
|
|
Py_DECREF(x);
|
|
|
|
}
|
|
|
|
|
2013-02-16 10:47:15 -04:00
|
|
|
x = PyLong_FromUnsignedLong(SRE_MAXREPEAT);
|
|
|
|
if (x) {
|
|
|
|
PyDict_SetItemString(d, "MAXREPEAT", x);
|
|
|
|
Py_DECREF(x);
|
|
|
|
}
|
|
|
|
|
2008-06-09 01:58:54 -03:00
|
|
|
x = PyString_FromString(copyright);
|
2001-09-18 15:47:09 -03:00
|
|
|
if (x) {
|
|
|
|
PyDict_SetItemString(d, "copyright", x);
|
|
|
|
Py_DECREF(x);
|
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
#endif /* !defined(SRE_RECURSIVE) */
|
2003-04-20 04:35:44 -03:00
|
|
|
|
|
|
|
/* vim:ts=4:sw=4:et
|
|
|
|
*/
|