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