2000-03-31 10:59:30 -04:00
|
|
|
/* -*- Mode: C; tab-width: 4 -*-
|
|
|
|
*
|
|
|
|
* 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-06-29 05:58:44 -03:00
|
|
|
* 99-10-24 fl created (based on existing template matcher code)
|
2000-03-31 10:59:30 -04:00
|
|
|
* 99-11-13 fl added categories, branching, and more (0.2)
|
|
|
|
* 99-11-16 fl some tweaks to compile on non-Windows platforms
|
|
|
|
* 99-12-18 fl non-literals, generic maximizing repeat (0.3)
|
2000-06-29 05:58:44 -03:00
|
|
|
* 00-02-28 fl tons of changes (not all to the better ;-) (0.4)
|
|
|
|
* 00-03-06 fl first alpha, sort of (0.5)
|
|
|
|
* 00-03-14 fl removed most compatibility stuff (0.6)
|
|
|
|
* 00-05-10 fl towards third alpha (0.8.2)
|
2000-06-29 13:57:40 -03:00
|
|
|
* 00-05-13 fl added experimental scanner stuff (0.8.3)
|
2000-06-29 05:58:44 -03:00
|
|
|
* 00-05-27 fl final bug hunt (0.8.4)
|
|
|
|
* 00-06-21 fl less bugs, more taste (0.8.5)
|
|
|
|
* 00-06-25 fl major changes to better deal with nested repeats (0.9)
|
|
|
|
* 00-06-28 fl fixed findall (0.9.1)
|
2000-06-29 13:57:40 -03:00
|
|
|
* 00-06-29 fl fixed split, added more scanner features (0.9.2)
|
2000-07-01 20:49:14 -03:00
|
|
|
* 00-06-30 fl added fast search optimization (0.9.3)
|
2000-06-30 10:55:15 -03:00
|
|
|
* 00-06-30 fl added assert (lookahead) primitives, etc (0.9.4)
|
2000-07-02 14:33:27 -03:00
|
|
|
* 00-07-02 fl added charset optimizations, etc (0.9.5)
|
2000-07-03 15:44:21 -03:00
|
|
|
* 00-07-03 fl store code in pattern object, lookbehind, etc
|
2000-03-31 10:59:30 -04:00
|
|
|
*
|
|
|
|
* Copyright (c) 1997-2000 by Secret Labs AB. All rights reserved.
|
|
|
|
*
|
|
|
|
* Portions of this engine have been developed in cooperation with
|
2000-07-01 14:50:59 -03:00
|
|
|
* CNRI. Hewlett-Packard provided funding for 2.0 integration and
|
2000-03-31 10:59:30 -04:00
|
|
|
* other compatibility work.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SRE_RECURSIVE
|
|
|
|
|
2000-07-02 14:33:27 -03:00
|
|
|
char copyright[] = " SRE 0.9.5 Copyright (c) 1997-2000 by Secret Labs AB ";
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
#include "Python.h"
|
|
|
|
|
|
|
|
#include "sre.h"
|
|
|
|
|
|
|
|
#if defined(HAVE_LIMITS_H)
|
|
|
|
#include <limits.h>
|
|
|
|
#else
|
|
|
|
#define INT_MAX 2147483647
|
|
|
|
#endif
|
|
|
|
|
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 */
|
|
|
|
#define MODULE "sre"
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
/* defining this one enables tracing */
|
|
|
|
#undef DEBUG
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
#if PY_VERSION_HEX >= 0x01060000
|
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
|
|
|
|
|
2000-06-29 20:33:12 -03:00
|
|
|
/* optional features */
|
|
|
|
#define USE_FAST_SEARCH
|
|
|
|
|
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
|
|
|
|
#else
|
2000-06-29 20:33:12 -03:00
|
|
|
#define LOCAL(type) static inline type
|
2000-03-31 10:59:30 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* error codes */
|
|
|
|
#define SRE_ERROR_ILLEGAL -1 /* illegal opcode */
|
|
|
|
#define SRE_ERROR_MEMORY -9 /* out of memory */
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
#if defined(DEBUG)
|
2000-03-31 10:59:30 -04:00
|
|
|
#define TRACE(v) printf v
|
|
|
|
#else
|
|
|
|
#define TRACE(v)
|
|
|
|
#endif
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
#define PTR(ptr) ((SRE_CHAR*) (ptr) - (SRE_CHAR*) state->beginning)
|
2000-03-31 10:59:30 -04:00
|
|
|
|
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
|
|
|
|
|
|
|
|
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 };
|
|
|
|
|
2000-06-29 09:48:37 -03:00
|
|
|
static unsigned int sre_lower(unsigned int ch)
|
2000-06-29 05:58:44 -03:00
|
|
|
{
|
2000-06-29 09:48:37 -03:00
|
|
|
return ((ch) < 128 ? sre_char_lower[ch] : ch);
|
2000-06-29 05:58:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#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
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
/* locale-specific character predicates */
|
2000-06-29 05:58:44 -03:00
|
|
|
|
2000-06-29 09:48:37 -03:00
|
|
|
static unsigned int sre_lower_locale(unsigned int ch)
|
2000-06-29 05:58:44 -03:00
|
|
|
{
|
|
|
|
return ((ch) < 256 ? tolower((ch)) : ch);
|
|
|
|
}
|
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) == '_')
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
/* unicode-specific character predicates */
|
|
|
|
|
|
|
|
#if defined(HAVE_UNICODE)
|
2000-06-29 09:48:37 -03:00
|
|
|
static unsigned int sre_lower_unicode(unsigned int ch)
|
2000-06-29 05:58:44 -03:00
|
|
|
{
|
|
|
|
return (unsigned int) Py_UNICODE_TOLOWER((Py_UNICODE)(ch));
|
|
|
|
}
|
|
|
|
#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-06-29 05:58:44 -03:00
|
|
|
#define SRE_UNI_IS_WORD(ch) (SRE_IS_ALNUM((ch)) || (ch) == '_')
|
|
|
|
#endif
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
LOCAL(int)
|
|
|
|
sre_category(SRE_CODE category, unsigned int ch)
|
|
|
|
{
|
|
|
|
switch (category) {
|
2000-06-29 05:58:44 -03:00
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
case SRE_CATEGORY_DIGIT:
|
2000-03-31 10:59:30 -04:00
|
|
|
return SRE_IS_DIGIT(ch);
|
2000-06-01 14:39:12 -03:00
|
|
|
case SRE_CATEGORY_NOT_DIGIT:
|
2000-03-31 10:59:30 -04:00
|
|
|
return !SRE_IS_DIGIT(ch);
|
2000-06-01 14:39:12 -03:00
|
|
|
case SRE_CATEGORY_SPACE:
|
2000-03-31 10:59:30 -04:00
|
|
|
return SRE_IS_SPACE(ch);
|
2000-06-01 14:39:12 -03:00
|
|
|
case SRE_CATEGORY_NOT_SPACE:
|
2000-03-31 10:59:30 -04:00
|
|
|
return !SRE_IS_SPACE(ch);
|
2000-06-01 14:39:12 -03:00
|
|
|
case SRE_CATEGORY_WORD:
|
2000-03-31 10:59:30 -04:00
|
|
|
return SRE_IS_WORD(ch);
|
2000-06-01 14:39:12 -03:00
|
|
|
case SRE_CATEGORY_NOT_WORD:
|
2000-03-31 10:59:30 -04:00
|
|
|
return !SRE_IS_WORD(ch);
|
2000-06-01 14:39:12 -03:00
|
|
|
case SRE_CATEGORY_LINEBREAK:
|
|
|
|
return SRE_IS_LINEBREAK(ch);
|
|
|
|
case SRE_CATEGORY_NOT_LINEBREAK:
|
|
|
|
return !SRE_IS_LINEBREAK(ch);
|
2000-06-29 05:58:44 -03:00
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
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)
|
|
|
|
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);
|
|
|
|
#endif
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* helpers */
|
|
|
|
|
|
|
|
LOCAL(int)
|
2000-06-29 08:34:28 -03:00
|
|
|
stack_free(SRE_STATE* state)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
|
|
|
if (state->stack) {
|
|
|
|
TRACE(("release stack\n"));
|
|
|
|
free(state->stack);
|
|
|
|
state->stack = NULL;
|
|
|
|
}
|
|
|
|
state->stacksize = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int /* shouldn't be LOCAL */
|
2000-06-29 08:34:28 -03:00
|
|
|
stack_extend(SRE_STATE* state, int lo, int hi)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2000-06-29 05:58:44 -03:00
|
|
|
SRE_STACK* stack;
|
2000-03-31 10:59:30 -04:00
|
|
|
int stacksize;
|
|
|
|
|
|
|
|
/* grow the stack to a suitable size; we need at least lo entries,
|
|
|
|
at most hi entries. if for some reason hi is lower than lo, lo
|
|
|
|
wins */
|
|
|
|
|
|
|
|
stacksize = state->stacksize;
|
|
|
|
|
|
|
|
if (stacksize == 0) {
|
|
|
|
/* create new stack */
|
|
|
|
stacksize = 512;
|
|
|
|
if (stacksize < lo)
|
|
|
|
stacksize = lo;
|
|
|
|
else if (stacksize > hi)
|
|
|
|
stacksize = hi;
|
|
|
|
TRACE(("allocate stack %d\n", stacksize));
|
2000-06-29 05:58:44 -03:00
|
|
|
stack = malloc(sizeof(SRE_STACK) * stacksize);
|
2000-03-31 10:59:30 -04:00
|
|
|
} else {
|
|
|
|
/* grow the stack (typically by a factor of two) */
|
|
|
|
while (stacksize < lo)
|
|
|
|
stacksize = 2 * stacksize;
|
2000-07-05 18:14:16 -03:00
|
|
|
/* FIXME: <fl> could trim size if it's much larger than hi,
|
|
|
|
as long it's larger than lo */
|
2000-03-31 10:59:30 -04:00
|
|
|
TRACE(("grow stack to %d\n", stacksize));
|
2000-06-29 05:58:44 -03:00
|
|
|
stack = realloc(state->stack, sizeof(SRE_STACK) * stacksize);
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!stack) {
|
2000-06-29 08:34:28 -03:00
|
|
|
stack_free(state);
|
2000-03-31 10:59:30 -04:00
|
|
|
return SRE_ERROR_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
state->stack = stack;
|
|
|
|
state->stacksize = stacksize;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
#define SRE_MEMBER sre_member
|
|
|
|
#define SRE_MATCH sre_match
|
|
|
|
#define SRE_SEARCH sre_search
|
|
|
|
|
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
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
#undef SRE_SEARCH
|
|
|
|
#undef SRE_MATCH
|
|
|
|
#undef SRE_MEMBER
|
|
|
|
#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
|
|
|
|
#define SRE_MEMBER sre_umember
|
|
|
|
#define SRE_MATCH sre_umatch
|
|
|
|
#define SRE_SEARCH sre_usearch
|
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-06-01 14:39:12 -03:00
|
|
|
/* check if pointer is at given position */
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
int this, that;
|
|
|
|
|
|
|
|
switch (at) {
|
2000-06-29 15:03:25 -03:00
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
case SRE_AT_BEGINNING:
|
2000-04-10 14:06:55 -03:00
|
|
|
return ((void*) ptr == state->beginning);
|
2000-06-29 15:03:25 -03:00
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
case SRE_AT_BEGINNING_LINE:
|
|
|
|
return ((void*) ptr == state->beginning ||
|
|
|
|
SRE_IS_LINEBREAK((int) ptr[-1]));
|
2000-06-29 15:03:25 -03:00
|
|
|
|
2000-06-01 14:39:12 -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-06-01 14:39:12 -03:00
|
|
|
case SRE_AT_END_LINE:
|
|
|
|
return ((void*) ptr == state->end ||
|
|
|
|
SRE_IS_LINEBREAK((int) ptr[0]));
|
2000-06-29 15:03:25 -03:00
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
case SRE_AT_BOUNDARY:
|
2000-03-31 10:59:30 -04:00
|
|
|
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;
|
2000-06-29 15:03:25 -03:00
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
case SRE_AT_NON_BOUNDARY:
|
2000-03-31 10:59:30 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCAL(int)
|
2000-06-30 10:55:15 -03:00
|
|
|
SRE_MEMBER(SRE_CODE* set, SRE_CODE ch)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2000-06-01 14:39:12 -03:00
|
|
|
/* check if character is a member of the given set */
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
int ok = 1;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
switch (*set++) {
|
|
|
|
|
|
|
|
case SRE_OP_NEGATE:
|
|
|
|
ok = !ok;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_FAILURE:
|
|
|
|
return !ok;
|
|
|
|
|
|
|
|
case SRE_OP_LITERAL:
|
2000-07-01 20:49:14 -03:00
|
|
|
/* args: <literal> */
|
2000-06-30 10:55:15 -03:00
|
|
|
if (ch == set[0])
|
2000-03-31 10:59:30 -04:00
|
|
|
return ok;
|
|
|
|
set++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_RANGE:
|
2000-07-01 20:49:14 -03:00
|
|
|
/* args: <lower> <upper> */
|
2000-06-30 10:55:15 -03:00
|
|
|
if (set[0] <= ch && ch <= set[1])
|
2000-03-31 10:59:30 -04:00
|
|
|
return ok;
|
|
|
|
set += 2;
|
|
|
|
break;
|
|
|
|
|
2000-07-02 09:00:07 -03:00
|
|
|
case SRE_OP_CHARSET:
|
|
|
|
/* args: <bitmap> (16 bits per code word) */
|
|
|
|
if (ch < 256 && (set[ch >> 4] & (1 << (ch & 15))))
|
|
|
|
return ok;
|
|
|
|
set += 16;
|
|
|
|
break;
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
case SRE_OP_CATEGORY:
|
2000-07-01 20:49:14 -03:00
|
|
|
/* args: <category> */
|
2000-03-31 10:59:30 -04:00
|
|
|
if (sre_category(set[0], (int) ch))
|
|
|
|
return ok;
|
|
|
|
set += 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2000-06-29 15:03:25 -03:00
|
|
|
/* internal error -- there's not much we can do about it
|
|
|
|
here, so let's just pretend it didn't match... */
|
2000-03-31 10:59:30 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCAL(int)
|
|
|
|
SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern)
|
|
|
|
{
|
|
|
|
/* check if string matches the given pattern. returns -1 for
|
|
|
|
error, 0 for failure, and 1 for success */
|
|
|
|
|
|
|
|
SRE_CHAR* end = state->end;
|
|
|
|
SRE_CHAR* ptr = state->ptr;
|
2000-06-29 05:58:44 -03:00
|
|
|
int stack;
|
2000-03-31 10:59:30 -04:00
|
|
|
int stackbase;
|
2000-06-29 05:58:44 -03:00
|
|
|
int lastmark;
|
2000-03-31 10:59:30 -04:00
|
|
|
int i, count;
|
2000-07-03 18:31:48 -03:00
|
|
|
SRE_STACK* sp;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
/* FIXME: this is a hack! */
|
2000-06-29 13:57:40 -03:00
|
|
|
void* mark_copy[SRE_MARK_SIZE];
|
2000-06-29 05:58:44 -03:00
|
|
|
void* mark = NULL;
|
|
|
|
|
|
|
|
TRACE(("%8d: enter\n", PTR(ptr)));
|
|
|
|
|
2000-06-29 20:33:12 -03:00
|
|
|
if (pattern[0] == SRE_OP_INFO) {
|
|
|
|
/* optimization info block */
|
|
|
|
/* args: <1=skip> <2=flags> <3=min> ... */
|
|
|
|
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-06-29 05:58:44 -03:00
|
|
|
stackbase = stack = state->stackbase;
|
|
|
|
lastmark = state->lastmark;
|
|
|
|
|
|
|
|
retry:
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
for (;;) {
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
switch (*pattern++) {
|
|
|
|
|
|
|
|
case SRE_OP_FAILURE:
|
|
|
|
/* immediate failure */
|
|
|
|
TRACE(("%8d: failure\n", PTR(ptr)));
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
case SRE_OP_SUCCESS:
|
|
|
|
/* end of pattern */
|
|
|
|
TRACE(("%8d: success\n", PTR(ptr)));
|
|
|
|
state->ptr = ptr;
|
2000-06-01 14:39:12 -03:00
|
|
|
goto success;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
case SRE_OP_AT:
|
|
|
|
/* match at given position */
|
2000-06-01 14:39:12 -03:00
|
|
|
/* args: <at> */
|
2000-06-29 05:58:44 -03:00
|
|
|
TRACE(("%8d: position %d\n", PTR(ptr), *pattern));
|
2000-03-31 10:59:30 -04:00
|
|
|
if (!SRE_AT(state, ptr, *pattern))
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
|
|
|
pattern++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_CATEGORY:
|
|
|
|
/* match at given category */
|
|
|
|
/* args: <category> */
|
2000-06-29 05:58:44 -03:00
|
|
|
TRACE(("%8d: category %d [category %d]\n", PTR(ptr),
|
|
|
|
*ptr, *pattern));
|
2000-06-01 14:39:12 -03:00
|
|
|
if (ptr >= end || !sre_category(pattern[0], ptr[0]))
|
|
|
|
goto failure;
|
2000-06-29 05:58:44 -03:00
|
|
|
TRACE(("%8d: category ok\n", PTR(ptr)));
|
2000-03-31 10:59:30 -04:00
|
|
|
pattern++;
|
2000-06-01 14:39:12 -03:00
|
|
|
ptr++;
|
2000-03-31 10:59:30 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_LITERAL:
|
2000-06-29 05:58:44 -03:00
|
|
|
/* match literal string */
|
2000-03-31 10:59:30 -04:00
|
|
|
/* args: <code> */
|
2000-06-30 10:55:15 -03:00
|
|
|
TRACE(("%8d: literal %c\n", PTR(ptr), pattern[0]));
|
|
|
|
if (ptr >= end || (SRE_CODE) ptr[0] != pattern[0])
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
2000-03-31 10:59:30 -04:00
|
|
|
pattern++;
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_NOT_LITERAL:
|
|
|
|
/* match anything that is not literal character */
|
|
|
|
/* args: <code> */
|
2000-06-30 10:55:15 -03:00
|
|
|
TRACE(("%8d: literal not %c\n", PTR(ptr), pattern[0]));
|
|
|
|
if (ptr >= end || (SRE_CODE) ptr[0] == pattern[0])
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
2000-03-31 10:59:30 -04:00
|
|
|
pattern++;
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_ANY:
|
|
|
|
/* match anything */
|
2000-06-29 05:58:44 -03:00
|
|
|
TRACE(("%8d: anything\n", PTR(ptr)));
|
2000-03-31 10:59:30 -04:00
|
|
|
if (ptr >= end)
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
2000-03-31 10:59:30 -04:00
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_IN:
|
|
|
|
/* match set member (or non_member) */
|
|
|
|
/* args: <skip> <set> */
|
|
|
|
TRACE(("%8d: set %c\n", PTR(ptr), *ptr));
|
|
|
|
if (ptr >= end || !SRE_MEMBER(pattern + 1, *ptr))
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
2000-03-31 10:59:30 -04:00
|
|
|
pattern += pattern[0];
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_GROUP:
|
|
|
|
/* match backreference */
|
2000-06-01 14:39:12 -03:00
|
|
|
TRACE(("%8d: group %d\n", PTR(ptr), pattern[0]));
|
2000-03-31 10:59:30 -04: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-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
2000-03-31 10:59:30 -04:00
|
|
|
while (p < e) {
|
|
|
|
if (ptr >= end || *ptr != *p)
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
|
|
|
p++; ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pattern++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_GROUP_IGNORE:
|
|
|
|
/* match backreference */
|
|
|
|
TRACE(("%8d: group ignore %d\n", PTR(ptr), pattern[0]));
|
|
|
|
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)
|
|
|
|
goto failure;
|
|
|
|
while (p < e) {
|
2000-06-29 05:58:44 -03:00
|
|
|
if (ptr >= end ||
|
2000-06-29 09:48:37 -03:00
|
|
|
state->lower(*ptr) != state->lower(*p))
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
2000-03-31 10:59:30 -04:00
|
|
|
p++; ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pattern++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_LITERAL_IGNORE:
|
2000-06-30 10:55:15 -03:00
|
|
|
TRACE(("%8d: literal lower(%c)\n", PTR(ptr), pattern[0]));
|
2000-06-29 05:58:44 -03:00
|
|
|
if (ptr >= end ||
|
2000-06-29 09:48:37 -03:00
|
|
|
state->lower(*ptr) != state->lower(*pattern))
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
2000-03-31 10:59:30 -04:00
|
|
|
pattern++;
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_NOT_LITERAL_IGNORE:
|
2000-06-30 10:55:15 -03:00
|
|
|
TRACE(("%8d: literal not lower(%c)\n", PTR(ptr), pattern[0]));
|
2000-06-29 05:58:44 -03:00
|
|
|
if (ptr >= end ||
|
2000-06-29 09:48:37 -03:00
|
|
|
state->lower(*ptr) == state->lower(*pattern))
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
2000-03-31 10:59:30 -04:00
|
|
|
pattern++;
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_IN_IGNORE:
|
|
|
|
TRACE(("%8d: set lower(%c)\n", PTR(ptr), *ptr));
|
|
|
|
if (ptr >= end
|
2000-06-30 10:55:15 -03:00
|
|
|
|| !SRE_MEMBER(pattern+1, (SRE_CODE) state->lower(*ptr)))
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
2000-03-31 10:59:30 -04:00
|
|
|
pattern += pattern[0];
|
|
|
|
ptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SRE_OP_MARK:
|
|
|
|
/* set mark */
|
|
|
|
/* args: <mark> */
|
2000-06-29 05:58:44 -03:00
|
|
|
TRACE(("%8d: set mark %d\n", PTR(ptr), pattern[0]));
|
2000-07-03 18:31:48 -03:00
|
|
|
if (state->lastmark < pattern[0]+1)
|
|
|
|
state->lastmark = pattern[0]+1;
|
2000-06-01 14:39:12 -03:00
|
|
|
if (!mark) {
|
2000-06-29 05:58:44 -03:00
|
|
|
mark = mark_copy;
|
|
|
|
memcpy(mark, state->mark, state->lastmark*sizeof(void*));
|
2000-06-01 14:39:12 -03:00
|
|
|
}
|
|
|
|
state->mark[pattern[0]] = ptr;
|
2000-03-31 10:59:30 -04:00
|
|
|
pattern++;
|
|
|
|
break;
|
|
|
|
|
2000-07-02 14:33:27 -03:00
|
|
|
case SRE_OP_INDEX:
|
|
|
|
/* set index */
|
|
|
|
/* args: <index> */
|
|
|
|
TRACE(("%8d: set index %d\n", PTR(ptr), pattern[0]));
|
2000-07-03 15:44:21 -03:00
|
|
|
state->lastindex = pattern[0];
|
2000-07-02 14:33:27 -03:00
|
|
|
pattern++;
|
|
|
|
break;
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
case SRE_OP_JUMP:
|
2000-06-29 05:58:44 -03:00
|
|
|
case SRE_OP_INFO:
|
2000-03-31 10:59:30 -04:00
|
|
|
/* jump forward */
|
|
|
|
/* args: <skip> */
|
|
|
|
TRACE(("%8d: jump +%d\n", PTR(ptr), pattern[0]));
|
|
|
|
pattern += pattern[0];
|
|
|
|
break;
|
|
|
|
|
2000-06-30 07:41:31 -03:00
|
|
|
case SRE_OP_ASSERT:
|
|
|
|
/* assert subpattern */
|
2000-07-03 15:44:21 -03:00
|
|
|
/* args: <skip> <back> <pattern> */
|
|
|
|
TRACE(("%8d: assert subpattern %d\n", PTR(ptr), pattern[1]));
|
|
|
|
state->ptr = ptr - pattern[1];
|
|
|
|
if (state->ptr < state->beginning)
|
|
|
|
goto failure;
|
|
|
|
i = SRE_MATCH(state, pattern + 2);
|
2000-06-29 05:58:44 -03:00
|
|
|
if (i < 0)
|
|
|
|
return i;
|
|
|
|
if (!i)
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
2000-03-31 10:59:30 -04:00
|
|
|
pattern += pattern[0];
|
|
|
|
break;
|
2000-06-30 07:41:31 -03:00
|
|
|
|
|
|
|
case SRE_OP_ASSERT_NOT:
|
|
|
|
/* assert not subpattern */
|
|
|
|
/* args: <skip> <pattern> */
|
2000-07-03 15:44:21 -03:00
|
|
|
TRACE(("%8d: assert not subpattern %d\n", PTR(ptr), pattern[1]));
|
|
|
|
state->ptr = ptr - pattern[1];
|
|
|
|
if (state->ptr < state->beginning)
|
|
|
|
goto failure;
|
|
|
|
i = SRE_MATCH(state, pattern + 2);
|
2000-06-30 07:41:31 -03:00
|
|
|
if (i < 0)
|
|
|
|
return i;
|
|
|
|
if (i)
|
|
|
|
goto failure;
|
|
|
|
pattern += pattern[0];
|
|
|
|
break;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
#if 0
|
2000-03-31 10:59:30 -04:00
|
|
|
case SRE_OP_MAX_REPEAT_ONE:
|
2000-06-01 14:39:12 -03:00
|
|
|
/* match repeated sequence (maximizing regexp) */
|
2000-06-29 05:58:44 -03:00
|
|
|
|
|
|
|
/* this operator only works if the repeated item is
|
|
|
|
exactly one character wide, and we're not already
|
|
|
|
collecting backtracking points. for other cases,
|
|
|
|
use the MAX_REPEAT operator instead */
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
/* args: <skip> <min> <max> <step> */
|
|
|
|
TRACE(("%8d: max repeat one {%d,%d}\n", PTR(ptr),
|
|
|
|
pattern[1], pattern[2]));
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
if (pattern[3] == SRE_OP_ANY) {
|
|
|
|
/* repeated wildcard. skip to the end of the target
|
|
|
|
string, and backtrack from there */
|
|
|
|
/* FIXME: must look for line endings */
|
|
|
|
if (ptr + pattern[1] > end)
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure; /* cannot match */
|
2000-03-31 10:59:30 -04:00
|
|
|
count = pattern[2];
|
|
|
|
if (count > end - ptr)
|
|
|
|
count = end - ptr;
|
|
|
|
ptr += count;
|
|
|
|
|
|
|
|
} else if (pattern[3] == SRE_OP_LITERAL) {
|
|
|
|
/* repeated literal */
|
2000-06-30 10:55:15 -03:00
|
|
|
SRE_CODE chr = pattern[4];
|
2000-03-31 10:59:30 -04:00
|
|
|
while (count < (int) pattern[2]) {
|
2000-06-30 10:55:15 -03:00
|
|
|
if (ptr >= end || (SRE_CODE) ptr[0] != chr)
|
2000-03-31 10:59:30 -04:00
|
|
|
break;
|
|
|
|
ptr++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (pattern[3] == SRE_OP_LITERAL_IGNORE) {
|
|
|
|
/* repeated literal */
|
2000-06-30 10:55:15 -03:00
|
|
|
SRE_CODE chr = pattern[4];
|
2000-03-31 10:59:30 -04:00
|
|
|
while (count < (int) pattern[2]) {
|
2000-06-30 10:55:15 -03:00
|
|
|
if (ptr >= end || (SRE_CODE) state->lower(*ptr) != chr)
|
2000-03-31 10:59:30 -04:00
|
|
|
break;
|
|
|
|
ptr++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (pattern[3] == SRE_OP_NOT_LITERAL) {
|
|
|
|
/* repeated non-literal */
|
2000-06-30 10:55:15 -03:00
|
|
|
SRE_CODE chr = pattern[4];
|
2000-03-31 10:59:30 -04:00
|
|
|
while (count < (int) pattern[2]) {
|
2000-06-30 10:55:15 -03:00
|
|
|
if (ptr >= end || (SRE_CODE) ptr[0] == chr)
|
2000-03-31 10:59:30 -04:00
|
|
|
break;
|
|
|
|
ptr++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (pattern[3] == SRE_OP_NOT_LITERAL_IGNORE) {
|
|
|
|
/* repeated non-literal */
|
2000-06-30 10:55:15 -03:00
|
|
|
SRE_CODE chr = pattern[4];
|
2000-03-31 10:59:30 -04:00
|
|
|
while (count < (int) pattern[2]) {
|
2000-06-30 10:55:15 -03:00
|
|
|
if (ptr >= end || (SRE_CODE) state->lower(ptr[0]) == chr)
|
2000-03-31 10:59:30 -04:00
|
|
|
break;
|
|
|
|
ptr++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (pattern[3] == SRE_OP_IN) {
|
|
|
|
/* repeated set */
|
|
|
|
while (count < (int) pattern[2]) {
|
|
|
|
if (ptr >= end || !SRE_MEMBER(pattern + 5, *ptr))
|
|
|
|
break;
|
|
|
|
ptr++;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* repeated single character pattern */
|
|
|
|
state->ptr = ptr;
|
|
|
|
while (count < (int) pattern[2]) {
|
|
|
|
i = SRE_MATCH(state, pattern + 3);
|
|
|
|
if (i < 0)
|
2000-06-29 05:58:44 -03:00
|
|
|
return i;
|
|
|
|
if (!i)
|
2000-03-31 10:59:30 -04:00
|
|
|
break;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
state->ptr = ptr;
|
|
|
|
ptr += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
|
|
|
|
TRACE(("%8d: repeat %d found\n", PTR(ptr), count));
|
|
|
|
|
|
|
|
if (count < (int) pattern[1])
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
if (pattern[pattern[0]] == SRE_OP_SUCCESS) {
|
|
|
|
/* tail is empty. we're finished */
|
|
|
|
TRACE(("%8d: tail is empty\n", PTR(ptr)));
|
|
|
|
state->ptr = ptr;
|
2000-06-01 14:39:12 -03:00
|
|
|
goto success;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
} else if (pattern[pattern[0]] == SRE_OP_LITERAL) {
|
2000-06-01 14:39:12 -03:00
|
|
|
/* tail starts with a literal. skip positions where
|
|
|
|
the rest of the pattern cannot possibly match */
|
2000-06-30 10:55:15 -03:00
|
|
|
SRE_CODE chr = pattern[pattern[0]+1];
|
2000-03-31 10:59:30 -04:00
|
|
|
TRACE(("%8d: tail is literal %d\n", PTR(ptr), chr));
|
|
|
|
for (;;) {
|
|
|
|
TRACE(("%8d: scan for tail match\n", PTR(ptr)));
|
|
|
|
while (count >= (int) pattern[1] &&
|
|
|
|
(ptr >= end || *ptr != chr)) {
|
|
|
|
ptr--;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
TRACE(("%8d: check tail\n", PTR(ptr)));
|
|
|
|
if (count < (int) pattern[1])
|
|
|
|
break;
|
|
|
|
state->ptr = ptr;
|
|
|
|
i = SRE_MATCH(state, pattern + pattern[0]);
|
|
|
|
if (i > 0) {
|
|
|
|
TRACE(("%8d: repeat %d picked\n", PTR(ptr), count));
|
2000-06-01 14:39:12 -03:00
|
|
|
goto success;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
TRACE(("%8d: BACKTRACK\n", PTR(ptr)));
|
|
|
|
ptr--;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2000-06-01 14:39:12 -03:00
|
|
|
/* general case */
|
2000-03-31 10:59:30 -04:00
|
|
|
TRACE(("%8d: tail is pattern\n", PTR(ptr)));
|
|
|
|
while (count >= (int) pattern[1]) {
|
|
|
|
state->ptr = ptr;
|
|
|
|
i = SRE_MATCH(state, pattern + pattern[0]);
|
2000-06-29 05:58:44 -03:00
|
|
|
if (i < 0)
|
|
|
|
return i;
|
|
|
|
if (i) {
|
2000-03-31 10:59:30 -04:00
|
|
|
TRACE(("%8d: repeat %d picked\n", PTR(ptr), count));
|
2000-06-01 14:39:12 -03:00
|
|
|
goto success;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
TRACE(("%8d: BACKTRACK\n", PTR(ptr)));
|
|
|
|
ptr--;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
}
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
2000-06-29 05:58:44 -03:00
|
|
|
#endif
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
case SRE_OP_MAX_REPEAT:
|
2000-07-03 18:31:48 -03:00
|
|
|
/* match repeated sequence (maximizing regexp) */
|
|
|
|
/* args: <skip> <1=min> <2=max> <3=save> <4=item> */
|
2000-06-29 05:58:44 -03:00
|
|
|
|
|
|
|
TRACE(("%8d: max repeat (%d %d)\n", PTR(ptr),
|
2000-03-31 10:59:30 -04:00
|
|
|
pattern[1], pattern[2]));
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
state->ptr = ptr;
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
/* match minimum number of items */
|
|
|
|
while (count < (int) pattern[1]) {
|
2000-07-03 18:31:48 -03:00
|
|
|
i = SRE_MATCH(state, pattern + 4);
|
2000-06-29 05:58:44 -03:00
|
|
|
if (i < 0)
|
|
|
|
return i;
|
|
|
|
if (!i)
|
|
|
|
goto failure;
|
|
|
|
if (state->ptr == ptr) {
|
|
|
|
/* if the match was successful but empty, set the
|
|
|
|
count to max and terminate the scanning loop */
|
|
|
|
count = (int) pattern[2];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
ptr = state->ptr;
|
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
TRACE(("%8d: found %d leading items\n", PTR(ptr), count));
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
if (count < (int) pattern[1])
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
/* match maximum number of items, pushing alternate end
|
|
|
|
points to the stack */
|
|
|
|
|
2000-07-02 14:33:27 -03:00
|
|
|
while (pattern[2] == 65535 || count < (int) pattern[2]) {
|
2000-06-29 05:58:44 -03:00
|
|
|
/* this position was valid; add it to the retry
|
|
|
|
stack */
|
|
|
|
if (stack >= state->stacksize) {
|
2000-06-29 08:34:28 -03:00
|
|
|
i = stack_extend(state, stack + 1,
|
2000-06-29 05:58:44 -03:00
|
|
|
stackbase + pattern[2]);
|
|
|
|
if (i < 0)
|
|
|
|
return i; /* out of memory */
|
|
|
|
}
|
2000-07-05 18:14:16 -03:00
|
|
|
TRACE(("%8d: stack[%d]\n", PTR(ptr), stack));
|
|
|
|
TRACE((" ptr %d mark %d %d %d\n",
|
|
|
|
PTR(ptr), pattern[3], PTR(mark0), PTR(mark1)));
|
2000-07-03 18:31:48 -03:00
|
|
|
sp = state->stack + stack;
|
|
|
|
sp->ptr = ptr;
|
|
|
|
sp->pattern = pattern + pattern[0];
|
|
|
|
sp->mark = pattern[3];
|
|
|
|
if (pattern[3] != 65535) {
|
2000-07-05 18:14:16 -03:00
|
|
|
sp->mark0 = state->mark[pattern[3]];
|
|
|
|
sp->mark1 = state->mark[pattern[3]+1];
|
2000-07-03 18:31:48 -03:00
|
|
|
}
|
2000-06-29 05:58:44 -03:00
|
|
|
stack++;
|
2000-07-05 18:14:16 -03:00
|
|
|
state->stackbase = stack;
|
|
|
|
i = SRE_MATCH(state, pattern + 4);
|
|
|
|
state->stackbase = stackbase;
|
|
|
|
if (i < 0)
|
|
|
|
return i;
|
|
|
|
if (!i)
|
|
|
|
break;
|
|
|
|
if (state->ptr == ptr) {
|
|
|
|
count = (int) pattern[2];
|
|
|
|
break;
|
|
|
|
}
|
2000-06-29 05:58:44 -03:00
|
|
|
/* move forward */
|
2000-03-31 10:59:30 -04:00
|
|
|
ptr = state->ptr;
|
2000-06-29 05:58:44 -03:00
|
|
|
count++;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
/* when we get here, count is the number of successful
|
|
|
|
matches, and ptr points to the tail. */
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
TRACE(("%8d: skip +%d\n", PTR(ptr), pattern[0]));
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
pattern += pattern[0];
|
|
|
|
break;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
case SRE_OP_MIN_REPEAT:
|
|
|
|
/* match repeated sequence (minimizing regexp) */
|
2000-07-03 18:31:48 -03:00
|
|
|
/* args: <skip> <1=min> <2=max> <3=save> <4=item> */
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
TRACE(("%8d: min repeat %d %d\n", PTR(ptr),
|
|
|
|
pattern[1], pattern[2]));
|
|
|
|
count = 0;
|
|
|
|
state->ptr = ptr;
|
|
|
|
/* match minimum number of items */
|
|
|
|
while (count < (int) pattern[1]) {
|
2000-07-03 18:31:48 -03:00
|
|
|
i = SRE_MATCH(state, pattern + 4);
|
2000-06-29 05:58:44 -03:00
|
|
|
if (i < 0)
|
|
|
|
return i;
|
|
|
|
if (!i)
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
2000-03-31 10:59:30 -04:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
/* move forward until the tail matches. */
|
|
|
|
while (count <= (int) pattern[2]) {
|
|
|
|
ptr = state->ptr;
|
|
|
|
i = SRE_MATCH(state, pattern + pattern[0]);
|
|
|
|
if (i > 0) {
|
|
|
|
TRACE(("%8d: repeat %d picked\n", PTR(ptr), count));
|
2000-06-01 14:39:12 -03:00
|
|
|
goto success;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
state->ptr = ptr; /* backtrack */
|
2000-07-03 18:31:48 -03:00
|
|
|
i = SRE_MATCH(state, pattern + 4);
|
2000-06-29 05:58:44 -03:00
|
|
|
if (i < 0)
|
|
|
|
return i;
|
|
|
|
if (!i)
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
2000-03-31 10:59:30 -04:00
|
|
|
count++;
|
|
|
|
}
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
case SRE_OP_BRANCH:
|
|
|
|
/* match one of several subpatterns */
|
|
|
|
/* format: <branch> <size> <head> ... <null> <tail> */
|
|
|
|
TRACE(("%8d: branch\n", PTR(ptr)));
|
|
|
|
while (*pattern) {
|
|
|
|
if (pattern[1] != SRE_OP_LITERAL ||
|
2000-06-30 10:55:15 -03:00
|
|
|
(ptr < end && (SRE_CODE) ptr[0] == pattern[2])) {
|
2000-03-31 10:59:30 -04:00
|
|
|
TRACE(("%8d: branch check\n", PTR(ptr)));
|
|
|
|
state->ptr = ptr;
|
|
|
|
i = SRE_MATCH(state, pattern + 1);
|
2000-06-29 05:58:44 -03:00
|
|
|
if (i < 0)
|
|
|
|
return i;
|
|
|
|
if (i) {
|
2000-03-31 10:59:30 -04:00
|
|
|
TRACE(("%8d: branch succeeded\n", PTR(ptr)));
|
2000-06-01 14:39:12 -03:00
|
|
|
goto success;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pattern += *pattern;
|
|
|
|
}
|
|
|
|
TRACE(("%8d: branch failed\n", PTR(ptr)));
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
case SRE_OP_REPEAT:
|
|
|
|
/* TEMPLATE: match repeated sequence (no backtracking) */
|
2000-06-29 05:58:44 -03:00
|
|
|
/* args: <skip> <min> <max> */
|
2000-03-31 10:59:30 -04:00
|
|
|
TRACE(("%8d: repeat %d %d\n", PTR(ptr), pattern[1], pattern[2]));
|
|
|
|
count = 0;
|
|
|
|
state->ptr = ptr;
|
|
|
|
while (count < (int) pattern[2]) {
|
|
|
|
i = SRE_MATCH(state, pattern + 3);
|
2000-06-29 05:58:44 -03:00
|
|
|
if (i < 0)
|
|
|
|
return i;
|
|
|
|
if (!i)
|
2000-03-31 10:59:30 -04:00
|
|
|
break;
|
2000-06-29 05:58:44 -03:00
|
|
|
if (state->ptr == ptr) {
|
|
|
|
count = (int) pattern[2];
|
|
|
|
break;
|
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
if (count <= (int) pattern[1])
|
2000-06-01 14:39:12 -03:00
|
|
|
goto failure;
|
2000-03-31 10:59:30 -04:00
|
|
|
TRACE(("%8d: repeat %d matches\n", PTR(ptr), count));
|
|
|
|
pattern += pattern[0];
|
|
|
|
ptr = state->ptr;
|
|
|
|
break;
|
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
default:
|
2000-06-29 05:58:44 -03:00
|
|
|
TRACE(("%8d: unknown opcode %d\n", PTR(ptr), pattern[-1]));
|
2000-03-31 10:59:30 -04:00
|
|
|
return SRE_ERROR_ILLEGAL;
|
|
|
|
}
|
|
|
|
}
|
2000-06-01 14:39:12 -03:00
|
|
|
|
|
|
|
failure:
|
2000-07-03 18:31:48 -03:00
|
|
|
TRACE(("%8d: leave (failure)\n", PTR(ptr)));
|
2000-06-29 05:58:44 -03:00
|
|
|
if (stack-- > stackbase) {
|
2000-07-05 18:14:16 -03:00
|
|
|
TRACE(("%8d: pop stack[%d]\n", stack));
|
2000-07-03 18:31:48 -03:00
|
|
|
sp = state->stack + stack;
|
|
|
|
ptr = sp->ptr;
|
|
|
|
pattern = sp->pattern;
|
|
|
|
if (sp->mark != 65535) {
|
|
|
|
state->mark[sp->mark] = sp->mark0;
|
|
|
|
state->mark[sp->mark+1] = sp->mark1;
|
|
|
|
}
|
2000-06-29 05:58:44 -03:00
|
|
|
TRACE(("%8d: retry (%d)\n", PTR(ptr), stack));
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
state->lastmark = lastmark;
|
2000-07-03 18:31:48 -03:00
|
|
|
state->stackbase = stackbase;
|
2000-06-01 14:39:12 -03:00
|
|
|
if (mark)
|
2000-06-29 05:58:44 -03:00
|
|
|
memcpy(state->mark, mark, state->lastmark*sizeof(void*));
|
2000-06-01 14:39:12 -03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
success:
|
2000-06-29 05:58:44 -03:00
|
|
|
TRACE(("%8d: leave (success)\n", PTR(ptr)));
|
|
|
|
state->stackbase = stackbase;
|
2000-06-01 14:39:12 -03:00
|
|
|
return 1;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
LOCAL(int)
|
|
|
|
SRE_SEARCH(SRE_STATE* state, SRE_CODE* pattern)
|
|
|
|
{
|
|
|
|
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-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-07-02 09:00:07 -03:00
|
|
|
/* args: <1=skip> <2=flags> <3=min> <4=max> <5=prefix info> */
|
|
|
|
|
|
|
|
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-07-02 09:00:07 -03:00
|
|
|
prefix_len = pattern[5];
|
|
|
|
prefix = pattern + 6;
|
|
|
|
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-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-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 */
|
|
|
|
TRACE(("%8d: === SEARCH === hit\n", PTR(ptr)));
|
|
|
|
state->start = ptr - prefix_len + 1;
|
|
|
|
state->ptr = ptr + 1;
|
2000-07-02 09:00:07 -03:00
|
|
|
if (flags & SRE_INFO_LITERAL)
|
|
|
|
return 1; /* we got all of it */
|
2000-06-29 20:33:12 -03:00
|
|
|
status = SRE_MATCH(state, pattern + 2*prefix_len);
|
|
|
|
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) {
|
|
|
|
/* pattern starts with a literal character. this is used
|
|
|
|
for short prefixes, and if fast search is disabled */
|
2000-06-30 10:55:15 -03:00
|
|
|
SRE_CODE chr = pattern[1];
|
2000-03-31 10:59:30 -04:00
|
|
|
for (;;) {
|
2000-06-30 10:55:15 -03:00
|
|
|
while (ptr < end && (SRE_CODE) ptr[0] != chr)
|
2000-03-31 10:59:30 -04:00
|
|
|
ptr++;
|
|
|
|
if (ptr == end)
|
|
|
|
return 0;
|
2000-06-29 05:58:44 -03:00
|
|
|
TRACE(("%8d: === SEARCH === literal\n", PTR(ptr)));
|
2000-03-31 10:59:30 -04:00
|
|
|
state->start = ptr;
|
|
|
|
state->ptr = ++ptr;
|
|
|
|
status = SRE_MATCH(state, pattern + 2);
|
|
|
|
if (status != 0)
|
|
|
|
break;
|
|
|
|
}
|
2000-07-02 09:00:07 -03:00
|
|
|
} else if (charset) {
|
|
|
|
/* pattern starts with a character from a known set */
|
|
|
|
for (;;) {
|
|
|
|
while (ptr < end && !SRE_MEMBER(charset, ptr[0]))
|
|
|
|
ptr++;
|
|
|
|
if (ptr == end)
|
|
|
|
return 0;
|
|
|
|
TRACE(("%8d: === SEARCH === charset\n", PTR(ptr)));
|
|
|
|
state->start = ptr;
|
|
|
|
state->ptr = ptr;
|
|
|
|
status = SRE_MATCH(state, pattern);
|
|
|
|
if (status != 0)
|
|
|
|
break;
|
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
} else
|
2000-06-29 20:33:12 -03:00
|
|
|
/* general case */
|
2000-03-31 10:59:30 -04:00
|
|
|
while (ptr <= end) {
|
2000-06-29 05:58:44 -03:00
|
|
|
TRACE(("%8d: === SEARCH ===\n", PTR(ptr)));
|
2000-03-31 10:59:30 -04:00
|
|
|
state->start = state->ptr = ptr++;
|
|
|
|
status = SRE_MATCH(state, pattern);
|
|
|
|
if (status != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
2000-07-02 09:00:07 -03:00
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
/* "compile" pattern descriptor to pattern object */
|
|
|
|
|
|
|
|
PatternObject* self;
|
2000-07-03 15:44:21 -03:00
|
|
|
int i, n;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
PyObject* pattern;
|
2000-06-01 14:39:12 -03:00
|
|
|
int flags = 0;
|
2000-03-31 10:59:30 -04:00
|
|
|
PyObject* code;
|
|
|
|
int groups = 0;
|
|
|
|
PyObject* groupindex = NULL;
|
2000-07-02 19:25:39 -03:00
|
|
|
PyObject* indexgroup = NULL;
|
2000-07-03 15:44:21 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "OiO|iOO", &pattern, &flags, &code,
|
2000-07-02 19:25:39 -03:00
|
|
|
&groups, &groupindex, &indexgroup))
|
2000-03-31 10:59:30 -04:00
|
|
|
return NULL;
|
|
|
|
|
2000-07-03 15:44:21 -03:00
|
|
|
code = PySequence_Fast(code, "code argument must be a sequence");
|
|
|
|
if (!code)
|
|
|
|
return NULL;
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-07-12 10:05:33 -03:00
|
|
|
n = PySequence_Size(code);
|
2000-07-03 15:44:21 -03:00
|
|
|
|
|
|
|
self = PyObject_NEW_VAR(PatternObject, &Pattern_Type, 100*n);
|
|
|
|
if (!self) {
|
|
|
|
Py_DECREF(code);
|
2000-03-31 10:59:30 -04:00
|
|
|
return NULL;
|
2000-07-03 15:44:21 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
PyObject *o = PySequence_Fast_GET_ITEM(code, i);
|
|
|
|
self->code[i] = (SRE_CODE) PyInt_AsLong(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_DECREF(code);
|
|
|
|
|
|
|
|
if (PyErr_Occurred())
|
|
|
|
return NULL;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
Py_INCREF(pattern);
|
|
|
|
self->pattern = pattern;
|
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
self->flags = flags;
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
self->groups = groups;
|
|
|
|
|
|
|
|
Py_XINCREF(groupindex);
|
|
|
|
self->groupindex = groupindex;
|
|
|
|
|
2000-07-02 19:25:39 -03:00
|
|
|
Py_XINCREF(indexgroup);
|
|
|
|
self->indexgroup = indexgroup;
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
return (PyObject*) self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
2000-06-29 05:58:44 -03:00
|
|
|
sre_codesize(PyObject* self, PyObject* args)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
|
|
|
return Py_BuildValue("i", sizeof(SRE_CODE));
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
if (!PyArg_ParseTuple(args, "ii", &character, &flags))
|
|
|
|
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 defined(HAVE_UNICODE)
|
|
|
|
if (flags & SRE_FLAG_UNICODE)
|
2000-06-29 09:48:37 -03:00
|
|
|
return Py_BuildValue("i", sre_lower_unicode(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-06-01 14:39:12 -03:00
|
|
|
LOCAL(PyObject*)
|
2000-06-29 08:34:28 -03:00
|
|
|
state_init(SRE_STATE* state, PatternObject* pattern, PyObject* args)
|
2000-06-01 14:39:12 -03:00
|
|
|
{
|
|
|
|
/* prepare state object */
|
|
|
|
|
|
|
|
PyBufferProcs *buffer;
|
|
|
|
int i, count;
|
|
|
|
void* ptr;
|
|
|
|
|
|
|
|
PyObject* string;
|
|
|
|
int start = 0;
|
|
|
|
int end = INT_MAX;
|
|
|
|
if (!PyArg_ParseTuple(args, "O|ii", &string, &start, &end))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* 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) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "expected read-only buffer");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* determine buffer size */
|
|
|
|
count = buffer->bf_getreadbuffer(string, 0, &ptr);
|
|
|
|
if (count < 0) {
|
|
|
|
/* sanity check */
|
|
|
|
PyErr_SetString(PyExc_TypeError, "buffer has negative size");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* determine character size */
|
2000-06-29 05:58:44 -03:00
|
|
|
#if defined(HAVE_UNICODE)
|
2000-06-01 14:39:12 -03:00
|
|
|
state->charsize = (PyUnicode_Check(string) ? sizeof(Py_UNICODE) : 1);
|
2000-06-29 05:58:44 -03:00
|
|
|
#else
|
|
|
|
state->charsize = 1;
|
|
|
|
#endif
|
2000-06-01 14:39:12 -03:00
|
|
|
|
|
|
|
count /= state->charsize;
|
|
|
|
|
|
|
|
/* adjust boundaries */
|
|
|
|
if (start < 0)
|
|
|
|
start = 0;
|
|
|
|
else if (start > count)
|
|
|
|
start = count;
|
|
|
|
|
|
|
|
if (end < 0)
|
|
|
|
end = 0;
|
|
|
|
else if (end > count)
|
|
|
|
end = count;
|
|
|
|
|
|
|
|
state->beginning = ptr;
|
|
|
|
|
|
|
|
state->start = (void*) ((char*) ptr + start * state->charsize);
|
|
|
|
state->end = (void*) ((char*) ptr + end * state->charsize);
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
state->lastmark = 0;
|
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
/* FIXME: dynamic! */
|
2000-06-29 13:57:40 -03:00
|
|
|
for (i = 0; i < SRE_MARK_SIZE; i++)
|
2000-06-01 14:39:12 -03:00
|
|
|
state->mark[i] = NULL;
|
|
|
|
|
2000-07-03 15:44:21 -03:00
|
|
|
state->lastindex = -1;
|
2000-07-02 14:33:27 -03:00
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
state->stack = NULL;
|
|
|
|
state->stackbase = 0;
|
|
|
|
state->stacksize = 0;
|
|
|
|
|
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
|
|
|
#if defined(HAVE_UNICODE)
|
|
|
|
else if (pattern->flags & SRE_FLAG_UNICODE)
|
2000-06-29 09:48:37 -03:00
|
|
|
state->lower = sre_lower_unicode;
|
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-06-01 14:39:12 -03:00
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
LOCAL(void)
|
|
|
|
state_fini(SRE_STATE* state)
|
|
|
|
{
|
|
|
|
stack_free(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCAL(PyObject*)
|
|
|
|
state_getslice(SRE_STATE* state, int index, PyObject* string)
|
|
|
|
{
|
|
|
|
index = (index - 1) * 2;
|
|
|
|
|
|
|
|
if (string == Py_None || !state->mark[index] || !state->mark[index+1]) {
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PySequence_GetSlice(
|
|
|
|
string,
|
|
|
|
((char*)state->mark[index] - (char*)state->beginning) /
|
|
|
|
state->charsize,
|
|
|
|
((char*)state->mark[index+1] - (char*)state->beginning) /
|
|
|
|
state->charsize
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
static PyObject*
|
2000-06-29 08:34:28 -03:00
|
|
|
pattern_new_match(PatternObject* pattern, SRE_STATE* state,
|
2000-03-31 10:59:30 -04:00
|
|
|
PyObject* string, int status)
|
|
|
|
{
|
|
|
|
/* create match object (from state object) */
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if (status > 0) {
|
|
|
|
|
|
|
|
/* create match object (with room for extra group marks) */
|
2000-07-03 15:44:21 -03:00
|
|
|
match = PyObject_NEW_VAR(MatchObject, &Match_Type,
|
|
|
|
2*(pattern->groups+1));
|
|
|
|
if (!match)
|
2000-03-31 10:59:30 -04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Py_INCREF(pattern);
|
|
|
|
match->pattern = pattern;
|
|
|
|
|
|
|
|
Py_INCREF(string);
|
|
|
|
match->string = string;
|
|
|
|
|
|
|
|
match->groups = pattern->groups+1;
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
base = (char*) state->beginning;
|
|
|
|
n = state->charsize;
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
/* group zero */
|
2000-06-29 05:58:44 -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
|
|
|
|
|
|
|
/* fill in the rest of the groups */
|
|
|
|
for (i = j = 0; i < pattern->groups; i++, j+=2)
|
2000-06-29 05:58:44 -03:00
|
|
|
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;
|
2000-03-31 10:59:30 -04:00
|
|
|
} else
|
|
|
|
match->mark[j+2] = match->mark[j+3] = -1; /* undefined */
|
|
|
|
|
2000-07-03 15:44:21 -03:00
|
|
|
match->lastindex = state->lastindex;
|
|
|
|
|
|
|
|
match->pos = ((char*) state->start - base) / n;
|
|
|
|
match->endpos = ((char*) state->end - base) / n;
|
2000-07-02 14:33:27 -03:00
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
return (PyObject*) match;
|
|
|
|
|
|
|
|
} else if (status < 0) {
|
|
|
|
|
|
|
|
/* internal error */
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_RuntimeError, "internal error in regular expression engine"
|
|
|
|
);
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
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-06-01 14:39:12 -03:00
|
|
|
/* create search state object */
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 13:57:40 -03:00
|
|
|
ScannerObject* self;
|
2000-06-01 14:39:12 -03:00
|
|
|
PyObject* string;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
/* create match object (with room for extra group marks) */
|
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-06-29 08:34:28 -03:00
|
|
|
string = state_init(&self->state, pattern, args);
|
2000-06-01 14:39:12 -03:00
|
|
|
if (!string) {
|
2000-07-03 15:44:21 -03: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-06-01 14:39:12 -03:00
|
|
|
Py_INCREF(string);
|
|
|
|
self->string = string;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-01 14:39:12 -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
|
|
|
{
|
|
|
|
Py_XDECREF(self->pattern);
|
|
|
|
Py_XDECREF(self->groupindex);
|
2000-07-03 15:44:21 -03:00
|
|
|
PyObject_DEL(self);
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-29 08:34:28 -03:00
|
|
|
pattern_match(PatternObject* self, PyObject* args)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
|
|
|
SRE_STATE state;
|
|
|
|
PyObject* string;
|
|
|
|
int status;
|
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
string = state_init(&state, self, args);
|
2000-03-31 10:59:30 -04:00
|
|
|
if (!string)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
state.ptr = state.start;
|
|
|
|
|
|
|
|
if (state.charsize == 1) {
|
|
|
|
status = sre_match(&state, PatternObject_GetCode(self));
|
|
|
|
} else {
|
2000-06-29 05:58:44 -03:00
|
|
|
#if defined(HAVE_UNICODE)
|
2000-03-31 10:59:30 -04:00
|
|
|
status = sre_umatch(&state, PatternObject_GetCode(self));
|
2000-06-29 05:58:44 -03:00
|
|
|
#endif
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
state_fini(&state);
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
return pattern_new_match(self, &state, string, status);
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-29 08:34:28 -03:00
|
|
|
pattern_search(PatternObject* self, PyObject* args)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
|
|
|
SRE_STATE state;
|
|
|
|
PyObject* string;
|
|
|
|
int status;
|
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
string = state_init(&state, self, args);
|
2000-03-31 10:59:30 -04:00
|
|
|
if (!string)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
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-03-31 10:59:30 -04:00
|
|
|
status = sre_usearch(&state, PatternObject_GetCode(self));
|
2000-06-29 05:58:44 -03:00
|
|
|
#endif
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
state_fini(&state);
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
return pattern_new_match(self, &state, string, status);
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-01 14:39:12 -03:00
|
|
|
call(char* function, PyObject* args)
|
|
|
|
{
|
|
|
|
PyObject* name;
|
|
|
|
PyObject* module;
|
|
|
|
PyObject* func;
|
|
|
|
PyObject* result;
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
name = PyString_FromString(MODULE);
|
2000-06-01 14:39:12 -03:00
|
|
|
if (!name)
|
|
|
|
return NULL;
|
|
|
|
module = PyImport_Import(name);
|
|
|
|
Py_DECREF(name);
|
|
|
|
if (!module)
|
|
|
|
return NULL;
|
|
|
|
func = PyObject_GetAttrString(module, function);
|
|
|
|
Py_DECREF(module);
|
|
|
|
if (!func)
|
|
|
|
return NULL;
|
|
|
|
result = PyObject_CallObject(func, args);
|
|
|
|
Py_DECREF(func);
|
|
|
|
Py_DECREF(args);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-29 08:34:28 -03:00
|
|
|
pattern_sub(PatternObject* self, PyObject* args)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
2000-06-01 14:39:12 -03:00
|
|
|
PyObject* template;
|
|
|
|
PyObject* string;
|
2000-07-05 18:14:16 -03:00
|
|
|
PyObject* count = Py_False; /* zero */
|
|
|
|
if (!PyArg_ParseTuple(args, "OO|O", &template, &string, &count))
|
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
|
|
|
/* delegate to Python code */
|
|
|
|
return call("_sub", Py_BuildValue("OOOO", self, template, string, count));
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-29 08:34:28 -03:00
|
|
|
pattern_subn(PatternObject* self, PyObject* args)
|
2000-06-01 14:39:12 -03:00
|
|
|
{
|
|
|
|
PyObject* template;
|
|
|
|
PyObject* string;
|
2000-07-05 18:14:16 -03:00
|
|
|
PyObject* count = Py_False; /* zero */
|
|
|
|
if (!PyArg_ParseTuple(args, "OO|O", &template, &string, &count))
|
2000-06-01 14:39:12 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* delegate to Python code */
|
|
|
|
return call("_subn", Py_BuildValue("OOOO", self, template, string, count));
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-29 08:34:28 -03:00
|
|
|
pattern_split(PatternObject* self, PyObject* args)
|
2000-06-01 14:39:12 -03:00
|
|
|
{
|
|
|
|
PyObject* string;
|
2000-07-05 18:14:16 -03:00
|
|
|
PyObject* maxsplit = Py_False; /* zero */
|
|
|
|
if (!PyArg_ParseTuple(args, "O|O", &string, &maxsplit))
|
2000-06-01 14:39:12 -03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* delegate to Python code */
|
|
|
|
return call("_split", Py_BuildValue("OOO", self, string, maxsplit));
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-29 08:34:28 -03:00
|
|
|
pattern_findall(PatternObject* self, PyObject* args)
|
2000-06-01 14:39:12 -03:00
|
|
|
{
|
2000-03-31 10:59:30 -04:00
|
|
|
SRE_STATE state;
|
|
|
|
PyObject* string;
|
|
|
|
PyObject* list;
|
|
|
|
int status;
|
2000-06-29 08:34:28 -03:00
|
|
|
int i;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
string = state_init(&state, self, args);
|
2000-03-31 10:59:30 -04:00
|
|
|
if (!string)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
list = PyList_New(0);
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
while (state.start <= state.end) {
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
PyObject* item;
|
|
|
|
|
|
|
|
state.ptr = state.start;
|
|
|
|
|
|
|
|
if (state.charsize == 1) {
|
2000-06-29 05:58:44 -03:00
|
|
|
status = sre_search(&state, PatternObject_GetCode(self));
|
2000-03-31 10:59:30 -04:00
|
|
|
} else {
|
2000-06-29 05:58:44 -03:00
|
|
|
#if defined(HAVE_UNICODE)
|
|
|
|
status = sre_usearch(&state, PatternObject_GetCode(self));
|
|
|
|
#endif
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
if (status > 0) {
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
/* don't bother to build a match object */
|
|
|
|
switch (self->groups) {
|
|
|
|
case 0:
|
|
|
|
item = PySequence_GetSlice(
|
|
|
|
string,
|
|
|
|
((char*) state.start - (char*) state.beginning) /
|
|
|
|
state.charsize,
|
|
|
|
((char*) state.ptr - (char*) state.beginning) /
|
|
|
|
state.charsize);
|
|
|
|
if (!item)
|
|
|
|
goto error;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
item = state_getslice(&state, 1, string);
|
|
|
|
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);
|
|
|
|
if (!o) {
|
|
|
|
Py_DECREF(item);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
PyTuple_SET_ITEM(item, i, o);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PyList_Append(list, item) < 0) {
|
|
|
|
Py_DECREF(item);
|
2000-06-29 05:58:44 -03:00
|
|
|
goto error;
|
2000-06-29 08:34:28 -03:00
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
if (state.ptr == state.start)
|
|
|
|
state.start = (void*) ((char*) state.ptr + state.charsize);
|
|
|
|
else
|
|
|
|
state.start = state.ptr;
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
if (status == 0)
|
|
|
|
break;
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
/* internal error */
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_RuntimeError,
|
|
|
|
"internal error in regular expression engine"
|
|
|
|
);
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
state_fini(&state);
|
2000-03-31 10:59:30 -04:00
|
|
|
return list;
|
|
|
|
|
|
|
|
error:
|
2000-06-29 08:34:28 -03:00
|
|
|
Py_DECREF(list);
|
|
|
|
state_fini(&state);
|
2000-03-31 10:59:30 -04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
static PyMethodDef pattern_methods[] = {
|
|
|
|
{"match", (PyCFunction) pattern_match, 1},
|
|
|
|
{"search", (PyCFunction) pattern_search, 1},
|
|
|
|
{"sub", (PyCFunction) pattern_sub, 1},
|
|
|
|
{"subn", (PyCFunction) pattern_subn, 1},
|
|
|
|
{"split", (PyCFunction) pattern_split, 1},
|
|
|
|
{"findall", (PyCFunction) pattern_findall, 1},
|
2000-06-01 14:39:12 -03:00
|
|
|
/* experimental */
|
2000-06-29 13:57:40 -03:00
|
|
|
{"scanner", (PyCFunction) pattern_scanner, 1},
|
2000-03-31 10:59:30 -04:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
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-06-29 08:34:28 -03:00
|
|
|
res = Py_FindMethod(pattern_methods, (PyObject*) self, name);
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
PyErr_Clear();
|
|
|
|
|
|
|
|
/* attributes */
|
|
|
|
if (!strcmp(name, "pattern")) {
|
|
|
|
Py_INCREF(self->pattern);
|
|
|
|
return self->pattern;
|
|
|
|
}
|
2000-06-01 14:39:12 -03:00
|
|
|
|
|
|
|
if (!strcmp(name, "flags"))
|
|
|
|
return Py_BuildValue("i", self->flags);
|
|
|
|
|
2000-06-29 21:27:46 -03:00
|
|
|
if (!strcmp(name, "groups"))
|
|
|
|
return Py_BuildValue("i", self->groups);
|
|
|
|
|
2000-06-01 14:39:12 -03:00
|
|
|
if (!strcmp(name, "groupindex") && self->groupindex) {
|
|
|
|
Py_INCREF(self->groupindex);
|
|
|
|
return self->groupindex;
|
|
|
|
}
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
statichere PyTypeObject Pattern_Type = {
|
|
|
|
PyObject_HEAD_INIT(NULL)
|
2000-07-03 15:44:21 -03:00
|
|
|
0, "SRE_Pattern",
|
|
|
|
sizeof(PatternObject), sizeof(SRE_CODE),
|
2000-06-29 08:34:28 -03:00
|
|
|
(destructor)pattern_dealloc, /*tp_dealloc*/
|
2000-03-31 10:59:30 -04:00
|
|
|
0, /*tp_print*/
|
2000-07-03 15:44:21 -03:00
|
|
|
(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
|
|
|
{
|
|
|
|
Py_XDECREF(self->string);
|
|
|
|
Py_DECREF(self->pattern);
|
2000-07-03 15:44:21 -03:00
|
|
|
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
|
|
|
{
|
|
|
|
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-07-03 15:44:21 -03:00
|
|
|
index *= 2;
|
|
|
|
|
|
|
|
if (self->string == Py_None || self->mark[index] < 0) {
|
2000-06-30 04:08:20 -03:00
|
|
|
/* return default value if the string or group is undefined */
|
|
|
|
Py_INCREF(def);
|
|
|
|
return def;
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return PySequence_GetSlice(
|
2000-07-03 15:44:21 -03:00
|
|
|
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
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (self->pattern->groupindex) {
|
|
|
|
index = PyObject_GetItem(self->pattern->groupindex, index);
|
|
|
|
if (index) {
|
|
|
|
if (PyInt_Check(index))
|
|
|
|
i = (int) PyInt_AS_LONG(index);
|
|
|
|
Py_DECREF(index);
|
|
|
|
} else
|
|
|
|
PyErr_Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-30 04:08:20 -03:00
|
|
|
return match_getslice_by_index(self, match_getindex(self, index), def);
|
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
|
|
|
{
|
|
|
|
PyObject* result;
|
|
|
|
int i, size;
|
|
|
|
|
|
|
|
size = PyTuple_GET_SIZE(args);
|
|
|
|
|
|
|
|
switch (size) {
|
|
|
|
case 0:
|
2000-06-30 04:08:20 -03:00
|
|
|
result = match_getslice(self, Py_False, Py_None);
|
2000-03-31 10:59:30 -04:00
|
|
|
break;
|
|
|
|
case 1:
|
2000-06-30 04:08:20 -03:00
|
|
|
result = match_getslice(self, PyTuple_GET_ITEM(args, 0), Py_None);
|
2000-03-31 10:59:30 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* fetch multiple items */
|
|
|
|
result = PyTuple_New(size);
|
|
|
|
if (!result)
|
|
|
|
return NULL;
|
|
|
|
for (i = 0; i < size; i++) {
|
2000-06-30 04:08:20 -03:00
|
|
|
PyObject* item = match_getslice(
|
|
|
|
self, PyTuple_GET_ITEM(args, i), Py_None
|
|
|
|
);
|
2000-03-31 10:59:30 -04:00
|
|
|
if (!item) {
|
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyTuple_SET_ITEM(result, i, item);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-29 08:34:28 -03:00
|
|
|
match_groups(MatchObject* self, PyObject* args)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
|
|
|
PyObject* result;
|
|
|
|
int index;
|
|
|
|
|
2000-06-30 04:08:20 -03:00
|
|
|
PyObject* def = Py_None;
|
|
|
|
if (!PyArg_ParseTuple(args, "|O", &def))
|
|
|
|
return NULL;
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
result = PyTuple_New(self->groups-1);
|
|
|
|
if (!result)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (index = 1; index < self->groups; index++) {
|
|
|
|
PyObject* item;
|
2000-06-30 04:08:20 -03:00
|
|
|
item = match_getslice_by_index(self, index, def);
|
2000-03-31 10:59:30 -04:00
|
|
|
if (!item) {
|
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyTuple_SET_ITEM(result, index-1, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-29 08:34:28 -03:00
|
|
|
match_groupdict(MatchObject* self, PyObject* args)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
|
|
|
PyObject* result;
|
|
|
|
PyObject* keys;
|
|
|
|
int index;
|
|
|
|
|
2000-06-30 04:08:20 -03:00
|
|
|
PyObject* def = Py_None;
|
|
|
|
if (!PyArg_ParseTuple(args, "|O", &def))
|
|
|
|
return NULL;
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
result = PyDict_New();
|
2000-06-30 04:08:20 -03:00
|
|
|
if (!result || !self->pattern->groupindex)
|
2000-03-31 10:59:30 -04:00
|
|
|
return result;
|
|
|
|
|
|
|
|
keys = PyMapping_Keys(self->pattern->groupindex);
|
2000-06-30 04:08:20 -03:00
|
|
|
if (!keys) {
|
|
|
|
Py_DECREF(result);
|
2000-03-31 10:59:30 -04:00
|
|
|
return NULL;
|
2000-06-30 04:08:20 -03:00
|
|
|
}
|
2000-03-31 10:59:30 -04:00
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
for (index = 0; index < PyList_GET_SIZE(keys); index++) {
|
2000-03-31 10:59:30 -04:00
|
|
|
PyObject* key;
|
|
|
|
PyObject* item;
|
2000-06-29 05:58:44 -03:00
|
|
|
key = PyList_GET_ITEM(keys, index);
|
2000-03-31 10:59:30 -04:00
|
|
|
if (!key) {
|
|
|
|
Py_DECREF(keys);
|
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-06-30 04:08:20 -03:00
|
|
|
item = match_getslice(self, key, def);
|
2000-03-31 10:59:30 -04:00
|
|
|
if (!item) {
|
|
|
|
Py_DECREF(key);
|
|
|
|
Py_DECREF(keys);
|
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* FIXME: <fl> this can fail, right? */
|
|
|
|
PyDict_SetItem(result, key, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_DECREF(keys);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05 18:14:16 -03:00
|
|
|
PyObject* index_ = Py_False; /* zero */
|
2000-06-29 05:58:44 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "|O", &index_))
|
2000-03-31 10:59:30 -04:00
|
|
|
return NULL;
|
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
index = match_getindex(self, index_);
|
2000-06-29 05:58:44 -03:00
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
if (index < 0 || index >= self->groups) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_IndexError,
|
|
|
|
"no such group"
|
|
|
|
);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->mark[index*2] < 0) {
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Py_BuildValue("i", self->mark[index*2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05 18:14:16 -03:00
|
|
|
PyObject* index_ = Py_False; /* zero */
|
2000-06-29 05:58:44 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "|O", &index_))
|
2000-03-31 10:59:30 -04:00
|
|
|
return NULL;
|
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
index = match_getindex(self, index_);
|
2000-06-29 05:58:44 -03:00
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
if (index < 0 || index >= self->groups) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_IndexError,
|
|
|
|
"no such group"
|
|
|
|
);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->mark[index*2] < 0) {
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Py_BuildValue("i", self->mark[index*2+1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05 18:14:16 -03:00
|
|
|
PyObject* index_ = Py_False; /* zero */
|
2000-06-29 05:58:44 -03:00
|
|
|
if (!PyArg_ParseTuple(args, "|O", &index_))
|
2000-03-31 10:59:30 -04:00
|
|
|
return NULL;
|
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
index = match_getindex(self, index_);
|
2000-06-29 05:58:44 -03:00
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
if (index < 0 || index >= self->groups) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_IndexError,
|
|
|
|
"no such group"
|
|
|
|
);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->mark[index*2] < 0) {
|
|
|
|
Py_INCREF(Py_None);
|
2000-06-01 14:39:12 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
return Py_BuildValue("OO", Py_None, Py_None);
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return Py_BuildValue("ii", self->mark[index*2], self->mark[index*2+1]);
|
|
|
|
}
|
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
static PyMethodDef match_methods[] = {
|
|
|
|
{"group", (PyCFunction) match_group, 1},
|
|
|
|
{"start", (PyCFunction) match_start, 1},
|
|
|
|
{"end", (PyCFunction) match_end, 1},
|
|
|
|
{"span", (PyCFunction) match_span, 1},
|
|
|
|
{"groups", (PyCFunction) match_groups, 1},
|
|
|
|
{"groupdict", (PyCFunction) match_groupdict, 1},
|
2000-03-31 10:59:30 -04:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-29 08:34:28 -03:00
|
|
|
match_getattr(MatchObject* self, char* name)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
|
|
|
PyObject* res;
|
|
|
|
|
2000-06-29 08:34:28 -03:00
|
|
|
res = Py_FindMethod(match_methods, (PyObject*) self, name);
|
2000-03-31 10:59:30 -04:00
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
PyErr_Clear();
|
|
|
|
|
2000-07-02 19:25:39 -03:00
|
|
|
if (!strcmp(name, "lastindex")) {
|
|
|
|
/* experimental */
|
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")) {
|
|
|
|
/* experimental */
|
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-03-31 10:59:30 -04:00
|
|
|
if (!strcmp(name, "string")) {
|
|
|
|
Py_INCREF(self->string);
|
|
|
|
return self->string;
|
|
|
|
}
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
if (!strcmp(name, "re")) {
|
|
|
|
Py_INCREF(self->pattern);
|
|
|
|
return (PyObject*) self->pattern;
|
|
|
|
}
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
if (!strcmp(name, "pos"))
|
2000-07-03 15:44:21 -03:00
|
|
|
return Py_BuildValue("i", self->pos);
|
2000-06-01 14:39:12 -03:00
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
if (!strcmp(name, "endpos"))
|
2000-07-03 15:44:21 -03:00
|
|
|
return Py_BuildValue("i", self->endpos);
|
2000-03-31 10:59:30 -04:00
|
|
|
|
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: implement setattr("string", None) as a special case (to
|
|
|
|
detach the associated string, if any */
|
|
|
|
|
|
|
|
statichere PyTypeObject Match_Type = {
|
|
|
|
PyObject_HEAD_INIT(NULL)
|
2000-06-29 13:57:40 -03:00
|
|
|
0, "SRE_Match",
|
2000-07-03 15:44:21 -03:00
|
|
|
sizeof(MatchObject), sizeof(int),
|
2000-06-29 08:34:28 -03:00
|
|
|
(destructor)match_dealloc, /*tp_dealloc*/
|
2000-03-31 10:59:30 -04:00
|
|
|
0, /*tp_print*/
|
2000-07-03 15:44:21 -03:00
|
|
|
(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-06-29 08:34:28 -03:00
|
|
|
state_fini(&self->state);
|
2000-06-01 14:39:12 -03:00
|
|
|
Py_DECREF(self->string);
|
|
|
|
Py_DECREF(self->pattern);
|
2000-07-03 15:44:21 -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;
|
|
|
|
|
|
|
|
state->ptr = state->start;
|
|
|
|
|
|
|
|
if (state->charsize == 1) {
|
|
|
|
status = sre_match(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_umatch(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-06-01 14:39:12 -03:00
|
|
|
state, self->string, status);
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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-06-01 14:39:12 -03:00
|
|
|
state, self->string, status);
|
|
|
|
|
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[] = {
|
|
|
|
{"match", (PyCFunction) scanner_match, 0},
|
|
|
|
{"search", (PyCFunction) scanner_search, 0},
|
2000-06-01 14:39:12 -03:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject*
|
2000-06-29 13:57:40 -03:00
|
|
|
scanner_getattr(ScannerObject* self, char* name)
|
2000-06-01 14:39:12 -03:00
|
|
|
{
|
|
|
|
PyObject* res;
|
|
|
|
|
2000-06-29 13:57:40 -03:00
|
|
|
res = Py_FindMethod(scanner_methods, (PyObject*) self, name);
|
2000-06-01 14:39:12 -03:00
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
PyErr_Clear();
|
|
|
|
|
|
|
|
/* attributes */
|
|
|
|
if (!strcmp(name, "pattern")) {
|
|
|
|
Py_INCREF(self->pattern);
|
|
|
|
return self->pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-06-29 13:57:40 -03:00
|
|
|
statichere PyTypeObject Scanner_Type = {
|
2000-06-01 14:39:12 -03:00
|
|
|
PyObject_HEAD_INIT(NULL)
|
2000-06-29 13:57:40 -03:00
|
|
|
0, "SRE_Scanner",
|
2000-07-03 15:44:21 -03:00
|
|
|
sizeof(ScannerObject), 0,
|
2000-06-29 13:57:40 -03:00
|
|
|
(destructor)scanner_dealloc, /*tp_dealloc*/
|
2000-06-01 14:39:12 -03:00
|
|
|
0, /*tp_print*/
|
2000-06-29 13:57:40 -03:00
|
|
|
(getattrfunc)scanner_getattr, /*tp_getattr*/
|
2000-06-01 14:39:12 -03:00
|
|
|
};
|
|
|
|
|
2000-03-31 10:59:30 -04:00
|
|
|
static PyMethodDef _functions[] = {
|
|
|
|
{"compile", _compile, 1},
|
2000-06-29 05:58:44 -03:00
|
|
|
{"getcodesize", sre_codesize, 1},
|
2000-06-29 09:48:37 -03:00
|
|
|
{"getlower", sre_getlower, 1},
|
2000-03-31 10:59:30 -04:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2000-06-29 05:58:44 -03:00
|
|
|
#if defined(WIN32)
|
2000-03-31 10:59:30 -04:00
|
|
|
__declspec(dllexport)
|
|
|
|
#endif
|
2000-07-21 03:00:07 -03:00
|
|
|
init_sre(void)
|
2000-03-31 10:59:30 -04:00
|
|
|
{
|
|
|
|
/* Patch object types */
|
2000-06-01 14:39:12 -03:00
|
|
|
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
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
Py_InitModule("_" MODULE, _functions);
|
2000-03-31 10:59:30 -04:00
|
|
|
}
|
|
|
|
|
2000-06-29 05:58:44 -03:00
|
|
|
#endif /* !defined(SRE_RECURSIVE) */
|