Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2 | * Secret Labs' Regular Expression Engine |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3 | * |
Fredrik Lundh | 6c68dc7 | 2000-06-29 10:34:56 +0000 | [diff] [blame] | 4 | * regular expression matching engine |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 5 | * |
| 6 | * partial history: |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 7 | * 1999-10-24 fl created (based on existing template matcher code) |
Fredrik Lundh | ebc37b2 | 2000-10-28 19:30:41 +0000 | [diff] [blame] | 8 | * 2000-03-06 fl first alpha, sort of |
Fredrik Lundh | ebc37b2 | 2000-10-28 19:30:41 +0000 | [diff] [blame] | 9 | * 2000-08-01 fl fixes for 1.6b1 |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 10 | * 2000-08-07 fl use PyOS_CheckStack() if available |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 11 | * 2000-09-20 fl added expand method |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 12 | * 2001-03-20 fl lots of fixes for 2.1b2 |
Fredrik Lundh | 9c7eab8 | 2001-04-15 19:00:58 +0000 | [diff] [blame] | 13 | * 2001-04-15 fl export copyright as Python attribute, not global |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 14 | * 2001-04-28 fl added __copy__ methods (work in progress) |
Fredrik Lundh | 09705f0 | 2002-11-22 12:46:35 +0000 | [diff] [blame] | 15 | * 2001-05-14 fl fixes for 1.5.2 compatibility |
Fredrik Lundh | f71ae46 | 2001-07-02 17:04:48 +0000 | [diff] [blame] | 16 | * 2001-07-01 fl added BIGCHARSET support (from Martin von Loewis) |
Fredrik Lundh | 397a654 | 2001-10-18 19:30:16 +0000 | [diff] [blame] | 17 | * 2001-10-18 fl fixed group reset issue (from Matthew Mueller) |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 18 | * 2001-10-20 fl added split primitive; reenable unicode for 1.6/2.0/2.1 |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 19 | * 2001-10-21 fl added sub/subn primitive |
Fredrik Lundh | 703ce81 | 2001-10-24 22:16:30 +0000 | [diff] [blame] | 20 | * 2001-10-24 fl added finditer primitive (for 2.2 only) |
Fredrik Lundh | 82b2307 | 2001-12-09 16:13:15 +0000 | [diff] [blame] | 21 | * 2001-12-07 fl fixed memory leak in sub/subn (Guido van Rossum) |
Fredrik Lundh | 09705f0 | 2002-11-22 12:46:35 +0000 | [diff] [blame] | 22 | * 2002-11-09 fl fixed empty sub/subn return type |
Martin v. Löwis | 78e2f06 | 2003-04-19 12:56:08 +0000 | [diff] [blame] | 23 | * 2003-04-18 mvl fully support 4-byte codes |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 24 | * 2003-10-17 gn implemented non recursive scheme |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 25 | * |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 26 | * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved. |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 27 | * |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 28 | * This version of the SRE library can be redistributed under CNRI's |
| 29 | * Python 1.6 license. For any other use, please contact Secret Labs |
| 30 | * AB (info@pythonware.com). |
| 31 | * |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 32 | * Portions of this engine have been developed in cooperation with |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 33 | * CNRI. Hewlett-Packard provided funding for 1.6 integration and |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 34 | * other compatibility work. |
| 35 | */ |
| 36 | |
| 37 | #ifndef SRE_RECURSIVE |
| 38 | |
Fredrik Lundh | 9c7eab8 | 2001-04-15 19:00:58 +0000 | [diff] [blame] | 39 | static char copyright[] = |
Fredrik Lundh | 09705f0 | 2002-11-22 12:46:35 +0000 | [diff] [blame] | 40 | " SRE 2.2.2 Copyright (c) 1997-2002 by Secret Labs AB "; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 41 | |
| 42 | #include "Python.h" |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 43 | #include "structmember.h" /* offsetof */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 44 | |
| 45 | #include "sre.h" |
| 46 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 47 | #include <ctype.h> |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 48 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 49 | /* name of this module, minus the leading underscore */ |
Fredrik Lundh | 1c5aa69 | 2001-01-16 07:37:30 +0000 | [diff] [blame] | 50 | #if !defined(SRE_MODULE) |
| 51 | #define SRE_MODULE "sre" |
| 52 | #endif |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 54 | /* defining this one enables tracing */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 55 | #undef VERBOSE |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 56 | |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 57 | #if PY_VERSION_HEX >= 0x01060000 |
| 58 | #if PY_VERSION_HEX < 0x02020000 || defined(Py_USING_UNICODE) |
Fredrik Lundh | 22d2546 | 2000-07-01 17:50:59 +0000 | [diff] [blame] | 59 | /* defining this enables unicode support (default under 1.6a1 and later) */ |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 60 | #define HAVE_UNICODE |
| 61 | #endif |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 62 | #endif |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 63 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 64 | /* -------------------------------------------------------------------- */ |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 65 | /* optional features */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 66 | |
Fredrik Lundh | 33accc1 | 2000-08-27 20:59:47 +0000 | [diff] [blame] | 67 | /* prevent run-away recursion (bad patterns on long strings) */ |
| 68 | |
Fredrik Lundh | 18c2aa2 | 2000-08-07 17:33:38 +0000 | [diff] [blame] | 69 | #if !defined(USE_STACKCHECK) |
Fredrik Lundh | 33accc1 | 2000-08-27 20:59:47 +0000 | [diff] [blame] | 70 | #if defined(MS_WIN64) || defined(__LP64__) || defined(_LP64) |
| 71 | /* require smaller recursion limit for a number of 64-bit platforms: |
| 72 | Win64 (MS_WIN64), Linux64 (__LP64__), Monterey (64-bit AIX) (_LP64) */ |
| 73 | /* FIXME: maybe the limit should be 40000 / sizeof(void*) ? */ |
| 74 | #define USE_RECURSION_LIMIT 7500 |
| 75 | #else |
Andrew MacIntyre | 1a44448 | 2003-06-09 08:22:11 +0000 | [diff] [blame] | 76 | |
| 77 | #if defined(__GNUC__) && defined(WITH_THREAD) && defined(__FreeBSD__) |
| 78 | /* the pthreads library on FreeBSD has a fixed 1MB stack size for the |
| 79 | * initial (or "primary") thread, which is insufficient for the default |
| 80 | * recursion limit. gcc 3.x at the default optimisation |
| 81 | * level (-O3) uses stack space more aggressively than gcc 2.95. |
Gustavo Niemeyer | c23fb77 | 2003-04-27 06:58:54 +0000 | [diff] [blame] | 82 | */ |
Andrew MacIntyre | 1a44448 | 2003-06-09 08:22:11 +0000 | [diff] [blame] | 83 | #if (__GNUC__ > 2) |
| 84 | #define USE_RECURSION_LIMIT 6500 |
| 85 | #else |
Gustavo Niemeyer | c23fb77 | 2003-04-27 06:58:54 +0000 | [diff] [blame] | 86 | #define USE_RECURSION_LIMIT 7500 |
Andrew MacIntyre | 1a44448 | 2003-06-09 08:22:11 +0000 | [diff] [blame] | 87 | #endif |
| 88 | |
Gustavo Niemeyer | c23fb77 | 2003-04-27 06:58:54 +0000 | [diff] [blame] | 89 | #else |
Fredrik Lundh | 33accc1 | 2000-08-27 20:59:47 +0000 | [diff] [blame] | 90 | #define USE_RECURSION_LIMIT 10000 |
| 91 | #endif |
Fredrik Lundh | 18c2aa2 | 2000-08-07 17:33:38 +0000 | [diff] [blame] | 92 | #endif |
Gustavo Niemeyer | c23fb77 | 2003-04-27 06:58:54 +0000 | [diff] [blame] | 93 | #endif |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 94 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 95 | /* enables usage of recursive scheme */ |
| 96 | #undef USE_RECURSION |
| 97 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 98 | /* enables fast searching */ |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 99 | #define USE_FAST_SEARCH |
| 100 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 101 | /* enables aggressive inlining (always on for Visual C) */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 102 | #undef USE_INLINE |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 103 | |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 104 | /* enables copy/deepcopy handling (work in progress) */ |
| 105 | #undef USE_BUILTIN_COPY |
| 106 | |
Fredrik Lundh | 1c5aa69 | 2001-01-16 07:37:30 +0000 | [diff] [blame] | 107 | #if PY_VERSION_HEX < 0x01060000 |
| 108 | #define PyObject_DEL(op) PyMem_DEL((op)) |
| 109 | #endif |
| 110 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 111 | /* -------------------------------------------------------------------- */ |
| 112 | |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 113 | #if defined(_MSC_VER) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 114 | #pragma optimize("agtw", on) /* doesn't seem to make much difference... */ |
Fredrik Lundh | 2855290 | 2000-07-05 21:14:16 +0000 | [diff] [blame] | 115 | #pragma warning(disable: 4710) /* who cares if functions are not inlined ;-) */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 116 | /* fastest possible local call under MSVC */ |
| 117 | #define LOCAL(type) static __inline type __fastcall |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 118 | #elif defined(USE_INLINE) |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 119 | #define LOCAL(type) static inline type |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 120 | #else |
| 121 | #define LOCAL(type) static type |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 122 | #endif |
| 123 | |
| 124 | /* error codes */ |
| 125 | #define SRE_ERROR_ILLEGAL -1 /* illegal opcode */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 126 | #define SRE_ERROR_STATE -2 /* illegal state */ |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 127 | #define SRE_ERROR_RECURSION_LIMIT -3 /* runaway recursion */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 128 | #define SRE_ERROR_MEMORY -9 /* out of memory */ |
| 129 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 130 | #if defined(VERBOSE) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 131 | #define TRACE(v) printf v |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 132 | #else |
| 133 | #define TRACE(v) |
| 134 | #endif |
| 135 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 136 | /* -------------------------------------------------------------------- */ |
| 137 | /* search engine state */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 138 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 139 | /* default character predicates (run sre_chars.py to regenerate tables) */ |
| 140 | |
| 141 | #define SRE_DIGIT_MASK 1 |
| 142 | #define SRE_SPACE_MASK 2 |
| 143 | #define SRE_LINEBREAK_MASK 4 |
| 144 | #define SRE_ALNUM_MASK 8 |
| 145 | #define SRE_WORD_MASK 16 |
| 146 | |
Fredrik Lundh | 21009b9 | 2001-09-18 18:47:09 +0000 | [diff] [blame] | 147 | /* FIXME: this assumes ASCII. create tables in init_sre() instead */ |
| 148 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 149 | static char sre_char_info[128] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 6, 2, |
| 150 | 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, |
| 151 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 25, 25, 25, 25, 25, 25, 25, |
| 152 | 25, 25, 0, 0, 0, 0, 0, 0, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, |
| 153 | 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 0, 0, |
| 154 | 0, 0, 16, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, |
| 155 | 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 0, 0, 0, 0, 0 }; |
| 156 | |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 157 | static char sre_char_lower[128] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 158 | 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, |
| 159 | 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, |
| 160 | 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, |
| 161 | 61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, |
| 162 | 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, |
| 163 | 122, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, |
| 164 | 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, |
| 165 | 120, 121, 122, 123, 124, 125, 126, 127 }; |
| 166 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 167 | #define SRE_IS_DIGIT(ch)\ |
| 168 | ((ch) < 128 ? (sre_char_info[(ch)] & SRE_DIGIT_MASK) : 0) |
| 169 | #define SRE_IS_SPACE(ch)\ |
| 170 | ((ch) < 128 ? (sre_char_info[(ch)] & SRE_SPACE_MASK) : 0) |
| 171 | #define SRE_IS_LINEBREAK(ch)\ |
| 172 | ((ch) < 128 ? (sre_char_info[(ch)] & SRE_LINEBREAK_MASK) : 0) |
| 173 | #define SRE_IS_ALNUM(ch)\ |
| 174 | ((ch) < 128 ? (sre_char_info[(ch)] & SRE_ALNUM_MASK) : 0) |
| 175 | #define SRE_IS_WORD(ch)\ |
| 176 | ((ch) < 128 ? (sre_char_info[(ch)] & SRE_WORD_MASK) : 0) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 177 | |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 178 | static unsigned int sre_lower(unsigned int ch) |
| 179 | { |
| 180 | return ((ch) < 128 ? sre_char_lower[ch] : ch); |
| 181 | } |
| 182 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 183 | /* locale-specific character predicates */ |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 184 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 185 | #define SRE_LOC_IS_DIGIT(ch) ((ch) < 256 ? isdigit((ch)) : 0) |
| 186 | #define SRE_LOC_IS_SPACE(ch) ((ch) < 256 ? isspace((ch)) : 0) |
| 187 | #define SRE_LOC_IS_LINEBREAK(ch) ((ch) == '\n') |
| 188 | #define SRE_LOC_IS_ALNUM(ch) ((ch) < 256 ? isalnum((ch)) : 0) |
| 189 | #define SRE_LOC_IS_WORD(ch) (SRE_LOC_IS_ALNUM((ch)) || (ch) == '_') |
| 190 | |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 191 | static unsigned int sre_lower_locale(unsigned int ch) |
| 192 | { |
| 193 | return ((ch) < 256 ? tolower((ch)) : ch); |
| 194 | } |
| 195 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 196 | /* unicode-specific character predicates */ |
| 197 | |
| 198 | #if defined(HAVE_UNICODE) |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 199 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 200 | #define SRE_UNI_IS_DIGIT(ch) Py_UNICODE_ISDIGIT((Py_UNICODE)(ch)) |
| 201 | #define SRE_UNI_IS_SPACE(ch) Py_UNICODE_ISSPACE((Py_UNICODE)(ch)) |
| 202 | #define SRE_UNI_IS_LINEBREAK(ch) Py_UNICODE_ISLINEBREAK((Py_UNICODE)(ch)) |
Fredrik Lundh | 22d2546 | 2000-07-01 17:50:59 +0000 | [diff] [blame] | 203 | #define SRE_UNI_IS_ALNUM(ch) Py_UNICODE_ISALNUM((Py_UNICODE)(ch)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 204 | #define SRE_UNI_IS_WORD(ch) (SRE_UNI_IS_ALNUM((ch)) || (ch) == '_') |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 205 | |
| 206 | static unsigned int sre_lower_unicode(unsigned int ch) |
| 207 | { |
| 208 | return (unsigned int) Py_UNICODE_TOLOWER((Py_UNICODE)(ch)); |
| 209 | } |
| 210 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 211 | #endif |
| 212 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 213 | LOCAL(int) |
| 214 | sre_category(SRE_CODE category, unsigned int ch) |
| 215 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 216 | switch (category) { |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 217 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 218 | case SRE_CATEGORY_DIGIT: |
| 219 | return SRE_IS_DIGIT(ch); |
| 220 | case SRE_CATEGORY_NOT_DIGIT: |
| 221 | return !SRE_IS_DIGIT(ch); |
| 222 | case SRE_CATEGORY_SPACE: |
| 223 | return SRE_IS_SPACE(ch); |
| 224 | case SRE_CATEGORY_NOT_SPACE: |
| 225 | return !SRE_IS_SPACE(ch); |
| 226 | case SRE_CATEGORY_WORD: |
| 227 | return SRE_IS_WORD(ch); |
| 228 | case SRE_CATEGORY_NOT_WORD: |
| 229 | return !SRE_IS_WORD(ch); |
| 230 | case SRE_CATEGORY_LINEBREAK: |
| 231 | return SRE_IS_LINEBREAK(ch); |
| 232 | case SRE_CATEGORY_NOT_LINEBREAK: |
| 233 | return !SRE_IS_LINEBREAK(ch); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 234 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 235 | case SRE_CATEGORY_LOC_WORD: |
| 236 | return SRE_LOC_IS_WORD(ch); |
| 237 | case SRE_CATEGORY_LOC_NOT_WORD: |
| 238 | return !SRE_LOC_IS_WORD(ch); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 239 | |
| 240 | #if defined(HAVE_UNICODE) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 241 | case SRE_CATEGORY_UNI_DIGIT: |
| 242 | return SRE_UNI_IS_DIGIT(ch); |
| 243 | case SRE_CATEGORY_UNI_NOT_DIGIT: |
| 244 | return !SRE_UNI_IS_DIGIT(ch); |
| 245 | case SRE_CATEGORY_UNI_SPACE: |
| 246 | return SRE_UNI_IS_SPACE(ch); |
| 247 | case SRE_CATEGORY_UNI_NOT_SPACE: |
| 248 | return !SRE_UNI_IS_SPACE(ch); |
| 249 | case SRE_CATEGORY_UNI_WORD: |
| 250 | return SRE_UNI_IS_WORD(ch); |
| 251 | case SRE_CATEGORY_UNI_NOT_WORD: |
| 252 | return !SRE_UNI_IS_WORD(ch); |
| 253 | case SRE_CATEGORY_UNI_LINEBREAK: |
| 254 | return SRE_UNI_IS_LINEBREAK(ch); |
| 255 | case SRE_CATEGORY_UNI_NOT_LINEBREAK: |
| 256 | return !SRE_UNI_IS_LINEBREAK(ch); |
Fredrik Lundh | 1c5aa69 | 2001-01-16 07:37:30 +0000 | [diff] [blame] | 257 | #else |
| 258 | case SRE_CATEGORY_UNI_DIGIT: |
| 259 | return SRE_IS_DIGIT(ch); |
| 260 | case SRE_CATEGORY_UNI_NOT_DIGIT: |
| 261 | return !SRE_IS_DIGIT(ch); |
| 262 | case SRE_CATEGORY_UNI_SPACE: |
| 263 | return SRE_IS_SPACE(ch); |
| 264 | case SRE_CATEGORY_UNI_NOT_SPACE: |
| 265 | return !SRE_IS_SPACE(ch); |
| 266 | case SRE_CATEGORY_UNI_WORD: |
| 267 | return SRE_LOC_IS_WORD(ch); |
| 268 | case SRE_CATEGORY_UNI_NOT_WORD: |
| 269 | return !SRE_LOC_IS_WORD(ch); |
| 270 | case SRE_CATEGORY_UNI_LINEBREAK: |
| 271 | return SRE_IS_LINEBREAK(ch); |
| 272 | case SRE_CATEGORY_UNI_NOT_LINEBREAK: |
| 273 | return !SRE_IS_LINEBREAK(ch); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 274 | #endif |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 275 | } |
| 276 | return 0; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* helpers */ |
| 280 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 281 | static void |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 282 | data_stack_dealloc(SRE_STATE* state) |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 283 | { |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 284 | if (state->data_stack) { |
| 285 | free(state->data_stack); |
| 286 | state->data_stack = NULL; |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 287 | } |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 288 | state->data_stack_size = state->data_stack_base = 0; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static int |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 292 | data_stack_grow(SRE_STATE* state, int size) |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 293 | { |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 294 | int minsize, cursize; |
| 295 | minsize = state->data_stack_base+size; |
| 296 | cursize = state->data_stack_size; |
| 297 | if (cursize < minsize) { |
| 298 | void* stack; |
| 299 | cursize = minsize+minsize/4+1024; |
| 300 | TRACE(("allocate/grow stack %d\n", cursize)); |
| 301 | stack = realloc(state->data_stack, cursize); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 302 | if (!stack) { |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 303 | data_stack_dealloc(state); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 304 | return SRE_ERROR_MEMORY; |
| 305 | } |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 306 | state->data_stack = stack; |
| 307 | state->data_stack_size = cursize; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 308 | } |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 309 | return 0; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 312 | /* generate 8-bit version */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 313 | |
| 314 | #define SRE_CHAR unsigned char |
| 315 | #define SRE_AT sre_at |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 316 | #define SRE_COUNT sre_count |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 317 | #define SRE_CHARSET sre_charset |
| 318 | #define SRE_INFO sre_info |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 319 | #define SRE_MATCH sre_match |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 320 | #define SRE_MATCH_CONTEXT sre_match_context |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 321 | #define SRE_SEARCH sre_search |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 322 | #define SRE_LITERAL_TEMPLATE sre_literal_template |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 323 | |
| 324 | #if defined(HAVE_UNICODE) |
| 325 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 326 | #define SRE_RECURSIVE |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 327 | #include "_sre.c" |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 328 | #undef SRE_RECURSIVE |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 329 | |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 330 | #undef SRE_LITERAL_TEMPLATE |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 331 | #undef SRE_SEARCH |
| 332 | #undef SRE_MATCH |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 333 | #undef SRE_MATCH_CONTEXT |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 334 | #undef SRE_INFO |
| 335 | #undef SRE_CHARSET |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 336 | #undef SRE_COUNT |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 337 | #undef SRE_AT |
| 338 | #undef SRE_CHAR |
| 339 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 340 | /* generate 16-bit unicode version */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 341 | |
| 342 | #define SRE_CHAR Py_UNICODE |
| 343 | #define SRE_AT sre_uat |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 344 | #define SRE_COUNT sre_ucount |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 345 | #define SRE_CHARSET sre_ucharset |
| 346 | #define SRE_INFO sre_uinfo |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 347 | #define SRE_MATCH sre_umatch |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 348 | #define SRE_MATCH_CONTEXT sre_umatch_context |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 349 | #define SRE_SEARCH sre_usearch |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 350 | #define SRE_LITERAL_TEMPLATE sre_uliteral_template |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 351 | #endif |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 352 | |
| 353 | #endif /* SRE_RECURSIVE */ |
| 354 | |
| 355 | /* -------------------------------------------------------------------- */ |
| 356 | /* String matching engine */ |
| 357 | |
| 358 | /* the following section is compiled twice, with different character |
| 359 | settings */ |
| 360 | |
| 361 | LOCAL(int) |
| 362 | SRE_AT(SRE_STATE* state, SRE_CHAR* ptr, SRE_CODE at) |
| 363 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 364 | /* check if pointer is at given position */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 365 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 366 | int this, that; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 367 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 368 | switch (at) { |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 369 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 370 | case SRE_AT_BEGINNING: |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 371 | case SRE_AT_BEGINNING_STRING: |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 372 | return ((void*) ptr == state->beginning); |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 373 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 374 | case SRE_AT_BEGINNING_LINE: |
| 375 | return ((void*) ptr == state->beginning || |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 376 | SRE_IS_LINEBREAK((int) ptr[-1])); |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 377 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 378 | case SRE_AT_END: |
Fredrik Lundh | ef34bd2 | 2000-06-30 21:40:20 +0000 | [diff] [blame] | 379 | return (((void*) (ptr+1) == state->end && |
| 380 | SRE_IS_LINEBREAK((int) ptr[0])) || |
| 381 | ((void*) ptr == state->end)); |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 382 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 383 | case SRE_AT_END_LINE: |
| 384 | return ((void*) ptr == state->end || |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 385 | SRE_IS_LINEBREAK((int) ptr[0])); |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 386 | |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 387 | case SRE_AT_END_STRING: |
| 388 | return ((void*) ptr == state->end); |
| 389 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 390 | case SRE_AT_BOUNDARY: |
| 391 | if (state->beginning == state->end) |
| 392 | return 0; |
| 393 | that = ((void*) ptr > state->beginning) ? |
| 394 | SRE_IS_WORD((int) ptr[-1]) : 0; |
| 395 | this = ((void*) ptr < state->end) ? |
| 396 | SRE_IS_WORD((int) ptr[0]) : 0; |
| 397 | return this != that; |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 398 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 399 | case SRE_AT_NON_BOUNDARY: |
| 400 | if (state->beginning == state->end) |
| 401 | return 0; |
| 402 | that = ((void*) ptr > state->beginning) ? |
| 403 | SRE_IS_WORD((int) ptr[-1]) : 0; |
| 404 | this = ((void*) ptr < state->end) ? |
| 405 | SRE_IS_WORD((int) ptr[0]) : 0; |
| 406 | return this == that; |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 407 | |
| 408 | case SRE_AT_LOC_BOUNDARY: |
| 409 | if (state->beginning == state->end) |
| 410 | return 0; |
| 411 | that = ((void*) ptr > state->beginning) ? |
| 412 | SRE_LOC_IS_WORD((int) ptr[-1]) : 0; |
| 413 | this = ((void*) ptr < state->end) ? |
| 414 | SRE_LOC_IS_WORD((int) ptr[0]) : 0; |
| 415 | return this != that; |
| 416 | |
| 417 | case SRE_AT_LOC_NON_BOUNDARY: |
| 418 | if (state->beginning == state->end) |
| 419 | return 0; |
| 420 | that = ((void*) ptr > state->beginning) ? |
| 421 | SRE_LOC_IS_WORD((int) ptr[-1]) : 0; |
| 422 | this = ((void*) ptr < state->end) ? |
| 423 | SRE_LOC_IS_WORD((int) ptr[0]) : 0; |
| 424 | return this == that; |
| 425 | |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 426 | #if defined(HAVE_UNICODE) |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 427 | case SRE_AT_UNI_BOUNDARY: |
| 428 | if (state->beginning == state->end) |
| 429 | return 0; |
| 430 | that = ((void*) ptr > state->beginning) ? |
| 431 | SRE_UNI_IS_WORD((int) ptr[-1]) : 0; |
| 432 | this = ((void*) ptr < state->end) ? |
| 433 | SRE_UNI_IS_WORD((int) ptr[0]) : 0; |
| 434 | return this != that; |
| 435 | |
| 436 | case SRE_AT_UNI_NON_BOUNDARY: |
| 437 | if (state->beginning == state->end) |
| 438 | return 0; |
| 439 | that = ((void*) ptr > state->beginning) ? |
| 440 | SRE_UNI_IS_WORD((int) ptr[-1]) : 0; |
| 441 | this = ((void*) ptr < state->end) ? |
| 442 | SRE_UNI_IS_WORD((int) ptr[0]) : 0; |
| 443 | return this == that; |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 444 | #endif |
| 445 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 446 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 447 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 448 | return 0; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | LOCAL(int) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 452 | SRE_CHARSET(SRE_CODE* set, SRE_CODE ch) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 453 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 454 | /* check if character is a member of the given set */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 455 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 456 | int ok = 1; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 457 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 458 | for (;;) { |
| 459 | switch (*set++) { |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 460 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 461 | case SRE_OP_FAILURE: |
| 462 | return !ok; |
| 463 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 464 | case SRE_OP_LITERAL: |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 465 | /* <LITERAL> <code> */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 466 | if (ch == set[0]) |
| 467 | return ok; |
| 468 | set++; |
| 469 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 470 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 471 | case SRE_OP_CATEGORY: |
| 472 | /* <CATEGORY> <code> */ |
| 473 | if (sre_category(set[0], (int) ch)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 474 | return ok; |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 475 | set += 1; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 476 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 477 | |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 478 | case SRE_OP_CHARSET: |
Martin v. Löwis | 78e2f06 | 2003-04-19 12:56:08 +0000 | [diff] [blame] | 479 | if (sizeof(SRE_CODE) == 2) { |
| 480 | /* <CHARSET> <bitmap> (16 bits per code word) */ |
| 481 | if (ch < 256 && (set[ch >> 4] & (1 << (ch & 15)))) |
| 482 | return ok; |
| 483 | set += 16; |
| 484 | } |
| 485 | else { |
| 486 | /* <CHARSET> <bitmap> (32 bits per code word) */ |
| 487 | if (ch < 256 && (set[ch >> 5] & (1 << (ch & 31)))) |
| 488 | return ok; |
| 489 | set += 8; |
| 490 | } |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 491 | break; |
| 492 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 493 | case SRE_OP_RANGE: |
| 494 | /* <RANGE> <lower> <upper> */ |
| 495 | if (set[0] <= ch && ch <= set[1]) |
| 496 | return ok; |
| 497 | set += 2; |
| 498 | break; |
| 499 | |
| 500 | case SRE_OP_NEGATE: |
| 501 | ok = !ok; |
| 502 | break; |
| 503 | |
Fredrik Lundh | f71ae46 | 2001-07-02 17:04:48 +0000 | [diff] [blame] | 504 | case SRE_OP_BIGCHARSET: |
| 505 | /* <BIGCHARSET> <blockcount> <256 blockindices> <blocks> */ |
| 506 | { |
| 507 | int count, block; |
| 508 | count = *(set++); |
Martin v. Löwis | 78e2f06 | 2003-04-19 12:56:08 +0000 | [diff] [blame] | 509 | |
| 510 | if (sizeof(SRE_CODE) == 2) { |
| 511 | block = ((unsigned char*)set)[ch >> 8]; |
| 512 | set += 128; |
| 513 | if (set[block*16 + ((ch & 255)>>4)] & (1 << (ch & 15))) |
| 514 | return ok; |
| 515 | set += count*16; |
| 516 | } |
| 517 | else { |
| 518 | if (ch < 65536) |
| 519 | block = ((unsigned char*)set)[ch >> 8]; |
| 520 | else |
| 521 | block = -1; |
| 522 | set += 64; |
| 523 | if (block >=0 && |
| 524 | (set[block*8 + ((ch & 255)>>5)] & (1 << (ch & 31)))) |
| 525 | return ok; |
| 526 | set += count*8; |
| 527 | } |
Fredrik Lundh | f71ae46 | 2001-07-02 17:04:48 +0000 | [diff] [blame] | 528 | break; |
| 529 | } |
Fredrik Lundh | 19af43d | 2001-07-02 16:58:38 +0000 | [diff] [blame] | 530 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 531 | default: |
| 532 | /* internal error -- there's not much we can do about it |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 533 | here, so let's just pretend it didn't match... */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 534 | return 0; |
| 535 | } |
| 536 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 539 | LOCAL(int) SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level); |
| 540 | |
| 541 | LOCAL(int) |
| 542 | SRE_COUNT(SRE_STATE* state, SRE_CODE* pattern, int maxcount, int level) |
| 543 | { |
| 544 | SRE_CODE chr; |
| 545 | SRE_CHAR* ptr = state->ptr; |
| 546 | SRE_CHAR* end = state->end; |
| 547 | int i; |
| 548 | |
| 549 | /* adjust end */ |
| 550 | if (maxcount < end - ptr && maxcount != 65535) |
| 551 | end = ptr + maxcount; |
| 552 | |
| 553 | switch (pattern[0]) { |
| 554 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 555 | case SRE_OP_IN: |
| 556 | /* repeated set */ |
| 557 | TRACE(("|%p|%p|COUNT IN\n", pattern, ptr)); |
| 558 | while (ptr < end && SRE_CHARSET(pattern + 2, *ptr)) |
| 559 | ptr++; |
| 560 | break; |
| 561 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 562 | case SRE_OP_ANY: |
| 563 | /* repeated dot wildcard. */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 564 | TRACE(("|%p|%p|COUNT ANY\n", pattern, ptr)); |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 565 | while (ptr < end && !SRE_IS_LINEBREAK(*ptr)) |
| 566 | ptr++; |
| 567 | break; |
| 568 | |
| 569 | case SRE_OP_ANY_ALL: |
| 570 | /* repeated dot wildcare. skip to the end of the target |
| 571 | string, and backtrack from there */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 572 | TRACE(("|%p|%p|COUNT ANY_ALL\n", pattern, ptr)); |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 573 | ptr = end; |
| 574 | break; |
| 575 | |
| 576 | case SRE_OP_LITERAL: |
| 577 | /* repeated literal */ |
| 578 | chr = pattern[1]; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 579 | TRACE(("|%p|%p|COUNT LITERAL %d\n", pattern, ptr, chr)); |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 580 | while (ptr < end && (SRE_CODE) *ptr == chr) |
| 581 | ptr++; |
| 582 | break; |
| 583 | |
| 584 | case SRE_OP_LITERAL_IGNORE: |
| 585 | /* repeated literal */ |
| 586 | chr = pattern[1]; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 587 | TRACE(("|%p|%p|COUNT LITERAL_IGNORE %d\n", pattern, ptr, chr)); |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 588 | while (ptr < end && (SRE_CODE) state->lower(*ptr) == chr) |
| 589 | ptr++; |
| 590 | break; |
| 591 | |
| 592 | case SRE_OP_NOT_LITERAL: |
| 593 | /* repeated non-literal */ |
| 594 | chr = pattern[1]; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 595 | TRACE(("|%p|%p|COUNT NOT_LITERAL %d\n", pattern, ptr, chr)); |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 596 | while (ptr < end && (SRE_CODE) *ptr != chr) |
| 597 | ptr++; |
| 598 | break; |
| 599 | |
| 600 | case SRE_OP_NOT_LITERAL_IGNORE: |
| 601 | /* repeated non-literal */ |
| 602 | chr = pattern[1]; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 603 | TRACE(("|%p|%p|COUNT NOT_LITERAL_IGNORE %d\n", pattern, ptr, chr)); |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 604 | while (ptr < end && (SRE_CODE) state->lower(*ptr) != chr) |
| 605 | ptr++; |
| 606 | break; |
| 607 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 608 | default: |
| 609 | /* repeated single character pattern */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 610 | TRACE(("|%p|%p|COUNT SUBPATTERN\n", pattern, ptr)); |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 611 | while ((SRE_CHAR*) state->ptr < end) { |
| 612 | i = SRE_MATCH(state, pattern, level); |
| 613 | if (i < 0) |
| 614 | return i; |
| 615 | if (!i) |
| 616 | break; |
| 617 | } |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 618 | TRACE(("|%p|%p|COUNT %d\n", pattern, ptr, |
| 619 | (SRE_CHAR*) state->ptr - ptr)); |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 620 | return (SRE_CHAR*) state->ptr - ptr; |
| 621 | } |
| 622 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 623 | TRACE(("|%p|%p|COUNT %d\n", pattern, ptr, ptr - (SRE_CHAR*) state->ptr)); |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 624 | return ptr - (SRE_CHAR*) state->ptr; |
| 625 | } |
| 626 | |
Fredrik Lundh | 33accc1 | 2000-08-27 20:59:47 +0000 | [diff] [blame] | 627 | #if 0 /* not used in this release */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 628 | LOCAL(int) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 629 | SRE_INFO(SRE_STATE* state, SRE_CODE* pattern) |
| 630 | { |
| 631 | /* check if an SRE_OP_INFO block matches at the current position. |
| 632 | returns the number of SRE_CODE objects to skip if successful, 0 |
| 633 | if no match */ |
| 634 | |
| 635 | SRE_CHAR* end = state->end; |
| 636 | SRE_CHAR* ptr = state->ptr; |
| 637 | int i; |
| 638 | |
| 639 | /* check minimal length */ |
| 640 | if (pattern[3] && (end - ptr) < pattern[3]) |
| 641 | return 0; |
| 642 | |
| 643 | /* check known prefix */ |
| 644 | if (pattern[2] & SRE_INFO_PREFIX && pattern[5] > 1) { |
| 645 | /* <length> <skip> <prefix data> <overlap data> */ |
| 646 | for (i = 0; i < pattern[5]; i++) |
| 647 | if ((SRE_CODE) ptr[i] != pattern[7 + i]) |
| 648 | return 0; |
| 649 | return pattern[0] + 2 * pattern[6]; |
| 650 | } |
| 651 | return pattern[0]; |
| 652 | } |
Fredrik Lundh | 33accc1 | 2000-08-27 20:59:47 +0000 | [diff] [blame] | 653 | #endif |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 654 | |
Gustavo Niemeyer | caf1c9d | 2003-04-27 14:42:54 +0000 | [diff] [blame] | 655 | /* The macros below should be used to protect recursive SRE_MATCH() |
| 656 | * calls that *failed* and do *not* return immediately (IOW, those |
| 657 | * that will backtrack). Explaining: |
| 658 | * |
| 659 | * - Recursive SRE_MATCH() returned true: that's usually a success |
| 660 | * (besides atypical cases like ASSERT_NOT), therefore there's no |
| 661 | * reason to restore lastmark; |
| 662 | * |
| 663 | * - Recursive SRE_MATCH() returned false but the current SRE_MATCH() |
| 664 | * is returning to the caller: If the current SRE_MATCH() is the |
| 665 | * top function of the recursion, returning false will be a matching |
| 666 | * failure, and it doesn't matter where lastmark is pointing to. |
| 667 | * If it's *not* the top function, it will be a recursive SRE_MATCH() |
| 668 | * failure by itself, and the calling SRE_MATCH() will have to deal |
| 669 | * with the failure by the same rules explained here (it will restore |
| 670 | * lastmark by itself if necessary); |
| 671 | * |
| 672 | * - Recursive SRE_MATCH() returned false, and will continue the |
| 673 | * outside 'for' loop: must be protected when breaking, since the next |
| 674 | * OP could potentially depend on lastmark; |
| 675 | * |
| 676 | * - Recursive SRE_MATCH() returned false, and will be called again |
| 677 | * inside a local for/while loop: must be protected between each |
| 678 | * loop iteration, since the recursive SRE_MATCH() could do anything, |
| 679 | * and could potentially depend on lastmark. |
| 680 | * |
| 681 | * For more information, check the discussion at SF patch #712900. |
| 682 | */ |
Gustavo Niemeyer | be733ee | 2003-04-20 07:35:44 +0000 | [diff] [blame] | 683 | #define LASTMARK_SAVE() \ |
| 684 | do { \ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 685 | ctx->lastmark = state->lastmark; \ |
| 686 | ctx->lastindex = state->lastindex; \ |
Gustavo Niemeyer | be733ee | 2003-04-20 07:35:44 +0000 | [diff] [blame] | 687 | } while (0) |
| 688 | #define LASTMARK_RESTORE() \ |
| 689 | do { \ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 690 | state->lastmark = ctx->lastmark; \ |
| 691 | state->lastindex = ctx->lastindex; \ |
Gustavo Niemeyer | be733ee | 2003-04-20 07:35:44 +0000 | [diff] [blame] | 692 | } while (0) |
| 693 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 694 | #define RETURN_ERROR(i) do { return i; } while(0) |
| 695 | #define RETURN_FAILURE do { ret = 0; goto exit; } while(0) |
| 696 | #define RETURN_SUCCESS do { ret = 1; goto exit; } while(0) |
| 697 | |
| 698 | #define RETURN_ON_ERROR(i) \ |
| 699 | do { if (i < 0) RETURN_ERROR(i); } while (0) |
| 700 | #define RETURN_ON_SUCCESS(i) \ |
| 701 | do { RETURN_ON_ERROR(i); if (i > 0) RETURN_SUCCESS; } while (0) |
| 702 | #define RETURN_ON_FAILURE(i) \ |
| 703 | do { RETURN_ON_ERROR(i); if (i == 0) RETURN_FAILURE; } while (0) |
| 704 | |
| 705 | #define SFY(x) #x |
| 706 | |
| 707 | #define DATA_STACK_ALLOC(state, type, ptr) \ |
| 708 | do { \ |
| 709 | alloc_pos = state->data_stack_base; \ |
| 710 | TRACE(("allocating %s in %d (%d)\n", \ |
| 711 | SFY(type), alloc_pos, sizeof(type))); \ |
| 712 | if (state->data_stack_size < alloc_pos+sizeof(type)) { \ |
| 713 | int j = data_stack_grow(state, sizeof(type)); \ |
| 714 | if (j < 0) return j; \ |
| 715 | if (ctx_pos != -1) \ |
| 716 | DATA_STACK_LOOKUP_AT(state, SRE_MATCH_CONTEXT, ctx, ctx_pos); \ |
| 717 | } \ |
| 718 | ptr = (type*)(state->data_stack+alloc_pos); \ |
| 719 | state->data_stack_base += sizeof(type); \ |
| 720 | } while (0) |
| 721 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 722 | #define DATA_STACK_LOOKUP_AT(state, type, ptr, pos) \ |
| 723 | do { \ |
| 724 | TRACE(("looking up %s at %d\n", SFY(type), pos)); \ |
| 725 | ptr = (type*)(state->data_stack+pos); \ |
| 726 | } while (0) |
| 727 | |
| 728 | #define DATA_STACK_PUSH(state, data, size) \ |
| 729 | do { \ |
| 730 | TRACE(("copy data in %p to %d (%d)\n", \ |
| 731 | data, state->data_stack_base, size)); \ |
| 732 | if (state->data_stack_size < state->data_stack_base+size) { \ |
| 733 | int j = data_stack_grow(state, size); \ |
| 734 | if (j < 0) return j; \ |
| 735 | if (ctx_pos != -1) \ |
| 736 | DATA_STACK_LOOKUP_AT(state, SRE_MATCH_CONTEXT, ctx, ctx_pos); \ |
| 737 | } \ |
| 738 | memcpy(state->data_stack+state->data_stack_base, data, size); \ |
| 739 | state->data_stack_base += size; \ |
| 740 | } while (0) |
| 741 | |
| 742 | #define DATA_STACK_POP(state, data, size, discard) \ |
| 743 | do { \ |
| 744 | TRACE(("copy data to %p from %d (%d)\n", \ |
| 745 | data, state->data_stack_base-size, size)); \ |
| 746 | memcpy(data, state->data_stack+state->data_stack_base-size, size); \ |
| 747 | if (discard) \ |
| 748 | state->data_stack_base -= size; \ |
| 749 | } while (0) |
| 750 | |
| 751 | #define DATA_STACK_POP_DISCARD(state, size) \ |
| 752 | do { \ |
| 753 | TRACE(("discard data from %d (%d)\n", \ |
| 754 | state->data_stack_base-size, size)); \ |
| 755 | state->data_stack_base -= size; \ |
| 756 | } while(0) |
| 757 | |
| 758 | #define DATA_PUSH(x) \ |
| 759 | DATA_STACK_PUSH(state, (x), sizeof(*(x))) |
| 760 | #define DATA_POP(x) \ |
| 761 | DATA_STACK_POP(state, (x), sizeof(*(x)), 1) |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 762 | #define DATA_POP_DISCARD(x) \ |
| 763 | DATA_STACK_POP_DISCARD(state, sizeof(*(x))) |
| 764 | #define DATA_ALLOC(t,p) \ |
| 765 | DATA_STACK_ALLOC(state, t, p) |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 766 | #define DATA_LOOKUP_AT(t,p,pos) \ |
| 767 | DATA_STACK_LOOKUP_AT(state,t,p,pos) |
| 768 | |
| 769 | #define MARK_PUSH(lastmark) \ |
| 770 | do if (lastmark > 0) { \ |
| 771 | i = lastmark; /* ctx->lastmark may change if reallocated */ \ |
| 772 | DATA_STACK_PUSH(state, state->mark, (i+1)*sizeof(void*)); \ |
| 773 | } while (0) |
| 774 | #define MARK_POP(lastmark) \ |
| 775 | do if (lastmark > 0) { \ |
| 776 | DATA_STACK_POP(state, state->mark, (lastmark+1)*sizeof(void*), 1); \ |
| 777 | } while (0) |
| 778 | #define MARK_POP_KEEP(lastmark) \ |
| 779 | do if (lastmark > 0) { \ |
| 780 | DATA_STACK_POP(state, state->mark, (lastmark+1)*sizeof(void*), 0); \ |
| 781 | } while (0) |
| 782 | #define MARK_POP_DISCARD(lastmark) \ |
| 783 | do if (lastmark > 0) { \ |
| 784 | DATA_STACK_POP_DISCARD(state, (lastmark+1)*sizeof(void*)); \ |
| 785 | } while (0) |
| 786 | |
| 787 | #define JUMP_NONE 0 |
| 788 | #define JUMP_MAX_UNTIL_1 1 |
| 789 | #define JUMP_MAX_UNTIL_2 2 |
| 790 | #define JUMP_MAX_UNTIL_3 3 |
| 791 | #define JUMP_MIN_UNTIL_1 4 |
| 792 | #define JUMP_MIN_UNTIL_2 5 |
| 793 | #define JUMP_MIN_UNTIL_3 6 |
| 794 | #define JUMP_REPEAT 7 |
| 795 | #define JUMP_REPEAT_ONE_1 8 |
| 796 | #define JUMP_REPEAT_ONE_2 9 |
| 797 | #define JUMP_MIN_REPEAT_ONE 10 |
| 798 | #define JUMP_BRANCH 11 |
| 799 | #define JUMP_ASSERT 12 |
| 800 | #define JUMP_ASSERT_NOT 13 |
| 801 | |
| 802 | #define DO_JUMP(jumpvalue, jumplabel, nextpattern) \ |
| 803 | DATA_ALLOC(SRE_MATCH_CONTEXT, nextctx); \ |
| 804 | nextctx->last_ctx_pos = ctx_pos; \ |
| 805 | nextctx->jump = jumpvalue; \ |
| 806 | nextctx->pattern = nextpattern; \ |
| 807 | ctx_pos = alloc_pos; \ |
| 808 | ctx = nextctx; \ |
| 809 | goto entrance; \ |
| 810 | jumplabel: \ |
| 811 | while (0) /* gcc doesn't like labels at end of scopes */ \ |
| 812 | |
| 813 | typedef struct { |
| 814 | int last_ctx_pos; |
| 815 | int jump; |
| 816 | SRE_CHAR* ptr; |
| 817 | SRE_CODE* pattern; |
| 818 | int count; |
| 819 | int lastmark; |
| 820 | int lastindex; |
| 821 | union { |
| 822 | SRE_CODE chr; |
| 823 | SRE_REPEAT* rep; |
| 824 | } u; |
| 825 | } SRE_MATCH_CONTEXT; |
| 826 | |
| 827 | /* check if string matches the given pattern. returns <0 for |
| 828 | error, 0 for failure, and 1 for success */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 829 | LOCAL(int) |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 830 | SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 831 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 832 | SRE_CHAR* end = state->end; |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 833 | int alloc_pos, ctx_pos = -1; |
| 834 | int i, ret = 0; |
| 835 | int jump; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 836 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 837 | SRE_MATCH_CONTEXT* ctx; |
| 838 | SRE_MATCH_CONTEXT* nextctx; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 839 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 840 | TRACE(("|%p|%p|ENTER %d\n", pattern, state->ptr, level)); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 841 | |
Fredrik Lundh | 18c2aa2 | 2000-08-07 17:33:38 +0000 | [diff] [blame] | 842 | #if defined(USE_STACKCHECK) |
Fredrik Lundh | 5810064 | 2000-08-09 09:14:35 +0000 | [diff] [blame] | 843 | if (level % 10 == 0 && PyOS_CheckStack()) |
Fredrik Lundh | 18c2aa2 | 2000-08-07 17:33:38 +0000 | [diff] [blame] | 844 | return SRE_ERROR_RECURSION_LIMIT; |
| 845 | #endif |
| 846 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 847 | #if defined(USE_RECURSION_LIMIT) |
| 848 | if (level > USE_RECURSION_LIMIT) |
| 849 | return SRE_ERROR_RECURSION_LIMIT; |
| 850 | #endif |
| 851 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 852 | DATA_ALLOC(SRE_MATCH_CONTEXT, ctx); |
| 853 | ctx->last_ctx_pos = -1; |
| 854 | ctx->jump = JUMP_NONE; |
| 855 | ctx->pattern = pattern; |
| 856 | ctx_pos = alloc_pos; |
| 857 | |
| 858 | entrance: |
| 859 | |
| 860 | ctx->ptr = state->ptr; |
| 861 | |
| 862 | if (ctx->pattern[0] == SRE_OP_INFO) { |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 863 | /* optimization info block */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 864 | /* <INFO> <1=skip> <2=flags> <3=min> ... */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 865 | if (ctx->pattern[3] && (end - ctx->ptr) < ctx->pattern[3]) { |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 866 | TRACE(("reject (got %d chars, need %d)\n", |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 867 | (end - ctx->ptr), ctx->pattern[3])); |
| 868 | RETURN_FAILURE; |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 869 | } |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 870 | ctx->pattern += ctx->pattern[1] + 1; |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 871 | } |
| 872 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 873 | for (;;) { |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 874 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 875 | switch (*ctx->pattern++) { |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 876 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 877 | case SRE_OP_MARK: |
| 878 | /* set mark */ |
| 879 | /* <MARK> <gid> */ |
| 880 | TRACE(("|%p|%p|MARK %d\n", ctx->pattern, |
| 881 | ctx->ptr, ctx->pattern[0])); |
| 882 | i = ctx->pattern[0]; |
| 883 | if (i & 1) |
| 884 | state->lastindex = i/2 + 1; |
| 885 | if (i > state->lastmark) { |
| 886 | /* state->lastmark is the highest valid index in the |
| 887 | state->mark array. If it is increased by more than 1, |
| 888 | the intervening marks must be set to NULL to signal |
| 889 | that these marks have not been encountered. */ |
| 890 | int j = state->lastmark + 1; |
| 891 | while (j < i) |
| 892 | state->mark[j++] = NULL; |
| 893 | state->lastmark = i; |
| 894 | } |
| 895 | state->mark[i] = ctx->ptr; |
| 896 | ctx->pattern++; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 897 | break; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 898 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 899 | case SRE_OP_LITERAL: |
| 900 | /* match literal string */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 901 | /* <LITERAL> <code> */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 902 | TRACE(("|%p|%p|LITERAL %d\n", ctx->pattern, |
| 903 | ctx->ptr, *ctx->pattern)); |
| 904 | if (ctx->ptr >= end || (SRE_CODE) ctx->ptr[0] != ctx->pattern[0]) |
| 905 | RETURN_FAILURE; |
| 906 | ctx->pattern++; |
| 907 | ctx->ptr++; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 908 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 909 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 910 | case SRE_OP_NOT_LITERAL: |
| 911 | /* match anything that is not literal character */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 912 | /* <NOT_LITERAL> <code> */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 913 | TRACE(("|%p|%p|NOT_LITERAL %d\n", ctx->pattern, |
| 914 | ctx->ptr, *ctx->pattern)); |
| 915 | if (ctx->ptr >= end || (SRE_CODE) ctx->ptr[0] == ctx->pattern[0]) |
| 916 | RETURN_FAILURE; |
| 917 | ctx->pattern++; |
| 918 | ctx->ptr++; |
| 919 | break; |
| 920 | |
| 921 | case SRE_OP_SUCCESS: |
| 922 | /* end of pattern */ |
| 923 | TRACE(("|%p|%p|SUCCESS\n", ctx->pattern, ctx->ptr)); |
| 924 | state->ptr = ctx->ptr; |
| 925 | RETURN_SUCCESS; |
| 926 | |
| 927 | case SRE_OP_AT: |
| 928 | /* match at given position */ |
| 929 | /* <AT> <code> */ |
| 930 | TRACE(("|%p|%p|AT %d\n", ctx->pattern, ctx->ptr, *ctx->pattern)); |
| 931 | if (!SRE_AT(state, ctx->ptr, *ctx->pattern)) |
| 932 | RETURN_FAILURE; |
| 933 | ctx->pattern++; |
| 934 | break; |
| 935 | |
| 936 | case SRE_OP_CATEGORY: |
| 937 | /* match at given category */ |
| 938 | /* <CATEGORY> <code> */ |
| 939 | TRACE(("|%p|%p|CATEGORY %d\n", ctx->pattern, |
| 940 | ctx->ptr, *ctx->pattern)); |
| 941 | if (ctx->ptr >= end || !sre_category(ctx->pattern[0], ctx->ptr[0])) |
| 942 | RETURN_FAILURE; |
| 943 | ctx->pattern++; |
| 944 | ctx->ptr++; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 945 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 946 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 947 | case SRE_OP_ANY: |
Fredrik Lundh | e186983 | 2000-08-01 22:47:49 +0000 | [diff] [blame] | 948 | /* match anything (except a newline) */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 949 | /* <ANY> */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 950 | TRACE(("|%p|%p|ANY\n", ctx->pattern, ctx->ptr)); |
| 951 | if (ctx->ptr >= end || SRE_IS_LINEBREAK(ctx->ptr[0])) |
| 952 | RETURN_FAILURE; |
| 953 | ctx->ptr++; |
Fredrik Lundh | e186983 | 2000-08-01 22:47:49 +0000 | [diff] [blame] | 954 | break; |
| 955 | |
| 956 | case SRE_OP_ANY_ALL: |
| 957 | /* match anything */ |
| 958 | /* <ANY_ALL> */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 959 | TRACE(("|%p|%p|ANY_ALL\n", ctx->pattern, ctx->ptr)); |
| 960 | if (ctx->ptr >= end) |
| 961 | RETURN_FAILURE; |
| 962 | ctx->ptr++; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 963 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 964 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 965 | case SRE_OP_IN: |
| 966 | /* match set member (or non_member) */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 967 | /* <IN> <skip> <set> */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 968 | TRACE(("|%p|%p|IN\n", ctx->pattern, ctx->ptr)); |
| 969 | if (ctx->ptr >= end || !SRE_CHARSET(ctx->pattern + 1, *ctx->ptr)) |
| 970 | RETURN_FAILURE; |
| 971 | ctx->pattern += ctx->pattern[0]; |
| 972 | ctx->ptr++; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 973 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 974 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 975 | case SRE_OP_LITERAL_IGNORE: |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 976 | TRACE(("|%p|%p|LITERAL_IGNORE %d\n", |
| 977 | ctx->pattern, ctx->ptr, ctx->pattern[0])); |
| 978 | if (ctx->ptr >= end || |
| 979 | state->lower(*ctx->ptr) != state->lower(*ctx->pattern)) |
| 980 | RETURN_FAILURE; |
| 981 | ctx->pattern++; |
| 982 | ctx->ptr++; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 983 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 984 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 985 | case SRE_OP_NOT_LITERAL_IGNORE: |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 986 | TRACE(("|%p|%p|NOT_LITERAL_IGNORE %d\n", |
| 987 | ctx->pattern, ctx->ptr, *ctx->pattern)); |
| 988 | if (ctx->ptr >= end || |
| 989 | state->lower(*ctx->ptr) == state->lower(*ctx->pattern)) |
| 990 | RETURN_FAILURE; |
| 991 | ctx->pattern++; |
| 992 | ctx->ptr++; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 993 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 994 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 995 | case SRE_OP_IN_IGNORE: |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 996 | TRACE(("|%p|%p|IN_IGNORE\n", ctx->pattern, ctx->ptr)); |
| 997 | if (ctx->ptr >= end |
| 998 | || !SRE_CHARSET(ctx->pattern+1, |
| 999 | (SRE_CODE)state->lower(*ctx->ptr))) |
| 1000 | RETURN_FAILURE; |
| 1001 | ctx->pattern += ctx->pattern[0]; |
| 1002 | ctx->ptr++; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1003 | break; |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 1004 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1005 | case SRE_OP_JUMP: |
| 1006 | case SRE_OP_INFO: |
| 1007 | /* jump forward */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1008 | /* <JUMP> <offset> */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1009 | TRACE(("|%p|%p|JUMP %d\n", ctx->pattern, |
| 1010 | ctx->ptr, ctx->pattern[0])); |
| 1011 | ctx->pattern += ctx->pattern[0]; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1012 | break; |
| 1013 | |
| 1014 | case SRE_OP_BRANCH: |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1015 | /* alternation */ |
| 1016 | /* <BRANCH> <0=skip> code <JUMP> ... <NULL> */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1017 | TRACE(("|%p|%p|BRANCH\n", ctx->pattern, ctx->ptr)); |
Gustavo Niemeyer | be733ee | 2003-04-20 07:35:44 +0000 | [diff] [blame] | 1018 | LASTMARK_SAVE(); |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1019 | ctx->u.rep = state->repeat; |
| 1020 | if (ctx->u.rep) |
| 1021 | MARK_PUSH(ctx->lastmark); |
| 1022 | for (; ctx->pattern[0]; ctx->pattern += ctx->pattern[0]) { |
| 1023 | if (ctx->pattern[1] == SRE_OP_LITERAL && |
| 1024 | (ctx->ptr >= end || |
| 1025 | (SRE_CODE) *ctx->ptr != ctx->pattern[2])) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1026 | continue; |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1027 | if (ctx->pattern[1] == SRE_OP_IN && |
| 1028 | (ctx->ptr >= end || |
| 1029 | !SRE_CHARSET(ctx->pattern + 3, (SRE_CODE) *ctx->ptr))) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1030 | continue; |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1031 | state->ptr = ctx->ptr; |
| 1032 | #ifdef USE_RECURSION |
| 1033 | ret = SRE_MATCH(state, ctx->pattern+1, level+1); |
| 1034 | #else |
| 1035 | DO_JUMP(JUMP_BRANCH, jump_branch, ctx->pattern+1); |
| 1036 | #endif |
| 1037 | if (ret) { |
| 1038 | if (ctx->u.rep) |
| 1039 | MARK_POP_DISCARD(ctx->lastmark); |
| 1040 | RETURN_ON_ERROR(ret); |
| 1041 | RETURN_SUCCESS; |
Gustavo Niemeyer | c34f255 | 2003-04-27 12:34:14 +0000 | [diff] [blame] | 1042 | } |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1043 | if (ctx->u.rep) |
| 1044 | MARK_POP_KEEP(ctx->lastmark); |
Gustavo Niemeyer | be733ee | 2003-04-20 07:35:44 +0000 | [diff] [blame] | 1045 | LASTMARK_RESTORE(); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1046 | } |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1047 | if (ctx->u.rep) |
| 1048 | MARK_POP_DISCARD(ctx->lastmark); |
| 1049 | RETURN_FAILURE; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1050 | |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1051 | case SRE_OP_REPEAT_ONE: |
| 1052 | /* match repeated sequence (maximizing regexp) */ |
| 1053 | |
| 1054 | /* this operator only works if the repeated item is |
| 1055 | exactly one character wide, and we're not already |
| 1056 | collecting backtracking points. for other cases, |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 1057 | use the MAX_REPEAT operator */ |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1058 | |
| 1059 | /* <REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */ |
| 1060 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1061 | TRACE(("|%p|%p|REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr, |
| 1062 | ctx->pattern[1], ctx->pattern[2])); |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1063 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1064 | if (ctx->ptr + ctx->pattern[1] > end) |
| 1065 | RETURN_FAILURE; /* cannot match */ |
Fredrik Lundh | e186983 | 2000-08-01 22:47:49 +0000 | [diff] [blame] | 1066 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1067 | state->ptr = ctx->ptr; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1068 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1069 | ctx->count = SRE_COUNT(state, ctx->pattern+3, ctx->pattern[2], |
| 1070 | level+1); |
| 1071 | RETURN_ON_ERROR(ctx->count); |
Fredrik Lundh | e186983 | 2000-08-01 22:47:49 +0000 | [diff] [blame] | 1072 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1073 | ctx->ptr += ctx->count; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1074 | |
| 1075 | /* when we arrive here, count contains the number of |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1076 | matches, and ctx->ptr points to the tail of the target |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1077 | string. check if the rest of the pattern matches, |
| 1078 | and backtrack if not. */ |
| 1079 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1080 | if (ctx->count < (int) ctx->pattern[1]) |
| 1081 | RETURN_FAILURE; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1082 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1083 | if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS) { |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1084 | /* tail is empty. we're finished */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1085 | state->ptr = ctx->ptr; |
| 1086 | RETURN_SUCCESS; |
Gustavo Niemeyer | be733ee | 2003-04-20 07:35:44 +0000 | [diff] [blame] | 1087 | } |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1088 | |
Gustavo Niemeyer | be733ee | 2003-04-20 07:35:44 +0000 | [diff] [blame] | 1089 | LASTMARK_SAVE(); |
| 1090 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1091 | if (ctx->pattern[ctx->pattern[0]] == SRE_OP_LITERAL) { |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1092 | /* tail starts with a literal. skip positions where |
| 1093 | the rest of the pattern cannot possibly match */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1094 | ctx->u.chr = ctx->pattern[ctx->pattern[0]+1]; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1095 | for (;;) { |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1096 | while (ctx->count >= (int) ctx->pattern[1] && |
| 1097 | (ctx->ptr >= end || *ctx->ptr != ctx->u.chr)) { |
| 1098 | ctx->ptr--; |
| 1099 | ctx->count--; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1100 | } |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1101 | if (ctx->count < (int) ctx->pattern[1]) |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1102 | break; |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1103 | state->ptr = ctx->ptr; |
| 1104 | #ifdef USE_RECURSION |
| 1105 | ret = SRE_MATCH(state, ctx->pattern+ctx->pattern[0], |
| 1106 | level+1); |
| 1107 | #else |
| 1108 | DO_JUMP(JUMP_REPEAT_ONE_1, jump_repeat_one_1, |
| 1109 | ctx->pattern+ctx->pattern[0]); |
| 1110 | #endif |
| 1111 | if (ret) { |
| 1112 | RETURN_ON_ERROR(ret); |
| 1113 | RETURN_SUCCESS; |
| 1114 | } |
| 1115 | |
Gustavo Niemeyer | be733ee | 2003-04-20 07:35:44 +0000 | [diff] [blame] | 1116 | LASTMARK_RESTORE(); |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1117 | |
| 1118 | ctx->ptr--; |
| 1119 | ctx->count--; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | } else { |
| 1123 | /* general case */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1124 | while (ctx->count >= (int) ctx->pattern[1]) { |
| 1125 | state->ptr = ctx->ptr; |
| 1126 | #ifdef USE_RECURSION |
| 1127 | ret = SRE_MATCH(state, ctx->pattern+ctx->pattern[0], |
| 1128 | level+1); |
| 1129 | #else |
| 1130 | DO_JUMP(JUMP_REPEAT_ONE_2, jump_repeat_one_2, |
| 1131 | ctx->pattern+ctx->pattern[0]); |
| 1132 | #endif |
| 1133 | if (ret) { |
| 1134 | RETURN_ON_ERROR(ret); |
| 1135 | RETURN_SUCCESS; |
| 1136 | } |
| 1137 | ctx->ptr--; |
| 1138 | ctx->count--; |
Gustavo Niemeyer | be733ee | 2003-04-20 07:35:44 +0000 | [diff] [blame] | 1139 | LASTMARK_RESTORE(); |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1140 | } |
| 1141 | } |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1142 | RETURN_FAILURE; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1143 | |
Guido van Rossum | 41c99e7 | 2003-04-14 17:59:34 +0000 | [diff] [blame] | 1144 | case SRE_OP_MIN_REPEAT_ONE: |
| 1145 | /* match repeated sequence (minimizing regexp) */ |
| 1146 | |
| 1147 | /* this operator only works if the repeated item is |
| 1148 | exactly one character wide, and we're not already |
| 1149 | collecting backtracking points. for other cases, |
| 1150 | use the MIN_REPEAT operator */ |
| 1151 | |
| 1152 | /* <MIN_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */ |
| 1153 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1154 | TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr, |
| 1155 | ctx->pattern[1], ctx->pattern[2])); |
Guido van Rossum | 41c99e7 | 2003-04-14 17:59:34 +0000 | [diff] [blame] | 1156 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1157 | if (ctx->ptr + ctx->pattern[1] > end) |
| 1158 | RETURN_FAILURE; /* cannot match */ |
Guido van Rossum | 41c99e7 | 2003-04-14 17:59:34 +0000 | [diff] [blame] | 1159 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1160 | state->ptr = ctx->ptr; |
Guido van Rossum | 41c99e7 | 2003-04-14 17:59:34 +0000 | [diff] [blame] | 1161 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1162 | if (ctx->pattern[1] == 0) |
| 1163 | ctx->count = 0; |
Guido van Rossum | 41c99e7 | 2003-04-14 17:59:34 +0000 | [diff] [blame] | 1164 | else { |
| 1165 | /* count using pattern min as the maximum */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1166 | ctx->count = SRE_COUNT(state, ctx->pattern+3, |
| 1167 | ctx->pattern[1], level+1); |
| 1168 | RETURN_ON_ERROR(ctx->count); |
| 1169 | if (ctx->count < (int) ctx->pattern[1]) |
| 1170 | /* didn't match minimum number of times */ |
| 1171 | RETURN_FAILURE; |
| 1172 | /* advance past minimum matches of repeat */ |
| 1173 | ctx->ptr += ctx->count; |
Guido van Rossum | 41c99e7 | 2003-04-14 17:59:34 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1176 | if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS) { |
Guido van Rossum | 41c99e7 | 2003-04-14 17:59:34 +0000 | [diff] [blame] | 1177 | /* tail is empty. we're finished */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1178 | state->ptr = ctx->ptr; |
| 1179 | RETURN_SUCCESS; |
Guido van Rossum | 41c99e7 | 2003-04-14 17:59:34 +0000 | [diff] [blame] | 1180 | |
| 1181 | } else { |
| 1182 | /* general case */ |
Gustavo Niemeyer | be733ee | 2003-04-20 07:35:44 +0000 | [diff] [blame] | 1183 | LASTMARK_SAVE(); |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1184 | while ((int)ctx->pattern[2] == 65535 |
| 1185 | || ctx->count <= (int)ctx->pattern[2]) { |
| 1186 | state->ptr = ctx->ptr; |
| 1187 | #ifdef USE_RECURSION |
| 1188 | ret = SRE_MATCH(state, ctx->pattern+ctx->pattern[0], |
| 1189 | level+1); |
| 1190 | #else |
| 1191 | DO_JUMP(JUMP_MIN_REPEAT_ONE,jump_min_repeat_one, |
| 1192 | ctx->pattern+ctx->pattern[0]); |
| 1193 | #endif |
| 1194 | if (ret) { |
| 1195 | RETURN_ON_ERROR(ret); |
| 1196 | RETURN_SUCCESS; |
| 1197 | } |
| 1198 | state->ptr = ctx->ptr; |
| 1199 | ret = SRE_COUNT(state, ctx->pattern+3, 1, level+1); |
| 1200 | RETURN_ON_ERROR(ret); |
| 1201 | if (ret == 0) |
Guido van Rossum | 41c99e7 | 2003-04-14 17:59:34 +0000 | [diff] [blame] | 1202 | break; |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1203 | assert(ret == 1); |
| 1204 | ctx->ptr++; |
| 1205 | ctx->count++; |
Gustavo Niemeyer | caf1c9d | 2003-04-27 14:42:54 +0000 | [diff] [blame] | 1206 | LASTMARK_RESTORE(); |
Guido van Rossum | 41c99e7 | 2003-04-14 17:59:34 +0000 | [diff] [blame] | 1207 | } |
Guido van Rossum | 41c99e7 | 2003-04-14 17:59:34 +0000 | [diff] [blame] | 1208 | } |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1209 | RETURN_FAILURE; |
Guido van Rossum | 41c99e7 | 2003-04-14 17:59:34 +0000 | [diff] [blame] | 1210 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1211 | case SRE_OP_REPEAT: |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1212 | /* create repeat context. all the hard work is done |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 1213 | by the UNTIL operator (MAX_UNTIL, MIN_UNTIL) */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1214 | /* <REPEAT> <skip> <1=min> <2=max> item <UNTIL> tail */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1215 | TRACE(("|%p|%p|REPEAT %d %d\n", ctx->pattern, ctx->ptr, |
| 1216 | ctx->pattern[1], ctx->pattern[2])); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1217 | |
| 1218 | /* install new repeat context */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1219 | ctx->u.rep = (SRE_REPEAT*) malloc(sizeof(*ctx->u.rep)); |
| 1220 | ctx->u.rep->count = -1; |
| 1221 | ctx->u.rep->pattern = ctx->pattern; |
| 1222 | ctx->u.rep->prev = state->repeat; |
| 1223 | ctx->u.rep->last_ptr = NULL; |
| 1224 | state->repeat = ctx->u.rep; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1225 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1226 | state->ptr = ctx->ptr; |
| 1227 | #ifdef USE_RECURSION |
| 1228 | ret = SRE_MATCH(state, ctx->pattern+ctx->pattern[0], level+1); |
| 1229 | #else |
| 1230 | DO_JUMP(JUMP_REPEAT, jump_repeat, ctx->pattern+ctx->pattern[0]); |
| 1231 | #endif |
| 1232 | state->repeat = ctx->u.rep->prev; |
| 1233 | free(ctx->u.rep); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1234 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1235 | if (ret) { |
| 1236 | RETURN_ON_ERROR(ret); |
| 1237 | RETURN_SUCCESS; |
| 1238 | } |
| 1239 | RETURN_FAILURE; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1240 | |
| 1241 | case SRE_OP_MAX_UNTIL: |
| 1242 | /* maximizing repeat */ |
| 1243 | /* <REPEAT> <skip> <1=min> <2=max> item <MAX_UNTIL> tail */ |
| 1244 | |
| 1245 | /* FIXME: we probably need to deal with zero-width |
| 1246 | matches in here... */ |
| 1247 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1248 | ctx->u.rep = state->repeat; |
| 1249 | if (!ctx->u.rep) |
| 1250 | RETURN_ERROR(SRE_ERROR_STATE); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1251 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1252 | state->ptr = ctx->ptr; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1253 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1254 | ctx->count = ctx->u.rep->count+1; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1255 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1256 | TRACE(("|%p|%p|MAX_UNTIL %d\n", ctx->pattern, |
| 1257 | ctx->ptr, ctx->count)); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1258 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1259 | if (ctx->count < ctx->u.rep->pattern[1]) { |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1260 | /* not enough matches */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1261 | ctx->u.rep->count = ctx->count; |
| 1262 | #ifdef USE_RECURSION |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 1263 | /* RECURSIVE */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1264 | ret = SRE_MATCH(state, ctx->u.rep->pattern+3, level+1); |
| 1265 | #else |
| 1266 | DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1, |
| 1267 | ctx->u.rep->pattern+3); |
| 1268 | #endif |
| 1269 | if (ret) { |
| 1270 | RETURN_ON_ERROR(ret); |
| 1271 | RETURN_SUCCESS; |
| 1272 | } |
| 1273 | ctx->u.rep->count = ctx->count-1; |
| 1274 | state->ptr = ctx->ptr; |
| 1275 | RETURN_FAILURE; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1276 | } |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1277 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1278 | if ((ctx->count < ctx->u.rep->pattern[2] || |
| 1279 | ctx->u.rep->pattern[2] == 65535) && |
| 1280 | state->ptr != ctx->u.rep->last_ptr) { |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1281 | /* we may have enough matches, but if we can |
| 1282 | match another item, do so */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1283 | ctx->u.rep->count = ctx->count; |
Gustavo Niemeyer | caf1c9d | 2003-04-27 14:42:54 +0000 | [diff] [blame] | 1284 | LASTMARK_SAVE(); |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1285 | MARK_PUSH(ctx->lastmark); |
| 1286 | /* zero-width match protection */ |
| 1287 | DATA_PUSH(&ctx->u.rep->last_ptr); |
| 1288 | ctx->u.rep->last_ptr = state->ptr; |
| 1289 | #ifdef USE_RECURSION |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 1290 | /* RECURSIVE */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1291 | ret = SRE_MATCH(state, ctx->u.rep->pattern+3, level+1); |
| 1292 | #else |
| 1293 | DO_JUMP(JUMP_MAX_UNTIL_2, jump_max_until_2, |
| 1294 | ctx->u.rep->pattern+3); |
| 1295 | #endif |
| 1296 | DATA_POP(&ctx->u.rep->last_ptr); |
| 1297 | if (ret) { |
| 1298 | MARK_POP_DISCARD(ctx->lastmark); |
| 1299 | RETURN_ON_ERROR(ret); |
| 1300 | RETURN_SUCCESS; |
| 1301 | } |
| 1302 | MARK_POP(ctx->lastmark); |
Gustavo Niemeyer | caf1c9d | 2003-04-27 14:42:54 +0000 | [diff] [blame] | 1303 | LASTMARK_RESTORE(); |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1304 | ctx->u.rep->count = ctx->count-1; |
| 1305 | state->ptr = ctx->ptr; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | /* cannot match more repeated items here. make sure the |
| 1309 | tail matches */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1310 | state->repeat = ctx->u.rep->prev; |
| 1311 | #ifdef USE_RECURSION |
| 1312 | ret = SRE_MATCH(state, ctx->pattern, level+1); |
| 1313 | #else |
| 1314 | DO_JUMP(JUMP_MAX_UNTIL_3, jump_max_until_3, ctx->pattern); |
| 1315 | #endif |
| 1316 | RETURN_ON_SUCCESS(ret); |
| 1317 | state->repeat = ctx->u.rep; |
| 1318 | state->ptr = ctx->ptr; |
| 1319 | RETURN_FAILURE; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1320 | |
| 1321 | case SRE_OP_MIN_UNTIL: |
| 1322 | /* minimizing repeat */ |
| 1323 | /* <REPEAT> <skip> <1=min> <2=max> item <MIN_UNTIL> tail */ |
| 1324 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1325 | ctx->u.rep = state->repeat; |
| 1326 | if (!ctx->u.rep) |
| 1327 | RETURN_ERROR(SRE_ERROR_STATE); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1328 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1329 | state->ptr = ctx->ptr; |
Gustavo Niemeyer | 3c9068b | 2003-04-22 15:39:09 +0000 | [diff] [blame] | 1330 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1331 | ctx->count = ctx->u.rep->count+1; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1332 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1333 | TRACE(("|%p|%p|MIN_UNTIL %d %p\n", ctx->pattern, |
| 1334 | ctx->ptr, ctx->count, ctx->u.rep->pattern)); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1335 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1336 | if (ctx->count < ctx->u.rep->pattern[1]) { |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1337 | /* not enough matches */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1338 | ctx->u.rep->count = ctx->count; |
| 1339 | #ifdef USE_RECURSION |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 1340 | /* RECURSIVE */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1341 | ret = SRE_MATCH(state, ctx->u.rep->pattern+3, level+1); |
| 1342 | #else |
| 1343 | DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1, |
| 1344 | ctx->u.rep->pattern+3); |
| 1345 | #endif |
| 1346 | if (ret) { |
| 1347 | RETURN_ON_ERROR(ret); |
| 1348 | RETURN_SUCCESS; |
| 1349 | } |
| 1350 | ctx->u.rep->count = ctx->count-1; |
| 1351 | state->ptr = ctx->ptr; |
| 1352 | RETURN_FAILURE; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Gustavo Niemeyer | caf1c9d | 2003-04-27 14:42:54 +0000 | [diff] [blame] | 1355 | LASTMARK_SAVE(); |
| 1356 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1357 | /* see if the tail matches */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1358 | state->repeat = ctx->u.rep->prev; |
| 1359 | #ifdef USE_RECURSION |
| 1360 | ret = SRE_MATCH(state, ctx->pattern, level+1); |
| 1361 | #else |
| 1362 | DO_JUMP(JUMP_MIN_UNTIL_2, jump_min_until_2, ctx->pattern); |
| 1363 | #endif |
| 1364 | if (ret) { |
| 1365 | RETURN_ON_ERROR(ret); |
| 1366 | RETURN_SUCCESS; |
| 1367 | } |
Fredrik Lundh | fa25a7d | 2001-01-14 23:55:55 +0000 | [diff] [blame] | 1368 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1369 | state->repeat = ctx->u.rep; |
| 1370 | state->ptr = ctx->ptr; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1371 | |
Gustavo Niemeyer | caf1c9d | 2003-04-27 14:42:54 +0000 | [diff] [blame] | 1372 | LASTMARK_RESTORE(); |
| 1373 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1374 | if (ctx->count >= ctx->u.rep->pattern[2] |
| 1375 | && ctx->u.rep->pattern[2] != 65535) |
| 1376 | RETURN_FAILURE; |
Gustavo Niemeyer | caf1c9d | 2003-04-27 14:42:54 +0000 | [diff] [blame] | 1377 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1378 | ctx->u.rep->count = ctx->count; |
| 1379 | #ifdef USE_RECURSION |
| 1380 | /* RECURSIVE */ |
| 1381 | ret = SRE_MATCH(state, ctx->u.rep->pattern+3, level+1); |
| 1382 | #else |
| 1383 | DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3, |
| 1384 | ctx->u.rep->pattern+3); |
| 1385 | #endif |
| 1386 | if (ret) { |
| 1387 | RETURN_ON_ERROR(ret); |
| 1388 | RETURN_SUCCESS; |
| 1389 | } |
| 1390 | ctx->u.rep->count = ctx->count-1; |
| 1391 | state->ptr = ctx->ptr; |
| 1392 | RETURN_FAILURE; |
| 1393 | |
| 1394 | case SRE_OP_GROUPREF: |
| 1395 | /* match backreference */ |
| 1396 | TRACE(("|%p|%p|GROUPREF %d\n", ctx->pattern, |
| 1397 | ctx->ptr, ctx->pattern[0])); |
| 1398 | i = ctx->pattern[0]; |
| 1399 | { |
| 1400 | int groupref = i+i; |
| 1401 | if (groupref >= state->lastmark) { |
| 1402 | RETURN_FAILURE; |
| 1403 | } else { |
| 1404 | SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref]; |
| 1405 | SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1]; |
| 1406 | if (!p || !e || e < p) |
| 1407 | RETURN_FAILURE; |
| 1408 | while (p < e) { |
| 1409 | if (ctx->ptr >= end || *ctx->ptr != *p) |
| 1410 | RETURN_FAILURE; |
| 1411 | p++; ctx->ptr++; |
| 1412 | } |
| 1413 | } |
| 1414 | } |
| 1415 | ctx->pattern++; |
| 1416 | break; |
| 1417 | |
| 1418 | case SRE_OP_GROUPREF_IGNORE: |
| 1419 | /* match backreference */ |
| 1420 | TRACE(("|%p|%p|GROUPREF_IGNORE %d\n", ctx->pattern, |
| 1421 | ctx->ptr, ctx->pattern[0])); |
| 1422 | i = ctx->pattern[0]; |
| 1423 | { |
| 1424 | int groupref = i+i; |
| 1425 | if (groupref >= state->lastmark) { |
| 1426 | RETURN_FAILURE; |
| 1427 | } else { |
| 1428 | SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref]; |
| 1429 | SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1]; |
| 1430 | if (!p || !e || e < p) |
| 1431 | RETURN_FAILURE; |
| 1432 | while (p < e) { |
| 1433 | if (ctx->ptr >= end || |
| 1434 | state->lower(*ctx->ptr) != state->lower(*p)) |
| 1435 | RETURN_FAILURE; |
| 1436 | p++; ctx->ptr++; |
| 1437 | } |
| 1438 | } |
| 1439 | } |
| 1440 | ctx->pattern++; |
| 1441 | break; |
| 1442 | |
| 1443 | case SRE_OP_GROUPREF_EXISTS: |
| 1444 | TRACE(("|%p|%p|GROUPREF_EXISTS %d\n", ctx->pattern, |
| 1445 | ctx->ptr, ctx->pattern[0])); |
| 1446 | /* <GROUPREF_EXISTS> <group> <skip> codeyes <JUMP> codeno ... */ |
| 1447 | i = ctx->pattern[0]; |
| 1448 | { |
| 1449 | int groupref = i+i; |
| 1450 | if (groupref >= state->lastmark) { |
| 1451 | ctx->pattern += ctx->pattern[1]; |
| 1452 | break; |
| 1453 | } else { |
| 1454 | SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref]; |
| 1455 | SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1]; |
| 1456 | if (!p || !e || e < p) { |
| 1457 | ctx->pattern += ctx->pattern[1]; |
| 1458 | break; |
| 1459 | } |
| 1460 | } |
| 1461 | } |
| 1462 | ctx->pattern += 2; |
| 1463 | break; |
| 1464 | |
| 1465 | case SRE_OP_ASSERT: |
| 1466 | /* assert subpattern */ |
| 1467 | /* <ASSERT> <skip> <back> <pattern> */ |
| 1468 | TRACE(("|%p|%p|ASSERT %d\n", ctx->pattern, |
| 1469 | ctx->ptr, ctx->pattern[1])); |
| 1470 | state->ptr = ctx->ptr - ctx->pattern[1]; |
| 1471 | if (state->ptr < state->beginning) |
| 1472 | RETURN_FAILURE; |
| 1473 | #ifdef USE_RECURSION |
| 1474 | ret = SRE_MATCH(state, ctx->pattern+2, level+1); |
| 1475 | #else |
| 1476 | DO_JUMP(JUMP_ASSERT, jump_assert, ctx->pattern+2); |
| 1477 | #endif |
| 1478 | RETURN_ON_FAILURE(ret); |
| 1479 | ctx->pattern += ctx->pattern[0]; |
| 1480 | break; |
| 1481 | |
| 1482 | case SRE_OP_ASSERT_NOT: |
| 1483 | /* assert not subpattern */ |
| 1484 | /* <ASSERT_NOT> <skip> <back> <pattern> */ |
| 1485 | TRACE(("|%p|%p|ASSERT_NOT %d\n", ctx->pattern, |
| 1486 | ctx->ptr, ctx->pattern[1])); |
| 1487 | state->ptr = ctx->ptr - ctx->pattern[1]; |
| 1488 | if (state->ptr >= state->beginning) { |
| 1489 | #ifdef USE_RECURSION |
| 1490 | ret = SRE_MATCH(state, ctx->pattern+2, level+1); |
| 1491 | #else |
| 1492 | DO_JUMP(JUMP_ASSERT_NOT, jump_assert_not, ctx->pattern+2); |
| 1493 | #endif |
| 1494 | if (ret) { |
| 1495 | RETURN_ON_ERROR(ret); |
| 1496 | RETURN_FAILURE; |
| 1497 | } |
| 1498 | } |
| 1499 | ctx->pattern += ctx->pattern[0]; |
| 1500 | break; |
| 1501 | |
| 1502 | case SRE_OP_FAILURE: |
| 1503 | /* immediate failure */ |
| 1504 | TRACE(("|%p|%p|FAILURE\n", ctx->pattern, ctx->ptr)); |
| 1505 | RETURN_FAILURE; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1506 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1507 | default: |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1508 | TRACE(("|%p|%p|UNKNOWN %d\n", ctx->pattern, ctx->ptr, |
| 1509 | ctx->pattern[-1])); |
| 1510 | RETURN_ERROR(SRE_ERROR_ILLEGAL); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1511 | } |
| 1512 | } |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1513 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1514 | exit: |
| 1515 | ctx_pos = ctx->last_ctx_pos; |
| 1516 | jump = ctx->jump; |
| 1517 | DATA_POP_DISCARD(ctx); |
| 1518 | if (ctx_pos == -1) |
| 1519 | return ret; |
| 1520 | DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos); |
| 1521 | |
| 1522 | #ifndef USE_RECURSION |
| 1523 | switch (jump) { |
| 1524 | case JUMP_MAX_UNTIL_2: |
| 1525 | TRACE(("|%p|%p|JUMP_MAX_UNTIL_2\n", ctx->pattern, ctx->ptr)); |
| 1526 | goto jump_max_until_2; |
| 1527 | case JUMP_MAX_UNTIL_3: |
| 1528 | TRACE(("|%p|%p|JUMP_MAX_UNTIL_3\n", ctx->pattern, ctx->ptr)); |
| 1529 | goto jump_max_until_3; |
| 1530 | case JUMP_MIN_UNTIL_2: |
| 1531 | TRACE(("|%p|%p|JUMP_MIN_UNTIL_2\n", ctx->pattern, ctx->ptr)); |
| 1532 | goto jump_min_until_2; |
| 1533 | case JUMP_MIN_UNTIL_3: |
| 1534 | TRACE(("|%p|%p|JUMP_MIN_UNTIL_3\n", ctx->pattern, ctx->ptr)); |
| 1535 | goto jump_min_until_3; |
| 1536 | case JUMP_BRANCH: |
| 1537 | TRACE(("|%p|%p|JUMP_BRANCH\n", ctx->pattern, ctx->ptr)); |
| 1538 | goto jump_branch; |
| 1539 | case JUMP_MAX_UNTIL_1: |
| 1540 | TRACE(("|%p|%p|JUMP_MAX_UNTIL_1\n", ctx->pattern, ctx->ptr)); |
| 1541 | goto jump_max_until_1; |
| 1542 | case JUMP_MIN_UNTIL_1: |
| 1543 | TRACE(("|%p|%p|JUMP_MIN_UNTIL_1\n", ctx->pattern, ctx->ptr)); |
| 1544 | goto jump_min_until_1; |
| 1545 | case JUMP_REPEAT: |
| 1546 | TRACE(("|%p|%p|JUMP_REPEAT\n", ctx->pattern, ctx->ptr)); |
| 1547 | goto jump_repeat; |
| 1548 | case JUMP_REPEAT_ONE_1: |
| 1549 | TRACE(("|%p|%p|JUMP_REPEAT_ONE_1\n", ctx->pattern, ctx->ptr)); |
| 1550 | goto jump_repeat_one_1; |
| 1551 | case JUMP_REPEAT_ONE_2: |
| 1552 | TRACE(("|%p|%p|JUMP_REPEAT_ONE_2\n", ctx->pattern, ctx->ptr)); |
| 1553 | goto jump_repeat_one_2; |
| 1554 | case JUMP_MIN_REPEAT_ONE: |
| 1555 | TRACE(("|%p|%p|JUMP_MIN_REPEAT_ONE\n", ctx->pattern, ctx->ptr)); |
| 1556 | goto jump_min_repeat_one; |
| 1557 | case JUMP_ASSERT: |
| 1558 | TRACE(("|%p|%p|JUMP_ASSERT\n", ctx->pattern, ctx->ptr)); |
| 1559 | goto jump_assert; |
| 1560 | case JUMP_ASSERT_NOT: |
| 1561 | TRACE(("|%p|%p|JUMP_ASSERT_NOT\n", ctx->pattern, ctx->ptr)); |
| 1562 | goto jump_assert_not; |
| 1563 | case JUMP_NONE: |
| 1564 | TRACE(("|%p|%p|RETURN %d\n", ctx->pattern, ctx->ptr, ret)); |
| 1565 | break; |
| 1566 | } |
| 1567 | #endif |
| 1568 | |
| 1569 | return ret; /* should never get here */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 1572 | LOCAL(int) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1573 | SRE_SEARCH(SRE_STATE* state, SRE_CODE* pattern) |
| 1574 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1575 | SRE_CHAR* ptr = state->start; |
| 1576 | SRE_CHAR* end = state->end; |
| 1577 | int status = 0; |
Fredrik Lundh | 2855290 | 2000-07-05 21:14:16 +0000 | [diff] [blame] | 1578 | int prefix_len = 0; |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 1579 | int prefix_skip = 0; |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1580 | SRE_CODE* prefix = NULL; |
| 1581 | SRE_CODE* charset = NULL; |
| 1582 | SRE_CODE* overlap = NULL; |
| 1583 | int flags = 0; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1584 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1585 | if (pattern[0] == SRE_OP_INFO) { |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 1586 | /* optimization info block */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1587 | /* <INFO> <1=skip> <2=flags> <3=min> <4=max> <5=prefix info> */ |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1588 | |
| 1589 | flags = pattern[2]; |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 1590 | |
Gustavo Niemeyer | 28b5bb3 | 2003-06-26 14:41:08 +0000 | [diff] [blame] | 1591 | if (pattern[3] > 1) { |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 1592 | /* adjust end point (but make sure we leave at least one |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1593 | character in there, so literal search will work) */ |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 1594 | end -= pattern[3]-1; |
| 1595 | if (end <= ptr) |
| 1596 | end = ptr+1; |
| 1597 | } |
| 1598 | |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1599 | if (flags & SRE_INFO_PREFIX) { |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 1600 | /* pattern starts with a known prefix */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1601 | /* <length> <skip> <prefix data> <overlap data> */ |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1602 | prefix_len = pattern[5]; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1603 | prefix_skip = pattern[6]; |
| 1604 | prefix = pattern + 7; |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1605 | overlap = prefix + prefix_len - 1; |
| 1606 | } else if (flags & SRE_INFO_CHARSET) |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 1607 | /* pattern starts with a character from a known set */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1608 | /* <charset> */ |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1609 | charset = pattern + 5; |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 1610 | |
| 1611 | pattern += 1 + pattern[1]; |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1612 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1613 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1614 | TRACE(("prefix = %p %d %d\n", prefix, prefix_len, prefix_skip)); |
| 1615 | TRACE(("charset = %p\n", charset)); |
| 1616 | |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 1617 | #if defined(USE_FAST_SEARCH) |
Fredrik Lundh | 2855290 | 2000-07-05 21:14:16 +0000 | [diff] [blame] | 1618 | if (prefix_len > 1) { |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 1619 | /* pattern starts with a known prefix. use the overlap |
| 1620 | table to skip forward as fast as we possibly can */ |
| 1621 | int i = 0; |
| 1622 | end = state->end; |
| 1623 | while (ptr < end) { |
| 1624 | for (;;) { |
Fredrik Lundh | 0640e11 | 2000-06-30 13:55:15 +0000 | [diff] [blame] | 1625 | if ((SRE_CODE) ptr[0] != prefix[i]) { |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 1626 | if (!i) |
| 1627 | break; |
| 1628 | else |
| 1629 | i = overlap[i]; |
| 1630 | } else { |
| 1631 | if (++i == prefix_len) { |
| 1632 | /* found a potential match */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1633 | TRACE(("|%p|%p|SEARCH SCAN\n", pattern, ptr)); |
| 1634 | state->start = ptr + 1 - prefix_len; |
| 1635 | state->ptr = ptr + 1 - prefix_len + prefix_skip; |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1636 | if (flags & SRE_INFO_LITERAL) |
| 1637 | return 1; /* we got all of it */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1638 | status = SRE_MATCH(state, pattern + 2*prefix_skip, 1); |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 1639 | if (status != 0) |
| 1640 | return status; |
| 1641 | /* close but no cigar -- try again */ |
| 1642 | i = overlap[i]; |
| 1643 | } |
| 1644 | break; |
| 1645 | } |
| 1646 | |
| 1647 | } |
| 1648 | ptr++; |
| 1649 | } |
| 1650 | return 0; |
| 1651 | } |
| 1652 | #endif |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 1653 | |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1654 | if (pattern[0] == SRE_OP_LITERAL) { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1655 | /* pattern starts with a literal character. this is used |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1656 | for short prefixes, and if fast search is disabled */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1657 | SRE_CODE chr = pattern[1]; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1658 | end = state->end; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1659 | for (;;) { |
| 1660 | while (ptr < end && (SRE_CODE) ptr[0] != chr) |
| 1661 | ptr++; |
Gustavo Niemeyer | c523b04 | 2002-11-07 03:28:56 +0000 | [diff] [blame] | 1662 | if (ptr >= end) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1663 | return 0; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1664 | TRACE(("|%p|%p|SEARCH LITERAL\n", pattern, ptr)); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1665 | state->start = ptr; |
| 1666 | state->ptr = ++ptr; |
Fredrik Lundh | dac5849 | 2001-10-21 21:48:30 +0000 | [diff] [blame] | 1667 | if (flags & SRE_INFO_LITERAL) |
| 1668 | return 1; /* we got all of it */ |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1669 | status = SRE_MATCH(state, pattern + 2, 1); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1670 | if (status != 0) |
| 1671 | break; |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1672 | } |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1673 | } else if (charset) { |
| 1674 | /* pattern starts with a character from a known set */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1675 | end = state->end; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1676 | for (;;) { |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1677 | while (ptr < end && !SRE_CHARSET(charset, ptr[0])) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1678 | ptr++; |
Gustavo Niemeyer | c523b04 | 2002-11-07 03:28:56 +0000 | [diff] [blame] | 1679 | if (ptr >= end) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1680 | return 0; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1681 | TRACE(("|%p|%p|SEARCH CHARSET\n", pattern, ptr)); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1682 | state->start = ptr; |
| 1683 | state->ptr = ptr; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1684 | status = SRE_MATCH(state, pattern, 1); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1685 | if (status != 0) |
| 1686 | break; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1687 | ptr++; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1688 | } |
| 1689 | } else |
| 1690 | /* general case */ |
| 1691 | while (ptr <= end) { |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1692 | TRACE(("|%p|%p|SEARCH\n", pattern, ptr)); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1693 | state->start = state->ptr = ptr++; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1694 | status = SRE_MATCH(state, pattern, 1); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1695 | if (status != 0) |
| 1696 | break; |
| 1697 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1698 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1699 | return status; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1700 | } |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1701 | |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 1702 | LOCAL(int) |
| 1703 | SRE_LITERAL_TEMPLATE(SRE_CHAR* ptr, int len) |
| 1704 | { |
| 1705 | /* check if given string is a literal template (i.e. no escapes) */ |
| 1706 | while (len-- > 0) |
| 1707 | if (*ptr++ == '\\') |
| 1708 | return 0; |
| 1709 | return 1; |
| 1710 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1711 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1712 | #if !defined(SRE_RECURSIVE) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1713 | |
| 1714 | /* -------------------------------------------------------------------- */ |
| 1715 | /* factories and destructors */ |
| 1716 | |
| 1717 | /* see sre.h for object declarations */ |
| 1718 | |
Jeremy Hylton | 938ace6 | 2002-07-17 16:30:39 +0000 | [diff] [blame] | 1719 | static PyTypeObject Pattern_Type; |
| 1720 | static PyTypeObject Match_Type; |
| 1721 | static PyTypeObject Scanner_Type; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1722 | |
| 1723 | static PyObject * |
| 1724 | _compile(PyObject* self_, PyObject* args) |
| 1725 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1726 | /* "compile" pattern descriptor to pattern object */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1727 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1728 | PatternObject* self; |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1729 | int i, n; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1730 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1731 | PyObject* pattern; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1732 | int flags = 0; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1733 | PyObject* code; |
| 1734 | int groups = 0; |
| 1735 | PyObject* groupindex = NULL; |
Fredrik Lundh | c230173 | 2000-07-02 22:25:39 +0000 | [diff] [blame] | 1736 | PyObject* indexgroup = NULL; |
Fredrik Lundh | 1c5aa69 | 2001-01-16 07:37:30 +0000 | [diff] [blame] | 1737 | if (!PyArg_ParseTuple(args, "OiO!|iOO", &pattern, &flags, |
| 1738 | &PyList_Type, &code, &groups, |
| 1739 | &groupindex, &indexgroup)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1740 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1741 | |
Fredrik Lundh | 1c5aa69 | 2001-01-16 07:37:30 +0000 | [diff] [blame] | 1742 | n = PyList_GET_SIZE(code); |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1743 | |
Fredrik Lundh | ebc37b2 | 2000-10-28 19:30:41 +0000 | [diff] [blame] | 1744 | self = PyObject_NEW_VAR(PatternObject, &Pattern_Type, n); |
Fredrik Lundh | 1c5aa69 | 2001-01-16 07:37:30 +0000 | [diff] [blame] | 1745 | if (!self) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1746 | return NULL; |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1747 | |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 1748 | self->codesize = n; |
| 1749 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1750 | for (i = 0; i < n; i++) { |
Fredrik Lundh | 1c5aa69 | 2001-01-16 07:37:30 +0000 | [diff] [blame] | 1751 | PyObject *o = PyList_GET_ITEM(code, i); |
Martin v. Löwis | 78e2f06 | 2003-04-19 12:56:08 +0000 | [diff] [blame] | 1752 | if (PyInt_Check(o)) |
| 1753 | self->code[i] = (SRE_CODE) PyInt_AsLong(o); |
| 1754 | else |
| 1755 | self->code[i] = (SRE_CODE) PyLong_AsUnsignedLong(o); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1756 | } |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1757 | |
Fredrik Lundh | 6f5cba6 | 2001-01-16 07:05:29 +0000 | [diff] [blame] | 1758 | if (PyErr_Occurred()) { |
| 1759 | PyObject_DEL(self); |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1760 | return NULL; |
Fredrik Lundh | 6f5cba6 | 2001-01-16 07:05:29 +0000 | [diff] [blame] | 1761 | } |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1762 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1763 | Py_INCREF(pattern); |
| 1764 | self->pattern = pattern; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1765 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1766 | self->flags = flags; |
| 1767 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1768 | self->groups = groups; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1769 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1770 | Py_XINCREF(groupindex); |
| 1771 | self->groupindex = groupindex; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1772 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1773 | Py_XINCREF(indexgroup); |
| 1774 | self->indexgroup = indexgroup; |
Fredrik Lundh | c230173 | 2000-07-02 22:25:39 +0000 | [diff] [blame] | 1775 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1776 | return (PyObject*) self; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
| 1779 | static PyObject * |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1780 | sre_codesize(PyObject* self, PyObject* args) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1781 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1782 | return Py_BuildValue("i", sizeof(SRE_CODE)); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1785 | static PyObject * |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 1786 | sre_getlower(PyObject* self, PyObject* args) |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1787 | { |
| 1788 | int character, flags; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1789 | if (!PyArg_ParseTuple(args, "ii", &character, &flags)) |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1790 | return NULL; |
| 1791 | if (flags & SRE_FLAG_LOCALE) |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 1792 | return Py_BuildValue("i", sre_lower_locale(character)); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1793 | if (flags & SRE_FLAG_UNICODE) |
Fredrik Lundh | 1c5aa69 | 2001-01-16 07:37:30 +0000 | [diff] [blame] | 1794 | #if defined(HAVE_UNICODE) |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 1795 | return Py_BuildValue("i", sre_lower_unicode(character)); |
Fredrik Lundh | 1c5aa69 | 2001-01-16 07:37:30 +0000 | [diff] [blame] | 1796 | #else |
| 1797 | return Py_BuildValue("i", sre_lower_locale(character)); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1798 | #endif |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 1799 | return Py_BuildValue("i", sre_lower(character)); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1802 | LOCAL(void) |
| 1803 | state_reset(SRE_STATE* state) |
| 1804 | { |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1805 | /* FIXME: dynamic! */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1806 | /*memset(state->mark, 0, sizeof(*state->mark) * SRE_MARK_SIZE);*/ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1807 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1808 | state->lastmark = -1; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1809 | state->lastindex = -1; |
| 1810 | |
| 1811 | state->repeat = NULL; |
| 1812 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1813 | data_stack_dealloc(state); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 1816 | static void* |
| 1817 | getstring(PyObject* string, int* p_length, int* p_charsize) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1818 | { |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 1819 | /* given a python object, return a data pointer, a length (in |
| 1820 | characters), and a character size. return NULL if the object |
| 1821 | is not a string (or not compatible) */ |
| 1822 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1823 | PyBufferProcs *buffer; |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 1824 | int size, bytes, charsize; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1825 | void* ptr; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1826 | |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 1827 | #if defined(HAVE_UNICODE) |
| 1828 | if (PyUnicode_Check(string)) { |
| 1829 | /* unicode strings doesn't always support the buffer interface */ |
| 1830 | ptr = (void*) PyUnicode_AS_DATA(string); |
| 1831 | bytes = PyUnicode_GET_DATA_SIZE(string); |
| 1832 | size = PyUnicode_GET_SIZE(string); |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 1833 | charsize = sizeof(Py_UNICODE); |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 1834 | |
| 1835 | } else { |
| 1836 | #endif |
| 1837 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1838 | /* get pointer to string buffer */ |
| 1839 | buffer = string->ob_type->tp_as_buffer; |
| 1840 | if (!buffer || !buffer->bf_getreadbuffer || !buffer->bf_getsegcount || |
| 1841 | buffer->bf_getsegcount(string, NULL) != 1) { |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1842 | PyErr_SetString(PyExc_TypeError, "expected string or buffer"); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1843 | return NULL; |
| 1844 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1845 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1846 | /* determine buffer size */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1847 | bytes = buffer->bf_getreadbuffer(string, 0, &ptr); |
| 1848 | if (bytes < 0) { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1849 | PyErr_SetString(PyExc_TypeError, "buffer has negative size"); |
| 1850 | return NULL; |
| 1851 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1852 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1853 | /* determine character size */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1854 | #if PY_VERSION_HEX >= 0x01060000 |
| 1855 | size = PyObject_Size(string); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1856 | #else |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1857 | size = PyObject_Length(string); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1858 | #endif |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1859 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1860 | if (PyString_Check(string) || bytes == size) |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 1861 | charsize = 1; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1862 | #if defined(HAVE_UNICODE) |
| 1863 | else if (bytes == (int) (size * sizeof(Py_UNICODE))) |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 1864 | charsize = sizeof(Py_UNICODE); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1865 | #endif |
| 1866 | else { |
| 1867 | PyErr_SetString(PyExc_TypeError, "buffer size mismatch"); |
| 1868 | return NULL; |
| 1869 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1870 | |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 1871 | #if defined(HAVE_UNICODE) |
| 1872 | } |
| 1873 | #endif |
| 1874 | |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 1875 | *p_length = size; |
| 1876 | *p_charsize = charsize; |
| 1877 | |
| 1878 | return ptr; |
| 1879 | } |
| 1880 | |
| 1881 | LOCAL(PyObject*) |
| 1882 | state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string, |
| 1883 | int start, int end) |
| 1884 | { |
| 1885 | /* prepare state object */ |
| 1886 | |
| 1887 | int length; |
| 1888 | int charsize; |
| 1889 | void* ptr; |
| 1890 | |
| 1891 | memset(state, 0, sizeof(SRE_STATE)); |
| 1892 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1893 | state->lastmark = -1; |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 1894 | state->lastindex = -1; |
| 1895 | |
| 1896 | ptr = getstring(string, &length, &charsize); |
| 1897 | if (!ptr) |
| 1898 | return NULL; |
| 1899 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1900 | /* adjust boundaries */ |
| 1901 | if (start < 0) |
| 1902 | start = 0; |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 1903 | else if (start > length) |
| 1904 | start = length; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1905 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1906 | if (end < 0) |
| 1907 | end = 0; |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 1908 | else if (end > length) |
| 1909 | end = length; |
| 1910 | |
| 1911 | state->charsize = charsize; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1912 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1913 | state->beginning = ptr; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1914 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1915 | state->start = (void*) ((char*) ptr + start * state->charsize); |
| 1916 | state->end = (void*) ((char*) ptr + end * state->charsize); |
| 1917 | |
| 1918 | Py_INCREF(string); |
| 1919 | state->string = string; |
| 1920 | state->pos = start; |
| 1921 | state->endpos = end; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1922 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1923 | if (pattern->flags & SRE_FLAG_LOCALE) |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 1924 | state->lower = sre_lower_locale; |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1925 | else if (pattern->flags & SRE_FLAG_UNICODE) |
Fredrik Lundh | 1c5aa69 | 2001-01-16 07:37:30 +0000 | [diff] [blame] | 1926 | #if defined(HAVE_UNICODE) |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 1927 | state->lower = sre_lower_unicode; |
Fredrik Lundh | 1c5aa69 | 2001-01-16 07:37:30 +0000 | [diff] [blame] | 1928 | #else |
| 1929 | state->lower = sre_lower_locale; |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1930 | #endif |
| 1931 | else |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 1932 | state->lower = sre_lower; |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1933 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1934 | return string; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1935 | } |
| 1936 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1937 | LOCAL(void) |
| 1938 | state_fini(SRE_STATE* state) |
| 1939 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1940 | Py_XDECREF(state->string); |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1941 | data_stack_dealloc(state); |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1944 | /* calculate offset from start of string */ |
| 1945 | #define STATE_OFFSET(state, member)\ |
| 1946 | (((char*)(member) - (char*)(state)->beginning) / (state)->charsize) |
| 1947 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1948 | LOCAL(PyObject*) |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 1949 | state_getslice(SRE_STATE* state, int index, PyObject* string, int empty) |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1950 | { |
Fredrik Lundh | 5810064 | 2000-08-09 09:14:35 +0000 | [diff] [blame] | 1951 | int i, j; |
| 1952 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1953 | index = (index - 1) * 2; |
| 1954 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 1955 | if (string == Py_None || index >= state->lastmark || !state->mark[index] || !state->mark[index+1]) { |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 1956 | if (empty) |
| 1957 | /* want empty string */ |
| 1958 | i = j = 0; |
| 1959 | else { |
| 1960 | Py_INCREF(Py_None); |
| 1961 | return Py_None; |
| 1962 | } |
Fredrik Lundh | 5810064 | 2000-08-09 09:14:35 +0000 | [diff] [blame] | 1963 | } else { |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1964 | i = STATE_OFFSET(state, state->mark[index]); |
| 1965 | j = STATE_OFFSET(state, state->mark[index+1]); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1966 | } |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1967 | |
Fredrik Lundh | 5810064 | 2000-08-09 09:14:35 +0000 | [diff] [blame] | 1968 | return PySequence_GetSlice(string, i, j); |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 1971 | static void |
| 1972 | pattern_error(int status) |
| 1973 | { |
| 1974 | switch (status) { |
| 1975 | case SRE_ERROR_RECURSION_LIMIT: |
| 1976 | PyErr_SetString( |
| 1977 | PyExc_RuntimeError, |
| 1978 | "maximum recursion limit exceeded" |
| 1979 | ); |
| 1980 | break; |
| 1981 | case SRE_ERROR_MEMORY: |
| 1982 | PyErr_NoMemory(); |
| 1983 | break; |
| 1984 | default: |
| 1985 | /* other error codes indicate compiler/engine bugs */ |
| 1986 | PyErr_SetString( |
| 1987 | PyExc_RuntimeError, |
| 1988 | "internal error in regular expression engine" |
| 1989 | ); |
| 1990 | } |
| 1991 | } |
| 1992 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1993 | static PyObject* |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1994 | pattern_new_match(PatternObject* pattern, SRE_STATE* state, int status) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1995 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1996 | /* create match object (from state object) */ |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1997 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1998 | MatchObject* match; |
| 1999 | int i, j; |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2000 | char* base; |
| 2001 | int n; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2002 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2003 | if (status > 0) { |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2004 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2005 | /* create match object (with room for extra group marks) */ |
| 2006 | match = PyObject_NEW_VAR(MatchObject, &Match_Type, |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2007 | 2*(pattern->groups+1)); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2008 | if (!match) |
| 2009 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2010 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2011 | Py_INCREF(pattern); |
| 2012 | match->pattern = pattern; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2013 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2014 | Py_INCREF(state->string); |
| 2015 | match->string = state->string; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2016 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2017 | match->regs = NULL; |
| 2018 | match->groups = pattern->groups+1; |
| 2019 | |
| 2020 | /* fill in group slices */ |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2021 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2022 | base = (char*) state->beginning; |
| 2023 | n = state->charsize; |
| 2024 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2025 | match->mark[0] = ((char*) state->start - base) / n; |
| 2026 | match->mark[1] = ((char*) state->ptr - base) / n; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2027 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2028 | for (i = j = 0; i < pattern->groups; i++, j+=2) |
| 2029 | if (j+1 <= state->lastmark && state->mark[j] && state->mark[j+1]) { |
| 2030 | match->mark[j+2] = ((char*) state->mark[j] - base) / n; |
| 2031 | match->mark[j+3] = ((char*) state->mark[j+1] - base) / n; |
| 2032 | } else |
| 2033 | match->mark[j+2] = match->mark[j+3] = -1; /* undefined */ |
| 2034 | |
| 2035 | match->pos = state->pos; |
| 2036 | match->endpos = state->endpos; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2037 | |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2038 | match->lastindex = state->lastindex; |
| 2039 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2040 | return (PyObject*) match; |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 2041 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 2042 | } else if (status == 0) { |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2043 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 2044 | /* no match */ |
| 2045 | Py_INCREF(Py_None); |
| 2046 | return Py_None; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2047 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2048 | } |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2049 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 2050 | /* internal error */ |
| 2051 | pattern_error(status); |
| 2052 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | static PyObject* |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 2056 | pattern_scanner(PatternObject* pattern, PyObject* args) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2057 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2058 | /* create search state object */ |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2059 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2060 | ScannerObject* self; |
| 2061 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2062 | PyObject* string; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2063 | int start = 0; |
| 2064 | int end = INT_MAX; |
| 2065 | if (!PyArg_ParseTuple(args, "O|ii:scanner", &string, &start, &end)) |
| 2066 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2067 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2068 | /* create scanner object */ |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 2069 | self = PyObject_NEW(ScannerObject, &Scanner_Type); |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2070 | if (!self) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2071 | return NULL; |
| 2072 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2073 | string = state_init(&self->state, pattern, string, start, end); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2074 | if (!string) { |
Fredrik Lundh | 1c5aa69 | 2001-01-16 07:37:30 +0000 | [diff] [blame] | 2075 | PyObject_DEL(self); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2076 | return NULL; |
| 2077 | } |
| 2078 | |
| 2079 | Py_INCREF(pattern); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2080 | self->pattern = (PyObject*) pattern; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2081 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2082 | return (PyObject*) self; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2083 | } |
| 2084 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2085 | static void |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2086 | pattern_dealloc(PatternObject* self) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2087 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2088 | Py_XDECREF(self->pattern); |
| 2089 | Py_XDECREF(self->groupindex); |
Fredrik Lundh | 6f5cba6 | 2001-01-16 07:05:29 +0000 | [diff] [blame] | 2090 | Py_XDECREF(self->indexgroup); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2091 | PyObject_DEL(self); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | static PyObject* |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 2095 | pattern_match(PatternObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2096 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2097 | SRE_STATE state; |
| 2098 | int status; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2099 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2100 | PyObject* string; |
| 2101 | int start = 0; |
| 2102 | int end = INT_MAX; |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 2103 | static char* kwlist[] = { "pattern", "pos", "endpos", NULL }; |
| 2104 | if (!PyArg_ParseTupleAndKeywords(args, kw, "O|ii:match", kwlist, |
| 2105 | &string, &start, &end)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2106 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2107 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2108 | string = state_init(&state, self, string, start, end); |
| 2109 | if (!string) |
| 2110 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2111 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2112 | state.ptr = state.start; |
| 2113 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 2114 | TRACE(("|%p|%p|MATCH\n", PatternObject_GetCode(self), state.ptr)); |
| 2115 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2116 | if (state.charsize == 1) { |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 2117 | status = sre_match(&state, PatternObject_GetCode(self), 1); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2118 | } else { |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2119 | #if defined(HAVE_UNICODE) |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 2120 | status = sre_umatch(&state, PatternObject_GetCode(self), 1); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2121 | #endif |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2122 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2123 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 2124 | TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr)); |
| 2125 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2126 | state_fini(&state); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2127 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2128 | return pattern_new_match(self, &state, status); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | static PyObject* |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 2132 | pattern_search(PatternObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2133 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2134 | SRE_STATE state; |
| 2135 | int status; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2136 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2137 | PyObject* string; |
| 2138 | int start = 0; |
| 2139 | int end = INT_MAX; |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 2140 | static char* kwlist[] = { "pattern", "pos", "endpos", NULL }; |
| 2141 | if (!PyArg_ParseTupleAndKeywords(args, kw, "O|ii:search", kwlist, |
| 2142 | &string, &start, &end)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2143 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2144 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2145 | string = state_init(&state, self, string, start, end); |
| 2146 | if (!string) |
| 2147 | return NULL; |
| 2148 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 2149 | TRACE(("|%p|%p|SEARCH\n", PatternObject_GetCode(self), state.ptr)); |
| 2150 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2151 | if (state.charsize == 1) { |
| 2152 | status = sre_search(&state, PatternObject_GetCode(self)); |
| 2153 | } else { |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2154 | #if defined(HAVE_UNICODE) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2155 | status = sre_usearch(&state, PatternObject_GetCode(self)); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2156 | #endif |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2157 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2158 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 2159 | TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr)); |
| 2160 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2161 | state_fini(&state); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2162 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2163 | return pattern_new_match(self, &state, status); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2164 | } |
| 2165 | |
| 2166 | static PyObject* |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2167 | call(char* module, char* function, PyObject* args) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2168 | { |
| 2169 | PyObject* name; |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2170 | PyObject* mod; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2171 | PyObject* func; |
| 2172 | PyObject* result; |
| 2173 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2174 | if (!args) |
| 2175 | return NULL; |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2176 | name = PyString_FromString(module); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2177 | if (!name) |
| 2178 | return NULL; |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2179 | mod = PyImport_Import(name); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2180 | Py_DECREF(name); |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2181 | if (!mod) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2182 | return NULL; |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2183 | func = PyObject_GetAttrString(mod, function); |
| 2184 | Py_DECREF(mod); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2185 | if (!func) |
| 2186 | return NULL; |
| 2187 | result = PyObject_CallObject(func, args); |
| 2188 | Py_DECREF(func); |
| 2189 | Py_DECREF(args); |
| 2190 | return result; |
| 2191 | } |
| 2192 | |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2193 | #ifdef USE_BUILTIN_COPY |
| 2194 | static int |
| 2195 | deepcopy(PyObject** object, PyObject* memo) |
| 2196 | { |
| 2197 | PyObject* copy; |
| 2198 | |
| 2199 | copy = call( |
| 2200 | "copy", "deepcopy", |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2201 | PyTuple_Pack(2, *object, memo) |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2202 | ); |
| 2203 | if (!copy) |
| 2204 | return 0; |
| 2205 | |
| 2206 | Py_DECREF(*object); |
| 2207 | *object = copy; |
| 2208 | |
| 2209 | return 1; /* success */ |
| 2210 | } |
| 2211 | #endif |
| 2212 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2213 | static PyObject* |
Michael W. Hudson | b6a4505 | 2002-07-31 09:54:24 +0000 | [diff] [blame] | 2214 | join_list(PyObject* list, PyObject* pattern) |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2215 | { |
| 2216 | /* join list elements */ |
| 2217 | |
| 2218 | PyObject* joiner; |
| 2219 | #if PY_VERSION_HEX >= 0x01060000 |
| 2220 | PyObject* function; |
| 2221 | PyObject* args; |
| 2222 | #endif |
| 2223 | PyObject* result; |
| 2224 | |
| 2225 | switch (PyList_GET_SIZE(list)) { |
| 2226 | case 0: |
| 2227 | Py_DECREF(list); |
Fredrik Lundh | 09705f0 | 2002-11-22 12:46:35 +0000 | [diff] [blame] | 2228 | return PySequence_GetSlice(pattern, 0, 0); |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2229 | case 1: |
| 2230 | result = PyList_GET_ITEM(list, 0); |
| 2231 | Py_INCREF(result); |
| 2232 | Py_DECREF(list); |
| 2233 | return result; |
| 2234 | } |
| 2235 | |
| 2236 | /* two or more elements: slice out a suitable separator from the |
| 2237 | first member, and use that to join the entire list */ |
| 2238 | |
| 2239 | joiner = PySequence_GetSlice(pattern, 0, 0); |
| 2240 | if (!joiner) |
| 2241 | return NULL; |
| 2242 | |
| 2243 | #if PY_VERSION_HEX >= 0x01060000 |
| 2244 | function = PyObject_GetAttrString(joiner, "join"); |
| 2245 | if (!function) { |
| 2246 | Py_DECREF(joiner); |
| 2247 | return NULL; |
| 2248 | } |
| 2249 | args = PyTuple_New(1); |
Fredrik Lundh | 1296a8d | 2001-10-21 18:04:11 +0000 | [diff] [blame] | 2250 | if (!args) { |
| 2251 | Py_DECREF(function); |
| 2252 | Py_DECREF(joiner); |
| 2253 | return NULL; |
| 2254 | } |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2255 | PyTuple_SET_ITEM(args, 0, list); |
| 2256 | result = PyObject_CallObject(function, args); |
| 2257 | Py_DECREF(args); /* also removes list */ |
| 2258 | Py_DECREF(function); |
| 2259 | #else |
| 2260 | result = call( |
| 2261 | "string", "join", |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2262 | PyTuple_Pack(2, list, joiner) |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2263 | ); |
| 2264 | #endif |
| 2265 | Py_DECREF(joiner); |
| 2266 | |
| 2267 | return result; |
| 2268 | } |
| 2269 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2270 | static PyObject* |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 2271 | pattern_findall(PatternObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2272 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2273 | SRE_STATE state; |
| 2274 | PyObject* list; |
| 2275 | int status; |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 2276 | int i, b, e; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2277 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2278 | PyObject* string; |
| 2279 | int start = 0; |
| 2280 | int end = INT_MAX; |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 2281 | static char* kwlist[] = { "source", "pos", "endpos", NULL }; |
| 2282 | if (!PyArg_ParseTupleAndKeywords(args, kw, "O|ii:findall", kwlist, |
| 2283 | &string, &start, &end)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2284 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2285 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2286 | string = state_init(&state, self, string, start, end); |
| 2287 | if (!string) |
| 2288 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2289 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2290 | list = PyList_New(0); |
Fredrik Lundh | 1296a8d | 2001-10-21 18:04:11 +0000 | [diff] [blame] | 2291 | if (!list) { |
| 2292 | state_fini(&state); |
| 2293 | return NULL; |
| 2294 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2295 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2296 | while (state.start <= state.end) { |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2297 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2298 | PyObject* item; |
| 2299 | |
Fredrik Lundh | ebc37b2 | 2000-10-28 19:30:41 +0000 | [diff] [blame] | 2300 | state_reset(&state); |
| 2301 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2302 | state.ptr = state.start; |
| 2303 | |
| 2304 | if (state.charsize == 1) { |
| 2305 | status = sre_search(&state, PatternObject_GetCode(self)); |
| 2306 | } else { |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2307 | #if defined(HAVE_UNICODE) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2308 | status = sre_usearch(&state, PatternObject_GetCode(self)); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2309 | #endif |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2310 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2311 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2312 | if (status <= 0) { |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2313 | if (status == 0) |
| 2314 | break; |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 2315 | pattern_error(status); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2316 | goto error; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2317 | } |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2318 | |
| 2319 | /* don't bother to build a match object */ |
| 2320 | switch (self->groups) { |
| 2321 | case 0: |
| 2322 | b = STATE_OFFSET(&state, state.start); |
| 2323 | e = STATE_OFFSET(&state, state.ptr); |
| 2324 | item = PySequence_GetSlice(string, b, e); |
| 2325 | if (!item) |
| 2326 | goto error; |
| 2327 | break; |
| 2328 | case 1: |
| 2329 | item = state_getslice(&state, 1, string, 1); |
| 2330 | if (!item) |
| 2331 | goto error; |
| 2332 | break; |
| 2333 | default: |
| 2334 | item = PyTuple_New(self->groups); |
| 2335 | if (!item) |
| 2336 | goto error; |
| 2337 | for (i = 0; i < self->groups; i++) { |
| 2338 | PyObject* o = state_getslice(&state, i+1, string, 1); |
| 2339 | if (!o) { |
| 2340 | Py_DECREF(item); |
| 2341 | goto error; |
| 2342 | } |
| 2343 | PyTuple_SET_ITEM(item, i, o); |
| 2344 | } |
| 2345 | break; |
| 2346 | } |
| 2347 | |
| 2348 | status = PyList_Append(list, item); |
| 2349 | Py_DECREF(item); |
| 2350 | if (status < 0) |
| 2351 | goto error; |
| 2352 | |
| 2353 | if (state.ptr == state.start) |
| 2354 | state.start = (void*) ((char*) state.ptr + state.charsize); |
| 2355 | else |
| 2356 | state.start = state.ptr; |
| 2357 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2358 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2359 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2360 | state_fini(&state); |
| 2361 | return list; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2362 | |
| 2363 | error: |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2364 | Py_DECREF(list); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2365 | state_fini(&state); |
| 2366 | return NULL; |
| 2367 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2368 | } |
| 2369 | |
Fredrik Lundh | 703ce81 | 2001-10-24 22:16:30 +0000 | [diff] [blame] | 2370 | #if PY_VERSION_HEX >= 0x02020000 |
| 2371 | static PyObject* |
| 2372 | pattern_finditer(PatternObject* pattern, PyObject* args) |
| 2373 | { |
| 2374 | PyObject* scanner; |
| 2375 | PyObject* search; |
| 2376 | PyObject* iterator; |
| 2377 | |
| 2378 | scanner = pattern_scanner(pattern, args); |
| 2379 | if (!scanner) |
| 2380 | return NULL; |
| 2381 | |
| 2382 | search = PyObject_GetAttrString(scanner, "search"); |
| 2383 | Py_DECREF(scanner); |
| 2384 | if (!search) |
| 2385 | return NULL; |
| 2386 | |
| 2387 | iterator = PyCallIter_New(search, Py_None); |
| 2388 | Py_DECREF(search); |
| 2389 | |
| 2390 | return iterator; |
| 2391 | } |
| 2392 | #endif |
| 2393 | |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 2394 | static PyObject* |
| 2395 | pattern_split(PatternObject* self, PyObject* args, PyObject* kw) |
| 2396 | { |
| 2397 | SRE_STATE state; |
| 2398 | PyObject* list; |
| 2399 | PyObject* item; |
| 2400 | int status; |
| 2401 | int n; |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2402 | int i; |
| 2403 | void* last; |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 2404 | |
| 2405 | PyObject* string; |
| 2406 | int maxsplit = 0; |
| 2407 | static char* kwlist[] = { "source", "maxsplit", NULL }; |
| 2408 | if (!PyArg_ParseTupleAndKeywords(args, kw, "O|i:split", kwlist, |
| 2409 | &string, &maxsplit)) |
| 2410 | return NULL; |
| 2411 | |
| 2412 | string = state_init(&state, self, string, 0, INT_MAX); |
| 2413 | if (!string) |
| 2414 | return NULL; |
| 2415 | |
| 2416 | list = PyList_New(0); |
Fredrik Lundh | 1296a8d | 2001-10-21 18:04:11 +0000 | [diff] [blame] | 2417 | if (!list) { |
| 2418 | state_fini(&state); |
| 2419 | return NULL; |
| 2420 | } |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 2421 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2422 | n = 0; |
| 2423 | last = state.start; |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 2424 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2425 | while (!maxsplit || n < maxsplit) { |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 2426 | |
| 2427 | state_reset(&state); |
| 2428 | |
| 2429 | state.ptr = state.start; |
| 2430 | |
| 2431 | if (state.charsize == 1) { |
| 2432 | status = sre_search(&state, PatternObject_GetCode(self)); |
| 2433 | } else { |
| 2434 | #if defined(HAVE_UNICODE) |
| 2435 | status = sre_usearch(&state, PatternObject_GetCode(self)); |
| 2436 | #endif |
| 2437 | } |
| 2438 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2439 | if (status <= 0) { |
| 2440 | if (status == 0) |
| 2441 | break; |
| 2442 | pattern_error(status); |
| 2443 | goto error; |
| 2444 | } |
| 2445 | |
| 2446 | if (state.start == state.ptr) { |
| 2447 | if (last == state.end) |
| 2448 | break; |
| 2449 | /* skip one character */ |
| 2450 | state.start = (void*) ((char*) state.ptr + state.charsize); |
| 2451 | continue; |
| 2452 | } |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 2453 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2454 | /* get segment before this match */ |
| 2455 | item = PySequence_GetSlice( |
| 2456 | string, STATE_OFFSET(&state, last), |
| 2457 | STATE_OFFSET(&state, state.start) |
| 2458 | ); |
| 2459 | if (!item) |
| 2460 | goto error; |
| 2461 | status = PyList_Append(list, item); |
| 2462 | Py_DECREF(item); |
| 2463 | if (status < 0) |
| 2464 | goto error; |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 2465 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2466 | /* add groups (if any) */ |
| 2467 | for (i = 0; i < self->groups; i++) { |
| 2468 | item = state_getslice(&state, i+1, string, 0); |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 2469 | if (!item) |
| 2470 | goto error; |
| 2471 | status = PyList_Append(list, item); |
| 2472 | Py_DECREF(item); |
| 2473 | if (status < 0) |
| 2474 | goto error; |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 2475 | } |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2476 | |
| 2477 | n = n + 1; |
| 2478 | |
| 2479 | last = state.start = state.ptr; |
| 2480 | |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 2481 | } |
| 2482 | |
Fredrik Lundh | f864aa8 | 2001-10-22 06:01:56 +0000 | [diff] [blame] | 2483 | /* get segment following last match (even if empty) */ |
| 2484 | item = PySequence_GetSlice( |
| 2485 | string, STATE_OFFSET(&state, last), state.endpos |
| 2486 | ); |
| 2487 | if (!item) |
| 2488 | goto error; |
| 2489 | status = PyList_Append(list, item); |
| 2490 | Py_DECREF(item); |
| 2491 | if (status < 0) |
| 2492 | goto error; |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 2493 | |
| 2494 | state_fini(&state); |
| 2495 | return list; |
| 2496 | |
| 2497 | error: |
| 2498 | Py_DECREF(list); |
| 2499 | state_fini(&state); |
| 2500 | return NULL; |
| 2501 | |
| 2502 | } |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 2503 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2504 | static PyObject* |
| 2505 | pattern_subx(PatternObject* self, PyObject* template, PyObject* string, |
| 2506 | int count, int subn) |
| 2507 | { |
| 2508 | SRE_STATE state; |
| 2509 | PyObject* list; |
| 2510 | PyObject* item; |
| 2511 | PyObject* filter; |
| 2512 | PyObject* args; |
| 2513 | PyObject* match; |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 2514 | void* ptr; |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2515 | int status; |
| 2516 | int n; |
| 2517 | int i, b, e; |
| 2518 | int filter_is_callable; |
| 2519 | |
Fredrik Lundh | dac5849 | 2001-10-21 21:48:30 +0000 | [diff] [blame] | 2520 | if (PyCallable_Check(template)) { |
| 2521 | /* sub/subn takes either a function or a template */ |
| 2522 | filter = template; |
| 2523 | Py_INCREF(filter); |
| 2524 | filter_is_callable = 1; |
| 2525 | } else { |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 2526 | /* if not callable, check if it's a literal string */ |
| 2527 | int literal; |
| 2528 | ptr = getstring(template, &n, &b); |
| 2529 | if (ptr) { |
| 2530 | if (b == 1) { |
| 2531 | literal = sre_literal_template(ptr, n); |
| 2532 | } else { |
| 2533 | #if defined(HAVE_UNICODE) |
| 2534 | literal = sre_uliteral_template(ptr, n); |
| 2535 | #endif |
| 2536 | } |
| 2537 | } else { |
| 2538 | PyErr_Clear(); |
| 2539 | literal = 0; |
| 2540 | } |
| 2541 | if (literal) { |
| 2542 | filter = template; |
| 2543 | Py_INCREF(filter); |
| 2544 | filter_is_callable = 0; |
| 2545 | } else { |
| 2546 | /* not a literal; hand it over to the template compiler */ |
| 2547 | filter = call( |
| 2548 | SRE_MODULE, "_subx", |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2549 | PyTuple_Pack(2, self, template) |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 2550 | ); |
| 2551 | if (!filter) |
| 2552 | return NULL; |
| 2553 | filter_is_callable = PyCallable_Check(filter); |
| 2554 | } |
Fredrik Lundh | dac5849 | 2001-10-21 21:48:30 +0000 | [diff] [blame] | 2555 | } |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2556 | |
| 2557 | string = state_init(&state, self, string, 0, INT_MAX); |
Fredrik Lundh | 82b2307 | 2001-12-09 16:13:15 +0000 | [diff] [blame] | 2558 | if (!string) { |
| 2559 | Py_DECREF(filter); |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2560 | return NULL; |
Fredrik Lundh | 82b2307 | 2001-12-09 16:13:15 +0000 | [diff] [blame] | 2561 | } |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2562 | |
| 2563 | list = PyList_New(0); |
Fredrik Lundh | 1296a8d | 2001-10-21 18:04:11 +0000 | [diff] [blame] | 2564 | if (!list) { |
Fredrik Lundh | 82b2307 | 2001-12-09 16:13:15 +0000 | [diff] [blame] | 2565 | Py_DECREF(filter); |
Fredrik Lundh | 1296a8d | 2001-10-21 18:04:11 +0000 | [diff] [blame] | 2566 | state_fini(&state); |
| 2567 | return NULL; |
| 2568 | } |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2569 | |
| 2570 | n = i = 0; |
| 2571 | |
| 2572 | while (!count || n < count) { |
| 2573 | |
| 2574 | state_reset(&state); |
| 2575 | |
| 2576 | state.ptr = state.start; |
| 2577 | |
| 2578 | if (state.charsize == 1) { |
| 2579 | status = sre_search(&state, PatternObject_GetCode(self)); |
| 2580 | } else { |
| 2581 | #if defined(HAVE_UNICODE) |
| 2582 | status = sre_usearch(&state, PatternObject_GetCode(self)); |
| 2583 | #endif |
| 2584 | } |
| 2585 | |
| 2586 | if (status <= 0) { |
| 2587 | if (status == 0) |
| 2588 | break; |
| 2589 | pattern_error(status); |
| 2590 | goto error; |
| 2591 | } |
| 2592 | |
| 2593 | b = STATE_OFFSET(&state, state.start); |
| 2594 | e = STATE_OFFSET(&state, state.ptr); |
| 2595 | |
| 2596 | if (i < b) { |
| 2597 | /* get segment before this match */ |
| 2598 | item = PySequence_GetSlice(string, i, b); |
| 2599 | if (!item) |
| 2600 | goto error; |
| 2601 | status = PyList_Append(list, item); |
| 2602 | Py_DECREF(item); |
| 2603 | if (status < 0) |
| 2604 | goto error; |
| 2605 | |
| 2606 | } else if (i == b && i == e && n > 0) |
| 2607 | /* ignore empty match on latest position */ |
| 2608 | goto next; |
| 2609 | |
| 2610 | if (filter_is_callable) { |
Fredrik Lundh | dac5849 | 2001-10-21 21:48:30 +0000 | [diff] [blame] | 2611 | /* pass match object through filter */ |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2612 | match = pattern_new_match(self, &state, 1); |
| 2613 | if (!match) |
| 2614 | goto error; |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2615 | args = PyTuple_Pack(1, match); |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2616 | if (!args) { |
Guido van Rossum | 4e17384 | 2001-12-07 04:25:10 +0000 | [diff] [blame] | 2617 | Py_DECREF(match); |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2618 | goto error; |
| 2619 | } |
| 2620 | item = PyObject_CallObject(filter, args); |
| 2621 | Py_DECREF(args); |
| 2622 | Py_DECREF(match); |
| 2623 | if (!item) |
| 2624 | goto error; |
| 2625 | } else { |
| 2626 | /* filter is literal string */ |
| 2627 | item = filter; |
Fredrik Lundh | dac5849 | 2001-10-21 21:48:30 +0000 | [diff] [blame] | 2628 | Py_INCREF(item); |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2629 | } |
| 2630 | |
| 2631 | /* add to list */ |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 2632 | if (item != Py_None) { |
| 2633 | status = PyList_Append(list, item); |
| 2634 | Py_DECREF(item); |
| 2635 | if (status < 0) |
| 2636 | goto error; |
| 2637 | } |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2638 | |
| 2639 | i = e; |
| 2640 | n = n + 1; |
| 2641 | |
| 2642 | next: |
| 2643 | /* move on */ |
| 2644 | if (state.ptr == state.start) |
| 2645 | state.start = (void*) ((char*) state.ptr + state.charsize); |
| 2646 | else |
| 2647 | state.start = state.ptr; |
| 2648 | |
| 2649 | } |
| 2650 | |
| 2651 | /* get segment following last match */ |
Fredrik Lundh | dac5849 | 2001-10-21 21:48:30 +0000 | [diff] [blame] | 2652 | if (i < state.endpos) { |
| 2653 | item = PySequence_GetSlice(string, i, state.endpos); |
| 2654 | if (!item) |
| 2655 | goto error; |
| 2656 | status = PyList_Append(list, item); |
| 2657 | Py_DECREF(item); |
| 2658 | if (status < 0) |
| 2659 | goto error; |
| 2660 | } |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2661 | |
| 2662 | state_fini(&state); |
| 2663 | |
Guido van Rossum | 4e17384 | 2001-12-07 04:25:10 +0000 | [diff] [blame] | 2664 | Py_DECREF(filter); |
| 2665 | |
Fredrik Lundh | dac5849 | 2001-10-21 21:48:30 +0000 | [diff] [blame] | 2666 | /* convert list to single string (also removes list) */ |
Michael W. Hudson | b6a4505 | 2002-07-31 09:54:24 +0000 | [diff] [blame] | 2667 | item = join_list(list, self->pattern); |
Fredrik Lundh | dac5849 | 2001-10-21 21:48:30 +0000 | [diff] [blame] | 2668 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2669 | if (!item) |
| 2670 | return NULL; |
| 2671 | |
| 2672 | if (subn) |
| 2673 | return Py_BuildValue("Ni", item, n); |
| 2674 | |
| 2675 | return item; |
| 2676 | |
| 2677 | error: |
| 2678 | Py_DECREF(list); |
| 2679 | state_fini(&state); |
Fredrik Lundh | 82b2307 | 2001-12-09 16:13:15 +0000 | [diff] [blame] | 2680 | Py_DECREF(filter); |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2681 | return NULL; |
| 2682 | |
| 2683 | } |
| 2684 | |
| 2685 | static PyObject* |
| 2686 | pattern_sub(PatternObject* self, PyObject* args, PyObject* kw) |
| 2687 | { |
| 2688 | PyObject* template; |
| 2689 | PyObject* string; |
| 2690 | int count = 0; |
| 2691 | static char* kwlist[] = { "repl", "string", "count", NULL }; |
| 2692 | if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|i:sub", kwlist, |
| 2693 | &template, &string, &count)) |
| 2694 | return NULL; |
| 2695 | |
| 2696 | return pattern_subx(self, template, string, count, 0); |
| 2697 | } |
| 2698 | |
| 2699 | static PyObject* |
| 2700 | pattern_subn(PatternObject* self, PyObject* args, PyObject* kw) |
| 2701 | { |
| 2702 | PyObject* template; |
| 2703 | PyObject* string; |
| 2704 | int count = 0; |
| 2705 | static char* kwlist[] = { "repl", "string", "count", NULL }; |
| 2706 | if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|i:subn", kwlist, |
| 2707 | &template, &string, &count)) |
| 2708 | return NULL; |
| 2709 | |
| 2710 | return pattern_subx(self, template, string, count, 1); |
| 2711 | } |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 2712 | |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2713 | static PyObject* |
| 2714 | pattern_copy(PatternObject* self, PyObject* args) |
| 2715 | { |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2716 | #ifdef USE_BUILTIN_COPY |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2717 | PatternObject* copy; |
| 2718 | int offset; |
| 2719 | |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2720 | if (args != Py_None && !PyArg_ParseTuple(args, ":__copy__")) |
| 2721 | return NULL; |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2722 | |
| 2723 | copy = PyObject_NEW_VAR(PatternObject, &Pattern_Type, self->codesize); |
| 2724 | if (!copy) |
| 2725 | return NULL; |
| 2726 | |
| 2727 | offset = offsetof(PatternObject, groups); |
| 2728 | |
| 2729 | Py_XINCREF(self->groupindex); |
| 2730 | Py_XINCREF(self->indexgroup); |
| 2731 | Py_XINCREF(self->pattern); |
| 2732 | |
| 2733 | memcpy((char*) copy + offset, (char*) self + offset, |
| 2734 | sizeof(PatternObject) + self->codesize * sizeof(SRE_CODE) - offset); |
| 2735 | |
| 2736 | return (PyObject*) copy; |
| 2737 | #else |
| 2738 | PyErr_SetString(PyExc_TypeError, "cannot copy this pattern object"); |
| 2739 | return NULL; |
| 2740 | #endif |
| 2741 | } |
| 2742 | |
| 2743 | static PyObject* |
| 2744 | pattern_deepcopy(PatternObject* self, PyObject* args) |
| 2745 | { |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2746 | #ifdef USE_BUILTIN_COPY |
| 2747 | PatternObject* copy; |
| 2748 | |
| 2749 | PyObject* memo; |
| 2750 | if (!PyArg_ParseTuple(args, "O:__deepcopy__", &memo)) |
| 2751 | return NULL; |
| 2752 | |
| 2753 | copy = (PatternObject*) pattern_copy(self, Py_None); |
| 2754 | if (!copy) |
| 2755 | return NULL; |
| 2756 | |
| 2757 | if (!deepcopy(©->groupindex, memo) || |
| 2758 | !deepcopy(©->indexgroup, memo) || |
| 2759 | !deepcopy(©->pattern, memo)) { |
| 2760 | Py_DECREF(copy); |
| 2761 | return NULL; |
| 2762 | } |
| 2763 | |
| 2764 | #else |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2765 | PyErr_SetString(PyExc_TypeError, "cannot deepcopy this pattern object"); |
| 2766 | return NULL; |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2767 | #endif |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2770 | static PyMethodDef pattern_methods[] = { |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 2771 | {"match", (PyCFunction) pattern_match, METH_VARARGS|METH_KEYWORDS}, |
| 2772 | {"search", (PyCFunction) pattern_search, METH_VARARGS|METH_KEYWORDS}, |
| 2773 | {"sub", (PyCFunction) pattern_sub, METH_VARARGS|METH_KEYWORDS}, |
| 2774 | {"subn", (PyCFunction) pattern_subn, METH_VARARGS|METH_KEYWORDS}, |
| 2775 | {"split", (PyCFunction) pattern_split, METH_VARARGS|METH_KEYWORDS}, |
| 2776 | {"findall", (PyCFunction) pattern_findall, METH_VARARGS|METH_KEYWORDS}, |
Fredrik Lundh | 703ce81 | 2001-10-24 22:16:30 +0000 | [diff] [blame] | 2777 | #if PY_VERSION_HEX >= 0x02020000 |
| 2778 | {"finditer", (PyCFunction) pattern_finditer, METH_VARARGS}, |
| 2779 | #endif |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 2780 | {"scanner", (PyCFunction) pattern_scanner, METH_VARARGS}, |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2781 | {"__copy__", (PyCFunction) pattern_copy, METH_VARARGS}, |
| 2782 | {"__deepcopy__", (PyCFunction) pattern_deepcopy, METH_VARARGS}, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2783 | {NULL, NULL} |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2784 | }; |
| 2785 | |
| 2786 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2787 | pattern_getattr(PatternObject* self, char* name) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2788 | { |
| 2789 | PyObject* res; |
| 2790 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2791 | res = Py_FindMethod(pattern_methods, (PyObject*) self, name); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2792 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2793 | if (res) |
| 2794 | return res; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2795 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2796 | PyErr_Clear(); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2797 | |
| 2798 | /* attributes */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2799 | if (!strcmp(name, "pattern")) { |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2800 | Py_INCREF(self->pattern); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2801 | return self->pattern; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2802 | } |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2803 | |
| 2804 | if (!strcmp(name, "flags")) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2805 | return Py_BuildValue("i", self->flags); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2806 | |
Fredrik Lundh | 01016fe | 2000-06-30 00:27:46 +0000 | [diff] [blame] | 2807 | if (!strcmp(name, "groups")) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2808 | return Py_BuildValue("i", self->groups); |
Fredrik Lundh | 01016fe | 2000-06-30 00:27:46 +0000 | [diff] [blame] | 2809 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2810 | if (!strcmp(name, "groupindex") && self->groupindex) { |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2811 | Py_INCREF(self->groupindex); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2812 | return self->groupindex; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2813 | } |
| 2814 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2815 | PyErr_SetString(PyExc_AttributeError, name); |
| 2816 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2817 | } |
| 2818 | |
| 2819 | statichere PyTypeObject Pattern_Type = { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2820 | PyObject_HEAD_INIT(NULL) |
Fredrik Lundh | 82b2307 | 2001-12-09 16:13:15 +0000 | [diff] [blame] | 2821 | 0, "_" SRE_MODULE ".SRE_Pattern", |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2822 | sizeof(PatternObject), sizeof(SRE_CODE), |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2823 | (destructor)pattern_dealloc, /*tp_dealloc*/ |
| 2824 | 0, /*tp_print*/ |
| 2825 | (getattrfunc)pattern_getattr /*tp_getattr*/ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2826 | }; |
| 2827 | |
| 2828 | /* -------------------------------------------------------------------- */ |
| 2829 | /* match methods */ |
| 2830 | |
| 2831 | static void |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2832 | match_dealloc(MatchObject* self) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2833 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2834 | Py_XDECREF(self->regs); |
| 2835 | Py_XDECREF(self->string); |
| 2836 | Py_DECREF(self->pattern); |
| 2837 | PyObject_DEL(self); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | static PyObject* |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 2841 | match_getslice_by_index(MatchObject* self, int index, PyObject* def) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2842 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2843 | if (index < 0 || index >= self->groups) { |
| 2844 | /* raise IndexError if we were given a bad group number */ |
| 2845 | PyErr_SetString( |
| 2846 | PyExc_IndexError, |
| 2847 | "no such group" |
| 2848 | ); |
| 2849 | return NULL; |
| 2850 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2851 | |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2852 | index *= 2; |
| 2853 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2854 | if (self->string == Py_None || self->mark[index] < 0) { |
| 2855 | /* return default value if the string or group is undefined */ |
| 2856 | Py_INCREF(def); |
| 2857 | return def; |
| 2858 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2859 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2860 | return PySequence_GetSlice( |
| 2861 | self->string, self->mark[index], self->mark[index+1] |
| 2862 | ); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2863 | } |
| 2864 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2865 | static int |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2866 | match_getindex(MatchObject* self, PyObject* index) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2867 | { |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2868 | int i; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2869 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2870 | if (PyInt_Check(index)) |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2871 | return (int) PyInt_AS_LONG(index); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2872 | |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2873 | i = -1; |
| 2874 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2875 | if (self->pattern->groupindex) { |
| 2876 | index = PyObject_GetItem(self->pattern->groupindex, index); |
| 2877 | if (index) { |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2878 | if (PyInt_Check(index)) |
| 2879 | i = (int) PyInt_AS_LONG(index); |
| 2880 | Py_DECREF(index); |
| 2881 | } else |
| 2882 | PyErr_Clear(); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2883 | } |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2884 | |
| 2885 | return i; |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
| 2888 | static PyObject* |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 2889 | match_getslice(MatchObject* self, PyObject* index, PyObject* def) |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2890 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2891 | return match_getslice_by_index(self, match_getindex(self, index), def); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
| 2894 | static PyObject* |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 2895 | match_expand(MatchObject* self, PyObject* args) |
| 2896 | { |
| 2897 | PyObject* template; |
| 2898 | if (!PyArg_ParseTuple(args, "O:expand", &template)) |
| 2899 | return NULL; |
| 2900 | |
| 2901 | /* delegate to Python code */ |
| 2902 | return call( |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2903 | SRE_MODULE, "_expand", |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 2904 | PyTuple_Pack(3, self->pattern, self, template) |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 2905 | ); |
| 2906 | } |
| 2907 | |
| 2908 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2909 | match_group(MatchObject* self, PyObject* args) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2910 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2911 | PyObject* result; |
| 2912 | int i, size; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2913 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2914 | size = PyTuple_GET_SIZE(args); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2915 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2916 | switch (size) { |
| 2917 | case 0: |
| 2918 | result = match_getslice(self, Py_False, Py_None); |
| 2919 | break; |
| 2920 | case 1: |
| 2921 | result = match_getslice(self, PyTuple_GET_ITEM(args, 0), Py_None); |
| 2922 | break; |
| 2923 | default: |
| 2924 | /* fetch multiple items */ |
| 2925 | result = PyTuple_New(size); |
| 2926 | if (!result) |
| 2927 | return NULL; |
| 2928 | for (i = 0; i < size; i++) { |
| 2929 | PyObject* item = match_getslice( |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 2930 | self, PyTuple_GET_ITEM(args, i), Py_None |
| 2931 | ); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2932 | if (!item) { |
| 2933 | Py_DECREF(result); |
| 2934 | return NULL; |
| 2935 | } |
| 2936 | PyTuple_SET_ITEM(result, i, item); |
| 2937 | } |
| 2938 | break; |
| 2939 | } |
| 2940 | return result; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2941 | } |
| 2942 | |
| 2943 | static PyObject* |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 2944 | match_groups(MatchObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2945 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2946 | PyObject* result; |
| 2947 | int index; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2948 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2949 | PyObject* def = Py_None; |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 2950 | static char* kwlist[] = { "default", NULL }; |
| 2951 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:groups", kwlist, &def)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2952 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2953 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2954 | result = PyTuple_New(self->groups-1); |
| 2955 | if (!result) |
| 2956 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2957 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2958 | for (index = 1; index < self->groups; index++) { |
| 2959 | PyObject* item; |
| 2960 | item = match_getslice_by_index(self, index, def); |
| 2961 | if (!item) { |
| 2962 | Py_DECREF(result); |
| 2963 | return NULL; |
| 2964 | } |
| 2965 | PyTuple_SET_ITEM(result, index-1, item); |
| 2966 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2967 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2968 | return result; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | static PyObject* |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 2972 | match_groupdict(MatchObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2973 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2974 | PyObject* result; |
| 2975 | PyObject* keys; |
| 2976 | int index; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2977 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2978 | PyObject* def = Py_None; |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 2979 | static char* kwlist[] = { "default", NULL }; |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2980 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:groupdict", kwlist, &def)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2981 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2982 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2983 | result = PyDict_New(); |
| 2984 | if (!result || !self->pattern->groupindex) |
| 2985 | return result; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2986 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2987 | keys = PyMapping_Keys(self->pattern->groupindex); |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2988 | if (!keys) |
| 2989 | goto failed; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2990 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2991 | for (index = 0; index < PyList_GET_SIZE(keys); index++) { |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2992 | int status; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2993 | PyObject* key; |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2994 | PyObject* value; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2995 | key = PyList_GET_ITEM(keys, index); |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2996 | if (!key) |
| 2997 | goto failed; |
| 2998 | value = match_getslice(self, key, def); |
| 2999 | if (!value) { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3000 | Py_DECREF(key); |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 3001 | goto failed; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3002 | } |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 3003 | status = PyDict_SetItem(result, key, value); |
| 3004 | Py_DECREF(value); |
| 3005 | if (status < 0) |
| 3006 | goto failed; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3007 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3008 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3009 | Py_DECREF(keys); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3010 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3011 | return result; |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 3012 | |
| 3013 | failed: |
| 3014 | Py_DECREF(keys); |
| 3015 | Py_DECREF(result); |
| 3016 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 3020 | match_start(MatchObject* self, PyObject* args) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3021 | { |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 3022 | int index; |
| 3023 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3024 | PyObject* index_ = Py_False; /* zero */ |
| 3025 | if (!PyArg_ParseTuple(args, "|O:start", &index_)) |
| 3026 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3027 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 3028 | index = match_getindex(self, index_); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 3029 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3030 | if (index < 0 || index >= self->groups) { |
| 3031 | PyErr_SetString( |
| 3032 | PyExc_IndexError, |
| 3033 | "no such group" |
| 3034 | ); |
| 3035 | return NULL; |
| 3036 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3037 | |
Fredrik Lundh | 510c97b | 2000-09-02 16:36:57 +0000 | [diff] [blame] | 3038 | /* mark is -1 if group is undefined */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3039 | return Py_BuildValue("i", self->mark[index*2]); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3040 | } |
| 3041 | |
| 3042 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 3043 | match_end(MatchObject* self, PyObject* args) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3044 | { |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 3045 | int index; |
| 3046 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3047 | PyObject* index_ = Py_False; /* zero */ |
| 3048 | if (!PyArg_ParseTuple(args, "|O:end", &index_)) |
| 3049 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3050 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 3051 | index = match_getindex(self, index_); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 3052 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3053 | if (index < 0 || index >= self->groups) { |
| 3054 | PyErr_SetString( |
| 3055 | PyExc_IndexError, |
| 3056 | "no such group" |
| 3057 | ); |
| 3058 | return NULL; |
| 3059 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3060 | |
Fredrik Lundh | 510c97b | 2000-09-02 16:36:57 +0000 | [diff] [blame] | 3061 | /* mark is -1 if group is undefined */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3062 | return Py_BuildValue("i", self->mark[index*2+1]); |
| 3063 | } |
| 3064 | |
| 3065 | LOCAL(PyObject*) |
| 3066 | _pair(int i1, int i2) |
| 3067 | { |
| 3068 | PyObject* pair; |
| 3069 | PyObject* item; |
| 3070 | |
| 3071 | pair = PyTuple_New(2); |
| 3072 | if (!pair) |
| 3073 | return NULL; |
| 3074 | |
| 3075 | item = PyInt_FromLong(i1); |
| 3076 | if (!item) |
| 3077 | goto error; |
| 3078 | PyTuple_SET_ITEM(pair, 0, item); |
| 3079 | |
| 3080 | item = PyInt_FromLong(i2); |
| 3081 | if (!item) |
| 3082 | goto error; |
| 3083 | PyTuple_SET_ITEM(pair, 1, item); |
| 3084 | |
| 3085 | return pair; |
| 3086 | |
| 3087 | error: |
| 3088 | Py_DECREF(pair); |
| 3089 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 3093 | match_span(MatchObject* self, PyObject* args) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3094 | { |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 3095 | int index; |
| 3096 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3097 | PyObject* index_ = Py_False; /* zero */ |
| 3098 | if (!PyArg_ParseTuple(args, "|O:span", &index_)) |
| 3099 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3100 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 3101 | index = match_getindex(self, index_); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 3102 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3103 | if (index < 0 || index >= self->groups) { |
| 3104 | PyErr_SetString( |
| 3105 | PyExc_IndexError, |
| 3106 | "no such group" |
| 3107 | ); |
| 3108 | return NULL; |
| 3109 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3110 | |
Fredrik Lundh | 510c97b | 2000-09-02 16:36:57 +0000 | [diff] [blame] | 3111 | /* marks are -1 if group is undefined */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3112 | return _pair(self->mark[index*2], self->mark[index*2+1]); |
| 3113 | } |
| 3114 | |
| 3115 | static PyObject* |
| 3116 | match_regs(MatchObject* self) |
| 3117 | { |
| 3118 | PyObject* regs; |
| 3119 | PyObject* item; |
| 3120 | int index; |
| 3121 | |
| 3122 | regs = PyTuple_New(self->groups); |
| 3123 | if (!regs) |
| 3124 | return NULL; |
| 3125 | |
| 3126 | for (index = 0; index < self->groups; index++) { |
| 3127 | item = _pair(self->mark[index*2], self->mark[index*2+1]); |
| 3128 | if (!item) { |
| 3129 | Py_DECREF(regs); |
| 3130 | return NULL; |
| 3131 | } |
| 3132 | PyTuple_SET_ITEM(regs, index, item); |
| 3133 | } |
| 3134 | |
| 3135 | Py_INCREF(regs); |
| 3136 | self->regs = regs; |
| 3137 | |
| 3138 | return regs; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3139 | } |
| 3140 | |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 3141 | static PyObject* |
| 3142 | match_copy(MatchObject* self, PyObject* args) |
| 3143 | { |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 3144 | #ifdef USE_BUILTIN_COPY |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 3145 | MatchObject* copy; |
| 3146 | int slots, offset; |
| 3147 | |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 3148 | if (args != Py_None && !PyArg_ParseTuple(args, ":__copy__")) |
| 3149 | return NULL; |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 3150 | |
| 3151 | slots = 2 * (self->pattern->groups+1); |
| 3152 | |
| 3153 | copy = PyObject_NEW_VAR(MatchObject, &Match_Type, slots); |
| 3154 | if (!copy) |
| 3155 | return NULL; |
| 3156 | |
| 3157 | /* this value a constant, but any compiler should be able to |
| 3158 | figure that out all by itself */ |
| 3159 | offset = offsetof(MatchObject, string); |
| 3160 | |
| 3161 | Py_XINCREF(self->pattern); |
| 3162 | Py_XINCREF(self->string); |
| 3163 | Py_XINCREF(self->regs); |
| 3164 | |
| 3165 | memcpy((char*) copy + offset, (char*) self + offset, |
| 3166 | sizeof(MatchObject) + slots * sizeof(int) - offset); |
| 3167 | |
| 3168 | return (PyObject*) copy; |
| 3169 | #else |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 3170 | PyErr_SetString(PyExc_TypeError, "cannot copy this match object"); |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 3171 | return NULL; |
| 3172 | #endif |
| 3173 | } |
| 3174 | |
| 3175 | static PyObject* |
| 3176 | match_deepcopy(MatchObject* self, PyObject* args) |
| 3177 | { |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 3178 | #ifdef USE_BUILTIN_COPY |
| 3179 | MatchObject* copy; |
| 3180 | |
| 3181 | PyObject* memo; |
| 3182 | if (!PyArg_ParseTuple(args, "O:__deepcopy__", &memo)) |
| 3183 | return NULL; |
| 3184 | |
| 3185 | copy = (MatchObject*) match_copy(self, Py_None); |
| 3186 | if (!copy) |
| 3187 | return NULL; |
| 3188 | |
| 3189 | if (!deepcopy((PyObject**) ©->pattern, memo) || |
| 3190 | !deepcopy(©->string, memo) || |
| 3191 | !deepcopy(©->regs, memo)) { |
| 3192 | Py_DECREF(copy); |
| 3193 | return NULL; |
| 3194 | } |
| 3195 | |
| 3196 | #else |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 3197 | PyErr_SetString(PyExc_TypeError, "cannot deepcopy this match object"); |
| 3198 | return NULL; |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 3199 | #endif |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 3202 | static PyMethodDef match_methods[] = { |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 3203 | {"group", (PyCFunction) match_group, METH_VARARGS}, |
| 3204 | {"start", (PyCFunction) match_start, METH_VARARGS}, |
| 3205 | {"end", (PyCFunction) match_end, METH_VARARGS}, |
| 3206 | {"span", (PyCFunction) match_span, METH_VARARGS}, |
| 3207 | {"groups", (PyCFunction) match_groups, METH_VARARGS|METH_KEYWORDS}, |
| 3208 | {"groupdict", (PyCFunction) match_groupdict, METH_VARARGS|METH_KEYWORDS}, |
| 3209 | {"expand", (PyCFunction) match_expand, METH_VARARGS}, |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 3210 | {"__copy__", (PyCFunction) match_copy, METH_VARARGS}, |
| 3211 | {"__deepcopy__", (PyCFunction) match_deepcopy, METH_VARARGS}, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3212 | {NULL, NULL} |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3213 | }; |
| 3214 | |
| 3215 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 3216 | match_getattr(MatchObject* self, char* name) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3217 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3218 | PyObject* res; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3219 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3220 | res = Py_FindMethod(match_methods, (PyObject*) self, name); |
| 3221 | if (res) |
| 3222 | return res; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3223 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3224 | PyErr_Clear(); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3225 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3226 | if (!strcmp(name, "lastindex")) { |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 3227 | if (self->lastindex >= 0) |
| 3228 | return Py_BuildValue("i", self->lastindex); |
Fredrik Lundh | c230173 | 2000-07-02 22:25:39 +0000 | [diff] [blame] | 3229 | Py_INCREF(Py_None); |
| 3230 | return Py_None; |
| 3231 | } |
| 3232 | |
| 3233 | if (!strcmp(name, "lastgroup")) { |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 3234 | if (self->pattern->indexgroup && self->lastindex >= 0) { |
Fredrik Lundh | c230173 | 2000-07-02 22:25:39 +0000 | [diff] [blame] | 3235 | PyObject* result = PySequence_GetItem( |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 3236 | self->pattern->indexgroup, self->lastindex |
Fredrik Lundh | c230173 | 2000-07-02 22:25:39 +0000 | [diff] [blame] | 3237 | ); |
| 3238 | if (result) |
| 3239 | return result; |
| 3240 | PyErr_Clear(); |
| 3241 | } |
| 3242 | Py_INCREF(Py_None); |
| 3243 | return Py_None; |
| 3244 | } |
| 3245 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3246 | if (!strcmp(name, "string")) { |
| 3247 | if (self->string) { |
| 3248 | Py_INCREF(self->string); |
| 3249 | return self->string; |
| 3250 | } else { |
| 3251 | Py_INCREF(Py_None); |
| 3252 | return Py_None; |
| 3253 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3254 | } |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3255 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3256 | if (!strcmp(name, "regs")) { |
| 3257 | if (self->regs) { |
| 3258 | Py_INCREF(self->regs); |
| 3259 | return self->regs; |
| 3260 | } else |
| 3261 | return match_regs(self); |
| 3262 | } |
| 3263 | |
| 3264 | if (!strcmp(name, "re")) { |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3265 | Py_INCREF(self->pattern); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3266 | return (PyObject*) self->pattern; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3267 | } |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3268 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3269 | if (!strcmp(name, "pos")) |
| 3270 | return Py_BuildValue("i", self->pos); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3271 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3272 | if (!strcmp(name, "endpos")) |
| 3273 | return Py_BuildValue("i", self->endpos); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3274 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3275 | PyErr_SetString(PyExc_AttributeError, name); |
| 3276 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
| 3279 | /* FIXME: implement setattr("string", None) as a special case (to |
| 3280 | detach the associated string, if any */ |
| 3281 | |
| 3282 | statichere PyTypeObject Match_Type = { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3283 | PyObject_HEAD_INIT(NULL) |
Fredrik Lundh | 82b2307 | 2001-12-09 16:13:15 +0000 | [diff] [blame] | 3284 | 0, "_" SRE_MODULE ".SRE_Match", |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3285 | sizeof(MatchObject), sizeof(int), |
| 3286 | (destructor)match_dealloc, /*tp_dealloc*/ |
| 3287 | 0, /*tp_print*/ |
| 3288 | (getattrfunc)match_getattr /*tp_getattr*/ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3289 | }; |
| 3290 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3291 | /* -------------------------------------------------------------------- */ |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 3292 | /* scanner methods (experimental) */ |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3293 | |
| 3294 | static void |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 3295 | scanner_dealloc(ScannerObject* self) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3296 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3297 | state_fini(&self->state); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3298 | Py_DECREF(self->pattern); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3299 | PyObject_DEL(self); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3300 | } |
| 3301 | |
| 3302 | static PyObject* |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 3303 | scanner_match(ScannerObject* self, PyObject* args) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3304 | { |
| 3305 | SRE_STATE* state = &self->state; |
| 3306 | PyObject* match; |
| 3307 | int status; |
| 3308 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 3309 | state_reset(state); |
| 3310 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3311 | state->ptr = state->start; |
| 3312 | |
| 3313 | if (state->charsize == 1) { |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 3314 | status = sre_match(state, PatternObject_GetCode(self->pattern), 1); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3315 | } else { |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 3316 | #if defined(HAVE_UNICODE) |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 3317 | status = sre_umatch(state, PatternObject_GetCode(self->pattern), 1); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 3318 | #endif |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3319 | } |
| 3320 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 3321 | match = pattern_new_match((PatternObject*) self->pattern, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3322 | state, status); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3323 | |
Gustavo Niemeyer | c523b04 | 2002-11-07 03:28:56 +0000 | [diff] [blame] | 3324 | if ((status == 0 || state->ptr == state->start) && |
| 3325 | state->ptr < state->end) |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 3326 | state->start = (void*) ((char*) state->ptr + state->charsize); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3327 | else |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 3328 | state->start = state->ptr; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3329 | |
| 3330 | return match; |
| 3331 | } |
| 3332 | |
| 3333 | |
| 3334 | static PyObject* |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 3335 | scanner_search(ScannerObject* self, PyObject* args) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3336 | { |
| 3337 | SRE_STATE* state = &self->state; |
| 3338 | PyObject* match; |
| 3339 | int status; |
| 3340 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 3341 | state_reset(state); |
| 3342 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3343 | state->ptr = state->start; |
| 3344 | |
| 3345 | if (state->charsize == 1) { |
| 3346 | status = sre_search(state, PatternObject_GetCode(self->pattern)); |
| 3347 | } else { |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 3348 | #if defined(HAVE_UNICODE) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3349 | status = sre_usearch(state, PatternObject_GetCode(self->pattern)); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 3350 | #endif |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3351 | } |
| 3352 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 3353 | match = pattern_new_match((PatternObject*) self->pattern, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3354 | state, status); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3355 | |
Gustavo Niemeyer | c523b04 | 2002-11-07 03:28:56 +0000 | [diff] [blame] | 3356 | if ((status == 0 || state->ptr == state->start) && |
| 3357 | state->ptr < state->end) |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 3358 | state->start = (void*) ((char*) state->ptr + state->charsize); |
| 3359 | else |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3360 | state->start = state->ptr; |
| 3361 | |
| 3362 | return match; |
| 3363 | } |
| 3364 | |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 3365 | static PyMethodDef scanner_methods[] = { |
Neal Norwitz | bb2769f | 2002-03-31 15:46:00 +0000 | [diff] [blame] | 3366 | /* FIXME: use METH_OLDARGS instead of 0 or fix to use METH_VARARGS */ |
| 3367 | /* METH_OLDARGS is not in Python 1.5.2 */ |
| 3368 | {"match", (PyCFunction) scanner_match, 0}, |
| 3369 | {"search", (PyCFunction) scanner_search, 0}, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3370 | {NULL, NULL} |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3371 | }; |
| 3372 | |
| 3373 | static PyObject* |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 3374 | scanner_getattr(ScannerObject* self, char* name) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3375 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3376 | PyObject* res; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3377 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3378 | res = Py_FindMethod(scanner_methods, (PyObject*) self, name); |
| 3379 | if (res) |
| 3380 | return res; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3381 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3382 | PyErr_Clear(); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3383 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3384 | /* attributes */ |
| 3385 | if (!strcmp(name, "pattern")) { |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3386 | Py_INCREF(self->pattern); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3387 | return self->pattern; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3388 | } |
| 3389 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3390 | PyErr_SetString(PyExc_AttributeError, name); |
| 3391 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3392 | } |
| 3393 | |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 3394 | statichere PyTypeObject Scanner_Type = { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3395 | PyObject_HEAD_INIT(NULL) |
Fredrik Lundh | 82b2307 | 2001-12-09 16:13:15 +0000 | [diff] [blame] | 3396 | 0, "_" SRE_MODULE ".SRE_Scanner", |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3397 | sizeof(ScannerObject), 0, |
| 3398 | (destructor)scanner_dealloc, /*tp_dealloc*/ |
| 3399 | 0, /*tp_print*/ |
| 3400 | (getattrfunc)scanner_getattr, /*tp_getattr*/ |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 3401 | }; |
| 3402 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3403 | static PyMethodDef _functions[] = { |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 3404 | {"compile", _compile, METH_VARARGS}, |
| 3405 | {"getcodesize", sre_codesize, METH_VARARGS}, |
| 3406 | {"getlower", sre_getlower, METH_VARARGS}, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3407 | {NULL, NULL} |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3408 | }; |
| 3409 | |
Andrew M. Kuchling | c24fe36 | 2003-04-30 13:09:08 +0000 | [diff] [blame] | 3410 | #if PY_VERSION_HEX < 0x02030000 |
| 3411 | DL_EXPORT(void) init_sre(void) |
| 3412 | #else |
Mark Hammond | 8235ea1 | 2002-07-19 06:55:41 +0000 | [diff] [blame] | 3413 | PyMODINIT_FUNC init_sre(void) |
Andrew M. Kuchling | c24fe36 | 2003-04-30 13:09:08 +0000 | [diff] [blame] | 3414 | #endif |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3415 | { |
Fredrik Lundh | b35ffc0 | 2001-01-15 12:46:09 +0000 | [diff] [blame] | 3416 | PyObject* m; |
| 3417 | PyObject* d; |
Barry Warsaw | 214a0b13 | 2001-08-16 20:33:48 +0000 | [diff] [blame] | 3418 | PyObject* x; |
Fredrik Lundh | b35ffc0 | 2001-01-15 12:46:09 +0000 | [diff] [blame] | 3419 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 3420 | /* Patch object types */ |
| 3421 | Pattern_Type.ob_type = Match_Type.ob_type = |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 3422 | Scanner_Type.ob_type = &PyType_Type; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3423 | |
Fredrik Lundh | 1c5aa69 | 2001-01-16 07:37:30 +0000 | [diff] [blame] | 3424 | m = Py_InitModule("_" SRE_MODULE, _functions); |
Fredrik Lundh | b35ffc0 | 2001-01-15 12:46:09 +0000 | [diff] [blame] | 3425 | d = PyModule_GetDict(m); |
| 3426 | |
Fredrik Lundh | 21009b9 | 2001-09-18 18:47:09 +0000 | [diff] [blame] | 3427 | x = PyInt_FromLong(SRE_MAGIC); |
| 3428 | if (x) { |
| 3429 | PyDict_SetItemString(d, "MAGIC", x); |
| 3430 | Py_DECREF(x); |
| 3431 | } |
Fredrik Lundh | 9c7eab8 | 2001-04-15 19:00:58 +0000 | [diff] [blame] | 3432 | |
Martin v. Löwis | 78e2f06 | 2003-04-19 12:56:08 +0000 | [diff] [blame] | 3433 | x = PyInt_FromLong(sizeof(SRE_CODE)); |
| 3434 | if (x) { |
| 3435 | PyDict_SetItemString(d, "CODESIZE", x); |
| 3436 | Py_DECREF(x); |
| 3437 | } |
| 3438 | |
Fredrik Lundh | 21009b9 | 2001-09-18 18:47:09 +0000 | [diff] [blame] | 3439 | x = PyString_FromString(copyright); |
| 3440 | if (x) { |
| 3441 | PyDict_SetItemString(d, "copyright", x); |
| 3442 | Py_DECREF(x); |
| 3443 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3444 | } |
| 3445 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 3446 | #endif /* !defined(SRE_RECURSIVE) */ |
Gustavo Niemeyer | be733ee | 2003-04-20 07:35:44 +0000 | [diff] [blame] | 3447 | |
| 3448 | /* vim:ts=4:sw=4:et |
| 3449 | */ |