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