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