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-06-30 fl added fast search optimization
|
|
|
|
* 2000-06-30 fl added assert (lookahead) primitives, etc
|
|
|
|
* 2000-07-02 fl added charset optimizations, etc
|
2000-09-21 14:03:25 -03:00
|
|
|
* 2000-07-03 fl store code in pattern object, lookbehind, etc
|
|
|
|
* 2000-07-08 fl added regs attribute
|
2000-10-28 16:30:41 -03:00
|
|
|
* 2000-07-21 fl reset lastindex in scanner methods
|
|
|
|
* 2000-08-01 fl fixes for 1.6b1
|
2000-09-21 14:03:25 -03:00
|
|
|
* 2000-08-03 fl added recursion limit
|
|
|
|
* 2000-08-07 fl use PyOS_CheckStack() if available
|
|
|
|
* 2000-08-08 fl changed findall to return empty strings instead of None
|
|
|
|
* 2000-08-27 fl properly propagate memory errors
|
|
|
|
* 2000-09-02 fl return -1 instead of None for start/end/span
|
|
|
|
* 2000-09-20 fl added expand method
|
|
|
|
* 2000-09-21 fl don't use the buffer interface for unicode strings
|
2000-10-03 17:43:34 -03:00
|
|
|
* 2000-10-03 fl fixed assert_not primitive; support keyword arguments
|
2000-10-28 16:30:41 -03:00
|
|
|
* 2000-10-24 fl really fixed assert_not; reset groups in findall
|
2001-01-14 11:06:11 -04:00
|
|
|
* 2000-12-21 fl fixed memory leak in groupdict
|
|
|
|
* 2001-01-02 fl properly reset pointer after failed assertion in MIN_UNTIL
|
2001-03-22 11:50:10 -04:00
|
|
|
* 2001-01-15 fl avoid recursion for MIN_UNTIL; fixed uppercase literal bug
|
2001-01-16 03:05:29 -04:00
|
|
|
* 2001-01-16 fl fixed memory leak in pattern destructor
|
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)
|
|
|
|
* 2001-05-14 fl fixes for 1.5.2
|
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-22 18:18:08 -03:00
|
|
|
* 2001-10-22 fl check for literal sub/subn templates
|
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)
|
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[] =
|
2001-10-21 13:47:57 -03:00
|
|
|
" SRE 2.2.1 Copyright (c) 1997-2001 by Secret Labs AB ";
|
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
|
|
|
|
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
|
|
|
|
2000-08-27 17:59:47 -03:00
|
|
|
/* prevent run-away recursion (bad patterns on long strings) */
|
|
|
|
|
2000-08-07 14:33:38 -03:00
|
|
|
#if !defined(USE_STACKCHECK)
|
2000-08-27 17:59:47 -03:00
|
|
|
#if defined(MS_WIN64) || defined(__LP64__) || defined(_LP64)
|
|
|
|
/* require smaller recursion limit for a number of 64-bit platforms:
|
|
|
|
Win64 (MS_WIN64), Linux64 (__LP64__), Monterey (64-bit AIX) (_LP64) */
|
|
|
|
/* FIXME: maybe the limit should be 40000 / sizeof(void*) ? */
|
|
|
|
#define USE_RECURSION_LIMIT 7500
|
|
|
|
#else
|
|
|
|
#define USE_RECURSION_LIMIT 10000
|
|
|
|
#endif
|
2000-08-07 14:33:38 -03:00
|
|
|
#endif
|
2000-08-03 13:29:50 -03:00
|
|
|
|
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 */
|
|
|
|
|
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
|
|
|
{
|
2001-03-22 11:50:10 -04:00
|
|
|
return ((ch) < 128 ? 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 */
|
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
#define SRE_LOC_IS_DIGIT(ch) ((ch) < 256 ? isdigit((ch)) : 0)
|
|
|
|
#define SRE_LOC_IS_SPACE(ch) ((ch) < 256 ? isspace((ch)) : 0)
|
|
|
|
#define SRE_LOC_IS_LINEBREAK(ch) ((ch) == '\n')
|
|
|
|
#define SRE_LOC_IS_ALNUM(ch) ((ch) < 256 ? isalnum((ch)) : 0)
|
|
|
|
#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)
|
|
|
|
{
|
|
|
|
return ((ch) < 256 ? tolower((ch)) : ch);
|
|
|
|
}
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
/* unicode-specific character predicates */
|
|
|
|
|
|
|
|
#if defined(HAVE_UNICODE)
|
2001-03-22 11:50:10 -04:00
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
#define SRE_UNI_IS_DIGIT(ch) Py_UNICODE_ISDIGIT((Py_UNICODE)(ch))
|
|
|
|
#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
|
|
|
|
mark_fini(SRE_STATE* state)
|
|
|
|
{
|
2000-08-03 13:29:50 -03:00
|
|
|
if (state->mark_stack) {
|
2000-08-01 15:20:07 -03:00
|
|
|
free(state->mark_stack);
|
2000-08-03 13:29:50 -03:00
|
|
|
state->mark_stack = NULL;
|
|
|
|
}
|
|
|
|
state->mark_stack_size = state->mark_stack_base = 0;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
static int
|
|
|
|
mark_save(SRE_STATE* state, int lo, int hi)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2000-08-01 15:20:07 -03:00
|
|
|
void* stack;
|
|
|
|
int size;
|
|
|
|
int minsize, newsize;
|
|
|
|
|
|
|
|
if (hi <= lo)
|
|
|
|
return 0;
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
size = (hi - lo) + 1;
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
newsize = state->mark_stack_size;
|
|
|
|
minsize = state->mark_stack_base + size;
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
if (newsize < minsize) {
|
2000-07-23 18:46:17 -03:00
|
|
|
/* create new stack */
|
2000-08-01 15:20:07 -03:00
|
|
|
if (!newsize) {
|
|
|
|
newsize = 512;
|
|
|
|
if (newsize < minsize)
|
|
|
|
newsize = minsize;
|
|
|
|
TRACE(("allocate stack %d\n", newsize));
|
|
|
|
stack = malloc(sizeof(void*) * newsize);
|
|
|
|
} else {
|
|
|
|
/* grow the stack */
|
|
|
|
while (newsize < minsize)
|
|
|
|
newsize += newsize;
|
|
|
|
TRACE(("grow stack to %d\n", newsize));
|
|
|
|
stack = realloc(state->mark_stack, sizeof(void*) * newsize);
|
|
|
|
}
|
|
|
|
if (!stack) {
|
|
|
|
mark_fini(state);
|
|
|
|
return SRE_ERROR_MEMORY;
|
|
|
|
}
|
|
|
|
state->mark_stack = stack;
|
|
|
|
state->mark_stack_size = newsize;
|
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(("copy %d:%d to %d (%d)\n", lo, hi, state->mark_stack_base, size));
|
2000-08-01 15:20:07 -03:00
|
|
|
|
|
|
|
memcpy(state->mark_stack + state->mark_stack_base, state->mark + lo,
|
|
|
|
size * sizeof(void*));
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
state->mark_stack_base += size;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
mark_restore(SRE_STATE* state, int lo, int hi)
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
|
|
|
|
if (hi <= lo)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
size = (hi - lo) + 1;
|
|
|
|
|
|
|
|
state->mark_stack_base -= size;
|
|
|
|
|
|
|
|
TRACE(("copy %d:%d from %d\n", lo, hi, state->mark_stack_base));
|
|
|
|
|
|
|
|
memcpy(state->mark + lo, state->mark_stack + state->mark_stack_base,
|
|
|
|
size * sizeof(void*));
|
2000-03-31 10:59:30 -04:00
|
|
|
|
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
|
|
|
|
#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
|
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
|
|
|
|
#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
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
int this, that;
|
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:
|
2000-06-30 18:40:20 -03:00
|
|
|
return (((void*) (ptr+1) == state->end &&
|
|
|
|
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;
|
|
|
|
that = ((void*) ptr > state->beginning) ?
|
|
|
|
SRE_IS_WORD((int) ptr[-1]) : 0;
|
|
|
|
this = ((void*) ptr < state->end) ?
|
|
|
|
SRE_IS_WORD((int) ptr[0]) : 0;
|
|
|
|
return this != that;
|
|
|
|
|
|
|
|
case SRE_AT_NON_BOUNDARY:
|
|
|
|
if (state->beginning == state->end)
|
|
|
|
return 0;
|
|
|
|
that = ((void*) ptr > state->beginning) ?
|
|
|
|
SRE_IS_WORD((int) ptr[-1]) : 0;
|
|
|
|
this = ((void*) ptr < state->end) ?
|
|
|
|
SRE_IS_WORD((int) ptr[0]) : 0;
|
|
|
|
return this == that;
|
2001-03-22 11:50:10 -04:00
|
|
|
|
|
|
|
case SRE_AT_LOC_BOUNDARY:
|
|
|
|
if (state->beginning == state->end)
|
|
|
|
return 0;
|
|
|
|
that = ((void*) ptr > state->beginning) ?
|
|
|
|
SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
|
|
|
|
this = ((void*) ptr < state->end) ?
|
|
|
|
SRE_LOC_IS_WORD((int) ptr[0]) : 0;
|
|
|
|
return this != that;
|
|
|
|
|
|
|
|
case SRE_AT_LOC_NON_BOUNDARY:
|
|
|
|
if (state->beginning == state->end)
|
|
|
|
return 0;
|
|
|
|
that = ((void*) ptr > state->beginning) ?
|
|
|
|
SRE_LOC_IS_WORD((int) ptr[-1]) : 0;
|
|
|
|
this = ((void*) ptr < state->end) ?
|
|
|
|
SRE_LOC_IS_WORD((int) ptr[0]) : 0;
|
|
|
|
return this == that;
|
|
|
|
|
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;
|
|
|
|
that = ((void*) ptr > state->beginning) ?
|
|
|
|
SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
|
|
|
|
this = ((void*) ptr < state->end) ?
|
|
|
|
SRE_UNI_IS_WORD((int) ptr[0]) : 0;
|
|
|
|
return this != that;
|
|
|
|
|
|
|
|
case SRE_AT_UNI_NON_BOUNDARY:
|
|
|
|
if (state->beginning == state->end)
|
|
|
|
return 0;
|
|
|
|
that = ((void*) ptr > state->beginning) ?
|
|
|
|
SRE_UNI_IS_WORD((int) ptr[-1]) : 0;
|
|
|
|
this = ((void*) ptr < state->end) ?
|
|
|
|
SRE_UNI_IS_WORD((int) ptr[0]) : 0;
|
|
|
|
return this == that;
|
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
|
|
|
|
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
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
case SRE_OP_RANGE:
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <RANGE> <lower> <upper> */
|
2000-07-23 18:46:17 -03:00
|
|
|
if (set[0] <= ch && ch <= set[1])
|
|
|
|
return ok;
|
|
|
|
set += 2;
|
|
|
|
break;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-02 09:00:07 -03:00
|
|
|
case SRE_OP_CHARSET:
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <CHARSET> <bitmap> (16 bits per code word) */
|
2000-07-02 09:00:07 -03:00
|
|
|
if (ch < 256 && (set[ch >> 4] & (1 << (ch & 15))))
|
|
|
|
return ok;
|
|
|
|
set += 16;
|
|
|
|
break;
|
|
|
|
|
2001-07-02 14:04:48 -03:00
|
|
|
case SRE_OP_BIGCHARSET:
|
|
|
|
/* <BIGCHARSET> <blockcount> <256 blockindices> <blocks> */
|
|
|
|
{
|
|
|
|
int count, block;
|
|
|
|
count = *(set++);
|
|
|
|
block = ((unsigned char*)set)[ch >> 8];
|
|
|
|
set += 128;
|
|
|
|
if (set[block*16 + ((ch & 255)>>4)] & (1 << (ch & 15)))
|
|
|
|
return ok;
|
|
|
|
set += count*16;
|
|
|
|
break;
|
|
|
|
}
|
2001-07-02 13:58:38 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
case SRE_OP_CATEGORY:
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <CATEGORY> <code> */
|
2000-07-23 18:46:17 -03:00
|
|
|
if (sre_category(set[0], (int) ch))
|
|
|
|
return ok;
|
|
|
|
set += 1;
|
|
|
|
break;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-08-03 13:29:50 -03:00
|
|
|
case SRE_OP_NEGATE:
|
|
|
|
ok = !ok;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_FAILURE:
|
|
|
|
return !ok;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2000-08-03 13:29:50 -03:00
|
|
|
LOCAL(int) SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level);
|
|
|
|
|
|
|
|
LOCAL(int)
|
|
|
|
SRE_COUNT(SRE_STATE* state, SRE_CODE* pattern, int maxcount, int level)
|
|
|
|
{
|
|
|
|
SRE_CODE chr;
|
|
|
|
SRE_CHAR* ptr = state->ptr;
|
|
|
|
SRE_CHAR* end = state->end;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* adjust end */
|
|
|
|
if (maxcount < end - ptr && maxcount != 65535)
|
|
|
|
end = ptr + maxcount;
|
|
|
|
|
|
|
|
switch (pattern[0]) {
|
|
|
|
|
|
|
|
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:
|
|
|
|
/* repeated dot wildcare. skip to the end of the target
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
case SRE_OP_IN:
|
|
|
|
/* repeated set */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|COUNT IN\n", pattern, ptr));
|
|
|
|
while (ptr < end && SRE_CHARSET(pattern + 2, *ptr))
|
2000-08-03 13:29:50 -03:00
|
|
|
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) {
|
|
|
|
i = SRE_MATCH(state, pattern, level);
|
|
|
|
if (i < 0)
|
|
|
|
return i;
|
|
|
|
if (!i)
|
|
|
|
break;
|
|
|
|
}
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|COUNT %d\n", pattern, ptr,
|
|
|
|
(SRE_CHAR*) state->ptr - ptr));
|
2000-08-03 13:29:50 -03:00
|
|
|
return (SRE_CHAR*) state->ptr - ptr;
|
|
|
|
}
|
|
|
|
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|COUNT %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;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
LOCAL(int)
|
2000-08-01 18:05:41 -03:00
|
|
|
SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2000-08-03 13:29:50 -03:00
|
|
|
/* check if string matches the given pattern. returns <0 for
|
2000-07-23 18:46:17 -03:00
|
|
|
error, 0 for failure, and 1 for success */
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
SRE_CHAR* end = state->end;
|
|
|
|
SRE_CHAR* ptr = state->ptr;
|
|
|
|
int i, count;
|
2000-08-01 15:20:07 -03:00
|
|
|
SRE_REPEAT* rp;
|
|
|
|
int lastmark;
|
2000-08-01 19:47:49 -03:00
|
|
|
SRE_CODE chr;
|
2000-08-01 15:20:07 -03:00
|
|
|
|
|
|
|
SRE_REPEAT rep; /* FIXME: <fl> allocate in STATE instead */
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|ENTER %d\n", pattern, ptr, level));
|
2000-06-29 05:58:44 -03:00
|
|
|
|
2000-08-07 14:33:38 -03:00
|
|
|
#if defined(USE_STACKCHECK)
|
2000-08-09 06:14:35 -03:00
|
|
|
if (level % 10 == 0 && PyOS_CheckStack())
|
2000-08-07 14:33:38 -03:00
|
|
|
return SRE_ERROR_RECURSION_LIMIT;
|
|
|
|
#endif
|
|
|
|
|
2000-08-03 13:29:50 -03:00
|
|
|
#if defined(USE_RECURSION_LIMIT)
|
|
|
|
if (level > USE_RECURSION_LIMIT)
|
|
|
|
return SRE_ERROR_RECURSION_LIMIT;
|
|
|
|
#endif
|
|
|
|
|
2000-06-29 20:33:12 -03:00
|
|
|
if (pattern[0] == SRE_OP_INFO) {
|
|
|
|
/* optimization info block */
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <INFO> <1=skip> <2=flags> <3=min> ... */
|
2000-06-29 20:33:12 -03:00
|
|
|
if (pattern[3] && (end - ptr) < pattern[3]) {
|
|
|
|
TRACE(("reject (got %d chars, need %d)\n",
|
|
|
|
(end - ptr), pattern[3]));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
pattern += pattern[1] + 1;
|
|
|
|
}
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
for (;;) {
|
|
|
|
|
|
|
|
switch (*pattern++) {
|
|
|
|
|
|
|
|
case SRE_OP_FAILURE:
|
|
|
|
/* immediate failure */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|FAILURE\n", pattern, ptr));
|
2000-08-01 15:20:07 -03:00
|
|
|
return 0;
|
2000-07-23 18:46:17 -03:00
|
|
|
|
|
|
|
case SRE_OP_SUCCESS:
|
|
|
|
/* end of pattern */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|SUCCESS\n", pattern, ptr));
|
2000-07-23 18:46:17 -03:00
|
|
|
state->ptr = ptr;
|
2000-08-01 15:20:07 -03:00
|
|
|
return 1;
|
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> */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|AT %d\n", pattern, ptr, *pattern));
|
2000-07-23 18:46:17 -03:00
|
|
|
if (!SRE_AT(state, ptr, *pattern))
|
2000-08-01 15:20:07 -03:00
|
|
|
return 0;
|
2000-07-23 18:46:17 -03:00
|
|
|
pattern++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_CATEGORY:
|
|
|
|
/* match at given category */
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <CATEGORY> <code> */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|CATEGORY %d\n", pattern, ptr, *pattern));
|
2000-07-23 18:46:17 -03:00
|
|
|
if (ptr >= end || !sre_category(pattern[0], ptr[0]))
|
2000-08-01 15:20:07 -03:00
|
|
|
return 0;
|
2000-07-23 18:46:17 -03:00
|
|
|
pattern++;
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_LITERAL:
|
|
|
|
/* match literal string */
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <LITERAL> <code> */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|LITERAL %d\n", pattern, ptr, *pattern));
|
2000-07-23 18:46:17 -03:00
|
|
|
if (ptr >= end || (SRE_CODE) ptr[0] != pattern[0])
|
2000-08-01 15:20:07 -03:00
|
|
|
return 0;
|
2000-07-23 18:46:17 -03:00
|
|
|
pattern++;
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_NOT_LITERAL:
|
|
|
|
/* match anything that is not literal character */
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <NOT_LITERAL> <code> */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|NOT_LITERAL %d\n", pattern, ptr, *pattern));
|
2000-07-23 18:46:17 -03:00
|
|
|
if (ptr >= end || (SRE_CODE) ptr[0] == pattern[0])
|
2000-08-01 15:20:07 -03:00
|
|
|
return 0;
|
2000-07-23 18:46:17 -03:00
|
|
|
pattern++;
|
2000-06-01 14:39:12 -03:00
|
|
|
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> */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|ANY\n", pattern, ptr));
|
2000-08-01 19:47:49 -03:00
|
|
|
if (ptr >= end || SRE_IS_LINEBREAK(ptr[0]))
|
|
|
|
return 0;
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_ANY_ALL:
|
|
|
|
/* match anything */
|
|
|
|
/* <ANY_ALL> */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|ANY_ALL\n", pattern, ptr));
|
2000-07-23 18:46:17 -03:00
|
|
|
if (ptr >= end)
|
2000-08-01 15:20:07 -03:00
|
|
|
return 0;
|
2000-07-23 18:46:17 -03:00
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_IN:
|
|
|
|
/* match set member (or non_member) */
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <IN> <skip> <set> */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|IN\n", pattern, ptr));
|
|
|
|
if (ptr >= end || !SRE_CHARSET(pattern + 1, *ptr))
|
2000-08-01 15:20:07 -03:00
|
|
|
return 0;
|
2000-07-23 18:46:17 -03:00
|
|
|
pattern += pattern[0];
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
case SRE_OP_GROUPREF:
|
2000-07-23 18:46:17 -03:00
|
|
|
/* match backreference */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|GROUPREF %d\n", pattern, ptr, pattern[0]));
|
2000-07-23 18:46:17 -03:00
|
|
|
i = pattern[0];
|
|
|
|
{
|
|
|
|
SRE_CHAR* p = (SRE_CHAR*) state->mark[i+i];
|
|
|
|
SRE_CHAR* e = (SRE_CHAR*) state->mark[i+i+1];
|
|
|
|
if (!p || !e || e < p)
|
2000-08-01 15:20:07 -03:00
|
|
|
return 0;
|
2000-07-23 18:46:17 -03:00
|
|
|
while (p < e) {
|
|
|
|
if (ptr >= end || *ptr != *p)
|
2000-08-01 15:20:07 -03:00
|
|
|
return 0;
|
2000-07-23 18:46:17 -03:00
|
|
|
p++; ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pattern++;
|
|
|
|
break;
|
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
case SRE_OP_GROUPREF_IGNORE:
|
2000-07-23 18:46:17 -03:00
|
|
|
/* match backreference */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|GROUPREF_IGNORE %d\n", pattern, ptr, pattern[0]));
|
2000-07-23 18:46:17 -03:00
|
|
|
i = pattern[0];
|
|
|
|
{
|
|
|
|
SRE_CHAR* p = (SRE_CHAR*) state->mark[i+i];
|
|
|
|
SRE_CHAR* e = (SRE_CHAR*) state->mark[i+i+1];
|
|
|
|
if (!p || !e || e < p)
|
2000-08-01 15:20:07 -03:00
|
|
|
return 0;
|
2000-07-23 18:46:17 -03:00
|
|
|
while (p < e) {
|
|
|
|
if (ptr >= end ||
|
2000-06-29 09:48:37 -03:00
|
|
|
state->lower(*ptr) != state->lower(*p))
|
2000-08-01 15:20:07 -03:00
|
|
|
return 0;
|
2000-07-23 18:46:17 -03:00
|
|
|
p++; ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pattern++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_LITERAL_IGNORE:
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|LITERAL_IGNORE %d\n", pattern, ptr, pattern[0]));
|
2000-07-23 18:46:17 -03:00
|
|
|
if (ptr >= end ||
|
2000-06-29 09:48:37 -03:00
|
|
|
state->lower(*ptr) != state->lower(*pattern))
|
2000-08-01 15:20:07 -03:00
|
|
|
return 0;
|
2000-07-23 18:46:17 -03:00
|
|
|
pattern++;
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_NOT_LITERAL_IGNORE:
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|NOT_LITERAL_IGNORE %d\n", pattern, ptr, *pattern));
|
2000-07-23 18:46:17 -03:00
|
|
|
if (ptr >= end ||
|
2000-06-29 09:48:37 -03:00
|
|
|
state->lower(*ptr) == state->lower(*pattern))
|
2000-08-01 15:20:07 -03:00
|
|
|
return 0;
|
2000-07-23 18:46:17 -03:00
|
|
|
pattern++;
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_IN_IGNORE:
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|IN_IGNORE\n", pattern, ptr));
|
2000-07-23 18:46:17 -03:00
|
|
|
if (ptr >= end
|
2000-08-07 17:59:04 -03:00
|
|
|
|| !SRE_CHARSET(pattern + 1, (SRE_CODE) state->lower(*ptr)))
|
2000-08-01 15:20:07 -03:00
|
|
|
return 0;
|
2000-07-23 18:46:17 -03:00
|
|
|
pattern += pattern[0];
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_MARK:
|
|
|
|
/* set mark */
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <MARK> <gid> */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|MARK %d\n", pattern, ptr, pattern[0]));
|
2000-08-01 15:20:07 -03:00
|
|
|
i = pattern[0];
|
|
|
|
if (i & 1)
|
|
|
|
state->lastindex = i/2 + 1;
|
|
|
|
if (i > state->lastmark)
|
|
|
|
state->lastmark = i;
|
|
|
|
state->mark[i] = ptr;
|
2000-07-23 18:46:17 -03:00
|
|
|
pattern++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_JUMP:
|
|
|
|
case SRE_OP_INFO:
|
|
|
|
/* jump forward */
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <JUMP> <offset> */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|JUMP %d\n", pattern, ptr, pattern[0]));
|
2000-07-23 18:46:17 -03:00
|
|
|
pattern += pattern[0];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_ASSERT:
|
|
|
|
/* assert subpattern */
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <ASSERT> <skip> <back> <pattern> */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|ASSERT %d\n", pattern, ptr, pattern[1]));
|
2000-07-23 18:46:17 -03:00
|
|
|
state->ptr = ptr - pattern[1];
|
2000-07-03 15:44:21 -03:00
|
|
|
if (state->ptr < state->beginning)
|
2000-08-01 15:20:07 -03:00
|
|
|
return 0;
|
2000-08-01 18:05:41 -03:00
|
|
|
i = SRE_MATCH(state, pattern + 2, level + 1);
|
2000-08-01 15:20:07 -03:00
|
|
|
if (i <= 0)
|
2000-06-29 05:58:44 -03:00
|
|
|
return i;
|
2000-07-23 18:46:17 -03:00
|
|
|
pattern += pattern[0];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_ASSERT_NOT:
|
|
|
|
/* assert not subpattern */
|
2000-08-01 15:20:07 -03:00
|
|
|
/* <ASSERT_NOT> <skip> <back> <pattern> */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|ASSERT_NOT %d\n", pattern, ptr, pattern[1]));
|
2000-07-23 18:46:17 -03:00
|
|
|
state->ptr = ptr - pattern[1];
|
2000-10-28 16:30:41 -03:00
|
|
|
if (state->ptr >= state->beginning) {
|
|
|
|
i = SRE_MATCH(state, pattern + 2, level + 1);
|
|
|
|
if (i < 0)
|
|
|
|
return i;
|
|
|
|
if (i)
|
|
|
|
return 0;
|
|
|
|
}
|
2000-07-23 18:46:17 -03:00
|
|
|
pattern += pattern[0];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_BRANCH:
|
2000-08-01 15:20:07 -03:00
|
|
|
/* alternation */
|
|
|
|
/* <BRANCH> <0=skip> code <JUMP> ... <NULL> */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|BRANCH\n", pattern, ptr));
|
2000-08-03 13:29:50 -03:00
|
|
|
lastmark = state->lastmark;
|
2000-08-07 17:59:04 -03:00
|
|
|
for (; pattern[0]; pattern += pattern[0]) {
|
|
|
|
if (pattern[1] == SRE_OP_LITERAL &&
|
|
|
|
(ptr >= end || (SRE_CODE) *ptr != pattern[2]))
|
|
|
|
continue;
|
|
|
|
if (pattern[1] == SRE_OP_IN &&
|
|
|
|
(ptr >= end || !SRE_CHARSET(pattern + 3, (SRE_CODE) *ptr)))
|
|
|
|
continue;
|
|
|
|
state->ptr = ptr;
|
|
|
|
i = SRE_MATCH(state, pattern + 1, level + 1);
|
|
|
|
if (i)
|
|
|
|
return i;
|
|
|
|
if (state->lastmark > lastmark) {
|
|
|
|
memset(
|
|
|
|
state->mark + lastmark + 1, 0,
|
|
|
|
(state->lastmark - lastmark) * sizeof(void*)
|
|
|
|
);
|
|
|
|
state->lastmark = lastmark;
|
2000-07-23 18:46:17 -03:00
|
|
|
}
|
2000-08-01 15:20:07 -03:00
|
|
|
}
|
|
|
|
return 0;
|
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 */
|
|
|
|
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|REPEAT_ONE %d %d\n", pattern, ptr,
|
2000-08-01 18:05:41 -03:00
|
|
|
pattern[1], pattern[2]));
|
|
|
|
|
2000-08-01 19:47:49 -03:00
|
|
|
if (ptr + pattern[1] > end)
|
|
|
|
return 0; /* cannot match */
|
|
|
|
|
2000-08-03 13:29:50 -03:00
|
|
|
state->ptr = ptr;
|
2000-08-01 18:05:41 -03:00
|
|
|
|
2000-08-03 13:29:50 -03:00
|
|
|
count = SRE_COUNT(state, pattern + 3, pattern[2], level + 1);
|
|
|
|
if (count < 0)
|
|
|
|
return count;
|
2000-08-01 18:05:41 -03:00
|
|
|
|
2000-08-03 13:29:50 -03:00
|
|
|
ptr += count;
|
2000-08-01 18:05:41 -03:00
|
|
|
|
|
|
|
/* when we arrive here, count contains the number of
|
|
|
|
matches, and ptr points to the tail of the target
|
|
|
|
string. check if the rest of the pattern matches,
|
|
|
|
and backtrack if not. */
|
|
|
|
|
|
|
|
if (count < (int) pattern[1])
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (pattern[pattern[0]] == SRE_OP_SUCCESS) {
|
|
|
|
/* tail is empty. we're finished */
|
|
|
|
state->ptr = ptr;
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
} else if (pattern[pattern[0]] == SRE_OP_LITERAL) {
|
|
|
|
/* tail starts with a literal. skip positions where
|
|
|
|
the rest of the pattern cannot possibly match */
|
2000-08-01 19:47:49 -03:00
|
|
|
chr = pattern[pattern[0]+1];
|
2000-08-01 18:05:41 -03:00
|
|
|
for (;;) {
|
|
|
|
while (count >= (int) pattern[1] &&
|
|
|
|
(ptr >= end || *ptr != chr)) {
|
|
|
|
ptr--;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
if (count < (int) pattern[1])
|
|
|
|
break;
|
|
|
|
state->ptr = ptr;
|
|
|
|
i = SRE_MATCH(state, pattern + pattern[0], level + 1);
|
2000-08-07 17:59:04 -03:00
|
|
|
if (i)
|
2000-08-27 17:59:47 -03:00
|
|
|
return i;
|
2000-08-01 18:05:41 -03:00
|
|
|
ptr--;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* general case */
|
2000-08-07 17:59:04 -03:00
|
|
|
lastmark = state->lastmark;
|
2000-08-01 18:05:41 -03:00
|
|
|
while (count >= (int) pattern[1]) {
|
|
|
|
state->ptr = ptr;
|
|
|
|
i = SRE_MATCH(state, pattern + pattern[0], level + 1);
|
2000-08-07 17:59:04 -03:00
|
|
|
if (i)
|
2000-08-27 17:59:47 -03:00
|
|
|
return i;
|
2000-08-01 18:05:41 -03:00
|
|
|
ptr--;
|
|
|
|
count--;
|
2000-08-07 17:59:04 -03:00
|
|
|
if (state->lastmark > lastmark) {
|
|
|
|
memset(
|
|
|
|
state->mark + lastmark + 1, 0,
|
|
|
|
(state->lastmark - lastmark) * sizeof(void*)
|
|
|
|
);
|
|
|
|
state->lastmark = lastmark;
|
|
|
|
}
|
2000-08-01 18:05:41 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
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 */
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|REPEAT %d %d\n", pattern, ptr,
|
2000-08-01 15:20:07 -03:00
|
|
|
pattern[1], pattern[2]));
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
rep.count = -1;
|
|
|
|
rep.pattern = pattern;
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
/* install new repeat context */
|
|
|
|
rep.prev = state->repeat;
|
|
|
|
state->repeat = &rep;
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2000-08-01 18:05:41 -03:00
|
|
|
state->ptr = ptr;
|
|
|
|
i = SRE_MATCH(state, pattern + pattern[0], level + 1);
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
state->repeat = rep.prev;
|
2000-07-23 18:46:17 -03:00
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
return i;
|
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
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
rp = state->repeat;
|
|
|
|
if (!rp)
|
|
|
|
return SRE_ERROR_STATE;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
state->ptr = ptr;
|
2000-06-29 05:58:44 -03:00
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
count = rp->count + 1;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|MAX_UNTIL %d\n", pattern, ptr, count));
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
if (count < rp->pattern[1]) {
|
|
|
|
/* not enough matches */
|
|
|
|
rp->count = count;
|
2000-08-03 13:29:50 -03:00
|
|
|
/* RECURSIVE */
|
2000-08-01 18:05:41 -03:00
|
|
|
i = SRE_MATCH(state, rp->pattern + 3, level + 1);
|
2000-08-01 15:20:07 -03:00
|
|
|
if (i)
|
2000-06-29 05:58:44 -03:00
|
|
|
return i;
|
2000-08-01 15:20:07 -03:00
|
|
|
rp->count = count - 1;
|
|
|
|
state->ptr = ptr;
|
|
|
|
return 0;
|
2000-06-29 05:58:44 -03:00
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
if (count < rp->pattern[2] || rp->pattern[2] == 65535) {
|
|
|
|
/* we may have enough matches, but if we can
|
|
|
|
match another item, do so */
|
|
|
|
rp->count = count;
|
|
|
|
lastmark = state->lastmark;
|
2000-08-27 17:59:47 -03:00
|
|
|
i = mark_save(state, 0, lastmark);
|
|
|
|
if (i < 0)
|
|
|
|
return i;
|
2000-08-03 13:29:50 -03:00
|
|
|
/* RECURSIVE */
|
2000-08-01 18:05:41 -03:00
|
|
|
i = SRE_MATCH(state, rp->pattern + 3, level + 1);
|
2000-08-01 15:20:07 -03:00
|
|
|
if (i)
|
2000-07-05 18:14:16 -03:00
|
|
|
return i;
|
2000-08-27 17:59:47 -03:00
|
|
|
i = mark_restore(state, 0, lastmark);
|
2001-10-18 16:30:16 -03:00
|
|
|
state->lastmark = lastmark;
|
2000-08-27 17:59:47 -03:00
|
|
|
if (i < 0)
|
|
|
|
return i;
|
2000-08-01 15:20:07 -03:00
|
|
|
rp->count = count - 1;
|
|
|
|
state->ptr = 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 */
|
|
|
|
state->repeat = rp->prev;
|
2000-08-01 18:05:41 -03:00
|
|
|
i = SRE_MATCH(state, pattern, level + 1);
|
|
|
|
if (i)
|
2000-08-01 15:20:07 -03:00
|
|
|
return i;
|
|
|
|
state->repeat = rp;
|
2001-01-14 11:06:11 -04:00
|
|
|
state->ptr = ptr;
|
2000-08-01 15:20:07 -03:00
|
|
|
return 0;
|
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
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
rp = state->repeat;
|
|
|
|
if (!rp)
|
|
|
|
return SRE_ERROR_STATE;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
count = rp->count + 1;
|
|
|
|
|
2001-01-14 11:06:11 -04:00
|
|
|
TRACE(("|%p|%p|MIN_UNTIL %d %p\n", pattern, ptr, count,
|
|
|
|
rp->pattern));
|
2000-07-03 18:31:48 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
state->ptr = ptr;
|
2000-08-01 15:20:07 -03:00
|
|
|
|
|
|
|
if (count < rp->pattern[1]) {
|
|
|
|
/* not enough matches */
|
|
|
|
rp->count = count;
|
2000-08-03 13:29:50 -03:00
|
|
|
/* RECURSIVE */
|
2000-08-01 18:05:41 -03:00
|
|
|
i = SRE_MATCH(state, rp->pattern + 3, level + 1);
|
2000-08-01 15:20:07 -03:00
|
|
|
if (i)
|
2000-06-29 05:58:44 -03:00
|
|
|
return i;
|
2000-08-01 15:20:07 -03:00
|
|
|
rp->count = count-1;
|
|
|
|
state->ptr = ptr;
|
|
|
|
return 0;
|
2000-07-23 18:46:17 -03:00
|
|
|
}
|
|
|
|
|
2000-08-01 15:20:07 -03:00
|
|
|
/* see if the tail matches */
|
|
|
|
state->repeat = rp->prev;
|
2001-12-09 12:13:15 -04:00
|
|
|
i = SRE_MATCH(state, pattern, level + 1);
|
|
|
|
if (i)
|
2000-08-01 15:20:07 -03:00
|
|
|
return i;
|
2001-01-14 19:55:55 -04:00
|
|
|
|
2001-01-14 11:06:11 -04:00
|
|
|
state->ptr = ptr;
|
2000-08-01 15:20:07 -03:00
|
|
|
state->repeat = rp;
|
|
|
|
|
|
|
|
if (count >= rp->pattern[2] && rp->pattern[2] != 65535)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rp->count = count;
|
2000-08-03 13:29:50 -03:00
|
|
|
/* RECURSIVE */
|
2000-08-01 18:05:41 -03:00
|
|
|
i = SRE_MATCH(state, rp->pattern + 3, level + 1);
|
2000-08-01 15:20:07 -03:00
|
|
|
if (i)
|
|
|
|
return i;
|
|
|
|
rp->count = count - 1;
|
2001-01-14 11:06:11 -04:00
|
|
|
state->ptr = ptr;
|
2000-08-01 15:20:07 -03:00
|
|
|
return 0;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
default:
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|UNKNOWN %d\n", pattern, ptr, pattern[-1]));
|
2000-07-23 18:46:17 -03:00
|
|
|
return SRE_ERROR_ILLEGAL;
|
|
|
|
}
|
|
|
|
}
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2001-08-30 11:37:07 -03:00
|
|
|
/* can't end up here */
|
2001-09-18 15:47:09 -03:00
|
|
|
/* return SRE_ERROR_ILLEGAL; -- see python-dev discussion */
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
2000-08-03 13:29:50 -03:00
|
|
|
LOCAL(int)
|
2000-03-31 10:59:30 -04:00
|
|
|
SRE_SEARCH(SRE_STATE* state, SRE_CODE* pattern)
|
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
SRE_CHAR* ptr = state->start;
|
|
|
|
SRE_CHAR* end = state->end;
|
|
|
|
int status = 0;
|
2000-07-05 18:14:16 -03:00
|
|
|
int prefix_len = 0;
|
2000-09-21 14:03:25 -03:00
|
|
|
int 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
|
|
|
|
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
|
|
|
|
|
|
|
if (pattern[3] > 0) {
|
|
|
|
/* 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
|
|
|
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("prefix = %p %d %d\n", prefix, prefix_len, prefix_skip));
|
|
|
|
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 */
|
|
|
|
int i = 0;
|
|
|
|
end = state->end;
|
|
|
|
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 */
|
2000-08-07 17:59:04 -03:00
|
|
|
status = SRE_MATCH(state, pattern + 2*prefix_skip, 1);
|
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];
|
2000-08-07 17:59:04 -03:00
|
|
|
end = state->end;
|
2000-07-23 18:46:17 -03:00
|
|
|
for (;;) {
|
|
|
|
while (ptr < end && (SRE_CODE) ptr[0] != chr)
|
|
|
|
ptr++;
|
|
|
|
if (ptr == end)
|
|
|
|
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 */
|
2000-08-01 18:05:41 -03:00
|
|
|
status = SRE_MATCH(state, pattern + 2, 1);
|
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 */
|
2000-08-07 17:59:04 -03:00
|
|
|
end = 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++;
|
|
|
|
if (ptr == end)
|
|
|
|
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;
|
2000-08-01 18:05:41 -03:00
|
|
|
status = SRE_MATCH(state, pattern, 1);
|
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
|
|
|
}
|
2000-07-23 18:46:17 -03:00
|
|
|
} else
|
|
|
|
/* general case */
|
|
|
|
while (ptr <= end) {
|
2000-08-07 17:59:04 -03:00
|
|
|
TRACE(("|%p|%p|SEARCH\n", pattern, ptr));
|
2000-07-23 18:46:17 -03:00
|
|
|
state->start = state->ptr = ptr++;
|
2000-08-01 18:05:41 -03:00
|
|
|
status = SRE_MATCH(state, pattern, 1);
|
2000-07-23 18:46:17 -03:00
|
|
|
if (status != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
2000-07-02 09:00:07 -03:00
|
|
|
|
2001-10-22 18:18:08 -03:00
|
|
|
LOCAL(int)
|
|
|
|
SRE_LITERAL_TEMPLATE(SRE_CHAR* ptr, int len)
|
|
|
|
{
|
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
staticforward PyTypeObject Pattern_Type;
|
|
|
|
staticforward PyTypeObject Match_Type;
|
2000-06-29 13:57:40 -03:00
|
|
|
staticforward PyTypeObject Scanner_Type;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_compile(PyObject* self_, PyObject* args)
|
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
/* "compile" pattern descriptor to pattern object */
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PatternObject* self;
|
2000-07-03 15:44:21 -03:00
|
|
|
int i, n;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject* pattern;
|
2000-06-01 14:39:12 -03:00
|
|
|
int flags = 0;
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject* code;
|
|
|
|
int groups = 0;
|
|
|
|
PyObject* groupindex = NULL;
|
2000-07-02 19:25:39 -03:00
|
|
|
PyObject* indexgroup = NULL;
|
2001-01-16 03:37:30 -04:00
|
|
|
if (!PyArg_ParseTuple(args, "OiO!|iOO", &pattern, &flags,
|
|
|
|
&PyList_Type, &code, &groups,
|
|
|
|
&groupindex, &indexgroup))
|
2000-07-23 18:46:17 -03:00
|
|
|
return NULL;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2001-01-16 03:37:30 -04:00
|
|
|
n = PyList_GET_SIZE(code);
|
2000-07-03 15:44:21 -03:00
|
|
|
|
2000-10-28 16:30:41 -03:00
|
|
|
self = PyObject_NEW_VAR(PatternObject, &Pattern_Type, n);
|
2001-01-16 03:37:30 -04:00
|
|
|
if (!self)
|
2000-07-23 18:46:17 -03:00
|
|
|
return NULL;
|
2000-07-03 15:44:21 -03:00
|
|
|
|
2001-07-02 13:42:49 -03:00
|
|
|
self->codesize = n;
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
for (i = 0; i < n; i++) {
|
2001-01-16 03:37:30 -04:00
|
|
|
PyObject *o = PyList_GET_ITEM(code, i);
|
2000-07-03 15:44:21 -03:00
|
|
|
self->code[i] = (SRE_CODE) PyInt_AsLong(o);
|
2000-07-23 18:46:17 -03:00
|
|
|
}
|
2000-07-03 15:44:21 -03:00
|
|
|
|
2001-01-16 03:05:29 -04:00
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
PyObject_DEL(self);
|
2000-07-03 15:44:21 -03:00
|
|
|
return NULL;
|
2001-01-16 03:05:29 -04:00
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
Py_INCREF(pattern);
|
|
|
|
self->pattern = pattern;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
self->flags = flags;
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
self->groups = groups;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
Py_XINCREF(groupindex);
|
|
|
|
self->groupindex = groupindex;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
Py_XINCREF(indexgroup);
|
|
|
|
self->indexgroup = indexgroup;
|
2000-07-02 19:25:39 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
return (PyObject*) self;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2000-06-29 05:58:44 -03:00
|
|
|
sre_codesize(PyObject* self, PyObject* args)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
return Py_BuildValue("i", 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)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
state->lastmark = 0;
|
|
|
|
|
|
|
|
/* FIXME: dynamic! */
|
|
|
|
for (i = 0; i < SRE_MARK_SIZE; i++)
|
|
|
|
state->mark[i] = NULL;
|
|
|
|
|
|
|
|
state->lastindex = -1;
|
|
|
|
|
|
|
|
state->repeat = NULL;
|
|
|
|
|
|
|
|
mark_fini(state);
|
|
|
|
}
|
|
|
|
|
2001-10-22 18:18:08 -03:00
|
|
|
static void*
|
|
|
|
getstring(PyObject* string, int* 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) */
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyBufferProcs *buffer;
|
2001-10-22 18:18:08 -03:00
|
|
|
int size, bytes, 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);
|
|
|
|
bytes = PyUnicode_GET_DATA_SIZE(string);
|
|
|
|
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 */
|
|
|
|
buffer = string->ob_type->tp_as_buffer;
|
|
|
|
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
|
|
|
|
2000-08-01 15:20:07 -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)
|
|
|
|
else if (bytes == (int) (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,
|
|
|
|
int start, int end)
|
|
|
|
{
|
|
|
|
/* prepare state object */
|
|
|
|
|
|
|
|
int length;
|
|
|
|
int charsize;
|
|
|
|
void* ptr;
|
|
|
|
|
|
|
|
memset(state, 0, sizeof(SRE_STATE));
|
|
|
|
|
|
|
|
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);
|
2000-08-01 15:20:07 -03:00
|
|
|
mark_fini(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*)
|
2001-10-20 14:48:46 -03:00
|
|
|
state_getslice(SRE_STATE* state, int index, PyObject* string, int empty)
|
2000-06-29 08:34:28 -03:00
|
|
|
{
|
2000-08-09 06:14:35 -03:00
|
|
|
int i, j;
|
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
index = (index - 1) * 2;
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
if (string == Py_None || !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;
|
|
|
|
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 PyObject*
|
2000-07-23 18:46:17 -03:00
|
|
|
pattern_new_match(PatternObject* pattern, SRE_STATE* state, int status)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
/* create match object (from state object) */
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
MatchObject* match;
|
|
|
|
int i, j;
|
2000-06-29 05:58:44 -03:00
|
|
|
char* base;
|
|
|
|
int n;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
if (status > 0) {
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
/* create match object (with room for extra group marks) */
|
|
|
|
match = PyObject_NEW_VAR(MatchObject, &Match_Type,
|
2000-07-03 15:44:21 -03:00
|
|
|
2*(pattern->groups+1));
|
2000-07-23 18:46:17 -03:00
|
|
|
if (!match)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Py_INCREF(pattern);
|
|
|
|
match->pattern = pattern;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
Py_INCREF(state->string);
|
|
|
|
match->string = state->string;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
match->regs = NULL;
|
|
|
|
match->groups = pattern->groups+1;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
/* fill in group slices */
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
base = (char*) state->beginning;
|
|
|
|
n = state->charsize;
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
match->mark[0] = ((char*) state->start - base) / n;
|
|
|
|
match->mark[1] = ((char*) state->ptr - base) / n;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
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 */
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
match->pos = state->pos;
|
|
|
|
match->endpos = state->endpos;
|
2000-07-03 15:44:21 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
match->lastindex = state->lastindex;
|
2000-07-02 14:33:27 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
return (PyObject*) match;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-08-03 13:29:50 -03:00
|
|
|
} else if (status == 0) {
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-08-03 13:29:50 -03:00
|
|
|
/* no match */
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-08-03 13:29:50 -03:00
|
|
|
/* internal error */
|
|
|
|
pattern_error(status);
|
|
|
|
return NULL;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
static PyObject*
|
2000-06-29 13:57:40 -03:00
|
|
|
pattern_scanner(PatternObject* pattern, PyObject* args)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
/* create search state object */
|
|
|
|
|
|
|
|
ScannerObject* self;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
PyObject* string;
|
2000-07-23 18:46:17 -03:00
|
|
|
int start = 0;
|
|
|
|
int end = INT_MAX;
|
|
|
|
if (!PyArg_ParseTuple(args, "O|ii:scanner", &string, &start, &end))
|
|
|
|
return NULL;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
/* create scanner object */
|
2000-06-29 13:57:40 -03:00
|
|
|
self = PyObject_NEW(ScannerObject, &Scanner_Type);
|
2000-07-03 15:44:21 -03:00
|
|
|
if (!self)
|
2000-06-01 14:39:12 -03:00
|
|
|
return NULL;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
string = state_init(&self->state, pattern, string, start, end);
|
2000-06-01 14:39:12 -03:00
|
|
|
if (!string) {
|
2001-01-16 03:37:30 -04:00
|
|
|
PyObject_DEL(self);
|
2000-06-01 14:39:12 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
Py_INCREF(pattern);
|
2000-06-29 05:58:44 -03:00
|
|
|
self->pattern = (PyObject*) pattern;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
return (PyObject*) self;
|
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
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
PyObject* string;
|
|
|
|
int start = 0;
|
|
|
|
int end = INT_MAX;
|
2000-10-03 17:43:34 -03:00
|
|
|
static char* kwlist[] = { "pattern", "pos", "endpos", NULL };
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kw, "O|ii:match", kwlist,
|
|
|
|
&string, &start, &end))
|
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) {
|
2000-08-01 18:05:41 -03:00
|
|
|
status = sre_match(&state, PatternObject_GetCode(self), 1);
|
2000-07-23 18:46:17 -03:00
|
|
|
} else {
|
2000-06-29 05:58:44 -03:00
|
|
|
#if defined(HAVE_UNICODE)
|
2000-08-01 18:05:41 -03:00
|
|
|
status = sre_umatch(&state, PatternObject_GetCode(self), 1);
|
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
|
|
|
|
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
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject* string;
|
|
|
|
int start = 0;
|
|
|
|
int end = INT_MAX;
|
2000-10-03 17:43:34 -03:00
|
|
|
static char* kwlist[] = { "pattern", "pos", "endpos", NULL };
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kw, "O|ii:search", kwlist,
|
|
|
|
&string, &start, &end))
|
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
|
|
|
|
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;
|
2001-07-03 17:32:36 -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",
|
|
|
|
Py_BuildValue("OO", *object, memo)
|
|
|
|
);
|
|
|
|
if (!copy)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Py_DECREF(*object);
|
|
|
|
*object = copy;
|
|
|
|
|
|
|
|
return 1; /* success */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
static PyObject*
|
|
|
|
join(PyObject* list, PyObject* pattern)
|
|
|
|
{
|
|
|
|
/* join list elements */
|
|
|
|
|
|
|
|
PyObject* joiner;
|
|
|
|
#if PY_VERSION_HEX >= 0x01060000
|
|
|
|
PyObject* function;
|
|
|
|
PyObject* args;
|
|
|
|
#endif
|
|
|
|
PyObject* result;
|
|
|
|
|
|
|
|
switch (PyList_GET_SIZE(list)) {
|
|
|
|
case 0:
|
|
|
|
Py_DECREF(list);
|
|
|
|
return PyString_FromString("");
|
|
|
|
case 1:
|
|
|
|
result = PyList_GET_ITEM(list, 0);
|
|
|
|
Py_INCREF(result);
|
|
|
|
Py_DECREF(list);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* two or more elements: slice out a suitable separator from the
|
|
|
|
first member, and use that to join the entire list */
|
|
|
|
|
|
|
|
joiner = PySequence_GetSlice(pattern, 0, 0);
|
|
|
|
if (!joiner)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
#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",
|
|
|
|
Py_BuildValue("OO", list, joiner)
|
|
|
|
);
|
|
|
|
#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;
|
2001-10-20 14:48:46 -03:00
|
|
|
int i, b, e;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject* string;
|
|
|
|
int start = 0;
|
|
|
|
int end = INT_MAX;
|
2000-10-03 17:43:34 -03:00
|
|
|
static char* kwlist[] = { "source", "pos", "endpos", NULL };
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kw, "O|ii:findall", kwlist,
|
|
|
|
&string, &start, &end))
|
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;
|
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
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;
|
|
|
|
int n;
|
2001-10-21 13:47:57 -03:00
|
|
|
int i;
|
|
|
|
void* last;
|
2001-10-20 14:48:46 -03:00
|
|
|
|
|
|
|
PyObject* string;
|
|
|
|
int maxsplit = 0;
|
|
|
|
static char* kwlist[] = { "source", "maxsplit", NULL };
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kw, "O|i:split", kwlist,
|
|
|
|
&string, &maxsplit))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
string = state_init(&state, self, string, 0, INT_MAX);
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
if (status <= 0) {
|
|
|
|
if (status == 0)
|
|
|
|
break;
|
|
|
|
pattern_error(status);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.start == state.ptr) {
|
|
|
|
if (last == state.end)
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
pattern_subx(PatternObject* self, PyObject* template, PyObject* string,
|
|
|
|
int count, int subn)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
int n;
|
|
|
|
int i, b, e;
|
|
|
|
int filter_is_callable;
|
|
|
|
|
2001-10-21 18:48:30 -03:00
|
|
|
if (PyCallable_Check(template)) {
|
|
|
|
/* sub/subn takes either a function or a template */
|
|
|
|
filter = template;
|
|
|
|
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;
|
|
|
|
ptr = getstring(template, &n, &b);
|
|
|
|
if (ptr) {
|
|
|
|
if (b == 1) {
|
|
|
|
literal = sre_literal_template(ptr, n);
|
|
|
|
} else {
|
|
|
|
#if defined(HAVE_UNICODE)
|
|
|
|
literal = sre_uliteral_template(ptr, n);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PyErr_Clear();
|
|
|
|
literal = 0;
|
|
|
|
}
|
|
|
|
if (literal) {
|
|
|
|
filter = template;
|
|
|
|
Py_INCREF(filter);
|
|
|
|
filter_is_callable = 0;
|
|
|
|
} else {
|
|
|
|
/* not a literal; hand it over to the template compiler */
|
|
|
|
filter = call(
|
|
|
|
SRE_MODULE, "_subx",
|
|
|
|
Py_BuildValue("OO", self, template)
|
|
|
|
);
|
|
|
|
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
|
|
|
|
|
|
|
string = state_init(&state, self, string, 0, INT_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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
args = Py_BuildValue("(O)", match);
|
|
|
|
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;
|
|
|
|
}
|
2001-10-21 13:47:57 -03:00
|
|
|
|
|
|
|
i = e;
|
|
|
|
n = n + 1;
|
|
|
|
|
|
|
|
next:
|
|
|
|
/* move on */
|
|
|
|
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) */
|
2001-10-21 13:47:57 -03:00
|
|
|
item = join(list, self->pattern);
|
2001-10-21 18:48:30 -03:00
|
|
|
|
2001-10-21 13:47:57 -03:00
|
|
|
if (!item)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (subn)
|
|
|
|
return Py_BuildValue("Ni", item, n);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
2001-10-21 13:47:57 -03:00
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
pattern_sub(PatternObject* self, PyObject* args, PyObject* kw)
|
|
|
|
{
|
|
|
|
PyObject* template;
|
|
|
|
PyObject* string;
|
|
|
|
int count = 0;
|
|
|
|
static char* kwlist[] = { "repl", "string", "count", NULL };
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|i:sub", kwlist,
|
|
|
|
&template, &string, &count))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return pattern_subx(self, template, string, count, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
pattern_subn(PatternObject* self, PyObject* args, PyObject* kw)
|
|
|
|
{
|
|
|
|
PyObject* template;
|
|
|
|
PyObject* string;
|
|
|
|
int count = 0;
|
|
|
|
static char* kwlist[] = { "repl", "string", "count", NULL };
|
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|i:subn", kwlist,
|
|
|
|
&template, &string, &count))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return pattern_subx(self, template, string, count, 1);
|
|
|
|
}
|
2001-10-20 14:48:46 -03:00
|
|
|
|
2001-07-02 13:42:49 -03:00
|
|
|
static PyObject*
|
|
|
|
pattern_copy(PatternObject* self, PyObject* args)
|
|
|
|
{
|
2001-07-03 17:32:36 -03:00
|
|
|
#ifdef USE_BUILTIN_COPY
|
2001-07-02 13:42:49 -03:00
|
|
|
PatternObject* copy;
|
|
|
|
int offset;
|
|
|
|
|
2001-07-03 17:32:36 -03:00
|
|
|
if (args != Py_None && !PyArg_ParseTuple(args, ":__copy__"))
|
|
|
|
return NULL;
|
2001-07-02 13:42:49 -03:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
return (PyObject*) copy;
|
|
|
|
#else
|
|
|
|
PyErr_SetString(PyExc_TypeError, "cannot copy this pattern object");
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
|
|
|
pattern_deepcopy(PatternObject* self, PyObject* args)
|
|
|
|
{
|
2001-07-03 17:32:36 -03:00
|
|
|
#ifdef USE_BUILTIN_COPY
|
|
|
|
PatternObject* copy;
|
|
|
|
|
|
|
|
PyObject* memo;
|
|
|
|
if (!PyArg_ParseTuple(args, "O:__deepcopy__", &memo))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
copy = (PatternObject*) pattern_copy(self, Py_None);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
static PyMethodDef pattern_methods[] = {
|
2000-10-03 17:43:34 -03:00
|
|
|
{"match", (PyCFunction) pattern_match, METH_VARARGS|METH_KEYWORDS},
|
|
|
|
{"search", (PyCFunction) pattern_search, METH_VARARGS|METH_KEYWORDS},
|
|
|
|
{"sub", (PyCFunction) pattern_sub, METH_VARARGS|METH_KEYWORDS},
|
|
|
|
{"subn", (PyCFunction) pattern_subn, METH_VARARGS|METH_KEYWORDS},
|
|
|
|
{"split", (PyCFunction) pattern_split, METH_VARARGS|METH_KEYWORDS},
|
|
|
|
{"findall", (PyCFunction) pattern_findall, METH_VARARGS|METH_KEYWORDS},
|
2001-10-24 19:16:30 -03:00
|
|
|
#if PY_VERSION_HEX >= 0x02020000
|
|
|
|
{"finditer", (PyCFunction) pattern_finditer, METH_VARARGS},
|
|
|
|
#endif
|
2000-10-03 17:43:34 -03:00
|
|
|
{"scanner", (PyCFunction) pattern_scanner, METH_VARARGS},
|
2001-07-02 13:42:49 -03:00
|
|
|
{"__copy__", (PyCFunction) pattern_copy, METH_VARARGS},
|
|
|
|
{"__deepcopy__", (PyCFunction) pattern_deepcopy, METH_VARARGS},
|
2000-07-23 18:46:17 -03:00
|
|
|
{NULL, NULL}
|
2000-03-31 10:59:30 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-29 08:34:28 -03:00
|
|
|
pattern_getattr(PatternObject* self, char* name)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
|
|
|
PyObject* res;
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
res = Py_FindMethod(pattern_methods, (PyObject*) self, name);
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
if (res)
|
|
|
|
return res;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyErr_Clear();
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
/* attributes */
|
2000-07-23 18:46:17 -03:00
|
|
|
if (!strcmp(name, "pattern")) {
|
2000-03-31 10:59:30 -04:00
|
|
|
Py_INCREF(self->pattern);
|
2000-07-23 18:46:17 -03:00
|
|
|
return self->pattern;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
2000-06-01 14:39:12 -03:00
|
|
|
|
|
|
|
if (!strcmp(name, "flags"))
|
2000-07-23 18:46:17 -03:00
|
|
|
return Py_BuildValue("i", self->flags);
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-06-29 21:27:46 -03:00
|
|
|
if (!strcmp(name, "groups"))
|
2000-07-23 18:46:17 -03:00
|
|
|
return Py_BuildValue("i", self->groups);
|
2000-06-29 21:27:46 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
if (!strcmp(name, "groupindex") && self->groupindex) {
|
2000-06-01 14:39:12 -03:00
|
|
|
Py_INCREF(self->groupindex);
|
2000-07-23 18:46:17 -03:00
|
|
|
return self->groupindex;
|
2000-06-01 14:39:12 -03:00
|
|
|
}
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
|
|
return NULL;
|
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*/
|
|
|
|
0, /*tp_print*/
|
|
|
|
(getattrfunc)pattern_getattr /*tp_getattr*/
|
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*
|
2000-06-30 04:08:20 -03:00
|
|
|
match_getslice_by_index(MatchObject* self, int 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
|
|
|
}
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
static int
|
2000-06-29 08:34:28 -03:00
|
|
|
match_getindex(MatchObject* self, PyObject* index)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2000-07-03 15:44:21 -03:00
|
|
|
int i;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
if (PyInt_Check(index))
|
2000-06-29 05:58:44 -03:00
|
|
|
return (int) PyInt_AS_LONG(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) {
|
2000-07-03 15:44:21 -03:00
|
|
|
if (PyInt_Check(index))
|
|
|
|
i = (int) PyInt_AS_LONG(index);
|
|
|
|
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*
|
|
|
|
match_expand(MatchObject* self, PyObject* args)
|
|
|
|
{
|
|
|
|
PyObject* template;
|
|
|
|
if (!PyArg_ParseTuple(args, "O:expand", &template))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* delegate to Python code */
|
|
|
|
return call(
|
2001-07-03 17:32:36 -03:00
|
|
|
SRE_MODULE, "_expand",
|
2000-09-21 14:03:25 -03:00
|
|
|
Py_BuildValue("OOO", self->pattern, self, template)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
int i, size;
|
|
|
|
|
|
|
|
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;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
PyObject* def = Py_None;
|
2000-10-03 17:43:34 -03:00
|
|
|
static char* kwlist[] = { "default", NULL };
|
|
|
|
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;
|
|
|
|
int index;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject* def = Py_None;
|
2000-10-03 17:43:34 -03: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:
|
|
|
|
Py_DECREF(keys);
|
|
|
|
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
|
|
|
{
|
2000-06-29 05:58:44 -03:00
|
|
|
int index;
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject* index_ = Py_False; /* zero */
|
|
|
|
if (!PyArg_ParseTuple(args, "|O:start", &index_))
|
|
|
|
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 */
|
2000-07-23 18:46:17 -03:00
|
|
|
return Py_BuildValue("i", 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
|
|
|
{
|
2000-06-29 05:58:44 -03:00
|
|
|
int index;
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject* index_ = Py_False; /* zero */
|
|
|
|
if (!PyArg_ParseTuple(args, "|O:end", &index_))
|
|
|
|
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 */
|
2000-07-23 18:46:17 -03:00
|
|
|
return Py_BuildValue("i", self->mark[index*2+1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCAL(PyObject*)
|
|
|
|
_pair(int i1, int i2)
|
|
|
|
{
|
|
|
|
PyObject* pair;
|
|
|
|
PyObject* item;
|
|
|
|
|
|
|
|
pair = PyTuple_New(2);
|
|
|
|
if (!pair)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
item = PyInt_FromLong(i1);
|
|
|
|
if (!item)
|
|
|
|
goto error;
|
|
|
|
PyTuple_SET_ITEM(pair, 0, item);
|
|
|
|
|
|
|
|
item = PyInt_FromLong(i2);
|
|
|
|
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
|
|
|
{
|
2000-06-29 05:58:44 -03:00
|
|
|
int index;
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject* index_ = Py_False; /* zero */
|
|
|
|
if (!PyArg_ParseTuple(args, "|O:span", &index_))
|
|
|
|
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;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
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*
|
|
|
|
match_copy(MatchObject* self, PyObject* args)
|
|
|
|
{
|
2001-07-03 17:32:36 -03:00
|
|
|
#ifdef USE_BUILTIN_COPY
|
2001-07-02 13:42:49 -03:00
|
|
|
MatchObject* copy;
|
|
|
|
int slots, offset;
|
|
|
|
|
2001-07-03 17:32:36 -03:00
|
|
|
if (args != Py_None && !PyArg_ParseTuple(args, ":__copy__"))
|
|
|
|
return NULL;
|
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,
|
|
|
|
sizeof(MatchObject) + slots * sizeof(int) - offset);
|
|
|
|
|
|
|
|
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*
|
|
|
|
match_deepcopy(MatchObject* self, PyObject* args)
|
|
|
|
{
|
2001-07-03 17:32:36 -03:00
|
|
|
#ifdef USE_BUILTIN_COPY
|
|
|
|
MatchObject* copy;
|
|
|
|
|
|
|
|
PyObject* memo;
|
|
|
|
if (!PyArg_ParseTuple(args, "O:__deepcopy__", &memo))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
copy = (MatchObject*) match_copy(self, Py_None);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
static PyMethodDef match_methods[] = {
|
2000-10-03 17:43:34 -03:00
|
|
|
{"group", (PyCFunction) match_group, METH_VARARGS},
|
|
|
|
{"start", (PyCFunction) match_start, METH_VARARGS},
|
|
|
|
{"end", (PyCFunction) match_end, METH_VARARGS},
|
|
|
|
{"span", (PyCFunction) match_span, METH_VARARGS},
|
|
|
|
{"groups", (PyCFunction) match_groups, METH_VARARGS|METH_KEYWORDS},
|
|
|
|
{"groupdict", (PyCFunction) match_groupdict, METH_VARARGS|METH_KEYWORDS},
|
|
|
|
{"expand", (PyCFunction) match_expand, METH_VARARGS},
|
2001-07-02 13:42:49 -03:00
|
|
|
{"__copy__", (PyCFunction) match_copy, METH_VARARGS},
|
|
|
|
{"__deepcopy__", (PyCFunction) match_deepcopy, METH_VARARGS},
|
2000-07-23 18:46:17 -03:00
|
|
|
{NULL, NULL}
|
2000-03-31 10:59:30 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-29 08:34:28 -03:00
|
|
|
match_getattr(MatchObject* self, char* name)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject* res;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
res = Py_FindMethod(match_methods, (PyObject*) self, name);
|
|
|
|
if (res)
|
|
|
|
return res;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyErr_Clear();
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
if (!strcmp(name, "lastindex")) {
|
2000-07-03 15:44:21 -03:00
|
|
|
if (self->lastindex >= 0)
|
|
|
|
return Py_BuildValue("i", self->lastindex);
|
2000-07-02 19:25:39 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(name, "lastgroup")) {
|
2000-07-03 15:44:21 -03:00
|
|
|
if (self->pattern->indexgroup && self->lastindex >= 0) {
|
2000-07-02 19:25:39 -03:00
|
|
|
PyObject* result = PySequence_GetItem(
|
2000-07-03 15:44:21 -03:00
|
|
|
self->pattern->indexgroup, self->lastindex
|
2000-07-02 19:25:39 -03:00
|
|
|
);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
if (!strcmp(name, "string")) {
|
|
|
|
if (self->string) {
|
|
|
|
Py_INCREF(self->string);
|
|
|
|
return self->string;
|
|
|
|
} else {
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(name, "regs")) {
|
|
|
|
if (self->regs) {
|
|
|
|
Py_INCREF(self->regs);
|
|
|
|
return self->regs;
|
|
|
|
} else
|
|
|
|
return match_regs(self);
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
if (!strcmp(name, "re")) {
|
2000-03-31 10:59:30 -04:00
|
|
|
Py_INCREF(self->pattern);
|
2000-07-23 18:46:17 -03:00
|
|
|
return (PyObject*) self->pattern;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
if (!strcmp(name, "pos"))
|
|
|
|
return Py_BuildValue("i", self->pos);
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
if (!strcmp(name, "endpos"))
|
|
|
|
return Py_BuildValue("i", self->endpos);
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
|
|
return 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 */
|
|
|
|
|
|
|
|
statichere PyTypeObject Match_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_Match",
|
2000-07-23 18:46:17 -03:00
|
|
|
sizeof(MatchObject), sizeof(int),
|
|
|
|
(destructor)match_dealloc, /*tp_dealloc*/
|
|
|
|
0, /*tp_print*/
|
|
|
|
(getattrfunc)match_getattr /*tp_getattr*/
|
2000-03-31 10:59:30 -04:00
|
|
|
};
|
|
|
|
|
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);
|
2000-06-01 14:39:12 -03:00
|
|
|
Py_DECREF(self->pattern);
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject_DEL(self);
|
2000-06-01 14:39:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-29 13:57:40 -03:00
|
|
|
scanner_match(ScannerObject* self, PyObject* args)
|
2000-06-01 14:39:12 -03:00
|
|
|
{
|
|
|
|
SRE_STATE* state = &self->state;
|
|
|
|
PyObject* match;
|
|
|
|
int status;
|
|
|
|
|
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) {
|
2000-08-01 18:05:41 -03:00
|
|
|
status = sre_match(state, PatternObject_GetCode(self->pattern), 1);
|
2000-06-01 14:39:12 -03:00
|
|
|
} else {
|
2000-06-29 05:58:44 -03:00
|
|
|
#if defined(HAVE_UNICODE)
|
2000-08-01 18:05:41 -03:00
|
|
|
status = sre_umatch(state, PatternObject_GetCode(self->pattern), 1);
|
2000-06-29 05:58:44 -03:00
|
|
|
#endif
|
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
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
if (status == 0 || state->ptr == state->start)
|
|
|
|
state->start = (void*) ((char*) state->ptr + state->charsize);
|
2000-06-01 14:39:12 -03:00
|
|
|
else
|
2000-06-29 05:58:44 -03:00
|
|
|
state->start = state->ptr;
|
2000-06-01 14:39:12 -03:00
|
|
|
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-29 13:57:40 -03:00
|
|
|
scanner_search(ScannerObject* self, PyObject* args)
|
2000-06-01 14:39:12 -03:00
|
|
|
{
|
|
|
|
SRE_STATE* state = &self->state;
|
|
|
|
PyObject* match;
|
|
|
|
int status;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2000-06-29 13:57:40 -03:00
|
|
|
if (status == 0 || state->ptr == state->start)
|
|
|
|
state->start = (void*) ((char*) state->ptr + state->charsize);
|
|
|
|
else
|
2000-06-01 14:39:12 -03:00
|
|
|
state->start = state->ptr;
|
|
|
|
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
2000-06-29 13:57:40 -03:00
|
|
|
static PyMethodDef scanner_methods[] = {
|
2002-03-31 11:46:00 -04:00
|
|
|
/* FIXME: use METH_OLDARGS instead of 0 or fix to use METH_VARARGS */
|
|
|
|
/* METH_OLDARGS is not in Python 1.5.2 */
|
|
|
|
{"match", (PyCFunction) scanner_match, 0},
|
|
|
|
{"search", (PyCFunction) scanner_search, 0},
|
2000-07-23 18:46:17 -03:00
|
|
|
{NULL, NULL}
|
2000-06-01 14:39:12 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-29 13:57:40 -03:00
|
|
|
scanner_getattr(ScannerObject* self, char* name)
|
2000-06-01 14:39:12 -03:00
|
|
|
{
|
2000-07-23 18:46:17 -03:00
|
|
|
PyObject* res;
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
res = Py_FindMethod(scanner_methods, (PyObject*) self, name);
|
|
|
|
if (res)
|
|
|
|
return res;
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyErr_Clear();
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
/* attributes */
|
|
|
|
if (!strcmp(name, "pattern")) {
|
2000-06-01 14:39:12 -03:00
|
|
|
Py_INCREF(self->pattern);
|
2000-07-23 18:46:17 -03:00
|
|
|
return self->pattern;
|
2000-06-01 14:39:12 -03:00
|
|
|
}
|
|
|
|
|
2000-07-23 18:46:17 -03:00
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
|
|
return NULL;
|
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*/
|
|
|
|
0, /*tp_print*/
|
|
|
|
(getattrfunc)scanner_getattr, /*tp_getattr*/
|
2000-06-01 14:39:12 -03:00
|
|
|
};
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
static PyMethodDef _functions[] = {
|
2002-03-31 10:44:22 -04:00
|
|
|
{"compile", _compile, METH_VARARGS},
|
|
|
|
{"getcodesize", sre_codesize, METH_VARARGS},
|
|
|
|
{"getlower", sre_getlower, METH_VARARGS},
|
2000-07-23 18:46:17 -03:00
|
|
|
{NULL, NULL}
|
2000-03-31 10:59:30 -04:00
|
|
|
};
|
|
|
|
|
2001-02-28 12:44:18 -04:00
|
|
|
DL_EXPORT(void)
|
2000-07-21 03:00:07 -03:00
|
|
|
init_sre(void)
|
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 */
|
|
|
|
Pattern_Type.ob_type = Match_Type.ob_type =
|
2000-06-29 13:57:40 -03:00
|
|
|
Scanner_Type.ob_type = &PyType_Type;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2001-01-16 03:37:30 -04:00
|
|
|
m = Py_InitModule("_" SRE_MODULE, _functions);
|
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
|
|
|
|
2001-09-18 15:47:09 -03:00
|
|
|
x = PyString_FromString(copyright);
|
|
|
|
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) */
|