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