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