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