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 |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 27 | * |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame^] | 28 | * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved. |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 29 | * |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 30 | * This version of the SRE library can be redistributed under CNRI's |
| 31 | * Python 1.6 license. For any other use, please contact Secret Labs |
| 32 | * AB (info@pythonware.com). |
| 33 | * |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 34 | * Portions of this engine have been developed in cooperation with |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 35 | * CNRI. Hewlett-Packard provided funding for 1.6 integration and |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 36 | * other compatibility work. |
| 37 | */ |
| 38 | |
| 39 | #ifndef SRE_RECURSIVE |
| 40 | |
Fredrik Lundh | ebc37b2 | 2000-10-28 19:30:41 +0000 | [diff] [blame] | 41 | char copyright[] = " SRE 0.9.9 Copyright (c) 1997-2000 by Secret Labs AB "; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 42 | |
| 43 | #include "Python.h" |
| 44 | |
| 45 | #include "sre.h" |
| 46 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 47 | #include <ctype.h> |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 48 | |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 49 | /* name of this module, minus the leading underscore */ |
| 50 | #define MODULE "sre" |
| 51 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 52 | /* defining this one enables tracing */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 53 | #undef VERBOSE |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 54 | |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 55 | #if PY_VERSION_HEX >= 0x01060000 |
Fredrik Lundh | 22d2546 | 2000-07-01 17:50:59 +0000 | [diff] [blame] | 56 | /* defining this enables unicode support (default under 1.6a1 and later) */ |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 57 | #define HAVE_UNICODE |
| 58 | #endif |
| 59 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 60 | /* -------------------------------------------------------------------- */ |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 61 | /* optional features */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 62 | |
Fredrik Lundh | 33accc1 | 2000-08-27 20:59:47 +0000 | [diff] [blame] | 63 | /* prevent run-away recursion (bad patterns on long strings) */ |
| 64 | |
Fredrik Lundh | 18c2aa2 | 2000-08-07 17:33:38 +0000 | [diff] [blame] | 65 | #if !defined(USE_STACKCHECK) |
Fredrik Lundh | 33accc1 | 2000-08-27 20:59:47 +0000 | [diff] [blame] | 66 | #if defined(MS_WIN64) || defined(__LP64__) || defined(_LP64) |
| 67 | /* require smaller recursion limit for a number of 64-bit platforms: |
| 68 | Win64 (MS_WIN64), Linux64 (__LP64__), Monterey (64-bit AIX) (_LP64) */ |
| 69 | /* FIXME: maybe the limit should be 40000 / sizeof(void*) ? */ |
| 70 | #define USE_RECURSION_LIMIT 7500 |
| 71 | #else |
| 72 | #define USE_RECURSION_LIMIT 10000 |
| 73 | #endif |
Fredrik Lundh | 18c2aa2 | 2000-08-07 17:33:38 +0000 | [diff] [blame] | 74 | #endif |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 75 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 76 | /* enables fast searching */ |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 77 | #define USE_FAST_SEARCH |
| 78 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 79 | /* enables aggressive inlining (always on for Visual C) */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 80 | #undef USE_INLINE |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 81 | |
| 82 | /* -------------------------------------------------------------------- */ |
| 83 | |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 84 | #if defined(_MSC_VER) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 85 | #pragma optimize("agtw", on) /* doesn't seem to make much difference... */ |
Fredrik Lundh | 2855290 | 2000-07-05 21:14:16 +0000 | [diff] [blame] | 86 | #pragma warning(disable: 4710) /* who cares if functions are not inlined ;-) */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 87 | /* fastest possible local call under MSVC */ |
| 88 | #define LOCAL(type) static __inline type __fastcall |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 89 | #elif defined(USE_INLINE) |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 90 | #define LOCAL(type) static inline type |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 91 | #else |
| 92 | #define LOCAL(type) static type |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 93 | #endif |
| 94 | |
| 95 | /* error codes */ |
| 96 | #define SRE_ERROR_ILLEGAL -1 /* illegal opcode */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 97 | #define SRE_ERROR_STATE -2 /* illegal state */ |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 98 | #define SRE_ERROR_RECURSION_LIMIT -3 /* runaway recursion */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 99 | #define SRE_ERROR_MEMORY -9 /* out of memory */ |
| 100 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 101 | #if defined(VERBOSE) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 102 | #define TRACE(v) printf v |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 103 | #else |
| 104 | #define TRACE(v) |
| 105 | #endif |
| 106 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 107 | /* -------------------------------------------------------------------- */ |
| 108 | /* search engine state */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 109 | |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 110 | /* default character predicates (run sre_chars.py to regenerate tables) */ |
| 111 | |
| 112 | #define SRE_DIGIT_MASK 1 |
| 113 | #define SRE_SPACE_MASK 2 |
| 114 | #define SRE_LINEBREAK_MASK 4 |
| 115 | #define SRE_ALNUM_MASK 8 |
| 116 | #define SRE_WORD_MASK 16 |
| 117 | |
| 118 | static char sre_char_info[128] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 6, 2, |
| 119 | 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, |
| 120 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 25, 25, 25, 25, 25, 25, 25, |
| 121 | 25, 25, 0, 0, 0, 0, 0, 0, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, |
| 122 | 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 0, 0, |
| 123 | 0, 0, 16, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, |
| 124 | 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 0, 0, 0, 0, 0 }; |
| 125 | |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 126 | static char sre_char_lower[128] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 127 | 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, |
| 128 | 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, |
| 129 | 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, |
| 130 | 61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, |
| 131 | 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, |
| 132 | 122, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, |
| 133 | 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, |
| 134 | 120, 121, 122, 123, 124, 125, 126, 127 }; |
| 135 | |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 136 | static unsigned int sre_lower(unsigned int ch) |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 137 | { |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 138 | return ((ch) < 128 ? sre_char_lower[ch] : ch); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | #define SRE_IS_DIGIT(ch)\ |
| 142 | ((ch) < 128 ? (sre_char_info[(ch)] & SRE_DIGIT_MASK) : 0) |
| 143 | #define SRE_IS_SPACE(ch)\ |
| 144 | ((ch) < 128 ? (sre_char_info[(ch)] & SRE_SPACE_MASK) : 0) |
| 145 | #define SRE_IS_LINEBREAK(ch)\ |
| 146 | ((ch) < 128 ? (sre_char_info[(ch)] & SRE_LINEBREAK_MASK) : 0) |
| 147 | #define SRE_IS_ALNUM(ch)\ |
| 148 | ((ch) < 128 ? (sre_char_info[(ch)] & SRE_ALNUM_MASK) : 0) |
| 149 | #define SRE_IS_WORD(ch)\ |
| 150 | ((ch) < 128 ? (sre_char_info[(ch)] & SRE_WORD_MASK) : 0) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 151 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 152 | /* locale-specific character predicates */ |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 153 | |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 154 | static unsigned int sre_lower_locale(unsigned int ch) |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 155 | { |
| 156 | return ((ch) < 256 ? tolower((ch)) : ch); |
| 157 | } |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 158 | #define SRE_LOC_IS_DIGIT(ch) ((ch) < 256 ? isdigit((ch)) : 0) |
| 159 | #define SRE_LOC_IS_SPACE(ch) ((ch) < 256 ? isspace((ch)) : 0) |
| 160 | #define SRE_LOC_IS_LINEBREAK(ch) ((ch) == '\n') |
| 161 | #define SRE_LOC_IS_ALNUM(ch) ((ch) < 256 ? isalnum((ch)) : 0) |
| 162 | #define SRE_LOC_IS_WORD(ch) (SRE_LOC_IS_ALNUM((ch)) || (ch) == '_') |
| 163 | |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 164 | /* unicode-specific character predicates */ |
| 165 | |
| 166 | #if defined(HAVE_UNICODE) |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 167 | static unsigned int sre_lower_unicode(unsigned int ch) |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 168 | { |
| 169 | return (unsigned int) Py_UNICODE_TOLOWER((Py_UNICODE)(ch)); |
| 170 | } |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 171 | #define SRE_UNI_IS_DIGIT(ch) Py_UNICODE_ISDIGIT((Py_UNICODE)(ch)) |
| 172 | #define SRE_UNI_IS_SPACE(ch) Py_UNICODE_ISSPACE((Py_UNICODE)(ch)) |
| 173 | #define SRE_UNI_IS_LINEBREAK(ch) Py_UNICODE_ISLINEBREAK((Py_UNICODE)(ch)) |
Fredrik Lundh | 22d2546 | 2000-07-01 17:50:59 +0000 | [diff] [blame] | 174 | #define SRE_UNI_IS_ALNUM(ch) Py_UNICODE_ISALNUM((Py_UNICODE)(ch)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 175 | #define SRE_UNI_IS_WORD(ch) (SRE_UNI_IS_ALNUM((ch)) || (ch) == '_') |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 176 | #endif |
| 177 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 178 | LOCAL(int) |
| 179 | sre_category(SRE_CODE category, unsigned int ch) |
| 180 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 181 | switch (category) { |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 182 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 183 | case SRE_CATEGORY_DIGIT: |
| 184 | return SRE_IS_DIGIT(ch); |
| 185 | case SRE_CATEGORY_NOT_DIGIT: |
| 186 | return !SRE_IS_DIGIT(ch); |
| 187 | case SRE_CATEGORY_SPACE: |
| 188 | return SRE_IS_SPACE(ch); |
| 189 | case SRE_CATEGORY_NOT_SPACE: |
| 190 | return !SRE_IS_SPACE(ch); |
| 191 | case SRE_CATEGORY_WORD: |
| 192 | return SRE_IS_WORD(ch); |
| 193 | case SRE_CATEGORY_NOT_WORD: |
| 194 | return !SRE_IS_WORD(ch); |
| 195 | case SRE_CATEGORY_LINEBREAK: |
| 196 | return SRE_IS_LINEBREAK(ch); |
| 197 | case SRE_CATEGORY_NOT_LINEBREAK: |
| 198 | return !SRE_IS_LINEBREAK(ch); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 199 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 200 | case SRE_CATEGORY_LOC_WORD: |
| 201 | return SRE_LOC_IS_WORD(ch); |
| 202 | case SRE_CATEGORY_LOC_NOT_WORD: |
| 203 | return !SRE_LOC_IS_WORD(ch); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 204 | |
| 205 | #if defined(HAVE_UNICODE) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 206 | case SRE_CATEGORY_UNI_DIGIT: |
| 207 | return SRE_UNI_IS_DIGIT(ch); |
| 208 | case SRE_CATEGORY_UNI_NOT_DIGIT: |
| 209 | return !SRE_UNI_IS_DIGIT(ch); |
| 210 | case SRE_CATEGORY_UNI_SPACE: |
| 211 | return SRE_UNI_IS_SPACE(ch); |
| 212 | case SRE_CATEGORY_UNI_NOT_SPACE: |
| 213 | return !SRE_UNI_IS_SPACE(ch); |
| 214 | case SRE_CATEGORY_UNI_WORD: |
| 215 | return SRE_UNI_IS_WORD(ch); |
| 216 | case SRE_CATEGORY_UNI_NOT_WORD: |
| 217 | return !SRE_UNI_IS_WORD(ch); |
| 218 | case SRE_CATEGORY_UNI_LINEBREAK: |
| 219 | return SRE_UNI_IS_LINEBREAK(ch); |
| 220 | case SRE_CATEGORY_UNI_NOT_LINEBREAK: |
| 221 | return !SRE_UNI_IS_LINEBREAK(ch); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 222 | #endif |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 223 | } |
| 224 | return 0; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /* helpers */ |
| 228 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 229 | static void |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 230 | mark_fini(SRE_STATE* state) |
| 231 | { |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 232 | if (state->mark_stack) { |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 233 | free(state->mark_stack); |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 234 | state->mark_stack = NULL; |
| 235 | } |
| 236 | state->mark_stack_size = state->mark_stack_base = 0; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static int |
| 240 | mark_save(SRE_STATE* state, int lo, int hi) |
| 241 | { |
| 242 | void* stack; |
| 243 | int size; |
| 244 | int minsize, newsize; |
| 245 | |
| 246 | if (hi <= lo) |
| 247 | return 0; |
| 248 | |
| 249 | size = (hi - lo) + 1; |
| 250 | |
| 251 | newsize = state->mark_stack_size; |
| 252 | minsize = state->mark_stack_base + size; |
| 253 | |
| 254 | if (newsize < minsize) { |
| 255 | /* create new stack */ |
| 256 | if (!newsize) { |
| 257 | newsize = 512; |
| 258 | if (newsize < minsize) |
| 259 | newsize = minsize; |
| 260 | TRACE(("allocate stack %d\n", newsize)); |
| 261 | stack = malloc(sizeof(void*) * newsize); |
| 262 | } else { |
| 263 | /* grow the stack */ |
| 264 | while (newsize < minsize) |
| 265 | newsize += newsize; |
| 266 | TRACE(("grow stack to %d\n", newsize)); |
| 267 | stack = realloc(state->mark_stack, sizeof(void*) * newsize); |
| 268 | } |
| 269 | if (!stack) { |
| 270 | mark_fini(state); |
| 271 | return SRE_ERROR_MEMORY; |
| 272 | } |
| 273 | state->mark_stack = stack; |
| 274 | state->mark_stack_size = newsize; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 275 | } |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 276 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 277 | 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] | 278 | |
| 279 | memcpy(state->mark_stack + state->mark_stack_base, state->mark + lo, |
| 280 | size * sizeof(void*)); |
| 281 | |
| 282 | state->mark_stack_base += size; |
| 283 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 284 | return 0; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 287 | static int |
| 288 | mark_restore(SRE_STATE* state, int lo, int hi) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 289 | { |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 290 | int size; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 291 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 292 | if (hi <= lo) |
| 293 | return 0; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 294 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 295 | size = (hi - lo) + 1; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 296 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 297 | state->mark_stack_base -= size; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 298 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 299 | 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] | 300 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 301 | memcpy(state->mark + lo, state->mark_stack + state->mark_stack_base, |
| 302 | size * sizeof(void*)); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 303 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 304 | return 0; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 307 | /* generate 8-bit version */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 308 | |
| 309 | #define SRE_CHAR unsigned char |
| 310 | #define SRE_AT sre_at |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 311 | #define SRE_COUNT sre_count |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 312 | #define SRE_CHARSET sre_charset |
| 313 | #define SRE_INFO sre_info |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 314 | #define SRE_MATCH sre_match |
| 315 | #define SRE_SEARCH sre_search |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 316 | |
| 317 | #if defined(HAVE_UNICODE) |
| 318 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 319 | #define SRE_RECURSIVE |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 320 | #include "_sre.c" |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 321 | #undef SRE_RECURSIVE |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 322 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 323 | #undef SRE_SEARCH |
| 324 | #undef SRE_MATCH |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 325 | #undef SRE_INFO |
| 326 | #undef SRE_CHARSET |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 327 | #undef SRE_COUNT |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 328 | #undef SRE_AT |
| 329 | #undef SRE_CHAR |
| 330 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 331 | /* generate 16-bit unicode version */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 332 | |
| 333 | #define SRE_CHAR Py_UNICODE |
| 334 | #define SRE_AT sre_uat |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 335 | #define SRE_COUNT sre_ucount |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 336 | #define SRE_CHARSET sre_ucharset |
| 337 | #define SRE_INFO sre_uinfo |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 338 | #define SRE_MATCH sre_umatch |
| 339 | #define SRE_SEARCH sre_usearch |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 340 | #endif |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 341 | |
| 342 | #endif /* SRE_RECURSIVE */ |
| 343 | |
| 344 | /* -------------------------------------------------------------------- */ |
| 345 | /* String matching engine */ |
| 346 | |
| 347 | /* the following section is compiled twice, with different character |
| 348 | settings */ |
| 349 | |
| 350 | LOCAL(int) |
| 351 | SRE_AT(SRE_STATE* state, SRE_CHAR* ptr, SRE_CODE at) |
| 352 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 353 | /* check if pointer is at given position */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 354 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 355 | int this, that; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 356 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 357 | switch (at) { |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 358 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 359 | case SRE_AT_BEGINNING: |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame^] | 360 | case SRE_AT_BEGINNING_STRING: |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 361 | return ((void*) ptr == state->beginning); |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 362 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 363 | case SRE_AT_BEGINNING_LINE: |
| 364 | return ((void*) ptr == state->beginning || |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 365 | SRE_IS_LINEBREAK((int) ptr[-1])); |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 366 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 367 | case SRE_AT_END: |
Fredrik Lundh | ef34bd2 | 2000-06-30 21:40:20 +0000 | [diff] [blame] | 368 | return (((void*) (ptr+1) == state->end && |
| 369 | SRE_IS_LINEBREAK((int) ptr[0])) || |
| 370 | ((void*) ptr == state->end)); |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 371 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 372 | case SRE_AT_END_LINE: |
| 373 | return ((void*) ptr == state->end || |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 374 | SRE_IS_LINEBREAK((int) ptr[0])); |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 375 | |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame^] | 376 | case SRE_AT_END_STRING: |
| 377 | return ((void*) ptr == state->end); |
| 378 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 379 | case SRE_AT_BOUNDARY: |
| 380 | if (state->beginning == state->end) |
| 381 | return 0; |
| 382 | that = ((void*) ptr > state->beginning) ? |
| 383 | SRE_IS_WORD((int) ptr[-1]) : 0; |
| 384 | this = ((void*) ptr < state->end) ? |
| 385 | SRE_IS_WORD((int) ptr[0]) : 0; |
| 386 | return this != that; |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 387 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 388 | case SRE_AT_NON_BOUNDARY: |
| 389 | if (state->beginning == state->end) |
| 390 | return 0; |
| 391 | that = ((void*) ptr > state->beginning) ? |
| 392 | SRE_IS_WORD((int) ptr[-1]) : 0; |
| 393 | this = ((void*) ptr < state->end) ? |
| 394 | SRE_IS_WORD((int) ptr[0]) : 0; |
| 395 | return this == that; |
| 396 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 397 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 398 | return 0; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | LOCAL(int) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 402 | SRE_CHARSET(SRE_CODE* set, SRE_CODE ch) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 403 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 404 | /* check if character is a member of the given set */ |
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 ok = 1; |
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 | for (;;) { |
| 409 | switch (*set++) { |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 410 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 411 | case SRE_OP_LITERAL: |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 412 | /* <LITERAL> <code> */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 413 | if (ch == set[0]) |
| 414 | return ok; |
| 415 | set++; |
| 416 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 417 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 418 | case SRE_OP_RANGE: |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 419 | /* <RANGE> <lower> <upper> */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 420 | if (set[0] <= ch && ch <= set[1]) |
| 421 | return ok; |
| 422 | set += 2; |
| 423 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 424 | |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 425 | case SRE_OP_CHARSET: |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 426 | /* <CHARSET> <bitmap> (16 bits per code word) */ |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 427 | if (ch < 256 && (set[ch >> 4] & (1 << (ch & 15)))) |
| 428 | return ok; |
| 429 | set += 16; |
| 430 | break; |
| 431 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 432 | case SRE_OP_CATEGORY: |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 433 | /* <CATEGORY> <code> */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 434 | if (sre_category(set[0], (int) ch)) |
| 435 | return ok; |
| 436 | set += 1; |
| 437 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 438 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 439 | case SRE_OP_NEGATE: |
| 440 | ok = !ok; |
| 441 | break; |
| 442 | |
| 443 | case SRE_OP_FAILURE: |
| 444 | return !ok; |
| 445 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 446 | default: |
| 447 | /* internal error -- there's not much we can do about it |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 448 | here, so let's just pretend it didn't match... */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 454 | LOCAL(int) SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level); |
| 455 | |
| 456 | LOCAL(int) |
| 457 | SRE_COUNT(SRE_STATE* state, SRE_CODE* pattern, int maxcount, int level) |
| 458 | { |
| 459 | SRE_CODE chr; |
| 460 | SRE_CHAR* ptr = state->ptr; |
| 461 | SRE_CHAR* end = state->end; |
| 462 | int i; |
| 463 | |
| 464 | /* adjust end */ |
| 465 | if (maxcount < end - ptr && maxcount != 65535) |
| 466 | end = ptr + maxcount; |
| 467 | |
| 468 | switch (pattern[0]) { |
| 469 | |
| 470 | case SRE_OP_ANY: |
| 471 | /* repeated dot wildcard. */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 472 | TRACE(("|%p|%p|COUNT ANY\n", pattern, ptr)); |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 473 | while (ptr < end && !SRE_IS_LINEBREAK(*ptr)) |
| 474 | ptr++; |
| 475 | break; |
| 476 | |
| 477 | case SRE_OP_ANY_ALL: |
| 478 | /* repeated dot wildcare. skip to the end of the target |
| 479 | string, and backtrack from there */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 480 | TRACE(("|%p|%p|COUNT ANY_ALL\n", pattern, ptr)); |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 481 | ptr = end; |
| 482 | break; |
| 483 | |
| 484 | case SRE_OP_LITERAL: |
| 485 | /* repeated literal */ |
| 486 | chr = pattern[1]; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 487 | TRACE(("|%p|%p|COUNT LITERAL %d\n", pattern, ptr, chr)); |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 488 | while (ptr < end && (SRE_CODE) *ptr == chr) |
| 489 | ptr++; |
| 490 | break; |
| 491 | |
| 492 | case SRE_OP_LITERAL_IGNORE: |
| 493 | /* repeated literal */ |
| 494 | chr = pattern[1]; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 495 | TRACE(("|%p|%p|COUNT LITERAL_IGNORE %d\n", pattern, ptr, chr)); |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 496 | while (ptr < end && (SRE_CODE) state->lower(*ptr) == chr) |
| 497 | ptr++; |
| 498 | break; |
| 499 | |
| 500 | case SRE_OP_NOT_LITERAL: |
| 501 | /* repeated non-literal */ |
| 502 | chr = pattern[1]; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 503 | TRACE(("|%p|%p|COUNT NOT_LITERAL %d\n", pattern, ptr, chr)); |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 504 | while (ptr < end && (SRE_CODE) *ptr != chr) |
| 505 | ptr++; |
| 506 | break; |
| 507 | |
| 508 | case SRE_OP_NOT_LITERAL_IGNORE: |
| 509 | /* repeated non-literal */ |
| 510 | chr = pattern[1]; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 511 | TRACE(("|%p|%p|COUNT NOT_LITERAL_IGNORE %d\n", pattern, ptr, chr)); |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 512 | while (ptr < end && (SRE_CODE) state->lower(*ptr) != chr) |
| 513 | ptr++; |
| 514 | break; |
| 515 | |
| 516 | case SRE_OP_IN: |
| 517 | /* repeated set */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 518 | TRACE(("|%p|%p|COUNT IN\n", pattern, ptr)); |
| 519 | while (ptr < end && SRE_CHARSET(pattern + 2, *ptr)) |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 520 | ptr++; |
| 521 | break; |
| 522 | |
| 523 | default: |
| 524 | /* repeated single character pattern */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 525 | TRACE(("|%p|%p|COUNT SUBPATTERN\n", pattern, ptr)); |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 526 | while ((SRE_CHAR*) state->ptr < end) { |
| 527 | i = SRE_MATCH(state, pattern, level); |
| 528 | if (i < 0) |
| 529 | return i; |
| 530 | if (!i) |
| 531 | break; |
| 532 | } |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 533 | TRACE(("|%p|%p|COUNT %d\n", pattern, ptr, |
| 534 | (SRE_CHAR*) state->ptr - ptr)); |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 535 | return (SRE_CHAR*) state->ptr - ptr; |
| 536 | } |
| 537 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 538 | 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] | 539 | return ptr - (SRE_CHAR*) state->ptr; |
| 540 | } |
| 541 | |
Fredrik Lundh | 33accc1 | 2000-08-27 20:59:47 +0000 | [diff] [blame] | 542 | #if 0 /* not used in this release */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 543 | LOCAL(int) |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 544 | SRE_INFO(SRE_STATE* state, SRE_CODE* pattern) |
| 545 | { |
| 546 | /* check if an SRE_OP_INFO block matches at the current position. |
| 547 | returns the number of SRE_CODE objects to skip if successful, 0 |
| 548 | if no match */ |
| 549 | |
| 550 | SRE_CHAR* end = state->end; |
| 551 | SRE_CHAR* ptr = state->ptr; |
| 552 | int i; |
| 553 | |
| 554 | /* check minimal length */ |
| 555 | if (pattern[3] && (end - ptr) < pattern[3]) |
| 556 | return 0; |
| 557 | |
| 558 | /* check known prefix */ |
| 559 | if (pattern[2] & SRE_INFO_PREFIX && pattern[5] > 1) { |
| 560 | /* <length> <skip> <prefix data> <overlap data> */ |
| 561 | for (i = 0; i < pattern[5]; i++) |
| 562 | if ((SRE_CODE) ptr[i] != pattern[7 + i]) |
| 563 | return 0; |
| 564 | return pattern[0] + 2 * pattern[6]; |
| 565 | } |
| 566 | return pattern[0]; |
| 567 | } |
Fredrik Lundh | 33accc1 | 2000-08-27 20:59:47 +0000 | [diff] [blame] | 568 | #endif |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 569 | |
| 570 | LOCAL(int) |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 571 | SRE_MATCH(SRE_STATE* state, SRE_CODE* pattern, int level) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 572 | { |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 573 | /* check if string matches the given pattern. returns <0 for |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 574 | error, 0 for failure, and 1 for success */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 575 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 576 | SRE_CHAR* end = state->end; |
| 577 | SRE_CHAR* ptr = state->ptr; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 578 | int i, count; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 579 | SRE_REPEAT* rp; |
| 580 | int lastmark; |
Fredrik Lundh | e186983 | 2000-08-01 22:47:49 +0000 | [diff] [blame] | 581 | SRE_CODE chr; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 582 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 583 | SRE_REPEAT rep; /* FIXME: <fl> allocate in STATE instead */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 584 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 585 | TRACE(("|%p|%p|ENTER %d\n", pattern, ptr, level)); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 586 | |
Fredrik Lundh | 18c2aa2 | 2000-08-07 17:33:38 +0000 | [diff] [blame] | 587 | #if defined(USE_STACKCHECK) |
Fredrik Lundh | 5810064 | 2000-08-09 09:14:35 +0000 | [diff] [blame] | 588 | if (level % 10 == 0 && PyOS_CheckStack()) |
Fredrik Lundh | 18c2aa2 | 2000-08-07 17:33:38 +0000 | [diff] [blame] | 589 | return SRE_ERROR_RECURSION_LIMIT; |
| 590 | #endif |
| 591 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 592 | #if defined(USE_RECURSION_LIMIT) |
| 593 | if (level > USE_RECURSION_LIMIT) |
| 594 | return SRE_ERROR_RECURSION_LIMIT; |
| 595 | #endif |
| 596 | |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 597 | if (pattern[0] == SRE_OP_INFO) { |
| 598 | /* optimization info block */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 599 | /* <INFO> <1=skip> <2=flags> <3=min> ... */ |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 600 | if (pattern[3] && (end - ptr) < pattern[3]) { |
| 601 | TRACE(("reject (got %d chars, need %d)\n", |
| 602 | (end - ptr), pattern[3])); |
| 603 | return 0; |
| 604 | } |
| 605 | pattern += pattern[1] + 1; |
| 606 | } |
| 607 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 608 | for (;;) { |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 609 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 610 | switch (*pattern++) { |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 611 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 612 | case SRE_OP_FAILURE: |
| 613 | /* immediate failure */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 614 | TRACE(("|%p|%p|FAILURE\n", pattern, ptr)); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 615 | return 0; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 616 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 617 | case SRE_OP_SUCCESS: |
| 618 | /* end of pattern */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 619 | TRACE(("|%p|%p|SUCCESS\n", pattern, ptr)); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 620 | state->ptr = ptr; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 621 | return 1; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 622 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 623 | case SRE_OP_AT: |
| 624 | /* match at given position */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 625 | /* <AT> <code> */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 626 | TRACE(("|%p|%p|AT %d\n", pattern, ptr, *pattern)); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 627 | if (!SRE_AT(state, ptr, *pattern)) |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 628 | return 0; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 629 | pattern++; |
| 630 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 631 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 632 | case SRE_OP_CATEGORY: |
| 633 | /* match at given category */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 634 | /* <CATEGORY> <code> */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 635 | TRACE(("|%p|%p|CATEGORY %d\n", pattern, ptr, *pattern)); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 636 | if (ptr >= end || !sre_category(pattern[0], ptr[0])) |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 637 | return 0; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 638 | pattern++; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 639 | ptr++; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 640 | break; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 641 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 642 | case SRE_OP_LITERAL: |
| 643 | /* match literal string */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 644 | /* <LITERAL> <code> */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 645 | TRACE(("|%p|%p|LITERAL %d\n", pattern, ptr, *pattern)); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 646 | if (ptr >= end || (SRE_CODE) ptr[0] != pattern[0]) |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 647 | return 0; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 648 | pattern++; |
| 649 | ptr++; |
| 650 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 651 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 652 | case SRE_OP_NOT_LITERAL: |
| 653 | /* match anything that is not literal character */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 654 | /* <NOT_LITERAL> <code> */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 655 | TRACE(("|%p|%p|NOT_LITERAL %d\n", pattern, ptr, *pattern)); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 656 | if (ptr >= end || (SRE_CODE) ptr[0] == pattern[0]) |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 657 | return 0; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 658 | pattern++; |
| 659 | ptr++; |
| 660 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 661 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 662 | case SRE_OP_ANY: |
Fredrik Lundh | e186983 | 2000-08-01 22:47:49 +0000 | [diff] [blame] | 663 | /* match anything (except a newline) */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 664 | /* <ANY> */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 665 | TRACE(("|%p|%p|ANY\n", pattern, ptr)); |
Fredrik Lundh | e186983 | 2000-08-01 22:47:49 +0000 | [diff] [blame] | 666 | if (ptr >= end || SRE_IS_LINEBREAK(ptr[0])) |
| 667 | return 0; |
| 668 | ptr++; |
| 669 | break; |
| 670 | |
| 671 | case SRE_OP_ANY_ALL: |
| 672 | /* match anything */ |
| 673 | /* <ANY_ALL> */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 674 | TRACE(("|%p|%p|ANY_ALL\n", pattern, ptr)); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 675 | if (ptr >= end) |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 676 | return 0; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 677 | ptr++; |
| 678 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 679 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 680 | case SRE_OP_IN: |
| 681 | /* match set member (or non_member) */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 682 | /* <IN> <skip> <set> */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 683 | TRACE(("|%p|%p|IN\n", pattern, ptr)); |
| 684 | if (ptr >= end || !SRE_CHARSET(pattern + 1, *ptr)) |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 685 | return 0; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 686 | pattern += pattern[0]; |
| 687 | ptr++; |
| 688 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 689 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 690 | case SRE_OP_GROUPREF: |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 691 | /* match backreference */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 692 | TRACE(("|%p|%p|GROUPREF %d\n", pattern, ptr, pattern[0])); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 693 | i = pattern[0]; |
| 694 | { |
| 695 | SRE_CHAR* p = (SRE_CHAR*) state->mark[i+i]; |
| 696 | SRE_CHAR* e = (SRE_CHAR*) state->mark[i+i+1]; |
| 697 | if (!p || !e || e < p) |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 698 | return 0; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 699 | while (p < e) { |
| 700 | if (ptr >= end || *ptr != *p) |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 701 | return 0; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 702 | p++; ptr++; |
| 703 | } |
| 704 | } |
| 705 | pattern++; |
| 706 | break; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 707 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 708 | case SRE_OP_GROUPREF_IGNORE: |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 709 | /* match backreference */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 710 | TRACE(("|%p|%p|GROUPREF_IGNORE %d\n", pattern, ptr, pattern[0])); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 711 | i = pattern[0]; |
| 712 | { |
| 713 | SRE_CHAR* p = (SRE_CHAR*) state->mark[i+i]; |
| 714 | SRE_CHAR* e = (SRE_CHAR*) state->mark[i+i+1]; |
| 715 | if (!p || !e || e < p) |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 716 | return 0; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 717 | while (p < e) { |
| 718 | if (ptr >= end || |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 719 | state->lower(*ptr) != state->lower(*p)) |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 720 | return 0; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 721 | p++; ptr++; |
| 722 | } |
| 723 | } |
| 724 | pattern++; |
| 725 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 726 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 727 | case SRE_OP_LITERAL_IGNORE: |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 728 | TRACE(("|%p|%p|LITERAL_IGNORE %d\n", pattern, ptr, pattern[0])); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 729 | if (ptr >= end || |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 730 | state->lower(*ptr) != state->lower(*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 | ptr++; |
| 734 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 735 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 736 | case SRE_OP_NOT_LITERAL_IGNORE: |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 737 | TRACE(("|%p|%p|NOT_LITERAL_IGNORE %d\n", pattern, ptr, *pattern)); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 738 | if (ptr >= end || |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 739 | state->lower(*ptr) == state->lower(*pattern)) |
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++; |
| 742 | ptr++; |
| 743 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 744 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 745 | case SRE_OP_IN_IGNORE: |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 746 | TRACE(("|%p|%p|IN_IGNORE\n", pattern, ptr)); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 747 | if (ptr >= end |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 748 | || !SRE_CHARSET(pattern + 1, (SRE_CODE) state->lower(*ptr))) |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 749 | return 0; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 750 | pattern += pattern[0]; |
| 751 | ptr++; |
| 752 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 753 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 754 | case SRE_OP_MARK: |
| 755 | /* set mark */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 756 | /* <MARK> <gid> */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 757 | TRACE(("|%p|%p|MARK %d\n", pattern, ptr, pattern[0])); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 758 | i = pattern[0]; |
| 759 | if (i & 1) |
| 760 | state->lastindex = i/2 + 1; |
| 761 | if (i > state->lastmark) |
| 762 | state->lastmark = i; |
| 763 | state->mark[i] = ptr; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 764 | pattern++; |
| 765 | break; |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 766 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 767 | case SRE_OP_JUMP: |
| 768 | case SRE_OP_INFO: |
| 769 | /* jump forward */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 770 | /* <JUMP> <offset> */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 771 | TRACE(("|%p|%p|JUMP %d\n", pattern, ptr, pattern[0])); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 772 | pattern += pattern[0]; |
| 773 | break; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 774 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 775 | case SRE_OP_ASSERT: |
| 776 | /* assert subpattern */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 777 | /* <ASSERT> <skip> <back> <pattern> */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 778 | TRACE(("|%p|%p|ASSERT %d\n", pattern, ptr, pattern[1])); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 779 | state->ptr = ptr - pattern[1]; |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 780 | if (state->ptr < state->beginning) |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 781 | return 0; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 782 | i = SRE_MATCH(state, pattern + 2, level + 1); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 783 | if (i <= 0) |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 784 | return i; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 785 | pattern += pattern[0]; |
| 786 | break; |
Fredrik Lundh | 43b3b49 | 2000-06-30 10:41:31 +0000 | [diff] [blame] | 787 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 788 | case SRE_OP_ASSERT_NOT: |
| 789 | /* assert not subpattern */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 790 | /* <ASSERT_NOT> <skip> <back> <pattern> */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 791 | TRACE(("|%p|%p|ASSERT_NOT %d\n", pattern, ptr, pattern[1])); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 792 | state->ptr = ptr - pattern[1]; |
Fredrik Lundh | ebc37b2 | 2000-10-28 19:30:41 +0000 | [diff] [blame] | 793 | if (state->ptr >= state->beginning) { |
| 794 | i = SRE_MATCH(state, pattern + 2, level + 1); |
| 795 | if (i < 0) |
| 796 | return i; |
| 797 | if (i) |
| 798 | return 0; |
| 799 | } |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 800 | pattern += pattern[0]; |
| 801 | break; |
| 802 | |
| 803 | case SRE_OP_BRANCH: |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 804 | /* alternation */ |
| 805 | /* <BRANCH> <0=skip> code <JUMP> ... <NULL> */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 806 | TRACE(("|%p|%p|BRANCH\n", pattern, ptr)); |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 807 | lastmark = state->lastmark; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 808 | for (; pattern[0]; pattern += pattern[0]) { |
| 809 | if (pattern[1] == SRE_OP_LITERAL && |
| 810 | (ptr >= end || (SRE_CODE) *ptr != pattern[2])) |
| 811 | continue; |
| 812 | if (pattern[1] == SRE_OP_IN && |
| 813 | (ptr >= end || !SRE_CHARSET(pattern + 3, (SRE_CODE) *ptr))) |
| 814 | continue; |
| 815 | state->ptr = ptr; |
| 816 | i = SRE_MATCH(state, pattern + 1, level + 1); |
| 817 | if (i) |
| 818 | return i; |
| 819 | if (state->lastmark > lastmark) { |
| 820 | memset( |
| 821 | state->mark + lastmark + 1, 0, |
| 822 | (state->lastmark - lastmark) * sizeof(void*) |
| 823 | ); |
| 824 | state->lastmark = lastmark; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 825 | } |
| 826 | } |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 827 | return 0; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 828 | |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 829 | case SRE_OP_REPEAT_ONE: |
| 830 | /* match repeated sequence (maximizing regexp) */ |
| 831 | |
| 832 | /* this operator only works if the repeated item is |
| 833 | exactly one character wide, and we're not already |
| 834 | collecting backtracking points. for other cases, |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame^] | 835 | use the MAX_REPEAT operator */ |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 836 | |
| 837 | /* <REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */ |
| 838 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 839 | TRACE(("|%p|%p|REPEAT_ONE %d %d\n", pattern, ptr, |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 840 | pattern[1], pattern[2])); |
| 841 | |
Fredrik Lundh | e186983 | 2000-08-01 22:47:49 +0000 | [diff] [blame] | 842 | if (ptr + pattern[1] > end) |
| 843 | return 0; /* cannot match */ |
| 844 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 845 | state->ptr = ptr; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 846 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 847 | count = SRE_COUNT(state, pattern + 3, pattern[2], level + 1); |
| 848 | if (count < 0) |
| 849 | return count; |
Fredrik Lundh | e186983 | 2000-08-01 22:47:49 +0000 | [diff] [blame] | 850 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 851 | ptr += count; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 852 | |
| 853 | /* when we arrive here, count contains the number of |
| 854 | matches, and ptr points to the tail of the target |
| 855 | string. check if the rest of the pattern matches, |
| 856 | and backtrack if not. */ |
| 857 | |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 858 | if (count < (int) pattern[1]) |
| 859 | return 0; |
| 860 | |
| 861 | if (pattern[pattern[0]] == SRE_OP_SUCCESS) { |
| 862 | /* tail is empty. we're finished */ |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 863 | state->ptr = ptr; |
| 864 | return 1; |
| 865 | |
| 866 | } else if (pattern[pattern[0]] == SRE_OP_LITERAL) { |
| 867 | /* tail starts with a literal. skip positions where |
| 868 | the rest of the pattern cannot possibly match */ |
Fredrik Lundh | e186983 | 2000-08-01 22:47:49 +0000 | [diff] [blame] | 869 | chr = pattern[pattern[0]+1]; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 870 | for (;;) { |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 871 | while (count >= (int) pattern[1] && |
| 872 | (ptr >= end || *ptr != chr)) { |
| 873 | ptr--; |
| 874 | count--; |
| 875 | } |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 876 | if (count < (int) pattern[1]) |
| 877 | break; |
| 878 | state->ptr = ptr; |
| 879 | i = SRE_MATCH(state, pattern + pattern[0], level + 1); |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 880 | if (i) |
Fredrik Lundh | 33accc1 | 2000-08-27 20:59:47 +0000 | [diff] [blame] | 881 | return i; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 882 | ptr--; |
| 883 | count--; |
| 884 | } |
| 885 | |
| 886 | } else { |
| 887 | /* general case */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 888 | lastmark = state->lastmark; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 889 | while (count >= (int) pattern[1]) { |
| 890 | state->ptr = ptr; |
| 891 | i = SRE_MATCH(state, pattern + pattern[0], level + 1); |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 892 | if (i) |
Fredrik Lundh | 33accc1 | 2000-08-27 20:59:47 +0000 | [diff] [blame] | 893 | return i; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 894 | ptr--; |
| 895 | count--; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 896 | if (state->lastmark > lastmark) { |
| 897 | memset( |
| 898 | state->mark + lastmark + 1, 0, |
| 899 | (state->lastmark - lastmark) * sizeof(void*) |
| 900 | ); |
| 901 | state->lastmark = lastmark; |
| 902 | } |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | return 0; |
| 906 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 907 | case SRE_OP_REPEAT: |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 908 | /* create repeat context. all the hard work is done |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame^] | 909 | by the UNTIL operator (MAX_UNTIL, MIN_UNTIL) */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 910 | /* <REPEAT> <skip> <1=min> <2=max> item <UNTIL> tail */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 911 | TRACE(("|%p|%p|REPEAT %d %d\n", pattern, ptr, |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 912 | pattern[1], pattern[2])); |
| 913 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 914 | rep.count = -1; |
| 915 | rep.pattern = pattern; |
| 916 | |
| 917 | /* install new repeat context */ |
| 918 | rep.prev = state->repeat; |
| 919 | state->repeat = &rep; |
| 920 | |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 921 | state->ptr = ptr; |
| 922 | i = SRE_MATCH(state, pattern + pattern[0], level + 1); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 923 | |
| 924 | state->repeat = rep.prev; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 925 | |
| 926 | return i; |
| 927 | |
| 928 | case SRE_OP_MAX_UNTIL: |
| 929 | /* maximizing repeat */ |
| 930 | /* <REPEAT> <skip> <1=min> <2=max> item <MAX_UNTIL> tail */ |
| 931 | |
| 932 | /* FIXME: we probably need to deal with zero-width |
| 933 | matches in here... */ |
| 934 | |
| 935 | rp = state->repeat; |
| 936 | if (!rp) |
| 937 | return SRE_ERROR_STATE; |
| 938 | |
| 939 | state->ptr = ptr; |
| 940 | |
| 941 | count = rp->count + 1; |
| 942 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 943 | TRACE(("|%p|%p|MAX_UNTIL %d\n", pattern, ptr, count)); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 944 | |
| 945 | if (count < rp->pattern[1]) { |
| 946 | /* not enough matches */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 947 | rp->count = count; |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 948 | /* RECURSIVE */ |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 949 | i = SRE_MATCH(state, rp->pattern + 3, level + 1); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 950 | if (i) |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 951 | return i; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 952 | rp->count = count - 1; |
| 953 | state->ptr = ptr; |
| 954 | return 0; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 955 | } |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 956 | |
| 957 | if (count < rp->pattern[2] || rp->pattern[2] == 65535) { |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 958 | /* we may have enough matches, but if we can |
| 959 | match another item, do so */ |
| 960 | rp->count = count; |
| 961 | lastmark = state->lastmark; |
Fredrik Lundh | 33accc1 | 2000-08-27 20:59:47 +0000 | [diff] [blame] | 962 | i = mark_save(state, 0, lastmark); |
| 963 | if (i < 0) |
| 964 | return i; |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 965 | /* RECURSIVE */ |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 966 | i = SRE_MATCH(state, rp->pattern + 3, level + 1); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 967 | if (i) |
| 968 | return i; |
Fredrik Lundh | 33accc1 | 2000-08-27 20:59:47 +0000 | [diff] [blame] | 969 | i = mark_restore(state, 0, lastmark); |
| 970 | if (i < 0) |
| 971 | return i; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 972 | rp->count = count - 1; |
| 973 | state->ptr = ptr; |
| 974 | } |
| 975 | |
| 976 | /* cannot match more repeated items here. make sure the |
| 977 | tail matches */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 978 | state->repeat = rp->prev; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 979 | i = SRE_MATCH(state, pattern, level + 1); |
| 980 | if (i) |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 981 | return i; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 982 | state->repeat = rp; |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame^] | 983 | state->ptr = ptr; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 984 | return 0; |
| 985 | |
| 986 | case SRE_OP_MIN_UNTIL: |
| 987 | /* minimizing repeat */ |
| 988 | /* <REPEAT> <skip> <1=min> <2=max> item <MIN_UNTIL> tail */ |
| 989 | |
| 990 | rp = state->repeat; |
| 991 | if (!rp) |
| 992 | return SRE_ERROR_STATE; |
| 993 | |
| 994 | count = rp->count + 1; |
| 995 | |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame^] | 996 | TRACE(("|%p|%p|MIN_UNTIL %d %p\n", pattern, ptr, count, |
| 997 | rp->pattern)); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 998 | |
| 999 | state->ptr = ptr; |
| 1000 | |
| 1001 | if (count < rp->pattern[1]) { |
| 1002 | /* not enough matches */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1003 | rp->count = count; |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 1004 | /* RECURSIVE */ |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1005 | i = SRE_MATCH(state, rp->pattern + 3, level + 1); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1006 | if (i) |
| 1007 | return i; |
| 1008 | rp->count = count-1; |
| 1009 | state->ptr = ptr; |
| 1010 | return 0; |
| 1011 | } |
| 1012 | |
| 1013 | /* see if the tail matches */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1014 | state->repeat = rp->prev; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1015 | i = SRE_MATCH(state, pattern, level + 1); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1016 | if (i) { |
| 1017 | /* free(rp); */ |
| 1018 | return i; |
| 1019 | } |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame^] | 1020 | state->ptr = ptr; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1021 | state->repeat = rp; |
| 1022 | |
| 1023 | if (count >= rp->pattern[2] && rp->pattern[2] != 65535) |
| 1024 | return 0; |
| 1025 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1026 | rp->count = count; |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 1027 | /* RECURSIVE */ |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1028 | i = SRE_MATCH(state, rp->pattern + 3, level + 1); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1029 | if (i) |
| 1030 | return i; |
| 1031 | rp->count = count - 1; |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame^] | 1032 | state->ptr = ptr; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1033 | return 0; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1034 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1035 | default: |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1036 | TRACE(("|%p|%p|UNKNOWN %d\n", pattern, ptr, pattern[-1])); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1037 | return SRE_ERROR_ILLEGAL; |
| 1038 | } |
| 1039 | } |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1040 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1041 | /* shouldn't end up here */ |
| 1042 | return SRE_ERROR_ILLEGAL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 1045 | LOCAL(int) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1046 | SRE_SEARCH(SRE_STATE* state, SRE_CODE* pattern) |
| 1047 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1048 | SRE_CHAR* ptr = state->start; |
| 1049 | SRE_CHAR* end = state->end; |
| 1050 | int status = 0; |
Fredrik Lundh | 2855290 | 2000-07-05 21:14:16 +0000 | [diff] [blame] | 1051 | int prefix_len = 0; |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 1052 | int prefix_skip = 0; |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1053 | SRE_CODE* prefix = NULL; |
| 1054 | SRE_CODE* charset = NULL; |
| 1055 | SRE_CODE* overlap = NULL; |
| 1056 | int flags = 0; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1057 | |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1058 | if (pattern[0] == SRE_OP_INFO) { |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 1059 | /* optimization info block */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1060 | /* <INFO> <1=skip> <2=flags> <3=min> <4=max> <5=prefix info> */ |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1061 | |
| 1062 | flags = pattern[2]; |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 1063 | |
| 1064 | if (pattern[3] > 0) { |
| 1065 | /* adjust end point (but make sure we leave at least one |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1066 | character in there, so literal search will work) */ |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 1067 | end -= pattern[3]-1; |
| 1068 | if (end <= ptr) |
| 1069 | end = ptr+1; |
| 1070 | } |
| 1071 | |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1072 | if (flags & SRE_INFO_PREFIX) { |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 1073 | /* pattern starts with a known prefix */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1074 | /* <length> <skip> <prefix data> <overlap data> */ |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1075 | prefix_len = pattern[5]; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1076 | prefix_skip = pattern[6]; |
| 1077 | prefix = pattern + 7; |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1078 | overlap = prefix + prefix_len - 1; |
| 1079 | } else if (flags & SRE_INFO_CHARSET) |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 1080 | /* pattern starts with a character from a known set */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1081 | /* <charset> */ |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1082 | charset = pattern + 5; |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 1083 | |
| 1084 | pattern += 1 + pattern[1]; |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1085 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1086 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1087 | TRACE(("prefix = %p %d %d\n", prefix, prefix_len, prefix_skip)); |
| 1088 | TRACE(("charset = %p\n", charset)); |
| 1089 | |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 1090 | #if defined(USE_FAST_SEARCH) |
Fredrik Lundh | 2855290 | 2000-07-05 21:14:16 +0000 | [diff] [blame] | 1091 | if (prefix_len > 1) { |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 1092 | /* pattern starts with a known prefix. use the overlap |
| 1093 | table to skip forward as fast as we possibly can */ |
| 1094 | int i = 0; |
| 1095 | end = state->end; |
| 1096 | while (ptr < end) { |
| 1097 | for (;;) { |
Fredrik Lundh | 0640e11 | 2000-06-30 13:55:15 +0000 | [diff] [blame] | 1098 | if ((SRE_CODE) ptr[0] != prefix[i]) { |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 1099 | if (!i) |
| 1100 | break; |
| 1101 | else |
| 1102 | i = overlap[i]; |
| 1103 | } else { |
| 1104 | if (++i == prefix_len) { |
| 1105 | /* found a potential match */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1106 | TRACE(("|%p|%p|SEARCH SCAN\n", pattern, ptr)); |
| 1107 | state->start = ptr + 1 - prefix_len; |
| 1108 | state->ptr = ptr + 1 - prefix_len + prefix_skip; |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1109 | if (flags & SRE_INFO_LITERAL) |
| 1110 | return 1; /* we got all of it */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1111 | status = SRE_MATCH(state, pattern + 2*prefix_skip, 1); |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 1112 | if (status != 0) |
| 1113 | return status; |
| 1114 | /* close but no cigar -- try again */ |
| 1115 | i = overlap[i]; |
| 1116 | } |
| 1117 | break; |
| 1118 | } |
| 1119 | |
| 1120 | } |
| 1121 | ptr++; |
| 1122 | } |
| 1123 | return 0; |
| 1124 | } |
| 1125 | #endif |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 1126 | |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1127 | if (pattern[0] == SRE_OP_LITERAL) { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1128 | /* pattern starts with a literal character. this is used |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1129 | for short prefixes, and if fast search is disabled */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1130 | SRE_CODE chr = pattern[1]; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1131 | end = state->end; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1132 | for (;;) { |
| 1133 | while (ptr < end && (SRE_CODE) ptr[0] != chr) |
| 1134 | ptr++; |
| 1135 | if (ptr == end) |
| 1136 | return 0; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1137 | TRACE(("|%p|%p|SEARCH LITERAL\n", pattern, ptr)); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1138 | state->start = ptr; |
| 1139 | state->ptr = ++ptr; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1140 | status = SRE_MATCH(state, pattern + 2, 1); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1141 | if (status != 0) |
| 1142 | break; |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1143 | } |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1144 | } else if (charset) { |
| 1145 | /* pattern starts with a character from a known set */ |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1146 | end = state->end; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1147 | for (;;) { |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1148 | while (ptr < end && !SRE_CHARSET(charset, ptr[0])) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1149 | ptr++; |
| 1150 | if (ptr == end) |
| 1151 | return 0; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1152 | TRACE(("|%p|%p|SEARCH CHARSET\n", pattern, ptr)); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1153 | state->start = ptr; |
| 1154 | state->ptr = ptr; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1155 | status = SRE_MATCH(state, pattern, 1); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1156 | if (status != 0) |
| 1157 | break; |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1158 | ptr++; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1159 | } |
| 1160 | } else |
| 1161 | /* general case */ |
| 1162 | while (ptr <= end) { |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1163 | TRACE(("|%p|%p|SEARCH\n", pattern, ptr)); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1164 | state->start = state->ptr = ptr++; |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1165 | status = SRE_MATCH(state, pattern, 1); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1166 | if (status != 0) |
| 1167 | break; |
| 1168 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1169 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1170 | return status; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1171 | } |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 1172 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1173 | |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1174 | #if !defined(SRE_RECURSIVE) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1175 | |
| 1176 | /* -------------------------------------------------------------------- */ |
| 1177 | /* factories and destructors */ |
| 1178 | |
| 1179 | /* see sre.h for object declarations */ |
| 1180 | |
| 1181 | staticforward PyTypeObject Pattern_Type; |
| 1182 | staticforward PyTypeObject Match_Type; |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 1183 | staticforward PyTypeObject Scanner_Type; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1184 | |
| 1185 | static PyObject * |
| 1186 | _compile(PyObject* self_, PyObject* args) |
| 1187 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1188 | /* "compile" pattern descriptor to pattern object */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1189 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1190 | PatternObject* self; |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1191 | int i, n; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1192 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1193 | PyObject* pattern; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1194 | int flags = 0; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1195 | PyObject* code; |
| 1196 | int groups = 0; |
| 1197 | PyObject* groupindex = NULL; |
Fredrik Lundh | c230173 | 2000-07-02 22:25:39 +0000 | [diff] [blame] | 1198 | PyObject* indexgroup = NULL; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1199 | if (!PyArg_ParseTuple(args, "OiO|iOO", &pattern, &flags, &code, |
Fredrik Lundh | c230173 | 2000-07-02 22:25:39 +0000 | [diff] [blame] | 1200 | &groups, &groupindex, &indexgroup)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1201 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1202 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1203 | code = PySequence_Fast(code, "code argument must be a sequence"); |
| 1204 | if (!code) |
| 1205 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1206 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1207 | #if PY_VERSION_HEX >= 0x01060000 |
Jeremy Hylton | 03657cf | 2000-07-12 13:05:33 +0000 | [diff] [blame] | 1208 | n = PySequence_Size(code); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1209 | #else |
| 1210 | n = PySequence_Length(code); |
| 1211 | #endif |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1212 | |
Fredrik Lundh | ebc37b2 | 2000-10-28 19:30:41 +0000 | [diff] [blame] | 1213 | self = PyObject_NEW_VAR(PatternObject, &Pattern_Type, n); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1214 | if (!self) { |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1215 | Py_DECREF(code); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1216 | return NULL; |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1217 | } |
| 1218 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1219 | for (i = 0; i < n; i++) { |
| 1220 | PyObject *o = PySequence_Fast_GET_ITEM(code, i); |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1221 | self->code[i] = (SRE_CODE) PyInt_AsLong(o); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1222 | } |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1223 | |
| 1224 | Py_DECREF(code); |
| 1225 | |
| 1226 | if (PyErr_Occurred()) |
| 1227 | return NULL; |
| 1228 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1229 | Py_INCREF(pattern); |
| 1230 | self->pattern = pattern; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1231 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1232 | self->flags = flags; |
| 1233 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1234 | self->groups = groups; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1235 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1236 | Py_XINCREF(groupindex); |
| 1237 | self->groupindex = groupindex; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1238 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1239 | Py_XINCREF(indexgroup); |
| 1240 | self->indexgroup = indexgroup; |
Fredrik Lundh | c230173 | 2000-07-02 22:25:39 +0000 | [diff] [blame] | 1241 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1242 | return (PyObject*) self; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
| 1245 | static PyObject * |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1246 | sre_codesize(PyObject* self, PyObject* args) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1247 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1248 | return Py_BuildValue("i", sizeof(SRE_CODE)); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1251 | static PyObject * |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 1252 | sre_getlower(PyObject* self, PyObject* args) |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1253 | { |
| 1254 | int character, flags; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1255 | if (!PyArg_ParseTuple(args, "ii", &character, &flags)) |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1256 | return NULL; |
| 1257 | if (flags & SRE_FLAG_LOCALE) |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 1258 | return Py_BuildValue("i", sre_lower_locale(character)); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1259 | #if defined(HAVE_UNICODE) |
| 1260 | if (flags & SRE_FLAG_UNICODE) |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 1261 | return Py_BuildValue("i", sre_lower_unicode(character)); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1262 | #endif |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 1263 | return Py_BuildValue("i", sre_lower(character)); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1266 | LOCAL(void) |
| 1267 | state_reset(SRE_STATE* state) |
| 1268 | { |
| 1269 | int i; |
| 1270 | |
| 1271 | state->lastmark = 0; |
| 1272 | |
| 1273 | /* FIXME: dynamic! */ |
| 1274 | for (i = 0; i < SRE_MARK_SIZE; i++) |
| 1275 | state->mark[i] = NULL; |
| 1276 | |
| 1277 | state->lastindex = -1; |
| 1278 | |
| 1279 | state->repeat = NULL; |
| 1280 | |
| 1281 | mark_fini(state); |
| 1282 | } |
| 1283 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1284 | LOCAL(PyObject*) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1285 | state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string, |
| 1286 | int start, int end) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1287 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1288 | /* prepare state object */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1289 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1290 | PyBufferProcs *buffer; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1291 | int size, bytes; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1292 | void* ptr; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1293 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 1294 | memset(state, 0, sizeof(SRE_STATE)); |
| 1295 | |
| 1296 | state->lastindex = -1; |
| 1297 | |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 1298 | #if defined(HAVE_UNICODE) |
| 1299 | if (PyUnicode_Check(string)) { |
| 1300 | /* unicode strings doesn't always support the buffer interface */ |
| 1301 | ptr = (void*) PyUnicode_AS_DATA(string); |
| 1302 | bytes = PyUnicode_GET_DATA_SIZE(string); |
| 1303 | size = PyUnicode_GET_SIZE(string); |
| 1304 | state->charsize = sizeof(Py_UNICODE); |
| 1305 | |
| 1306 | } else { |
| 1307 | #endif |
| 1308 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1309 | /* get pointer to string buffer */ |
| 1310 | buffer = string->ob_type->tp_as_buffer; |
| 1311 | if (!buffer || !buffer->bf_getreadbuffer || !buffer->bf_getsegcount || |
| 1312 | buffer->bf_getsegcount(string, NULL) != 1) { |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1313 | PyErr_SetString(PyExc_TypeError, "expected string or buffer"); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1314 | return NULL; |
| 1315 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1316 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1317 | /* determine buffer size */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1318 | bytes = buffer->bf_getreadbuffer(string, 0, &ptr); |
| 1319 | if (bytes < 0) { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1320 | PyErr_SetString(PyExc_TypeError, "buffer has negative size"); |
| 1321 | return NULL; |
| 1322 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1323 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1324 | /* determine character size */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1325 | #if PY_VERSION_HEX >= 0x01060000 |
| 1326 | size = PyObject_Size(string); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1327 | #else |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1328 | size = PyObject_Length(string); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1329 | #endif |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1330 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1331 | if (PyString_Check(string) || bytes == size) |
| 1332 | state->charsize = 1; |
| 1333 | #if defined(HAVE_UNICODE) |
| 1334 | else if (bytes == (int) (size * sizeof(Py_UNICODE))) |
| 1335 | state->charsize = sizeof(Py_UNICODE); |
| 1336 | #endif |
| 1337 | else { |
| 1338 | PyErr_SetString(PyExc_TypeError, "buffer size mismatch"); |
| 1339 | return NULL; |
| 1340 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1341 | |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 1342 | #if defined(HAVE_UNICODE) |
| 1343 | } |
| 1344 | #endif |
| 1345 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1346 | /* adjust boundaries */ |
| 1347 | if (start < 0) |
| 1348 | start = 0; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1349 | else if (start > size) |
| 1350 | start = size; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1351 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1352 | if (end < 0) |
| 1353 | end = 0; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1354 | else if (end > size) |
| 1355 | end = size; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1356 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1357 | state->beginning = ptr; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1358 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1359 | state->start = (void*) ((char*) ptr + start * state->charsize); |
| 1360 | state->end = (void*) ((char*) ptr + end * state->charsize); |
| 1361 | |
| 1362 | Py_INCREF(string); |
| 1363 | state->string = string; |
| 1364 | state->pos = start; |
| 1365 | state->endpos = end; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1366 | |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1367 | if (pattern->flags & SRE_FLAG_LOCALE) |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 1368 | state->lower = sre_lower_locale; |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1369 | #if defined(HAVE_UNICODE) |
| 1370 | else if (pattern->flags & SRE_FLAG_UNICODE) |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 1371 | state->lower = sre_lower_unicode; |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1372 | #endif |
| 1373 | else |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 1374 | state->lower = sre_lower; |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1375 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1376 | return string; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1379 | LOCAL(void) |
| 1380 | state_fini(SRE_STATE* state) |
| 1381 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1382 | Py_XDECREF(state->string); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 1383 | mark_fini(state); |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | LOCAL(PyObject*) |
| 1387 | state_getslice(SRE_STATE* state, int index, PyObject* string) |
| 1388 | { |
Fredrik Lundh | 5810064 | 2000-08-09 09:14:35 +0000 | [diff] [blame] | 1389 | int i, j; |
| 1390 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1391 | index = (index - 1) * 2; |
| 1392 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1393 | if (string == Py_None || !state->mark[index] || !state->mark[index+1]) { |
Fredrik Lundh | 5810064 | 2000-08-09 09:14:35 +0000 | [diff] [blame] | 1394 | i = j = 0; |
| 1395 | } else { |
| 1396 | i = ((char*)state->mark[index] - (char*)state->beginning) / |
| 1397 | state->charsize; |
| 1398 | j = ((char*)state->mark[index+1] - (char*)state->beginning) / |
| 1399 | state->charsize; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1400 | } |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1401 | |
Fredrik Lundh | 5810064 | 2000-08-09 09:14:35 +0000 | [diff] [blame] | 1402 | return PySequence_GetSlice(string, i, j); |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 1405 | static void |
| 1406 | pattern_error(int status) |
| 1407 | { |
| 1408 | switch (status) { |
| 1409 | case SRE_ERROR_RECURSION_LIMIT: |
| 1410 | PyErr_SetString( |
| 1411 | PyExc_RuntimeError, |
| 1412 | "maximum recursion limit exceeded" |
| 1413 | ); |
| 1414 | break; |
| 1415 | case SRE_ERROR_MEMORY: |
| 1416 | PyErr_NoMemory(); |
| 1417 | break; |
| 1418 | default: |
| 1419 | /* other error codes indicate compiler/engine bugs */ |
| 1420 | PyErr_SetString( |
| 1421 | PyExc_RuntimeError, |
| 1422 | "internal error in regular expression engine" |
| 1423 | ); |
| 1424 | } |
| 1425 | } |
| 1426 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1427 | static PyObject* |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1428 | pattern_new_match(PatternObject* pattern, SRE_STATE* state, int status) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1429 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1430 | /* create match object (from state object) */ |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1431 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1432 | MatchObject* match; |
| 1433 | int i, j; |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1434 | char* base; |
| 1435 | int n; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1436 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1437 | if (status > 0) { |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1438 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1439 | /* create match object (with room for extra group marks) */ |
| 1440 | match = PyObject_NEW_VAR(MatchObject, &Match_Type, |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1441 | 2*(pattern->groups+1)); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1442 | if (!match) |
| 1443 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1444 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1445 | Py_INCREF(pattern); |
| 1446 | match->pattern = pattern; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1447 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1448 | Py_INCREF(state->string); |
| 1449 | match->string = state->string; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1450 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1451 | match->regs = NULL; |
| 1452 | match->groups = pattern->groups+1; |
| 1453 | |
| 1454 | /* fill in group slices */ |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1455 | |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1456 | base = (char*) state->beginning; |
| 1457 | n = state->charsize; |
| 1458 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1459 | match->mark[0] = ((char*) state->start - base) / n; |
| 1460 | match->mark[1] = ((char*) state->ptr - base) / n; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1461 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1462 | for (i = j = 0; i < pattern->groups; i++, j+=2) |
| 1463 | if (j+1 <= state->lastmark && state->mark[j] && state->mark[j+1]) { |
| 1464 | match->mark[j+2] = ((char*) state->mark[j] - base) / n; |
| 1465 | match->mark[j+3] = ((char*) state->mark[j+1] - base) / n; |
| 1466 | } else |
| 1467 | match->mark[j+2] = match->mark[j+3] = -1; /* undefined */ |
| 1468 | |
| 1469 | match->pos = state->pos; |
| 1470 | match->endpos = state->endpos; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1471 | |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1472 | match->lastindex = state->lastindex; |
| 1473 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1474 | return (PyObject*) match; |
Fredrik Lundh | 7cafe4d | 2000-07-02 17:33:27 +0000 | [diff] [blame] | 1475 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 1476 | } else if (status == 0) { |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1477 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 1478 | /* no match */ |
| 1479 | Py_INCREF(Py_None); |
| 1480 | return Py_None; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1481 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1482 | } |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1483 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 1484 | /* internal error */ |
| 1485 | pattern_error(status); |
| 1486 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | static PyObject* |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 1490 | pattern_scanner(PatternObject* pattern, PyObject* args) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1491 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1492 | /* create search state object */ |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1493 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1494 | ScannerObject* self; |
| 1495 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1496 | PyObject* string; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1497 | int start = 0; |
| 1498 | int end = INT_MAX; |
| 1499 | if (!PyArg_ParseTuple(args, "O|ii:scanner", &string, &start, &end)) |
| 1500 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1501 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1502 | /* create scanner object */ |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 1503 | self = PyObject_NEW(ScannerObject, &Scanner_Type); |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1504 | if (!self) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1505 | return NULL; |
| 1506 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1507 | string = state_init(&self->state, pattern, string, start, end); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1508 | if (!string) { |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1509 | PyObject_Del(self); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1510 | return NULL; |
| 1511 | } |
| 1512 | |
| 1513 | Py_INCREF(pattern); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1514 | self->pattern = (PyObject*) pattern; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1515 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1516 | return (PyObject*) self; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1519 | static void |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1520 | pattern_dealloc(PatternObject* self) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1521 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1522 | Py_XDECREF(self->pattern); |
| 1523 | Py_XDECREF(self->groupindex); |
| 1524 | PyObject_DEL(self); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | static PyObject* |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 1528 | pattern_match(PatternObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1529 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1530 | SRE_STATE state; |
| 1531 | int status; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1532 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1533 | PyObject* string; |
| 1534 | int start = 0; |
| 1535 | int end = INT_MAX; |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 1536 | static char* kwlist[] = { "pattern", "pos", "endpos", NULL }; |
| 1537 | if (!PyArg_ParseTupleAndKeywords(args, kw, "O|ii:match", kwlist, |
| 1538 | &string, &start, &end)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1539 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1540 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1541 | string = state_init(&state, self, string, start, end); |
| 1542 | if (!string) |
| 1543 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1544 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1545 | state.ptr = state.start; |
| 1546 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1547 | TRACE(("|%p|%p|MATCH\n", PatternObject_GetCode(self), state.ptr)); |
| 1548 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1549 | if (state.charsize == 1) { |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1550 | status = sre_match(&state, PatternObject_GetCode(self), 1); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1551 | } else { |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1552 | #if defined(HAVE_UNICODE) |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 1553 | status = sre_umatch(&state, PatternObject_GetCode(self), 1); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1554 | #endif |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1555 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1556 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1557 | TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr)); |
| 1558 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1559 | state_fini(&state); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1560 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1561 | return pattern_new_match(self, &state, status); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | static PyObject* |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 1565 | pattern_search(PatternObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1566 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1567 | SRE_STATE state; |
| 1568 | int status; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1569 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1570 | PyObject* string; |
| 1571 | int start = 0; |
| 1572 | int end = INT_MAX; |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 1573 | static char* kwlist[] = { "pattern", "pos", "endpos", NULL }; |
| 1574 | if (!PyArg_ParseTupleAndKeywords(args, kw, "O|ii:search", kwlist, |
| 1575 | &string, &start, &end)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1576 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1577 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1578 | string = state_init(&state, self, string, start, end); |
| 1579 | if (!string) |
| 1580 | return NULL; |
| 1581 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1582 | TRACE(("|%p|%p|SEARCH\n", PatternObject_GetCode(self), state.ptr)); |
| 1583 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1584 | if (state.charsize == 1) { |
| 1585 | status = sre_search(&state, PatternObject_GetCode(self)); |
| 1586 | } else { |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1587 | #if defined(HAVE_UNICODE) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1588 | status = sre_usearch(&state, PatternObject_GetCode(self)); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1589 | #endif |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1590 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1591 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 1592 | TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr)); |
| 1593 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1594 | state_fini(&state); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1595 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1596 | return pattern_new_match(self, &state, status); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | static PyObject* |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1600 | call(char* function, PyObject* args) |
| 1601 | { |
| 1602 | PyObject* name; |
| 1603 | PyObject* module; |
| 1604 | PyObject* func; |
| 1605 | PyObject* result; |
| 1606 | |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1607 | name = PyString_FromString(MODULE); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1608 | if (!name) |
| 1609 | return NULL; |
| 1610 | module = PyImport_Import(name); |
| 1611 | Py_DECREF(name); |
| 1612 | if (!module) |
| 1613 | return NULL; |
| 1614 | func = PyObject_GetAttrString(module, function); |
| 1615 | Py_DECREF(module); |
| 1616 | if (!func) |
| 1617 | return NULL; |
| 1618 | result = PyObject_CallObject(func, args); |
| 1619 | Py_DECREF(func); |
| 1620 | Py_DECREF(args); |
| 1621 | return result; |
| 1622 | } |
| 1623 | |
| 1624 | static PyObject* |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 1625 | pattern_sub(PatternObject* self, PyObject* args, PyObject* kw) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1626 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1627 | PyObject* template; |
| 1628 | PyObject* string; |
Fredrik Lundh | 2855290 | 2000-07-05 21:14:16 +0000 | [diff] [blame] | 1629 | PyObject* count = Py_False; /* zero */ |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 1630 | static char* kwlist[] = { "repl", "string", "count", NULL }; |
| 1631 | if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|O:sub", kwlist, |
| 1632 | &template, &string, &count)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1633 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1634 | |
| 1635 | /* delegate to Python code */ |
| 1636 | return call("_sub", Py_BuildValue("OOOO", self, template, string, count)); |
| 1637 | } |
| 1638 | |
| 1639 | static PyObject* |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 1640 | pattern_subn(PatternObject* self, PyObject* args, PyObject* kw) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1641 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1642 | PyObject* template; |
| 1643 | PyObject* string; |
Fredrik Lundh | 2855290 | 2000-07-05 21:14:16 +0000 | [diff] [blame] | 1644 | PyObject* count = Py_False; /* zero */ |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 1645 | static char* kwlist[] = { "repl", "string", "count", NULL }; |
| 1646 | if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|O:subn", kwlist, |
| 1647 | &template, &string, &count)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1648 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1649 | |
| 1650 | /* delegate to Python code */ |
| 1651 | return call("_subn", Py_BuildValue("OOOO", self, template, string, count)); |
| 1652 | } |
| 1653 | |
| 1654 | static PyObject* |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 1655 | pattern_split(PatternObject* self, PyObject* args, PyObject* kw) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1656 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1657 | PyObject* string; |
Fredrik Lundh | 2855290 | 2000-07-05 21:14:16 +0000 | [diff] [blame] | 1658 | PyObject* maxsplit = Py_False; /* zero */ |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 1659 | static char* kwlist[] = { "source", "maxsplit", NULL }; |
| 1660 | if (!PyArg_ParseTupleAndKeywords(args, kw, "O|O:split", kwlist, |
| 1661 | &string, &maxsplit)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1662 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1663 | |
| 1664 | /* delegate to Python code */ |
| 1665 | return call("_split", Py_BuildValue("OOO", self, string, maxsplit)); |
| 1666 | } |
| 1667 | |
| 1668 | static PyObject* |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 1669 | pattern_findall(PatternObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1670 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1671 | SRE_STATE state; |
| 1672 | PyObject* list; |
| 1673 | int status; |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1674 | int i; |
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 | PyObject* string; |
| 1677 | int start = 0; |
| 1678 | int end = INT_MAX; |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 1679 | static char* kwlist[] = { "source", "pos", "endpos", NULL }; |
| 1680 | if (!PyArg_ParseTupleAndKeywords(args, kw, "O|ii:findall", kwlist, |
| 1681 | &string, &start, &end)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1682 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1683 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1684 | string = state_init(&state, self, string, start, end); |
| 1685 | if (!string) |
| 1686 | return NULL; |
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 | list = PyList_New(0); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1689 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1690 | while (state.start <= state.end) { |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1691 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1692 | PyObject* item; |
| 1693 | |
Fredrik Lundh | ebc37b2 | 2000-10-28 19:30:41 +0000 | [diff] [blame] | 1694 | state_reset(&state); |
| 1695 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1696 | state.ptr = state.start; |
| 1697 | |
| 1698 | if (state.charsize == 1) { |
| 1699 | status = sre_search(&state, PatternObject_GetCode(self)); |
| 1700 | } else { |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1701 | #if defined(HAVE_UNICODE) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1702 | status = sre_usearch(&state, PatternObject_GetCode(self)); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1703 | #endif |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1704 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1705 | |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1706 | if (status > 0) { |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1707 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1708 | /* don't bother to build a match object */ |
| 1709 | switch (self->groups) { |
| 1710 | case 0: |
| 1711 | item = PySequence_GetSlice( |
| 1712 | string, |
| 1713 | ((char*) state.start - (char*) state.beginning) / |
| 1714 | state.charsize, |
| 1715 | ((char*) state.ptr - (char*) state.beginning) / |
| 1716 | state.charsize); |
| 1717 | if (!item) |
| 1718 | goto error; |
| 1719 | break; |
| 1720 | case 1: |
| 1721 | item = state_getslice(&state, 1, string); |
| 1722 | if (!item) |
| 1723 | goto error; |
| 1724 | break; |
| 1725 | default: |
| 1726 | item = PyTuple_New(self->groups); |
| 1727 | if (!item) |
| 1728 | goto error; |
| 1729 | for (i = 0; i < self->groups; i++) { |
| 1730 | PyObject* o = state_getslice(&state, i+1, string); |
| 1731 | if (!o) { |
| 1732 | Py_DECREF(item); |
| 1733 | goto error; |
| 1734 | } |
| 1735 | PyTuple_SET_ITEM(item, i, o); |
| 1736 | } |
| 1737 | break; |
| 1738 | } |
| 1739 | |
Fredrik Lundh | e67d8e5 | 2000-08-27 21:32:46 +0000 | [diff] [blame] | 1740 | status = PyList_Append(list, item); |
| 1741 | Py_DECREF(item); |
| 1742 | |
| 1743 | if (status < 0) |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1744 | goto error; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1745 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1746 | if (state.ptr == state.start) |
| 1747 | state.start = (void*) ((char*) state.ptr + state.charsize); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1748 | else |
| 1749 | state.start = state.ptr; |
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 | } else { |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1752 | |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1753 | if (status == 0) |
| 1754 | break; |
| 1755 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 1756 | pattern_error(status); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1757 | goto error; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1758 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1759 | } |
| 1760 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1761 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1762 | state_fini(&state); |
| 1763 | return list; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1764 | |
| 1765 | error: |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1766 | Py_DECREF(list); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1767 | state_fini(&state); |
| 1768 | return NULL; |
| 1769 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1772 | static PyMethodDef pattern_methods[] = { |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 1773 | {"match", (PyCFunction) pattern_match, METH_VARARGS|METH_KEYWORDS}, |
| 1774 | {"search", (PyCFunction) pattern_search, METH_VARARGS|METH_KEYWORDS}, |
| 1775 | {"sub", (PyCFunction) pattern_sub, METH_VARARGS|METH_KEYWORDS}, |
| 1776 | {"subn", (PyCFunction) pattern_subn, METH_VARARGS|METH_KEYWORDS}, |
| 1777 | {"split", (PyCFunction) pattern_split, METH_VARARGS|METH_KEYWORDS}, |
| 1778 | {"findall", (PyCFunction) pattern_findall, METH_VARARGS|METH_KEYWORDS}, |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1779 | /* experimental */ |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 1780 | {"scanner", (PyCFunction) pattern_scanner, METH_VARARGS}, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1781 | {NULL, NULL} |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1782 | }; |
| 1783 | |
| 1784 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1785 | pattern_getattr(PatternObject* self, char* name) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1786 | { |
| 1787 | PyObject* res; |
| 1788 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1789 | res = Py_FindMethod(pattern_methods, (PyObject*) self, name); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1790 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1791 | if (res) |
| 1792 | return res; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1793 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1794 | PyErr_Clear(); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1795 | |
| 1796 | /* attributes */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1797 | if (!strcmp(name, "pattern")) { |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1798 | Py_INCREF(self->pattern); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1799 | return self->pattern; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1800 | } |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1801 | |
| 1802 | if (!strcmp(name, "flags")) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1803 | return Py_BuildValue("i", self->flags); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1804 | |
Fredrik Lundh | 01016fe | 2000-06-30 00:27:46 +0000 | [diff] [blame] | 1805 | if (!strcmp(name, "groups")) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1806 | return Py_BuildValue("i", self->groups); |
Fredrik Lundh | 01016fe | 2000-06-30 00:27:46 +0000 | [diff] [blame] | 1807 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1808 | if (!strcmp(name, "groupindex") && self->groupindex) { |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1809 | Py_INCREF(self->groupindex); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1810 | return self->groupindex; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1813 | PyErr_SetString(PyExc_AttributeError, name); |
| 1814 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | statichere PyTypeObject Pattern_Type = { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1818 | PyObject_HEAD_INIT(NULL) |
| 1819 | 0, "SRE_Pattern", |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1820 | sizeof(PatternObject), sizeof(SRE_CODE), |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1821 | (destructor)pattern_dealloc, /*tp_dealloc*/ |
| 1822 | 0, /*tp_print*/ |
| 1823 | (getattrfunc)pattern_getattr /*tp_getattr*/ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1824 | }; |
| 1825 | |
| 1826 | /* -------------------------------------------------------------------- */ |
| 1827 | /* match methods */ |
| 1828 | |
| 1829 | static void |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1830 | match_dealloc(MatchObject* self) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1831 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1832 | Py_XDECREF(self->regs); |
| 1833 | Py_XDECREF(self->string); |
| 1834 | Py_DECREF(self->pattern); |
| 1835 | PyObject_DEL(self); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | static PyObject* |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 1839 | match_getslice_by_index(MatchObject* self, int index, PyObject* def) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1840 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1841 | if (index < 0 || index >= self->groups) { |
| 1842 | /* raise IndexError if we were given a bad group number */ |
| 1843 | PyErr_SetString( |
| 1844 | PyExc_IndexError, |
| 1845 | "no such group" |
| 1846 | ); |
| 1847 | return NULL; |
| 1848 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1849 | |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1850 | index *= 2; |
| 1851 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1852 | if (self->string == Py_None || self->mark[index] < 0) { |
| 1853 | /* return default value if the string or group is undefined */ |
| 1854 | Py_INCREF(def); |
| 1855 | return def; |
| 1856 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1857 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1858 | return PySequence_GetSlice( |
| 1859 | self->string, self->mark[index], self->mark[index+1] |
| 1860 | ); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1863 | static int |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1864 | match_getindex(MatchObject* self, PyObject* index) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1865 | { |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1866 | int i; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1867 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1868 | if (PyInt_Check(index)) |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1869 | return (int) PyInt_AS_LONG(index); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1870 | |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1871 | i = -1; |
| 1872 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1873 | if (self->pattern->groupindex) { |
| 1874 | index = PyObject_GetItem(self->pattern->groupindex, index); |
| 1875 | if (index) { |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1876 | if (PyInt_Check(index)) |
| 1877 | i = (int) PyInt_AS_LONG(index); |
| 1878 | Py_DECREF(index); |
| 1879 | } else |
| 1880 | PyErr_Clear(); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1881 | } |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1882 | |
| 1883 | return i; |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | static PyObject* |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 1887 | match_getslice(MatchObject* self, PyObject* index, PyObject* def) |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 1888 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1889 | return match_getslice_by_index(self, match_getindex(self, index), def); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | static PyObject* |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 1893 | match_expand(MatchObject* self, PyObject* args) |
| 1894 | { |
| 1895 | PyObject* template; |
| 1896 | if (!PyArg_ParseTuple(args, "O:expand", &template)) |
| 1897 | return NULL; |
| 1898 | |
| 1899 | /* delegate to Python code */ |
| 1900 | return call( |
| 1901 | "_expand", |
| 1902 | Py_BuildValue("OOO", self->pattern, self, template) |
| 1903 | ); |
| 1904 | } |
| 1905 | |
| 1906 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1907 | match_group(MatchObject* self, PyObject* args) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1908 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1909 | PyObject* result; |
| 1910 | int i, size; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1911 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1912 | size = PyTuple_GET_SIZE(args); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1913 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1914 | switch (size) { |
| 1915 | case 0: |
| 1916 | result = match_getslice(self, Py_False, Py_None); |
| 1917 | break; |
| 1918 | case 1: |
| 1919 | result = match_getslice(self, PyTuple_GET_ITEM(args, 0), Py_None); |
| 1920 | break; |
| 1921 | default: |
| 1922 | /* fetch multiple items */ |
| 1923 | result = PyTuple_New(size); |
| 1924 | if (!result) |
| 1925 | return NULL; |
| 1926 | for (i = 0; i < size; i++) { |
| 1927 | PyObject* item = match_getslice( |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 1928 | self, PyTuple_GET_ITEM(args, i), Py_None |
| 1929 | ); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1930 | if (!item) { |
| 1931 | Py_DECREF(result); |
| 1932 | return NULL; |
| 1933 | } |
| 1934 | PyTuple_SET_ITEM(result, i, item); |
| 1935 | } |
| 1936 | break; |
| 1937 | } |
| 1938 | return result; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | static PyObject* |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 1942 | match_groups(MatchObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1943 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1944 | PyObject* result; |
| 1945 | int index; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1946 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1947 | PyObject* def = Py_None; |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 1948 | static char* kwlist[] = { "default", NULL }; |
| 1949 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:groups", kwlist, &def)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1950 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1951 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1952 | result = PyTuple_New(self->groups-1); |
| 1953 | if (!result) |
| 1954 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1955 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1956 | for (index = 1; index < self->groups; index++) { |
| 1957 | PyObject* item; |
| 1958 | item = match_getslice_by_index(self, index, def); |
| 1959 | if (!item) { |
| 1960 | Py_DECREF(result); |
| 1961 | return NULL; |
| 1962 | } |
| 1963 | PyTuple_SET_ITEM(result, index-1, item); |
| 1964 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1965 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1966 | return result; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1967 | } |
| 1968 | |
| 1969 | static PyObject* |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 1970 | match_groupdict(MatchObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1971 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1972 | PyObject* result; |
| 1973 | PyObject* keys; |
| 1974 | int index; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1975 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1976 | PyObject* def = Py_None; |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 1977 | static char* kwlist[] = { "default", NULL }; |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame^] | 1978 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:groupdict", kwlist, &def)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1979 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 1980 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1981 | result = PyDict_New(); |
| 1982 | if (!result || !self->pattern->groupindex) |
| 1983 | return result; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1984 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1985 | keys = PyMapping_Keys(self->pattern->groupindex); |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame^] | 1986 | if (!keys) |
| 1987 | goto failed; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1988 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1989 | for (index = 0; index < PyList_GET_SIZE(keys); index++) { |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame^] | 1990 | int status; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1991 | PyObject* key; |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame^] | 1992 | PyObject* value; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1993 | key = PyList_GET_ITEM(keys, index); |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame^] | 1994 | if (!key) |
| 1995 | goto failed; |
| 1996 | value = match_getslice(self, key, def); |
| 1997 | if (!value) { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1998 | Py_DECREF(key); |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame^] | 1999 | goto failed; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2000 | } |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame^] | 2001 | status = PyDict_SetItem(result, key, value); |
| 2002 | Py_DECREF(value); |
| 2003 | if (status < 0) |
| 2004 | goto failed; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2005 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2006 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2007 | Py_DECREF(keys); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2008 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2009 | return result; |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame^] | 2010 | |
| 2011 | failed: |
| 2012 | Py_DECREF(keys); |
| 2013 | Py_DECREF(result); |
| 2014 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
| 2017 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2018 | match_start(MatchObject* self, PyObject* args) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2019 | { |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2020 | int index; |
| 2021 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2022 | PyObject* index_ = Py_False; /* zero */ |
| 2023 | if (!PyArg_ParseTuple(args, "|O:start", &index_)) |
| 2024 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2025 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2026 | index = match_getindex(self, index_); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2027 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2028 | if (index < 0 || index >= self->groups) { |
| 2029 | PyErr_SetString( |
| 2030 | PyExc_IndexError, |
| 2031 | "no such group" |
| 2032 | ); |
| 2033 | return NULL; |
| 2034 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2035 | |
Fredrik Lundh | 510c97b | 2000-09-02 16:36:57 +0000 | [diff] [blame] | 2036 | /* mark is -1 if group is undefined */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2037 | return Py_BuildValue("i", self->mark[index*2]); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
| 2040 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2041 | match_end(MatchObject* self, PyObject* args) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2042 | { |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2043 | int index; |
| 2044 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2045 | PyObject* index_ = Py_False; /* zero */ |
| 2046 | if (!PyArg_ParseTuple(args, "|O:end", &index_)) |
| 2047 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2048 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2049 | index = match_getindex(self, index_); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2050 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2051 | if (index < 0 || index >= self->groups) { |
| 2052 | PyErr_SetString( |
| 2053 | PyExc_IndexError, |
| 2054 | "no such group" |
| 2055 | ); |
| 2056 | return NULL; |
| 2057 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2058 | |
Fredrik Lundh | 510c97b | 2000-09-02 16:36:57 +0000 | [diff] [blame] | 2059 | /* mark is -1 if group is undefined */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2060 | return Py_BuildValue("i", self->mark[index*2+1]); |
| 2061 | } |
| 2062 | |
| 2063 | LOCAL(PyObject*) |
| 2064 | _pair(int i1, int i2) |
| 2065 | { |
| 2066 | PyObject* pair; |
| 2067 | PyObject* item; |
| 2068 | |
| 2069 | pair = PyTuple_New(2); |
| 2070 | if (!pair) |
| 2071 | return NULL; |
| 2072 | |
| 2073 | item = PyInt_FromLong(i1); |
| 2074 | if (!item) |
| 2075 | goto error; |
| 2076 | PyTuple_SET_ITEM(pair, 0, item); |
| 2077 | |
| 2078 | item = PyInt_FromLong(i2); |
| 2079 | if (!item) |
| 2080 | goto error; |
| 2081 | PyTuple_SET_ITEM(pair, 1, item); |
| 2082 | |
| 2083 | return pair; |
| 2084 | |
| 2085 | error: |
| 2086 | Py_DECREF(pair); |
| 2087 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2091 | match_span(MatchObject* self, PyObject* args) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2092 | { |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2093 | int index; |
| 2094 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2095 | PyObject* index_ = Py_False; /* zero */ |
| 2096 | if (!PyArg_ParseTuple(args, "|O:span", &index_)) |
| 2097 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2098 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2099 | index = match_getindex(self, index_); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2100 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2101 | if (index < 0 || index >= self->groups) { |
| 2102 | PyErr_SetString( |
| 2103 | PyExc_IndexError, |
| 2104 | "no such group" |
| 2105 | ); |
| 2106 | return NULL; |
| 2107 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2108 | |
Fredrik Lundh | 510c97b | 2000-09-02 16:36:57 +0000 | [diff] [blame] | 2109 | /* marks are -1 if group is undefined */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2110 | return _pair(self->mark[index*2], self->mark[index*2+1]); |
| 2111 | } |
| 2112 | |
| 2113 | static PyObject* |
| 2114 | match_regs(MatchObject* self) |
| 2115 | { |
| 2116 | PyObject* regs; |
| 2117 | PyObject* item; |
| 2118 | int index; |
| 2119 | |
| 2120 | regs = PyTuple_New(self->groups); |
| 2121 | if (!regs) |
| 2122 | return NULL; |
| 2123 | |
| 2124 | for (index = 0; index < self->groups; index++) { |
| 2125 | item = _pair(self->mark[index*2], self->mark[index*2+1]); |
| 2126 | if (!item) { |
| 2127 | Py_DECREF(regs); |
| 2128 | return NULL; |
| 2129 | } |
| 2130 | PyTuple_SET_ITEM(regs, index, item); |
| 2131 | } |
| 2132 | |
| 2133 | Py_INCREF(regs); |
| 2134 | self->regs = regs; |
| 2135 | |
| 2136 | return regs; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2139 | static PyMethodDef match_methods[] = { |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 2140 | {"group", (PyCFunction) match_group, METH_VARARGS}, |
| 2141 | {"start", (PyCFunction) match_start, METH_VARARGS}, |
| 2142 | {"end", (PyCFunction) match_end, METH_VARARGS}, |
| 2143 | {"span", (PyCFunction) match_span, METH_VARARGS}, |
| 2144 | {"groups", (PyCFunction) match_groups, METH_VARARGS|METH_KEYWORDS}, |
| 2145 | {"groupdict", (PyCFunction) match_groupdict, METH_VARARGS|METH_KEYWORDS}, |
| 2146 | {"expand", (PyCFunction) match_expand, METH_VARARGS}, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2147 | {NULL, NULL} |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2148 | }; |
| 2149 | |
| 2150 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2151 | match_getattr(MatchObject* self, char* name) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2152 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2153 | PyObject* res; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2154 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2155 | res = Py_FindMethod(match_methods, (PyObject*) self, name); |
| 2156 | if (res) |
| 2157 | return res; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2158 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2159 | PyErr_Clear(); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2160 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2161 | if (!strcmp(name, "lastindex")) { |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2162 | if (self->lastindex >= 0) |
| 2163 | return Py_BuildValue("i", self->lastindex); |
Fredrik Lundh | c230173 | 2000-07-02 22:25:39 +0000 | [diff] [blame] | 2164 | Py_INCREF(Py_None); |
| 2165 | return Py_None; |
| 2166 | } |
| 2167 | |
| 2168 | if (!strcmp(name, "lastgroup")) { |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2169 | if (self->pattern->indexgroup && self->lastindex >= 0) { |
Fredrik Lundh | c230173 | 2000-07-02 22:25:39 +0000 | [diff] [blame] | 2170 | PyObject* result = PySequence_GetItem( |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2171 | self->pattern->indexgroup, self->lastindex |
Fredrik Lundh | c230173 | 2000-07-02 22:25:39 +0000 | [diff] [blame] | 2172 | ); |
| 2173 | if (result) |
| 2174 | return result; |
| 2175 | PyErr_Clear(); |
| 2176 | } |
| 2177 | Py_INCREF(Py_None); |
| 2178 | return Py_None; |
| 2179 | } |
| 2180 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2181 | if (!strcmp(name, "string")) { |
| 2182 | if (self->string) { |
| 2183 | Py_INCREF(self->string); |
| 2184 | return self->string; |
| 2185 | } else { |
| 2186 | Py_INCREF(Py_None); |
| 2187 | return Py_None; |
| 2188 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2189 | } |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2190 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2191 | if (!strcmp(name, "regs")) { |
| 2192 | if (self->regs) { |
| 2193 | Py_INCREF(self->regs); |
| 2194 | return self->regs; |
| 2195 | } else |
| 2196 | return match_regs(self); |
| 2197 | } |
| 2198 | |
| 2199 | if (!strcmp(name, "re")) { |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2200 | Py_INCREF(self->pattern); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2201 | return (PyObject*) self->pattern; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2202 | } |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2203 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2204 | if (!strcmp(name, "pos")) |
| 2205 | return Py_BuildValue("i", self->pos); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2206 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2207 | if (!strcmp(name, "endpos")) |
| 2208 | return Py_BuildValue("i", self->endpos); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2209 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2210 | PyErr_SetString(PyExc_AttributeError, name); |
| 2211 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | /* FIXME: implement setattr("string", None) as a special case (to |
| 2215 | detach the associated string, if any */ |
| 2216 | |
| 2217 | statichere PyTypeObject Match_Type = { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2218 | PyObject_HEAD_INIT(NULL) |
| 2219 | 0, "SRE_Match", |
| 2220 | sizeof(MatchObject), sizeof(int), |
| 2221 | (destructor)match_dealloc, /*tp_dealloc*/ |
| 2222 | 0, /*tp_print*/ |
| 2223 | (getattrfunc)match_getattr /*tp_getattr*/ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2224 | }; |
| 2225 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2226 | /* -------------------------------------------------------------------- */ |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 2227 | /* scanner methods (experimental) */ |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2228 | |
| 2229 | static void |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 2230 | scanner_dealloc(ScannerObject* self) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2231 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2232 | state_fini(&self->state); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2233 | Py_DECREF(self->pattern); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2234 | PyObject_DEL(self); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | static PyObject* |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 2238 | scanner_match(ScannerObject* self, PyObject* args) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2239 | { |
| 2240 | SRE_STATE* state = &self->state; |
| 2241 | PyObject* match; |
| 2242 | int status; |
| 2243 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 2244 | state_reset(state); |
| 2245 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2246 | state->ptr = state->start; |
| 2247 | |
| 2248 | if (state->charsize == 1) { |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 2249 | status = sre_match(state, PatternObject_GetCode(self->pattern), 1); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2250 | } else { |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2251 | #if defined(HAVE_UNICODE) |
Fredrik Lundh | 2f2c67d | 2000-08-01 21:05:41 +0000 | [diff] [blame] | 2252 | status = sre_umatch(state, PatternObject_GetCode(self->pattern), 1); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2253 | #endif |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2254 | } |
| 2255 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2256 | match = pattern_new_match((PatternObject*) self->pattern, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2257 | state, status); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2258 | |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2259 | if (status == 0 || state->ptr == state->start) |
| 2260 | state->start = (void*) ((char*) state->ptr + state->charsize); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2261 | else |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2262 | state->start = state->ptr; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2263 | |
| 2264 | return match; |
| 2265 | } |
| 2266 | |
| 2267 | |
| 2268 | static PyObject* |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 2269 | scanner_search(ScannerObject* self, PyObject* args) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2270 | { |
| 2271 | SRE_STATE* state = &self->state; |
| 2272 | PyObject* match; |
| 2273 | int status; |
| 2274 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 2275 | state_reset(state); |
| 2276 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2277 | state->ptr = state->start; |
| 2278 | |
| 2279 | if (state->charsize == 1) { |
| 2280 | status = sre_search(state, PatternObject_GetCode(self->pattern)); |
| 2281 | } else { |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2282 | #if defined(HAVE_UNICODE) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2283 | status = sre_usearch(state, PatternObject_GetCode(self->pattern)); |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2284 | #endif |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2287 | match = pattern_new_match((PatternObject*) self->pattern, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2288 | state, status); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2289 | |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 2290 | if (status == 0 || state->ptr == state->start) |
| 2291 | state->start = (void*) ((char*) state->ptr + state->charsize); |
| 2292 | else |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2293 | state->start = state->ptr; |
| 2294 | |
| 2295 | return match; |
| 2296 | } |
| 2297 | |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 2298 | static PyMethodDef scanner_methods[] = { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2299 | {"match", (PyCFunction) scanner_match, 0}, |
| 2300 | {"search", (PyCFunction) scanner_search, 0}, |
| 2301 | {NULL, NULL} |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2302 | }; |
| 2303 | |
| 2304 | static PyObject* |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 2305 | scanner_getattr(ScannerObject* self, char* name) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2306 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2307 | PyObject* res; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2308 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2309 | res = Py_FindMethod(scanner_methods, (PyObject*) self, name); |
| 2310 | if (res) |
| 2311 | return res; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2312 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2313 | PyErr_Clear(); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2314 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2315 | /* attributes */ |
| 2316 | if (!strcmp(name, "pattern")) { |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2317 | Py_INCREF(self->pattern); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2318 | return self->pattern; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2321 | PyErr_SetString(PyExc_AttributeError, name); |
| 2322 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 2325 | statichere PyTypeObject Scanner_Type = { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2326 | PyObject_HEAD_INIT(NULL) |
| 2327 | 0, "SRE_Scanner", |
| 2328 | sizeof(ScannerObject), 0, |
| 2329 | (destructor)scanner_dealloc, /*tp_dealloc*/ |
| 2330 | 0, /*tp_print*/ |
| 2331 | (getattrfunc)scanner_getattr, /*tp_getattr*/ |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2332 | }; |
| 2333 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2334 | static PyMethodDef _functions[] = { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2335 | {"compile", _compile, 1}, |
| 2336 | {"getcodesize", sre_codesize, 1}, |
| 2337 | {"getlower", sre_getlower, 1}, |
| 2338 | {NULL, NULL} |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2339 | }; |
| 2340 | |
| 2341 | void |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2342 | #if defined(WIN32) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2343 | __declspec(dllexport) |
| 2344 | #endif |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 2345 | init_sre(void) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2346 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2347 | /* Patch object types */ |
| 2348 | Pattern_Type.ob_type = Match_Type.ob_type = |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 2349 | Scanner_Type.ob_type = &PyType_Type; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2350 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2351 | Py_InitModule("_" MODULE, _functions); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2354 | #endif /* !defined(SRE_RECURSIVE) */ |