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: |
Serhiy Storchaka | 32eddc1 | 2013-11-23 23:20:30 +0200 | [diff] [blame] | 7 | * 1999-10-24 fl created (based on existing template matcher code) |
| 8 | * 2000-03-06 fl first alpha, sort of |
| 9 | * 2000-08-01 fl fixes for 1.6b1 |
| 10 | * 2000-08-07 fl use PyOS_CheckStack() if available |
| 11 | * 2000-09-20 fl added expand method |
| 12 | * 2001-03-20 fl lots of fixes for 2.1b2 |
| 13 | * 2001-04-15 fl export copyright as Python attribute, not global |
| 14 | * 2001-04-28 fl added __copy__ methods (work in progress) |
| 15 | * 2001-05-14 fl fixes for 1.5.2 compatibility |
| 16 | * 2001-07-01 fl added BIGCHARSET support (from Martin von Loewis) |
| 17 | * 2001-10-18 fl fixed group reset issue (from Matthew Mueller) |
| 18 | * 2001-10-20 fl added split primitive; reenable unicode for 1.6/2.0/2.1 |
| 19 | * 2001-10-21 fl added sub/subn primitive |
| 20 | * 2001-10-24 fl added finditer primitive (for 2.2 only) |
| 21 | * 2001-12-07 fl fixed memory leak in sub/subn (Guido van Rossum) |
| 22 | * 2002-11-09 fl fixed empty sub/subn return type |
| 23 | * 2003-04-18 mvl fully support 4-byte codes |
| 24 | * 2003-10-17 gn implemented non recursive scheme |
| 25 | * 2013-02-04 mrab added fullmatch primitive |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 26 | * |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 27 | * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved. |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 28 | * |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 29 | * This version of the SRE library can be redistributed under CNRI's |
| 30 | * Python 1.6 license. For any other use, please contact Secret Labs |
| 31 | * AB (info@pythonware.com). |
| 32 | * |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 33 | * Portions of this engine have been developed in cooperation with |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 34 | * CNRI. Hewlett-Packard provided funding for 1.6 integration and |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 35 | * other compatibility work. |
| 36 | */ |
| 37 | |
Fredrik Lundh | 9c7eab8 | 2001-04-15 19:00:58 +0000 | [diff] [blame] | 38 | static char copyright[] = |
Fredrik Lundh | 09705f0 | 2002-11-22 12:46:35 +0000 | [diff] [blame] | 39 | " SRE 2.2.2 Copyright (c) 1997-2002 by Secret Labs AB "; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 40 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 41 | #define PY_SSIZE_T_CLEAN |
| 42 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 43 | #include "Python.h" |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 44 | #include "structmember.h" /* offsetof */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 45 | |
| 46 | #include "sre.h" |
| 47 | |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 48 | #define SRE_CODE_BITS (8 * sizeof(SRE_CODE)) |
| 49 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 50 | #include <ctype.h> |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 51 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 52 | /* name of this module, minus the leading underscore */ |
Fredrik Lundh | 1c5aa69 | 2001-01-16 07:37:30 +0000 | [diff] [blame] | 53 | #if !defined(SRE_MODULE) |
| 54 | #define SRE_MODULE "sre" |
| 55 | #endif |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 56 | |
Thomas Wouters | 9ada3d6 | 2006-04-21 09:47:09 +0000 | [diff] [blame] | 57 | #define SRE_PY_MODULE "re" |
| 58 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 59 | /* defining this one enables tracing */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 60 | #undef VERBOSE |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 61 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 62 | /* -------------------------------------------------------------------- */ |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 63 | /* optional features */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 64 | |
| 65 | /* enables fast searching */ |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 66 | #define USE_FAST_SEARCH |
| 67 | |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 68 | /* enables copy/deepcopy handling (work in progress) */ |
| 69 | #undef USE_BUILTIN_COPY |
| 70 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 71 | /* -------------------------------------------------------------------- */ |
| 72 | |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 73 | #if defined(_MSC_VER) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 74 | #pragma optimize("agtw", on) /* doesn't seem to make much difference... */ |
Fredrik Lundh | 2855290 | 2000-07-05 21:14:16 +0000 | [diff] [blame] | 75 | #pragma warning(disable: 4710) /* who cares if functions are not inlined ;-) */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 76 | /* fastest possible local call under MSVC */ |
| 77 | #define LOCAL(type) static __inline type __fastcall |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 78 | #elif defined(USE_INLINE) |
Fredrik Lundh | 29c08be | 2000-06-29 23:33:12 +0000 | [diff] [blame] | 79 | #define LOCAL(type) static inline type |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 80 | #else |
| 81 | #define LOCAL(type) static type |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 82 | #endif |
| 83 | |
| 84 | /* error codes */ |
| 85 | #define SRE_ERROR_ILLEGAL -1 /* illegal opcode */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 86 | #define SRE_ERROR_STATE -2 /* illegal state */ |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 87 | #define SRE_ERROR_RECURSION_LIMIT -3 /* runaway recursion */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 88 | #define SRE_ERROR_MEMORY -9 /* out of memory */ |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 89 | #define SRE_ERROR_INTERRUPTED -10 /* signal handler raised exception */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 90 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 91 | #if defined(VERBOSE) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 92 | #define TRACE(v) printf v |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 93 | #else |
| 94 | #define TRACE(v) |
| 95 | #endif |
| 96 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 97 | /* -------------------------------------------------------------------- */ |
| 98 | /* search engine state */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 99 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 100 | #define SRE_IS_DIGIT(ch)\ |
Serhiy Storchaka | 5aa4744 | 2014-10-10 11:10:46 +0300 | [diff] [blame] | 101 | ((ch) < 128 && Py_ISDIGIT(ch)) |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 102 | #define SRE_IS_SPACE(ch)\ |
Serhiy Storchaka | 5aa4744 | 2014-10-10 11:10:46 +0300 | [diff] [blame] | 103 | ((ch) < 128 && Py_ISSPACE(ch)) |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 104 | #define SRE_IS_LINEBREAK(ch)\ |
Serhiy Storchaka | 5aa4744 | 2014-10-10 11:10:46 +0300 | [diff] [blame] | 105 | ((ch) == '\n') |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 106 | #define SRE_IS_ALNUM(ch)\ |
Serhiy Storchaka | 5aa4744 | 2014-10-10 11:10:46 +0300 | [diff] [blame] | 107 | ((ch) < 128 && Py_ISALNUM(ch)) |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 108 | #define SRE_IS_WORD(ch)\ |
Serhiy Storchaka | 5aa4744 | 2014-10-10 11:10:46 +0300 | [diff] [blame] | 109 | ((ch) < 128 && (Py_ISALNUM(ch) || (ch) == '_')) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 110 | |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 111 | static unsigned int sre_lower(unsigned int ch) |
| 112 | { |
Serhiy Storchaka | 5aa4744 | 2014-10-10 11:10:46 +0300 | [diff] [blame] | 113 | return ((ch) < 128 ? Py_TOLOWER(ch) : ch); |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Serhiy Storchaka | 4b8f894 | 2014-10-31 12:36:56 +0200 | [diff] [blame] | 116 | static unsigned int sre_upper(unsigned int ch) |
| 117 | { |
| 118 | return ((ch) < 128 ? Py_TOUPPER(ch) : ch); |
| 119 | } |
| 120 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 121 | /* locale-specific character predicates */ |
Gustavo Niemeyer | 601b963 | 2004-02-14 00:31:13 +0000 | [diff] [blame] | 122 | /* !(c & ~N) == (c < N+1) for any unsigned c, this avoids |
| 123 | * warnings when c's type supports only numbers < N+1 */ |
Gustavo Niemeyer | 601b963 | 2004-02-14 00:31:13 +0000 | [diff] [blame] | 124 | #define SRE_LOC_IS_ALNUM(ch) (!((ch) & ~255) ? isalnum((ch)) : 0) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 125 | #define SRE_LOC_IS_WORD(ch) (SRE_LOC_IS_ALNUM((ch)) || (ch) == '_') |
| 126 | |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 127 | static unsigned int sre_lower_locale(unsigned int ch) |
| 128 | { |
Gustavo Niemeyer | 601b963 | 2004-02-14 00:31:13 +0000 | [diff] [blame] | 129 | return ((ch) < 256 ? (unsigned int)tolower((ch)) : ch); |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Serhiy Storchaka | 4b8f894 | 2014-10-31 12:36:56 +0200 | [diff] [blame] | 132 | static unsigned int sre_upper_locale(unsigned int ch) |
| 133 | { |
| 134 | return ((ch) < 256 ? (unsigned int)toupper((ch)) : ch); |
| 135 | } |
| 136 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 137 | /* unicode-specific character predicates */ |
| 138 | |
Victor Stinner | 0058b86 | 2011-09-29 03:27:47 +0200 | [diff] [blame] | 139 | #define SRE_UNI_IS_DIGIT(ch) Py_UNICODE_ISDECIMAL(ch) |
| 140 | #define SRE_UNI_IS_SPACE(ch) Py_UNICODE_ISSPACE(ch) |
| 141 | #define SRE_UNI_IS_LINEBREAK(ch) Py_UNICODE_ISLINEBREAK(ch) |
| 142 | #define SRE_UNI_IS_ALNUM(ch) Py_UNICODE_ISALNUM(ch) |
| 143 | #define SRE_UNI_IS_WORD(ch) (SRE_UNI_IS_ALNUM(ch) || (ch) == '_') |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 144 | |
| 145 | static unsigned int sre_lower_unicode(unsigned int ch) |
| 146 | { |
Victor Stinner | 0058b86 | 2011-09-29 03:27:47 +0200 | [diff] [blame] | 147 | return (unsigned int) Py_UNICODE_TOLOWER(ch); |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Serhiy Storchaka | 4b8f894 | 2014-10-31 12:36:56 +0200 | [diff] [blame] | 150 | static unsigned int sre_upper_unicode(unsigned int ch) |
| 151 | { |
| 152 | return (unsigned int) Py_UNICODE_TOUPPER(ch); |
| 153 | } |
| 154 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 155 | LOCAL(int) |
| 156 | sre_category(SRE_CODE category, unsigned int ch) |
| 157 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 158 | switch (category) { |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 159 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 160 | case SRE_CATEGORY_DIGIT: |
| 161 | return SRE_IS_DIGIT(ch); |
| 162 | case SRE_CATEGORY_NOT_DIGIT: |
| 163 | return !SRE_IS_DIGIT(ch); |
| 164 | case SRE_CATEGORY_SPACE: |
| 165 | return SRE_IS_SPACE(ch); |
| 166 | case SRE_CATEGORY_NOT_SPACE: |
| 167 | return !SRE_IS_SPACE(ch); |
| 168 | case SRE_CATEGORY_WORD: |
| 169 | return SRE_IS_WORD(ch); |
| 170 | case SRE_CATEGORY_NOT_WORD: |
| 171 | return !SRE_IS_WORD(ch); |
| 172 | case SRE_CATEGORY_LINEBREAK: |
| 173 | return SRE_IS_LINEBREAK(ch); |
| 174 | case SRE_CATEGORY_NOT_LINEBREAK: |
| 175 | return !SRE_IS_LINEBREAK(ch); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 176 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 177 | case SRE_CATEGORY_LOC_WORD: |
| 178 | return SRE_LOC_IS_WORD(ch); |
| 179 | case SRE_CATEGORY_LOC_NOT_WORD: |
| 180 | return !SRE_LOC_IS_WORD(ch); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 181 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 182 | case SRE_CATEGORY_UNI_DIGIT: |
| 183 | return SRE_UNI_IS_DIGIT(ch); |
| 184 | case SRE_CATEGORY_UNI_NOT_DIGIT: |
| 185 | return !SRE_UNI_IS_DIGIT(ch); |
| 186 | case SRE_CATEGORY_UNI_SPACE: |
| 187 | return SRE_UNI_IS_SPACE(ch); |
| 188 | case SRE_CATEGORY_UNI_NOT_SPACE: |
| 189 | return !SRE_UNI_IS_SPACE(ch); |
| 190 | case SRE_CATEGORY_UNI_WORD: |
| 191 | return SRE_UNI_IS_WORD(ch); |
| 192 | case SRE_CATEGORY_UNI_NOT_WORD: |
| 193 | return !SRE_UNI_IS_WORD(ch); |
| 194 | case SRE_CATEGORY_UNI_LINEBREAK: |
| 195 | return SRE_UNI_IS_LINEBREAK(ch); |
| 196 | case SRE_CATEGORY_UNI_NOT_LINEBREAK: |
| 197 | return !SRE_UNI_IS_LINEBREAK(ch); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 198 | } |
| 199 | return 0; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /* helpers */ |
| 203 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 204 | static void |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 205 | data_stack_dealloc(SRE_STATE* state) |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 206 | { |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 207 | if (state->data_stack) { |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 208 | PyMem_FREE(state->data_stack); |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 209 | state->data_stack = NULL; |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 210 | } |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 211 | state->data_stack_size = state->data_stack_base = 0; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static int |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 215 | data_stack_grow(SRE_STATE* state, Py_ssize_t size) |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 216 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 217 | Py_ssize_t minsize, cursize; |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 218 | minsize = state->data_stack_base+size; |
| 219 | cursize = state->data_stack_size; |
| 220 | if (cursize < minsize) { |
| 221 | void* stack; |
| 222 | cursize = minsize+minsize/4+1024; |
Serhiy Storchaka | 134f0de | 2013-09-05 18:01:15 +0300 | [diff] [blame] | 223 | TRACE(("allocate/grow stack %" PY_FORMAT_SIZE_T "d\n", cursize)); |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 224 | stack = PyMem_REALLOC(state->data_stack, cursize); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 225 | if (!stack) { |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 226 | data_stack_dealloc(state); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 227 | return SRE_ERROR_MEMORY; |
| 228 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 229 | state->data_stack = (char *)stack; |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 230 | state->data_stack_size = cursize; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 231 | } |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 232 | return 0; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 235 | /* generate 8-bit version */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 236 | |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 237 | #define SRE_CHAR Py_UCS1 |
| 238 | #define SIZEOF_SRE_CHAR 1 |
| 239 | #define SRE(F) sre_ucs1_##F |
Serhiy Storchaka | 8444ebb | 2013-10-26 11:18:42 +0300 | [diff] [blame] | 240 | #include "sre_lib.h" |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 241 | |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 242 | /* generate 16-bit unicode version */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 243 | |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 244 | #define SRE_CHAR Py_UCS2 |
| 245 | #define SIZEOF_SRE_CHAR 2 |
| 246 | #define SRE(F) sre_ucs2_##F |
Serhiy Storchaka | 8444ebb | 2013-10-26 11:18:42 +0300 | [diff] [blame] | 247 | #include "sre_lib.h" |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 248 | |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 249 | /* generate 32-bit unicode version */ |
| 250 | |
| 251 | #define SRE_CHAR Py_UCS4 |
| 252 | #define SIZEOF_SRE_CHAR 4 |
| 253 | #define SRE(F) sre_ucs4_##F |
Serhiy Storchaka | 8444ebb | 2013-10-26 11:18:42 +0300 | [diff] [blame] | 254 | #include "sre_lib.h" |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 255 | |
| 256 | /* -------------------------------------------------------------------- */ |
| 257 | /* factories and destructors */ |
| 258 | |
| 259 | /* see sre.h for object declarations */ |
Victor Stinner | f558778 | 2013-11-15 23:21:11 +0100 | [diff] [blame] | 260 | static PyObject*pattern_new_match(PatternObject*, SRE_STATE*, Py_ssize_t); |
Sean Reifschneider | 7b3c975 | 2012-03-12 18:22:38 -0600 | [diff] [blame] | 261 | static PyObject*pattern_scanner(PatternObject*, PyObject*, PyObject* kw); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 262 | |
| 263 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 264 | sre_codesize(PyObject* self, PyObject *unused) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 265 | { |
Antoine Pitrou | 43fb54c | 2012-12-02 12:52:36 +0100 | [diff] [blame] | 266 | return PyLong_FromSize_t(sizeof(SRE_CODE)); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 269 | static PyObject * |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 270 | sre_getlower(PyObject* self, PyObject* args) |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 271 | { |
| 272 | int character, flags; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 273 | if (!PyArg_ParseTuple(args, "ii", &character, &flags)) |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 274 | return NULL; |
| 275 | if (flags & SRE_FLAG_LOCALE) |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 276 | return Py_BuildValue("i", sre_lower_locale(character)); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 277 | if (flags & SRE_FLAG_UNICODE) |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 278 | return Py_BuildValue("i", sre_lower_unicode(character)); |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 279 | return Py_BuildValue("i", sre_lower(character)); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 282 | LOCAL(void) |
| 283 | state_reset(SRE_STATE* state) |
| 284 | { |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 285 | /* FIXME: dynamic! */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 286 | /*memset(state->mark, 0, sizeof(*state->mark) * SRE_MARK_SIZE);*/ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 287 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 288 | state->lastmark = -1; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 289 | state->lastindex = -1; |
| 290 | |
| 291 | state->repeat = NULL; |
| 292 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 293 | data_stack_dealloc(state); |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 296 | static void* |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 297 | getstring(PyObject* string, Py_ssize_t* p_length, |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 298 | int* p_isbytes, int* p_charsize, |
Benjamin Peterson | 33d21a2 | 2012-03-07 14:59:13 -0600 | [diff] [blame] | 299 | Py_buffer *view) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 300 | { |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 301 | /* given a python object, return a data pointer, a length (in |
| 302 | characters), and a character size. return NULL if the object |
| 303 | is not a string (or not compatible) */ |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 304 | |
Alexandre Vassalotti | 70a2371 | 2007-10-14 02:05:51 +0000 | [diff] [blame] | 305 | /* Unicode objects do not support the buffer API. So, get the data |
| 306 | directly instead. */ |
| 307 | if (PyUnicode_Check(string)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 308 | if (PyUnicode_READY(string) == -1) |
| 309 | return NULL; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 310 | *p_length = PyUnicode_GET_LENGTH(string); |
Martin v. Löwis | c47adb0 | 2011-10-07 20:55:35 +0200 | [diff] [blame] | 311 | *p_charsize = PyUnicode_KIND(string); |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 312 | *p_isbytes = 0; |
| 313 | return PyUnicode_DATA(string); |
Alexandre Vassalotti | 70a2371 | 2007-10-14 02:05:51 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Victor Stinner | 0058b86 | 2011-09-29 03:27:47 +0200 | [diff] [blame] | 316 | /* get pointer to byte string buffer */ |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 317 | if (PyObject_GetBuffer(string, view, PyBUF_SIMPLE) != 0) { |
Serhiy Storchaka | 632a77e | 2015-03-25 21:03:47 +0200 | [diff] [blame] | 318 | PyErr_SetString(PyExc_TypeError, "expected string or bytes-like object"); |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 319 | return NULL; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 320 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 321 | |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 322 | *p_length = view->len; |
| 323 | *p_charsize = 1; |
| 324 | *p_isbytes = 1; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 325 | |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 326 | if (view->buf == NULL) { |
| 327 | PyErr_SetString(PyExc_ValueError, "Buffer is NULL"); |
| 328 | PyBuffer_Release(view); |
| 329 | view->buf = NULL; |
| 330 | return NULL; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 331 | } |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 332 | return view->buf; |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | LOCAL(PyObject*) |
| 336 | state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 337 | Py_ssize_t start, Py_ssize_t end) |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 338 | { |
| 339 | /* prepare state object */ |
| 340 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 341 | Py_ssize_t length; |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 342 | int isbytes, charsize; |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 343 | void* ptr; |
| 344 | |
| 345 | memset(state, 0, sizeof(SRE_STATE)); |
| 346 | |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 347 | state->mark = PyMem_New(void *, pattern->groups * 2); |
| 348 | if (!state->mark) { |
| 349 | PyErr_NoMemory(); |
| 350 | goto err; |
| 351 | } |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 352 | state->lastmark = -1; |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 353 | state->lastindex = -1; |
| 354 | |
Benjamin Peterson | e48944b | 2012-03-07 14:50:25 -0600 | [diff] [blame] | 355 | state->buffer.buf = NULL; |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 356 | ptr = getstring(string, &length, &isbytes, &charsize, &state->buffer); |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 357 | if (!ptr) |
Benjamin Peterson | e48944b | 2012-03-07 14:50:25 -0600 | [diff] [blame] | 358 | goto err; |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 359 | |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 360 | if (isbytes && pattern->isbytes == 0) { |
Benjamin Peterson | e48944b | 2012-03-07 14:50:25 -0600 | [diff] [blame] | 361 | PyErr_SetString(PyExc_TypeError, |
Serhiy Storchaka | 632a77e | 2015-03-25 21:03:47 +0200 | [diff] [blame] | 362 | "cannot use a string pattern on a bytes-like object"); |
Benjamin Peterson | e48944b | 2012-03-07 14:50:25 -0600 | [diff] [blame] | 363 | goto err; |
| 364 | } |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 365 | if (!isbytes && pattern->isbytes > 0) { |
Benjamin Peterson | e48944b | 2012-03-07 14:50:25 -0600 | [diff] [blame] | 366 | PyErr_SetString(PyExc_TypeError, |
Serhiy Storchaka | 632a77e | 2015-03-25 21:03:47 +0200 | [diff] [blame] | 367 | "cannot use a bytes pattern on a string-like object"); |
Benjamin Peterson | e48944b | 2012-03-07 14:50:25 -0600 | [diff] [blame] | 368 | goto err; |
| 369 | } |
Antoine Pitrou | fd03645 | 2008-08-19 17:56:33 +0000 | [diff] [blame] | 370 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 371 | /* adjust boundaries */ |
| 372 | if (start < 0) |
| 373 | start = 0; |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 374 | else if (start > length) |
| 375 | start = length; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 376 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 377 | if (end < 0) |
| 378 | end = 0; |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 379 | else if (end > length) |
| 380 | end = length; |
| 381 | |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 382 | state->isbytes = isbytes; |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 383 | state->charsize = charsize; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 384 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 385 | state->beginning = ptr; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 386 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 387 | state->start = (void*) ((char*) ptr + start * state->charsize); |
| 388 | state->end = (void*) ((char*) ptr + end * state->charsize); |
| 389 | |
| 390 | Py_INCREF(string); |
| 391 | state->string = string; |
| 392 | state->pos = start; |
| 393 | state->endpos = end; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 394 | |
Serhiy Storchaka | 4b8f894 | 2014-10-31 12:36:56 +0200 | [diff] [blame] | 395 | if (pattern->flags & SRE_FLAG_LOCALE) { |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 396 | state->lower = sre_lower_locale; |
Serhiy Storchaka | 4b8f894 | 2014-10-31 12:36:56 +0200 | [diff] [blame] | 397 | state->upper = sre_upper_locale; |
| 398 | } |
| 399 | else if (pattern->flags & SRE_FLAG_UNICODE) { |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 400 | state->lower = sre_lower_unicode; |
Serhiy Storchaka | 4b8f894 | 2014-10-31 12:36:56 +0200 | [diff] [blame] | 401 | state->upper = sre_upper_unicode; |
| 402 | } |
| 403 | else { |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 404 | state->lower = sre_lower; |
Serhiy Storchaka | 4b8f894 | 2014-10-31 12:36:56 +0200 | [diff] [blame] | 405 | state->upper = sre_upper; |
| 406 | } |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 407 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 408 | return string; |
Benjamin Peterson | e48944b | 2012-03-07 14:50:25 -0600 | [diff] [blame] | 409 | err: |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 410 | PyMem_Del(state->mark); |
| 411 | state->mark = NULL; |
Benjamin Peterson | e48944b | 2012-03-07 14:50:25 -0600 | [diff] [blame] | 412 | if (state->buffer.buf) |
| 413 | PyBuffer_Release(&state->buffer); |
| 414 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 417 | LOCAL(void) |
| 418 | state_fini(SRE_STATE* state) |
| 419 | { |
Benjamin Peterson | e48944b | 2012-03-07 14:50:25 -0600 | [diff] [blame] | 420 | if (state->buffer.buf) |
| 421 | PyBuffer_Release(&state->buffer); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 422 | Py_XDECREF(state->string); |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 423 | data_stack_dealloc(state); |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 424 | PyMem_Del(state->mark); |
| 425 | state->mark = NULL; |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 428 | /* calculate offset from start of string */ |
| 429 | #define STATE_OFFSET(state, member)\ |
| 430 | (((char*)(member) - (char*)(state)->beginning) / (state)->charsize) |
| 431 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 432 | LOCAL(PyObject*) |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 433 | getslice(int isbytes, const void *ptr, |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 434 | PyObject* string, Py_ssize_t start, Py_ssize_t end) |
| 435 | { |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 436 | if (isbytes) { |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 437 | if (PyBytes_CheckExact(string) && |
| 438 | start == 0 && end == PyBytes_GET_SIZE(string)) { |
| 439 | Py_INCREF(string); |
| 440 | return string; |
| 441 | } |
| 442 | return PyBytes_FromStringAndSize( |
| 443 | (const char *)ptr + start, end - start); |
| 444 | } |
| 445 | else { |
| 446 | return PyUnicode_Substring(string, start, end); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | LOCAL(PyObject*) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 451 | state_getslice(SRE_STATE* state, Py_ssize_t index, PyObject* string, int empty) |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 452 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 453 | Py_ssize_t i, j; |
Fredrik Lundh | 5810064 | 2000-08-09 09:14:35 +0000 | [diff] [blame] | 454 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 455 | index = (index - 1) * 2; |
| 456 | |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 457 | if (string == Py_None || index >= state->lastmark || !state->mark[index] || !state->mark[index+1]) { |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 458 | if (empty) |
| 459 | /* want empty string */ |
| 460 | i = j = 0; |
| 461 | else { |
| 462 | Py_INCREF(Py_None); |
| 463 | return Py_None; |
| 464 | } |
Fredrik Lundh | 5810064 | 2000-08-09 09:14:35 +0000 | [diff] [blame] | 465 | } else { |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 466 | i = STATE_OFFSET(state, state->mark[index]); |
| 467 | j = STATE_OFFSET(state, state->mark[index+1]); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 468 | } |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 469 | |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 470 | return getslice(state->isbytes, state->beginning, string, i, j); |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 473 | static void |
Victor Stinner | f558778 | 2013-11-15 23:21:11 +0100 | [diff] [blame] | 474 | pattern_error(Py_ssize_t status) |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 475 | { |
| 476 | switch (status) { |
| 477 | case SRE_ERROR_RECURSION_LIMIT: |
| 478 | PyErr_SetString( |
| 479 | PyExc_RuntimeError, |
| 480 | "maximum recursion limit exceeded" |
| 481 | ); |
| 482 | break; |
| 483 | case SRE_ERROR_MEMORY: |
| 484 | PyErr_NoMemory(); |
| 485 | break; |
Christian Heimes | 2380ac7 | 2008-01-09 00:17:24 +0000 | [diff] [blame] | 486 | case SRE_ERROR_INTERRUPTED: |
| 487 | /* An exception has already been raised, so let it fly */ |
| 488 | break; |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 489 | default: |
| 490 | /* other error codes indicate compiler/engine bugs */ |
| 491 | PyErr_SetString( |
| 492 | PyExc_RuntimeError, |
| 493 | "internal error in regular expression engine" |
| 494 | ); |
| 495 | } |
| 496 | } |
| 497 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 498 | static void |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 499 | pattern_dealloc(PatternObject* self) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 500 | { |
Raymond Hettinger | 027bb63 | 2004-05-31 03:09:25 +0000 | [diff] [blame] | 501 | if (self->weakreflist != NULL) |
| 502 | PyObject_ClearWeakRefs((PyObject *) self); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 503 | Py_XDECREF(self->pattern); |
| 504 | Py_XDECREF(self->groupindex); |
Fredrik Lundh | 6f5cba6 | 2001-01-16 07:05:29 +0000 | [diff] [blame] | 505 | Py_XDECREF(self->indexgroup); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 506 | PyObject_DEL(self); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 509 | LOCAL(Py_ssize_t) |
Serhiy Storchaka | 429b59e | 2014-05-14 21:48:17 +0300 | [diff] [blame] | 510 | sre_match(SRE_STATE* state, SRE_CODE* pattern, int match_all) |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 511 | { |
| 512 | if (state->charsize == 1) |
Serhiy Storchaka | 429b59e | 2014-05-14 21:48:17 +0300 | [diff] [blame] | 513 | return sre_ucs1_match(state, pattern, match_all); |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 514 | if (state->charsize == 2) |
Serhiy Storchaka | 429b59e | 2014-05-14 21:48:17 +0300 | [diff] [blame] | 515 | return sre_ucs2_match(state, pattern, match_all); |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 516 | assert(state->charsize == 4); |
Serhiy Storchaka | 429b59e | 2014-05-14 21:48:17 +0300 | [diff] [blame] | 517 | return sre_ucs4_match(state, pattern, match_all); |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | LOCAL(Py_ssize_t) |
| 521 | sre_search(SRE_STATE* state, SRE_CODE* pattern) |
| 522 | { |
| 523 | if (state->charsize == 1) |
| 524 | return sre_ucs1_search(state, pattern); |
| 525 | if (state->charsize == 2) |
| 526 | return sre_ucs2_search(state, pattern); |
| 527 | assert(state->charsize == 4); |
| 528 | return sre_ucs4_search(state, pattern); |
| 529 | } |
| 530 | |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 531 | static PyObject * |
Serhiy Storchaka | ccdf352 | 2014-03-06 11:28:32 +0200 | [diff] [blame] | 532 | fix_string_param(PyObject *string, PyObject *string2, const char *oldname) |
| 533 | { |
| 534 | if (string2 != NULL) { |
| 535 | if (string != NULL) { |
| 536 | PyErr_Format(PyExc_TypeError, |
| 537 | "Argument given by name ('%s') and position (1)", |
| 538 | oldname); |
| 539 | return NULL; |
| 540 | } |
| 541 | if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1, |
| 542 | "The '%s' keyword parameter name is deprecated. " |
| 543 | "Use 'string' instead.", oldname) < 0) |
| 544 | return NULL; |
| 545 | return string2; |
| 546 | } |
| 547 | if (string == NULL) { |
| 548 | PyErr_SetString(PyExc_TypeError, |
| 549 | "Required argument 'string' (pos 1) not found"); |
| 550 | return NULL; |
| 551 | } |
| 552 | return string; |
| 553 | } |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 554 | |
| 555 | static PyObject * |
Larry Hastings | 5c66189 | 2014-01-24 06:17:25 -0800 | [diff] [blame] | 556 | pattern_match(PatternObject *self, PyObject *args, PyObject *kwargs) |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 557 | { |
Serhiy Storchaka | a537eb4 | 2014-03-06 11:36:15 +0200 | [diff] [blame] | 558 | static char *_keywords[] = {"string", "pos", "endpos", "pattern", NULL}; |
| 559 | PyObject *string = NULL; |
Larry Hastings | 16c5191 | 2014-01-07 11:53:01 -0800 | [diff] [blame] | 560 | Py_ssize_t pos = 0; |
| 561 | Py_ssize_t endpos = PY_SSIZE_T_MAX; |
Serhiy Storchaka | a537eb4 | 2014-03-06 11:36:15 +0200 | [diff] [blame] | 562 | PyObject *pattern = NULL; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 563 | SRE_STATE state; |
Victor Stinner | 7a6d7cf | 2012-10-31 00:37:41 +0100 | [diff] [blame] | 564 | Py_ssize_t status; |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 565 | PyObject *match; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 566 | |
Serhiy Storchaka | a537eb4 | 2014-03-06 11:36:15 +0200 | [diff] [blame] | 567 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, |
| 568 | "|Onn$O:match", _keywords, |
| 569 | &string, &pos, &endpos, &pattern)) |
| 570 | return NULL; |
| 571 | string = fix_string_param(string, pattern, "pattern"); |
| 572 | if (!string) |
| 573 | return NULL; |
| 574 | string = state_init(&state, (PatternObject *)self, string, pos, endpos); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 575 | if (!string) |
| 576 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 577 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 578 | state.ptr = state.start; |
| 579 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 580 | TRACE(("|%p|%p|MATCH\n", PatternObject_GetCode(self), state.ptr)); |
| 581 | |
Serhiy Storchaka | 429b59e | 2014-05-14 21:48:17 +0300 | [diff] [blame] | 582 | status = sre_match(&state, PatternObject_GetCode(self), 0); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 583 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 584 | TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr)); |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 585 | if (PyErr_Occurred()) { |
| 586 | state_fini(&state); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 587 | return NULL; |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 588 | } |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 589 | |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 590 | match = pattern_new_match(self, &state, status); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 591 | state_fini(&state); |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 592 | return match; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | static PyObject* |
Serhiy Storchaka | 32eddc1 | 2013-11-23 23:20:30 +0200 | [diff] [blame] | 596 | pattern_fullmatch(PatternObject* self, PyObject* args, PyObject* kw) |
| 597 | { |
| 598 | SRE_STATE state; |
| 599 | Py_ssize_t status; |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 600 | PyObject *match; |
Serhiy Storchaka | 32eddc1 | 2013-11-23 23:20:30 +0200 | [diff] [blame] | 601 | |
Serhiy Storchaka | ccdf352 | 2014-03-06 11:28:32 +0200 | [diff] [blame] | 602 | PyObject *string = NULL, *string2 = NULL; |
Serhiy Storchaka | 32eddc1 | 2013-11-23 23:20:30 +0200 | [diff] [blame] | 603 | Py_ssize_t start = 0; |
| 604 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Serhiy Storchaka | ccdf352 | 2014-03-06 11:28:32 +0200 | [diff] [blame] | 605 | static char* kwlist[] = { "string", "pos", "endpos", "pattern", NULL }; |
Serhiy Storchaka | a537eb4 | 2014-03-06 11:36:15 +0200 | [diff] [blame] | 606 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|Onn$O:fullmatch", kwlist, |
Serhiy Storchaka | ccdf352 | 2014-03-06 11:28:32 +0200 | [diff] [blame] | 607 | &string, &start, &end, &string2)) |
| 608 | return NULL; |
| 609 | |
| 610 | string = fix_string_param(string, string2, "pattern"); |
| 611 | if (!string) |
Serhiy Storchaka | 32eddc1 | 2013-11-23 23:20:30 +0200 | [diff] [blame] | 612 | return NULL; |
| 613 | |
| 614 | string = state_init(&state, self, string, start, end); |
| 615 | if (!string) |
| 616 | return NULL; |
| 617 | |
Serhiy Storchaka | 32eddc1 | 2013-11-23 23:20:30 +0200 | [diff] [blame] | 618 | state.ptr = state.start; |
| 619 | |
| 620 | TRACE(("|%p|%p|FULLMATCH\n", PatternObject_GetCode(self), state.ptr)); |
| 621 | |
Serhiy Storchaka | 429b59e | 2014-05-14 21:48:17 +0300 | [diff] [blame] | 622 | status = sre_match(&state, PatternObject_GetCode(self), 1); |
Serhiy Storchaka | 32eddc1 | 2013-11-23 23:20:30 +0200 | [diff] [blame] | 623 | |
| 624 | TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr)); |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 625 | if (PyErr_Occurred()) { |
| 626 | state_fini(&state); |
Serhiy Storchaka | 32eddc1 | 2013-11-23 23:20:30 +0200 | [diff] [blame] | 627 | return NULL; |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 628 | } |
Serhiy Storchaka | 32eddc1 | 2013-11-23 23:20:30 +0200 | [diff] [blame] | 629 | |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 630 | match = pattern_new_match(self, &state, status); |
Serhiy Storchaka | 32eddc1 | 2013-11-23 23:20:30 +0200 | [diff] [blame] | 631 | state_fini(&state); |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 632 | return match; |
Serhiy Storchaka | 32eddc1 | 2013-11-23 23:20:30 +0200 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | static PyObject* |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 636 | pattern_search(PatternObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 637 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 638 | SRE_STATE state; |
Victor Stinner | f558778 | 2013-11-15 23:21:11 +0100 | [diff] [blame] | 639 | Py_ssize_t status; |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 640 | PyObject *match; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 641 | |
Serhiy Storchaka | ccdf352 | 2014-03-06 11:28:32 +0200 | [diff] [blame] | 642 | PyObject *string = NULL, *string2 = NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 643 | Py_ssize_t start = 0; |
| 644 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Serhiy Storchaka | ccdf352 | 2014-03-06 11:28:32 +0200 | [diff] [blame] | 645 | static char* kwlist[] = { "string", "pos", "endpos", "pattern", NULL }; |
| 646 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|Onn$O:search", kwlist, |
| 647 | &string, &start, &end, &string2)) |
| 648 | return NULL; |
| 649 | |
| 650 | string = fix_string_param(string, string2, "pattern"); |
| 651 | if (!string) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 652 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 653 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 654 | string = state_init(&state, self, string, start, end); |
| 655 | if (!string) |
| 656 | return NULL; |
| 657 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 658 | TRACE(("|%p|%p|SEARCH\n", PatternObject_GetCode(self), state.ptr)); |
| 659 | |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 660 | status = sre_search(&state, PatternObject_GetCode(self)); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 661 | |
Fredrik Lundh | 7898c3e | 2000-08-07 20:59:04 +0000 | [diff] [blame] | 662 | TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr)); |
| 663 | |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 664 | if (PyErr_Occurred()) { |
| 665 | state_fini(&state); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 666 | return NULL; |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 667 | } |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 668 | |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 669 | match = pattern_new_match(self, &state, status); |
| 670 | state_fini(&state); |
| 671 | return match; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | static PyObject* |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 675 | call(char* module, char* function, PyObject* args) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 676 | { |
| 677 | PyObject* name; |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 678 | PyObject* mod; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 679 | PyObject* func; |
| 680 | PyObject* result; |
| 681 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 682 | if (!args) |
| 683 | return NULL; |
Neal Norwitz | fe53713 | 2007-08-26 03:55:15 +0000 | [diff] [blame] | 684 | name = PyUnicode_FromString(module); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 685 | if (!name) |
| 686 | return NULL; |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 687 | mod = PyImport_Import(name); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 688 | Py_DECREF(name); |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 689 | if (!mod) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 690 | return NULL; |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 691 | func = PyObject_GetAttrString(mod, function); |
| 692 | Py_DECREF(mod); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 693 | if (!func) |
| 694 | return NULL; |
| 695 | result = PyObject_CallObject(func, args); |
| 696 | Py_DECREF(func); |
| 697 | Py_DECREF(args); |
| 698 | return result; |
| 699 | } |
| 700 | |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 701 | #ifdef USE_BUILTIN_COPY |
| 702 | static int |
| 703 | deepcopy(PyObject** object, PyObject* memo) |
| 704 | { |
| 705 | PyObject* copy; |
| 706 | |
| 707 | copy = call( |
| 708 | "copy", "deepcopy", |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 709 | PyTuple_Pack(2, *object, memo) |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 710 | ); |
| 711 | if (!copy) |
| 712 | return 0; |
| 713 | |
| 714 | Py_DECREF(*object); |
| 715 | *object = copy; |
| 716 | |
| 717 | return 1; /* success */ |
| 718 | } |
| 719 | #endif |
| 720 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 721 | static PyObject* |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 722 | pattern_findall(PatternObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 723 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 724 | SRE_STATE state; |
| 725 | PyObject* list; |
Victor Stinner | 7a6d7cf | 2012-10-31 00:37:41 +0100 | [diff] [blame] | 726 | Py_ssize_t status; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 727 | Py_ssize_t i, b, e; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 728 | |
Serhiy Storchaka | ccdf352 | 2014-03-06 11:28:32 +0200 | [diff] [blame] | 729 | PyObject *string = NULL, *string2 = NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 730 | Py_ssize_t start = 0; |
| 731 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Serhiy Storchaka | ccdf352 | 2014-03-06 11:28:32 +0200 | [diff] [blame] | 732 | static char* kwlist[] = { "string", "pos", "endpos", "source", NULL }; |
| 733 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|Onn$O:findall", kwlist, |
| 734 | &string, &start, &end, &string2)) |
| 735 | return NULL; |
| 736 | |
| 737 | string = fix_string_param(string, string2, "source"); |
| 738 | if (!string) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 739 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 740 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 741 | string = state_init(&state, self, string, start, end); |
| 742 | if (!string) |
| 743 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 744 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 745 | list = PyList_New(0); |
Fredrik Lundh | 1296a8d | 2001-10-21 18:04:11 +0000 | [diff] [blame] | 746 | if (!list) { |
| 747 | state_fini(&state); |
| 748 | return NULL; |
| 749 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 750 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 751 | while (state.start <= state.end) { |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 752 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 753 | PyObject* item; |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 754 | |
Fredrik Lundh | ebc37b2 | 2000-10-28 19:30:41 +0000 | [diff] [blame] | 755 | state_reset(&state); |
| 756 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 757 | state.ptr = state.start; |
| 758 | |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 759 | status = sre_search(&state, PatternObject_GetCode(self)); |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 760 | if (PyErr_Occurred()) |
| 761 | goto error; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 762 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 763 | if (status <= 0) { |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 764 | if (status == 0) |
| 765 | break; |
Fredrik Lundh | 96ab465 | 2000-08-03 16:29:50 +0000 | [diff] [blame] | 766 | pattern_error(status); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 767 | goto error; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 768 | } |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 769 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 770 | /* don't bother to build a match object */ |
| 771 | switch (self->groups) { |
| 772 | case 0: |
| 773 | b = STATE_OFFSET(&state, state.start); |
| 774 | e = STATE_OFFSET(&state, state.ptr); |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 775 | item = getslice(state.isbytes, state.beginning, |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 776 | string, b, e); |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 777 | if (!item) |
| 778 | goto error; |
| 779 | break; |
| 780 | case 1: |
| 781 | item = state_getslice(&state, 1, string, 1); |
| 782 | if (!item) |
| 783 | goto error; |
| 784 | break; |
| 785 | default: |
| 786 | item = PyTuple_New(self->groups); |
| 787 | if (!item) |
| 788 | goto error; |
| 789 | for (i = 0; i < self->groups; i++) { |
| 790 | PyObject* o = state_getslice(&state, i+1, string, 1); |
| 791 | if (!o) { |
| 792 | Py_DECREF(item); |
| 793 | goto error; |
| 794 | } |
| 795 | PyTuple_SET_ITEM(item, i, o); |
| 796 | } |
| 797 | break; |
| 798 | } |
| 799 | |
| 800 | status = PyList_Append(list, item); |
| 801 | Py_DECREF(item); |
| 802 | if (status < 0) |
| 803 | goto error; |
| 804 | |
| 805 | if (state.ptr == state.start) |
| 806 | state.start = (void*) ((char*) state.ptr + state.charsize); |
| 807 | else |
| 808 | state.start = state.ptr; |
| 809 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 810 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 811 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 812 | state_fini(&state); |
| 813 | return list; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 814 | |
| 815 | error: |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 816 | Py_DECREF(list); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 817 | state_fini(&state); |
| 818 | return NULL; |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 819 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Fredrik Lundh | 703ce81 | 2001-10-24 22:16:30 +0000 | [diff] [blame] | 822 | static PyObject* |
Sean Reifschneider | 7b3c975 | 2012-03-12 18:22:38 -0600 | [diff] [blame] | 823 | pattern_finditer(PatternObject* pattern, PyObject* args, PyObject* kw) |
Fredrik Lundh | 703ce81 | 2001-10-24 22:16:30 +0000 | [diff] [blame] | 824 | { |
| 825 | PyObject* scanner; |
| 826 | PyObject* search; |
| 827 | PyObject* iterator; |
| 828 | |
Sean Reifschneider | 7b3c975 | 2012-03-12 18:22:38 -0600 | [diff] [blame] | 829 | scanner = pattern_scanner(pattern, args, kw); |
Fredrik Lundh | 703ce81 | 2001-10-24 22:16:30 +0000 | [diff] [blame] | 830 | if (!scanner) |
| 831 | return NULL; |
| 832 | |
| 833 | search = PyObject_GetAttrString(scanner, "search"); |
| 834 | Py_DECREF(scanner); |
| 835 | if (!search) |
| 836 | return NULL; |
| 837 | |
| 838 | iterator = PyCallIter_New(search, Py_None); |
| 839 | Py_DECREF(search); |
| 840 | |
| 841 | return iterator; |
| 842 | } |
Fredrik Lundh | 703ce81 | 2001-10-24 22:16:30 +0000 | [diff] [blame] | 843 | |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 844 | static PyObject* |
| 845 | pattern_split(PatternObject* self, PyObject* args, PyObject* kw) |
| 846 | { |
| 847 | SRE_STATE state; |
| 848 | PyObject* list; |
| 849 | PyObject* item; |
Victor Stinner | 7a6d7cf | 2012-10-31 00:37:41 +0100 | [diff] [blame] | 850 | Py_ssize_t status; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 851 | Py_ssize_t n; |
| 852 | Py_ssize_t i; |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 853 | void* last; |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 854 | |
Serhiy Storchaka | ccdf352 | 2014-03-06 11:28:32 +0200 | [diff] [blame] | 855 | PyObject *string = NULL, *string2 = NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 856 | Py_ssize_t maxsplit = 0; |
Serhiy Storchaka | ccdf352 | 2014-03-06 11:28:32 +0200 | [diff] [blame] | 857 | static char* kwlist[] = { "string", "maxsplit", "source", NULL }; |
| 858 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|On$O:split", kwlist, |
| 859 | &string, &maxsplit, &string2)) |
| 860 | return NULL; |
| 861 | |
| 862 | string = fix_string_param(string, string2, "source"); |
| 863 | if (!string) |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 864 | return NULL; |
| 865 | |
Serhiy Storchaka | 83e8027 | 2015-02-03 11:04:19 +0200 | [diff] [blame] | 866 | assert(self->codesize != 0); |
| 867 | if (self->code[0] != SRE_OP_INFO || self->code[3] == 0) { |
| 868 | if (self->code[0] == SRE_OP_INFO && self->code[4] == 0) { |
| 869 | PyErr_SetString(PyExc_ValueError, |
| 870 | "split() requires a non-empty pattern match."); |
| 871 | return NULL; |
| 872 | } |
| 873 | if (PyErr_WarnEx(PyExc_FutureWarning, |
| 874 | "split() requires a non-empty pattern match.", |
| 875 | 1) < 0) |
| 876 | return NULL; |
| 877 | } |
| 878 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 879 | string = state_init(&state, self, string, 0, PY_SSIZE_T_MAX); |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 880 | if (!string) |
| 881 | return NULL; |
| 882 | |
| 883 | list = PyList_New(0); |
Fredrik Lundh | 1296a8d | 2001-10-21 18:04:11 +0000 | [diff] [blame] | 884 | if (!list) { |
| 885 | state_fini(&state); |
| 886 | return NULL; |
| 887 | } |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 888 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 889 | n = 0; |
| 890 | last = state.start; |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 891 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 892 | while (!maxsplit || n < maxsplit) { |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 893 | |
| 894 | state_reset(&state); |
| 895 | |
| 896 | state.ptr = state.start; |
| 897 | |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 898 | status = sre_search(&state, PatternObject_GetCode(self)); |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 899 | if (PyErr_Occurred()) |
| 900 | goto error; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 901 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 902 | if (status <= 0) { |
| 903 | if (status == 0) |
| 904 | break; |
| 905 | pattern_error(status); |
| 906 | goto error; |
| 907 | } |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 908 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 909 | if (state.start == state.ptr) { |
| 910 | if (last == state.end) |
| 911 | break; |
| 912 | /* skip one character */ |
| 913 | state.start = (void*) ((char*) state.ptr + state.charsize); |
| 914 | continue; |
| 915 | } |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 916 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 917 | /* get segment before this match */ |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 918 | item = getslice(state.isbytes, state.beginning, |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 919 | string, STATE_OFFSET(&state, last), |
| 920 | STATE_OFFSET(&state, state.start) |
| 921 | ); |
| 922 | if (!item) |
| 923 | goto error; |
| 924 | status = PyList_Append(list, item); |
| 925 | Py_DECREF(item); |
| 926 | if (status < 0) |
| 927 | goto error; |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 928 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 929 | /* add groups (if any) */ |
| 930 | for (i = 0; i < self->groups; i++) { |
| 931 | item = state_getslice(&state, i+1, string, 0); |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 932 | if (!item) |
| 933 | goto error; |
| 934 | status = PyList_Append(list, item); |
| 935 | Py_DECREF(item); |
| 936 | if (status < 0) |
| 937 | goto error; |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 938 | } |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 939 | |
| 940 | n = n + 1; |
| 941 | |
| 942 | last = state.start = state.ptr; |
| 943 | |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Fredrik Lundh | f864aa8 | 2001-10-22 06:01:56 +0000 | [diff] [blame] | 946 | /* get segment following last match (even if empty) */ |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 947 | item = getslice(state.isbytes, state.beginning, |
Fredrik Lundh | f864aa8 | 2001-10-22 06:01:56 +0000 | [diff] [blame] | 948 | string, STATE_OFFSET(&state, last), state.endpos |
| 949 | ); |
| 950 | if (!item) |
| 951 | goto error; |
| 952 | status = PyList_Append(list, item); |
| 953 | Py_DECREF(item); |
| 954 | if (status < 0) |
| 955 | goto error; |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 956 | |
| 957 | state_fini(&state); |
| 958 | return list; |
| 959 | |
| 960 | error: |
| 961 | Py_DECREF(list); |
| 962 | state_fini(&state); |
| 963 | return NULL; |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 964 | |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 965 | } |
Fredrik Lundh | 971e78b | 2001-10-20 17:48:46 +0000 | [diff] [blame] | 966 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 967 | static PyObject* |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 968 | pattern_subx(PatternObject* self, PyObject* ptemplate, PyObject* string, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 969 | Py_ssize_t count, Py_ssize_t subn) |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 970 | { |
| 971 | SRE_STATE state; |
| 972 | PyObject* list; |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 973 | PyObject* joiner; |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 974 | PyObject* item; |
| 975 | PyObject* filter; |
| 976 | PyObject* args; |
| 977 | PyObject* match; |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 978 | void* ptr; |
Victor Stinner | 7a6d7cf | 2012-10-31 00:37:41 +0100 | [diff] [blame] | 979 | Py_ssize_t status; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 980 | Py_ssize_t n; |
| 981 | Py_ssize_t i, b, e; |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 982 | int isbytes, charsize; |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 983 | int filter_is_callable; |
Benjamin Peterson | e48944b | 2012-03-07 14:50:25 -0600 | [diff] [blame] | 984 | Py_buffer view; |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 985 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 986 | if (PyCallable_Check(ptemplate)) { |
Fredrik Lundh | dac5849 | 2001-10-21 21:48:30 +0000 | [diff] [blame] | 987 | /* sub/subn takes either a function or a template */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 988 | filter = ptemplate; |
Fredrik Lundh | dac5849 | 2001-10-21 21:48:30 +0000 | [diff] [blame] | 989 | Py_INCREF(filter); |
| 990 | filter_is_callable = 1; |
| 991 | } else { |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 992 | /* if not callable, check if it's a literal string */ |
| 993 | int literal; |
Benjamin Peterson | e48944b | 2012-03-07 14:50:25 -0600 | [diff] [blame] | 994 | view.buf = NULL; |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 995 | ptr = getstring(ptemplate, &n, &isbytes, &charsize, &view); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 996 | b = charsize; |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 997 | if (ptr) { |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 998 | if (charsize == 1) |
| 999 | literal = memchr(ptr, '\\', n) == NULL; |
| 1000 | else |
| 1001 | literal = PyUnicode_FindChar(ptemplate, '\\', 0, n, 1) == -1; |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 1002 | } else { |
| 1003 | PyErr_Clear(); |
| 1004 | literal = 0; |
| 1005 | } |
Benjamin Peterson | e48944b | 2012-03-07 14:50:25 -0600 | [diff] [blame] | 1006 | if (view.buf) |
| 1007 | PyBuffer_Release(&view); |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 1008 | if (literal) { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1009 | filter = ptemplate; |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 1010 | Py_INCREF(filter); |
| 1011 | filter_is_callable = 0; |
| 1012 | } else { |
| 1013 | /* not a literal; hand it over to the template compiler */ |
| 1014 | filter = call( |
Thomas Wouters | 9ada3d6 | 2006-04-21 09:47:09 +0000 | [diff] [blame] | 1015 | SRE_PY_MODULE, "_subx", |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1016 | PyTuple_Pack(2, self, ptemplate) |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 1017 | ); |
| 1018 | if (!filter) |
| 1019 | return NULL; |
| 1020 | filter_is_callable = PyCallable_Check(filter); |
| 1021 | } |
Fredrik Lundh | dac5849 | 2001-10-21 21:48:30 +0000 | [diff] [blame] | 1022 | } |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1023 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1024 | string = state_init(&state, self, string, 0, PY_SSIZE_T_MAX); |
Fredrik Lundh | 82b2307 | 2001-12-09 16:13:15 +0000 | [diff] [blame] | 1025 | if (!string) { |
| 1026 | Py_DECREF(filter); |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1027 | return NULL; |
Fredrik Lundh | 82b2307 | 2001-12-09 16:13:15 +0000 | [diff] [blame] | 1028 | } |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1029 | |
| 1030 | list = PyList_New(0); |
Fredrik Lundh | 1296a8d | 2001-10-21 18:04:11 +0000 | [diff] [blame] | 1031 | if (!list) { |
Fredrik Lundh | 82b2307 | 2001-12-09 16:13:15 +0000 | [diff] [blame] | 1032 | Py_DECREF(filter); |
Fredrik Lundh | 1296a8d | 2001-10-21 18:04:11 +0000 | [diff] [blame] | 1033 | state_fini(&state); |
| 1034 | return NULL; |
| 1035 | } |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1036 | |
| 1037 | n = i = 0; |
| 1038 | |
| 1039 | while (!count || n < count) { |
| 1040 | |
| 1041 | state_reset(&state); |
| 1042 | |
| 1043 | state.ptr = state.start; |
| 1044 | |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 1045 | status = sre_search(&state, PatternObject_GetCode(self)); |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 1046 | if (PyErr_Occurred()) |
| 1047 | goto error; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1048 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1049 | if (status <= 0) { |
| 1050 | if (status == 0) |
| 1051 | break; |
| 1052 | pattern_error(status); |
| 1053 | goto error; |
| 1054 | } |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 1055 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1056 | b = STATE_OFFSET(&state, state.start); |
| 1057 | e = STATE_OFFSET(&state, state.ptr); |
| 1058 | |
| 1059 | if (i < b) { |
| 1060 | /* get segment before this match */ |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 1061 | item = getslice(state.isbytes, state.beginning, |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 1062 | string, i, b); |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1063 | if (!item) |
| 1064 | goto error; |
| 1065 | status = PyList_Append(list, item); |
| 1066 | Py_DECREF(item); |
| 1067 | if (status < 0) |
| 1068 | goto error; |
| 1069 | |
| 1070 | } else if (i == b && i == e && n > 0) |
| 1071 | /* ignore empty match on latest position */ |
| 1072 | goto next; |
| 1073 | |
| 1074 | if (filter_is_callable) { |
Fredrik Lundh | dac5849 | 2001-10-21 21:48:30 +0000 | [diff] [blame] | 1075 | /* pass match object through filter */ |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1076 | match = pattern_new_match(self, &state, 1); |
| 1077 | if (!match) |
| 1078 | goto error; |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1079 | args = PyTuple_Pack(1, match); |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1080 | if (!args) { |
Guido van Rossum | 4e17384 | 2001-12-07 04:25:10 +0000 | [diff] [blame] | 1081 | Py_DECREF(match); |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1082 | goto error; |
| 1083 | } |
| 1084 | item = PyObject_CallObject(filter, args); |
| 1085 | Py_DECREF(args); |
| 1086 | Py_DECREF(match); |
| 1087 | if (!item) |
| 1088 | goto error; |
| 1089 | } else { |
| 1090 | /* filter is literal string */ |
| 1091 | item = filter; |
Fredrik Lundh | dac5849 | 2001-10-21 21:48:30 +0000 | [diff] [blame] | 1092 | Py_INCREF(item); |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | /* add to list */ |
Fredrik Lundh | 6de22ef | 2001-10-22 21:18:08 +0000 | [diff] [blame] | 1096 | if (item != Py_None) { |
| 1097 | status = PyList_Append(list, item); |
| 1098 | Py_DECREF(item); |
| 1099 | if (status < 0) |
| 1100 | goto error; |
| 1101 | } |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 1102 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1103 | i = e; |
| 1104 | n = n + 1; |
| 1105 | |
| 1106 | next: |
| 1107 | /* move on */ |
| 1108 | if (state.ptr == state.start) |
| 1109 | state.start = (void*) ((char*) state.ptr + state.charsize); |
| 1110 | else |
| 1111 | state.start = state.ptr; |
| 1112 | |
| 1113 | } |
| 1114 | |
| 1115 | /* get segment following last match */ |
Fredrik Lundh | dac5849 | 2001-10-21 21:48:30 +0000 | [diff] [blame] | 1116 | if (i < state.endpos) { |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 1117 | item = getslice(state.isbytes, state.beginning, |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 1118 | string, i, state.endpos); |
Fredrik Lundh | dac5849 | 2001-10-21 21:48:30 +0000 | [diff] [blame] | 1119 | if (!item) |
| 1120 | goto error; |
| 1121 | status = PyList_Append(list, item); |
| 1122 | Py_DECREF(item); |
| 1123 | if (status < 0) |
| 1124 | goto error; |
| 1125 | } |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1126 | |
| 1127 | state_fini(&state); |
| 1128 | |
Guido van Rossum | 4e17384 | 2001-12-07 04:25:10 +0000 | [diff] [blame] | 1129 | Py_DECREF(filter); |
| 1130 | |
Fredrik Lundh | dac5849 | 2001-10-21 21:48:30 +0000 | [diff] [blame] | 1131 | /* convert list to single string (also removes list) */ |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 1132 | joiner = getslice(state.isbytes, state.beginning, string, 0, 0); |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 1133 | if (!joiner) { |
| 1134 | Py_DECREF(list); |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1135 | return NULL; |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 1136 | } |
| 1137 | if (PyList_GET_SIZE(list) == 0) { |
| 1138 | Py_DECREF(list); |
| 1139 | item = joiner; |
| 1140 | } |
| 1141 | else { |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 1142 | if (state.isbytes) |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 1143 | item = _PyBytes_Join(joiner, list); |
| 1144 | else |
| 1145 | item = PyUnicode_Join(joiner, list); |
| 1146 | Py_DECREF(joiner); |
Brett Cannon | baced56 | 2013-10-18 14:03:16 -0400 | [diff] [blame] | 1147 | Py_DECREF(list); |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 1148 | if (!item) |
| 1149 | return NULL; |
| 1150 | } |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1151 | |
| 1152 | if (subn) |
Antoine Pitrou | 43fb54c | 2012-12-02 12:52:36 +0100 | [diff] [blame] | 1153 | return Py_BuildValue("Nn", item, n); |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1154 | |
| 1155 | return item; |
| 1156 | |
| 1157 | error: |
| 1158 | Py_DECREF(list); |
| 1159 | state_fini(&state); |
Fredrik Lundh | 82b2307 | 2001-12-09 16:13:15 +0000 | [diff] [blame] | 1160 | Py_DECREF(filter); |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1161 | return NULL; |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 1162 | |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | static PyObject* |
| 1166 | pattern_sub(PatternObject* self, PyObject* args, PyObject* kw) |
| 1167 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1168 | PyObject* ptemplate; |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1169 | PyObject* string; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1170 | Py_ssize_t count = 0; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 1171 | static char* kwlist[] = { "repl", "string", "count", NULL }; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1172 | if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|n:sub", kwlist, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1173 | &ptemplate, &string, &count)) |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1174 | return NULL; |
| 1175 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1176 | return pattern_subx(self, ptemplate, string, count, 0); |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | static PyObject* |
| 1180 | pattern_subn(PatternObject* self, PyObject* args, PyObject* kw) |
| 1181 | { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1182 | PyObject* ptemplate; |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1183 | PyObject* string; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1184 | Py_ssize_t count = 0; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 1185 | static char* kwlist[] = { "repl", "string", "count", NULL }; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1186 | if (!PyArg_ParseTupleAndKeywords(args, kw, "OO|n:subn", kwlist, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1187 | &ptemplate, &string, &count)) |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1188 | return NULL; |
| 1189 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1190 | return pattern_subx(self, ptemplate, string, count, 1); |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1191 | } |
Fredrik Lundh | bec95b9 | 2001-10-21 16:47:57 +0000 | [diff] [blame] | 1192 | |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 1193 | static PyObject* |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1194 | pattern_copy(PatternObject* self, PyObject *unused) |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 1195 | { |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 1196 | #ifdef USE_BUILTIN_COPY |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 1197 | PatternObject* copy; |
| 1198 | int offset; |
| 1199 | |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 1200 | copy = PyObject_NEW_VAR(PatternObject, &Pattern_Type, self->codesize); |
| 1201 | if (!copy) |
| 1202 | return NULL; |
| 1203 | |
| 1204 | offset = offsetof(PatternObject, groups); |
| 1205 | |
| 1206 | Py_XINCREF(self->groupindex); |
| 1207 | Py_XINCREF(self->indexgroup); |
| 1208 | Py_XINCREF(self->pattern); |
| 1209 | |
| 1210 | memcpy((char*) copy + offset, (char*) self + offset, |
| 1211 | sizeof(PatternObject) + self->codesize * sizeof(SRE_CODE) - offset); |
Raymond Hettinger | 027bb63 | 2004-05-31 03:09:25 +0000 | [diff] [blame] | 1212 | copy->weakreflist = NULL; |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 1213 | |
| 1214 | return (PyObject*) copy; |
| 1215 | #else |
| 1216 | PyErr_SetString(PyExc_TypeError, "cannot copy this pattern object"); |
| 1217 | return NULL; |
| 1218 | #endif |
| 1219 | } |
| 1220 | |
| 1221 | static PyObject* |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1222 | pattern_deepcopy(PatternObject* self, PyObject* memo) |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 1223 | { |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 1224 | #ifdef USE_BUILTIN_COPY |
| 1225 | PatternObject* copy; |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 1226 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1227 | copy = (PatternObject*) pattern_copy(self); |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 1228 | if (!copy) |
| 1229 | return NULL; |
| 1230 | |
| 1231 | if (!deepcopy(©->groupindex, memo) || |
| 1232 | !deepcopy(©->indexgroup, memo) || |
| 1233 | !deepcopy(©->pattern, memo)) { |
| 1234 | Py_DECREF(copy); |
| 1235 | return NULL; |
| 1236 | } |
| 1237 | |
| 1238 | #else |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 1239 | PyErr_SetString(PyExc_TypeError, "cannot deepcopy this pattern object"); |
| 1240 | return NULL; |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 1241 | #endif |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Serhiy Storchaka | 5c24d0e | 2013-11-23 22:42:43 +0200 | [diff] [blame] | 1244 | static PyObject * |
| 1245 | pattern_repr(PatternObject *obj) |
| 1246 | { |
| 1247 | static const struct { |
| 1248 | const char *name; |
| 1249 | int value; |
| 1250 | } flag_names[] = { |
| 1251 | {"re.TEMPLATE", SRE_FLAG_TEMPLATE}, |
| 1252 | {"re.IGNORECASE", SRE_FLAG_IGNORECASE}, |
| 1253 | {"re.LOCALE", SRE_FLAG_LOCALE}, |
| 1254 | {"re.MULTILINE", SRE_FLAG_MULTILINE}, |
| 1255 | {"re.DOTALL", SRE_FLAG_DOTALL}, |
| 1256 | {"re.UNICODE", SRE_FLAG_UNICODE}, |
| 1257 | {"re.VERBOSE", SRE_FLAG_VERBOSE}, |
| 1258 | {"re.DEBUG", SRE_FLAG_DEBUG}, |
| 1259 | {"re.ASCII", SRE_FLAG_ASCII}, |
| 1260 | }; |
| 1261 | PyObject *result = NULL; |
| 1262 | PyObject *flag_items; |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1263 | size_t i; |
Serhiy Storchaka | 5c24d0e | 2013-11-23 22:42:43 +0200 | [diff] [blame] | 1264 | int flags = obj->flags; |
| 1265 | |
| 1266 | /* Omit re.UNICODE for valid string patterns. */ |
| 1267 | if (obj->isbytes == 0 && |
| 1268 | (flags & (SRE_FLAG_LOCALE|SRE_FLAG_UNICODE|SRE_FLAG_ASCII)) == |
| 1269 | SRE_FLAG_UNICODE) |
| 1270 | flags &= ~SRE_FLAG_UNICODE; |
| 1271 | |
| 1272 | flag_items = PyList_New(0); |
| 1273 | if (!flag_items) |
| 1274 | return NULL; |
| 1275 | |
| 1276 | for (i = 0; i < Py_ARRAY_LENGTH(flag_names); i++) { |
| 1277 | if (flags & flag_names[i].value) { |
| 1278 | PyObject *item = PyUnicode_FromString(flag_names[i].name); |
| 1279 | if (!item) |
| 1280 | goto done; |
| 1281 | |
| 1282 | if (PyList_Append(flag_items, item) < 0) { |
| 1283 | Py_DECREF(item); |
| 1284 | goto done; |
| 1285 | } |
| 1286 | Py_DECREF(item); |
| 1287 | flags &= ~flag_names[i].value; |
| 1288 | } |
| 1289 | } |
| 1290 | if (flags) { |
| 1291 | PyObject *item = PyUnicode_FromFormat("0x%x", flags); |
| 1292 | if (!item) |
| 1293 | goto done; |
| 1294 | |
| 1295 | if (PyList_Append(flag_items, item) < 0) { |
| 1296 | Py_DECREF(item); |
| 1297 | goto done; |
| 1298 | } |
| 1299 | Py_DECREF(item); |
| 1300 | } |
| 1301 | |
| 1302 | if (PyList_Size(flag_items) > 0) { |
| 1303 | PyObject *flags_result; |
| 1304 | PyObject *sep = PyUnicode_FromString("|"); |
| 1305 | if (!sep) |
| 1306 | goto done; |
| 1307 | flags_result = PyUnicode_Join(sep, flag_items); |
| 1308 | Py_DECREF(sep); |
| 1309 | if (!flags_result) |
| 1310 | goto done; |
| 1311 | result = PyUnicode_FromFormat("re.compile(%.200R, %S)", |
| 1312 | obj->pattern, flags_result); |
| 1313 | Py_DECREF(flags_result); |
| 1314 | } |
| 1315 | else { |
| 1316 | result = PyUnicode_FromFormat("re.compile(%.200R)", obj->pattern); |
| 1317 | } |
| 1318 | |
| 1319 | done: |
| 1320 | Py_DECREF(flag_items); |
| 1321 | return result; |
| 1322 | } |
| 1323 | |
Raymond Hettinger | 9447874 | 2004-09-24 04:31:19 +0000 | [diff] [blame] | 1324 | PyDoc_STRVAR(pattern_match_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 1325 | "match(string[, pos[, endpos]]) -> match object or None.\n\ |
Raymond Hettinger | 9447874 | 2004-09-24 04:31:19 +0000 | [diff] [blame] | 1326 | Matches zero or more characters at the beginning of the string"); |
| 1327 | |
Serhiy Storchaka | 32eddc1 | 2013-11-23 23:20:30 +0200 | [diff] [blame] | 1328 | PyDoc_STRVAR(pattern_fullmatch_doc, |
| 1329 | "fullmatch(string[, pos[, endpos]]) -> match object or None.\n\ |
| 1330 | Matches against all of the string"); |
| 1331 | |
Raymond Hettinger | 9447874 | 2004-09-24 04:31:19 +0000 | [diff] [blame] | 1332 | PyDoc_STRVAR(pattern_search_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 1333 | "search(string[, pos[, endpos]]) -> match object or None.\n\ |
Raymond Hettinger | 9447874 | 2004-09-24 04:31:19 +0000 | [diff] [blame] | 1334 | Scan through string looking for a match, and return a corresponding\n\ |
Andrew Svetlov | 0b64c14 | 2012-12-25 18:48:54 +0200 | [diff] [blame] | 1335 | match object instance. Return None if no position in the string matches."); |
Raymond Hettinger | 9447874 | 2004-09-24 04:31:19 +0000 | [diff] [blame] | 1336 | |
| 1337 | PyDoc_STRVAR(pattern_split_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 1338 | "split(string[, maxsplit = 0]) -> list.\n\ |
Raymond Hettinger | 9447874 | 2004-09-24 04:31:19 +0000 | [diff] [blame] | 1339 | Split string by the occurrences of pattern."); |
| 1340 | |
| 1341 | PyDoc_STRVAR(pattern_findall_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 1342 | "findall(string[, pos[, endpos]]) -> list.\n\ |
Raymond Hettinger | 9447874 | 2004-09-24 04:31:19 +0000 | [diff] [blame] | 1343 | Return a list of all non-overlapping matches of pattern in string."); |
| 1344 | |
| 1345 | PyDoc_STRVAR(pattern_finditer_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 1346 | "finditer(string[, pos[, endpos]]) -> iterator.\n\ |
Raymond Hettinger | 9447874 | 2004-09-24 04:31:19 +0000 | [diff] [blame] | 1347 | Return an iterator over all non-overlapping matches for the \n\ |
| 1348 | RE pattern in string. For each match, the iterator returns a\n\ |
| 1349 | match object."); |
| 1350 | |
| 1351 | PyDoc_STRVAR(pattern_sub_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 1352 | "sub(repl, string[, count = 0]) -> newstring.\n\ |
Raymond Hettinger | 9447874 | 2004-09-24 04:31:19 +0000 | [diff] [blame] | 1353 | Return the string obtained by replacing the leftmost non-overlapping\n\ |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 1354 | occurrences of pattern in string by the replacement repl."); |
Raymond Hettinger | 9447874 | 2004-09-24 04:31:19 +0000 | [diff] [blame] | 1355 | |
| 1356 | PyDoc_STRVAR(pattern_subn_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 1357 | "subn(repl, string[, count = 0]) -> (newstring, number of subs)\n\ |
Raymond Hettinger | 9447874 | 2004-09-24 04:31:19 +0000 | [diff] [blame] | 1358 | Return the tuple (new_string, number_of_subs_made) found by replacing\n\ |
| 1359 | the leftmost non-overlapping occurrences of pattern with the\n\ |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 1360 | replacement repl."); |
Raymond Hettinger | 9447874 | 2004-09-24 04:31:19 +0000 | [diff] [blame] | 1361 | |
| 1362 | PyDoc_STRVAR(pattern_doc, "Compiled regular expression objects"); |
| 1363 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1364 | static PyMethodDef pattern_methods[] = { |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 1365 | {"match", (PyCFunction) pattern_match, METH_VARARGS|METH_KEYWORDS, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 1366 | pattern_match_doc}, |
Serhiy Storchaka | 32eddc1 | 2013-11-23 23:20:30 +0200 | [diff] [blame] | 1367 | {"fullmatch", (PyCFunction) pattern_fullmatch, METH_VARARGS|METH_KEYWORDS, |
| 1368 | pattern_fullmatch_doc}, |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 1369 | {"search", (PyCFunction) pattern_search, METH_VARARGS|METH_KEYWORDS, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 1370 | pattern_search_doc}, |
Raymond Hettinger | 9447874 | 2004-09-24 04:31:19 +0000 | [diff] [blame] | 1371 | {"sub", (PyCFunction) pattern_sub, METH_VARARGS|METH_KEYWORDS, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 1372 | pattern_sub_doc}, |
Raymond Hettinger | 9447874 | 2004-09-24 04:31:19 +0000 | [diff] [blame] | 1373 | {"subn", (PyCFunction) pattern_subn, METH_VARARGS|METH_KEYWORDS, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 1374 | pattern_subn_doc}, |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 1375 | {"split", (PyCFunction) pattern_split, METH_VARARGS|METH_KEYWORDS, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 1376 | pattern_split_doc}, |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 1377 | {"findall", (PyCFunction) pattern_findall, METH_VARARGS|METH_KEYWORDS, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 1378 | pattern_findall_doc}, |
Sean Reifschneider | 7b3c975 | 2012-03-12 18:22:38 -0600 | [diff] [blame] | 1379 | {"finditer", (PyCFunction) pattern_finditer, METH_VARARGS|METH_KEYWORDS, |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 1380 | pattern_finditer_doc}, |
Sean Reifschneider | 7b3c975 | 2012-03-12 18:22:38 -0600 | [diff] [blame] | 1381 | {"scanner", (PyCFunction) pattern_scanner, METH_VARARGS|METH_KEYWORDS}, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1382 | {"__copy__", (PyCFunction) pattern_copy, METH_NOARGS}, |
| 1383 | {"__deepcopy__", (PyCFunction) pattern_deepcopy, METH_O}, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1384 | {NULL, NULL} |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1385 | }; |
| 1386 | |
Serhiy Storchaka | 07360df | 2015-03-30 01:01:48 +0300 | [diff] [blame] | 1387 | /* PatternObject's 'groupindex' method. */ |
| 1388 | static PyObject * |
| 1389 | pattern_groupindex(PatternObject *self) |
| 1390 | { |
| 1391 | return PyDictProxy_New(self->groupindex); |
| 1392 | } |
| 1393 | |
| 1394 | static PyGetSetDef pattern_getset[] = { |
| 1395 | {"groupindex", (getter)pattern_groupindex, (setter)NULL, |
| 1396 | "A dictionary mapping group names to group numbers."}, |
| 1397 | {NULL} /* Sentinel */ |
| 1398 | }; |
| 1399 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 1400 | #define PAT_OFF(x) offsetof(PatternObject, x) |
| 1401 | static PyMemberDef pattern_members[] = { |
| 1402 | {"pattern", T_OBJECT, PAT_OFF(pattern), READONLY}, |
| 1403 | {"flags", T_INT, PAT_OFF(flags), READONLY}, |
| 1404 | {"groups", T_PYSSIZET, PAT_OFF(groups), READONLY}, |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 1405 | {NULL} /* Sentinel */ |
| 1406 | }; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1407 | |
Neal Norwitz | 57c179c | 2006-03-22 07:18:02 +0000 | [diff] [blame] | 1408 | static PyTypeObject Pattern_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1409 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1410 | "_" SRE_MODULE ".SRE_Pattern", |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1411 | sizeof(PatternObject), sizeof(SRE_CODE), |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 1412 | (destructor)pattern_dealloc, /* tp_dealloc */ |
| 1413 | 0, /* tp_print */ |
| 1414 | 0, /* tp_getattr */ |
| 1415 | 0, /* tp_setattr */ |
| 1416 | 0, /* tp_reserved */ |
Serhiy Storchaka | 5c24d0e | 2013-11-23 22:42:43 +0200 | [diff] [blame] | 1417 | (reprfunc)pattern_repr, /* tp_repr */ |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 1418 | 0, /* tp_as_number */ |
| 1419 | 0, /* tp_as_sequence */ |
| 1420 | 0, /* tp_as_mapping */ |
| 1421 | 0, /* tp_hash */ |
| 1422 | 0, /* tp_call */ |
| 1423 | 0, /* tp_str */ |
| 1424 | 0, /* tp_getattro */ |
| 1425 | 0, /* tp_setattro */ |
| 1426 | 0, /* tp_as_buffer */ |
| 1427 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 1428 | pattern_doc, /* tp_doc */ |
| 1429 | 0, /* tp_traverse */ |
| 1430 | 0, /* tp_clear */ |
| 1431 | 0, /* tp_richcompare */ |
| 1432 | offsetof(PatternObject, weakreflist), /* tp_weaklistoffset */ |
| 1433 | 0, /* tp_iter */ |
| 1434 | 0, /* tp_iternext */ |
| 1435 | pattern_methods, /* tp_methods */ |
| 1436 | pattern_members, /* tp_members */ |
Serhiy Storchaka | 07360df | 2015-03-30 01:01:48 +0300 | [diff] [blame] | 1437 | pattern_getset, /* tp_getset */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1438 | }; |
| 1439 | |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1440 | static int _validate(PatternObject *self); /* Forward */ |
| 1441 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1442 | static PyObject * |
| 1443 | _compile(PyObject* self_, PyObject* args) |
| 1444 | { |
| 1445 | /* "compile" pattern descriptor to pattern object */ |
| 1446 | |
| 1447 | PatternObject* self; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1448 | Py_ssize_t i, n; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1449 | |
| 1450 | PyObject* pattern; |
| 1451 | int flags = 0; |
| 1452 | PyObject* code; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1453 | Py_ssize_t groups = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1454 | PyObject* groupindex = NULL; |
| 1455 | PyObject* indexgroup = NULL; |
Benjamin Peterson | e48944b | 2012-03-07 14:50:25 -0600 | [diff] [blame] | 1456 | |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 1457 | if (!PyArg_ParseTuple(args, "OiO!nOO", &pattern, &flags, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1458 | &PyList_Type, &code, &groups, |
| 1459 | &groupindex, &indexgroup)) |
| 1460 | return NULL; |
| 1461 | |
| 1462 | n = PyList_GET_SIZE(code); |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 1463 | /* coverity[ampersand_in_size] */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1464 | self = PyObject_NEW_VAR(PatternObject, &Pattern_Type, n); |
| 1465 | if (!self) |
| 1466 | return NULL; |
Antoine Pitrou | 82feb1f | 2010-01-14 17:34:48 +0000 | [diff] [blame] | 1467 | self->weakreflist = NULL; |
| 1468 | self->pattern = NULL; |
| 1469 | self->groupindex = NULL; |
| 1470 | self->indexgroup = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1471 | |
| 1472 | self->codesize = n; |
| 1473 | |
| 1474 | for (i = 0; i < n; i++) { |
| 1475 | PyObject *o = PyList_GET_ITEM(code, i); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1476 | unsigned long value = PyLong_AsUnsignedLong(o); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1477 | self->code[i] = (SRE_CODE) value; |
| 1478 | if ((unsigned long) self->code[i] != value) { |
| 1479 | PyErr_SetString(PyExc_OverflowError, |
| 1480 | "regular expression code size limit exceeded"); |
| 1481 | break; |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | if (PyErr_Occurred()) { |
Antoine Pitrou | 82feb1f | 2010-01-14 17:34:48 +0000 | [diff] [blame] | 1486 | Py_DECREF(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1487 | return NULL; |
| 1488 | } |
| 1489 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1490 | if (pattern == Py_None) { |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 1491 | self->isbytes = -1; |
Victor Stinner | 63ab875 | 2011-11-22 03:31:20 +0100 | [diff] [blame] | 1492 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1493 | else { |
| 1494 | Py_ssize_t p_length; |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 1495 | int charsize; |
| 1496 | Py_buffer view; |
| 1497 | view.buf = NULL; |
| 1498 | if (!getstring(pattern, &p_length, &self->isbytes, |
| 1499 | &charsize, &view)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1500 | Py_DECREF(self); |
| 1501 | return NULL; |
| 1502 | } |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 1503 | if (view.buf) |
| 1504 | PyBuffer_Release(&view); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1505 | } |
Antoine Pitrou | fd03645 | 2008-08-19 17:56:33 +0000 | [diff] [blame] | 1506 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1507 | Py_INCREF(pattern); |
| 1508 | self->pattern = pattern; |
| 1509 | |
| 1510 | self->flags = flags; |
| 1511 | |
| 1512 | self->groups = groups; |
| 1513 | |
| 1514 | Py_XINCREF(groupindex); |
| 1515 | self->groupindex = groupindex; |
| 1516 | |
| 1517 | Py_XINCREF(indexgroup); |
| 1518 | self->indexgroup = indexgroup; |
| 1519 | |
| 1520 | self->weakreflist = NULL; |
| 1521 | |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1522 | if (!_validate(self)) { |
| 1523 | Py_DECREF(self); |
| 1524 | return NULL; |
| 1525 | } |
| 1526 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1527 | return (PyObject*) self; |
| 1528 | } |
| 1529 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1530 | /* -------------------------------------------------------------------- */ |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1531 | /* Code validation */ |
| 1532 | |
| 1533 | /* To learn more about this code, have a look at the _compile() function in |
| 1534 | Lib/sre_compile.py. The validation functions below checks the code array |
| 1535 | for conformance with the code patterns generated there. |
| 1536 | |
| 1537 | The nice thing about the generated code is that it is position-independent: |
| 1538 | all jumps are relative jumps forward. Also, jumps don't cross each other: |
| 1539 | the target of a later jump is always earlier than the target of an earlier |
| 1540 | jump. IOW, this is okay: |
| 1541 | |
| 1542 | J---------J-------T--------T |
| 1543 | \ \_____/ / |
| 1544 | \______________________/ |
| 1545 | |
| 1546 | but this is not: |
| 1547 | |
| 1548 | J---------J-------T--------T |
| 1549 | \_________\_____/ / |
| 1550 | \____________/ |
| 1551 | |
Serhiy Storchaka | efa5a39 | 2013-10-27 08:04:58 +0200 | [diff] [blame] | 1552 | It also helps that SRE_CODE is always an unsigned type. |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1553 | */ |
| 1554 | |
| 1555 | /* Defining this one enables tracing of the validator */ |
| 1556 | #undef VVERBOSE |
| 1557 | |
| 1558 | /* Trace macro for the validator */ |
| 1559 | #if defined(VVERBOSE) |
| 1560 | #define VTRACE(v) printf v |
| 1561 | #else |
Senthil Kumaran | 202a3c4 | 2011-10-20 02:15:36 +0800 | [diff] [blame] | 1562 | #define VTRACE(v) do {} while(0) /* do nothing */ |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1563 | #endif |
| 1564 | |
| 1565 | /* Report failure */ |
| 1566 | #define FAIL do { VTRACE(("FAIL: %d\n", __LINE__)); return 0; } while (0) |
| 1567 | |
| 1568 | /* Extract opcode, argument, or skip count from code array */ |
| 1569 | #define GET_OP \ |
| 1570 | do { \ |
| 1571 | VTRACE(("%p: ", code)); \ |
| 1572 | if (code >= end) FAIL; \ |
| 1573 | op = *code++; \ |
| 1574 | VTRACE(("%lu (op)\n", (unsigned long)op)); \ |
| 1575 | } while (0) |
| 1576 | #define GET_ARG \ |
| 1577 | do { \ |
| 1578 | VTRACE(("%p= ", code)); \ |
| 1579 | if (code >= end) FAIL; \ |
| 1580 | arg = *code++; \ |
| 1581 | VTRACE(("%lu (arg)\n", (unsigned long)arg)); \ |
| 1582 | } while (0) |
Guido van Rossum | 92f8f3e | 2008-09-10 14:30:50 +0000 | [diff] [blame] | 1583 | #define GET_SKIP_ADJ(adj) \ |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1584 | do { \ |
| 1585 | VTRACE(("%p= ", code)); \ |
| 1586 | if (code >= end) FAIL; \ |
| 1587 | skip = *code; \ |
| 1588 | VTRACE(("%lu (skip to %p)\n", \ |
| 1589 | (unsigned long)skip, code+skip)); \ |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1590 | if (skip-adj > (Py_uintptr_t)(end - code)) \ |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1591 | FAIL; \ |
| 1592 | code++; \ |
| 1593 | } while (0) |
Guido van Rossum | 92f8f3e | 2008-09-10 14:30:50 +0000 | [diff] [blame] | 1594 | #define GET_SKIP GET_SKIP_ADJ(0) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1595 | |
| 1596 | static int |
| 1597 | _validate_charset(SRE_CODE *code, SRE_CODE *end) |
| 1598 | { |
| 1599 | /* Some variables are manipulated by the macros above */ |
| 1600 | SRE_CODE op; |
| 1601 | SRE_CODE arg; |
| 1602 | SRE_CODE offset; |
| 1603 | int i; |
| 1604 | |
| 1605 | while (code < end) { |
| 1606 | GET_OP; |
| 1607 | switch (op) { |
| 1608 | |
| 1609 | case SRE_OP_NEGATE: |
| 1610 | break; |
| 1611 | |
| 1612 | case SRE_OP_LITERAL: |
| 1613 | GET_ARG; |
| 1614 | break; |
| 1615 | |
| 1616 | case SRE_OP_RANGE: |
Serhiy Storchaka | 4b8f894 | 2014-10-31 12:36:56 +0200 | [diff] [blame] | 1617 | case SRE_OP_RANGE_IGNORE: |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1618 | GET_ARG; |
| 1619 | GET_ARG; |
| 1620 | break; |
| 1621 | |
| 1622 | case SRE_OP_CHARSET: |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 1623 | offset = 256/SRE_CODE_BITS; /* 256-bit bitmap */ |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1624 | if (offset > (Py_uintptr_t)(end - code)) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1625 | FAIL; |
| 1626 | code += offset; |
| 1627 | break; |
| 1628 | |
| 1629 | case SRE_OP_BIGCHARSET: |
| 1630 | GET_ARG; /* Number of blocks */ |
| 1631 | offset = 256/sizeof(SRE_CODE); /* 256-byte table */ |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1632 | if (offset > (Py_uintptr_t)(end - code)) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1633 | FAIL; |
| 1634 | /* Make sure that each byte points to a valid block */ |
| 1635 | for (i = 0; i < 256; i++) { |
| 1636 | if (((unsigned char *)code)[i] >= arg) |
| 1637 | FAIL; |
| 1638 | } |
| 1639 | code += offset; |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 1640 | offset = arg * (256/SRE_CODE_BITS); /* 256-bit bitmap times arg */ |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1641 | if (offset > (Py_uintptr_t)(end - code)) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1642 | FAIL; |
| 1643 | code += offset; |
| 1644 | break; |
| 1645 | |
| 1646 | case SRE_OP_CATEGORY: |
| 1647 | GET_ARG; |
| 1648 | switch (arg) { |
| 1649 | case SRE_CATEGORY_DIGIT: |
| 1650 | case SRE_CATEGORY_NOT_DIGIT: |
| 1651 | case SRE_CATEGORY_SPACE: |
| 1652 | case SRE_CATEGORY_NOT_SPACE: |
| 1653 | case SRE_CATEGORY_WORD: |
| 1654 | case SRE_CATEGORY_NOT_WORD: |
| 1655 | case SRE_CATEGORY_LINEBREAK: |
| 1656 | case SRE_CATEGORY_NOT_LINEBREAK: |
| 1657 | case SRE_CATEGORY_LOC_WORD: |
| 1658 | case SRE_CATEGORY_LOC_NOT_WORD: |
| 1659 | case SRE_CATEGORY_UNI_DIGIT: |
| 1660 | case SRE_CATEGORY_UNI_NOT_DIGIT: |
| 1661 | case SRE_CATEGORY_UNI_SPACE: |
| 1662 | case SRE_CATEGORY_UNI_NOT_SPACE: |
| 1663 | case SRE_CATEGORY_UNI_WORD: |
| 1664 | case SRE_CATEGORY_UNI_NOT_WORD: |
| 1665 | case SRE_CATEGORY_UNI_LINEBREAK: |
| 1666 | case SRE_CATEGORY_UNI_NOT_LINEBREAK: |
| 1667 | break; |
| 1668 | default: |
| 1669 | FAIL; |
| 1670 | } |
| 1671 | break; |
| 1672 | |
| 1673 | default: |
| 1674 | FAIL; |
| 1675 | |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | return 1; |
| 1680 | } |
| 1681 | |
| 1682 | static int |
| 1683 | _validate_inner(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups) |
| 1684 | { |
| 1685 | /* Some variables are manipulated by the macros above */ |
| 1686 | SRE_CODE op; |
| 1687 | SRE_CODE arg; |
| 1688 | SRE_CODE skip; |
| 1689 | |
| 1690 | VTRACE(("code=%p, end=%p\n", code, end)); |
| 1691 | |
| 1692 | if (code > end) |
| 1693 | FAIL; |
| 1694 | |
| 1695 | while (code < end) { |
| 1696 | GET_OP; |
| 1697 | switch (op) { |
| 1698 | |
| 1699 | case SRE_OP_MARK: |
| 1700 | /* We don't check whether marks are properly nested; the |
| 1701 | sre_match() code is robust even if they don't, and the worst |
| 1702 | you can get is nonsensical match results. */ |
| 1703 | GET_ARG; |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1704 | if (arg > 2 * (size_t)groups + 1) { |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1705 | VTRACE(("arg=%d, groups=%d\n", (int)arg, (int)groups)); |
| 1706 | FAIL; |
| 1707 | } |
| 1708 | break; |
| 1709 | |
| 1710 | case SRE_OP_LITERAL: |
| 1711 | case SRE_OP_NOT_LITERAL: |
| 1712 | case SRE_OP_LITERAL_IGNORE: |
| 1713 | case SRE_OP_NOT_LITERAL_IGNORE: |
| 1714 | GET_ARG; |
| 1715 | /* The arg is just a character, nothing to check */ |
| 1716 | break; |
| 1717 | |
| 1718 | case SRE_OP_SUCCESS: |
| 1719 | case SRE_OP_FAILURE: |
| 1720 | /* Nothing to check; these normally end the matching process */ |
| 1721 | break; |
| 1722 | |
| 1723 | case SRE_OP_AT: |
| 1724 | GET_ARG; |
| 1725 | switch (arg) { |
| 1726 | case SRE_AT_BEGINNING: |
| 1727 | case SRE_AT_BEGINNING_STRING: |
| 1728 | case SRE_AT_BEGINNING_LINE: |
| 1729 | case SRE_AT_END: |
| 1730 | case SRE_AT_END_LINE: |
| 1731 | case SRE_AT_END_STRING: |
| 1732 | case SRE_AT_BOUNDARY: |
| 1733 | case SRE_AT_NON_BOUNDARY: |
| 1734 | case SRE_AT_LOC_BOUNDARY: |
| 1735 | case SRE_AT_LOC_NON_BOUNDARY: |
| 1736 | case SRE_AT_UNI_BOUNDARY: |
| 1737 | case SRE_AT_UNI_NON_BOUNDARY: |
| 1738 | break; |
| 1739 | default: |
| 1740 | FAIL; |
| 1741 | } |
| 1742 | break; |
| 1743 | |
| 1744 | case SRE_OP_ANY: |
| 1745 | case SRE_OP_ANY_ALL: |
| 1746 | /* These have no operands */ |
| 1747 | break; |
| 1748 | |
| 1749 | case SRE_OP_IN: |
| 1750 | case SRE_OP_IN_IGNORE: |
| 1751 | GET_SKIP; |
| 1752 | /* Stop 1 before the end; we check the FAILURE below */ |
| 1753 | if (!_validate_charset(code, code+skip-2)) |
| 1754 | FAIL; |
| 1755 | if (code[skip-2] != SRE_OP_FAILURE) |
| 1756 | FAIL; |
| 1757 | code += skip-1; |
| 1758 | break; |
| 1759 | |
| 1760 | case SRE_OP_INFO: |
| 1761 | { |
| 1762 | /* A minimal info field is |
| 1763 | <INFO> <1=skip> <2=flags> <3=min> <4=max>; |
| 1764 | If SRE_INFO_PREFIX or SRE_INFO_CHARSET is in the flags, |
| 1765 | more follows. */ |
Ross Lagerwall | 88748d7 | 2012-03-06 21:48:57 +0200 | [diff] [blame] | 1766 | SRE_CODE flags, i; |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1767 | SRE_CODE *newcode; |
| 1768 | GET_SKIP; |
| 1769 | newcode = code+skip-1; |
| 1770 | GET_ARG; flags = arg; |
Ross Lagerwall | 88748d7 | 2012-03-06 21:48:57 +0200 | [diff] [blame] | 1771 | GET_ARG; |
| 1772 | GET_ARG; |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1773 | /* Check that only valid flags are present */ |
| 1774 | if ((flags & ~(SRE_INFO_PREFIX | |
| 1775 | SRE_INFO_LITERAL | |
| 1776 | SRE_INFO_CHARSET)) != 0) |
| 1777 | FAIL; |
| 1778 | /* PREFIX and CHARSET are mutually exclusive */ |
| 1779 | if ((flags & SRE_INFO_PREFIX) && |
| 1780 | (flags & SRE_INFO_CHARSET)) |
| 1781 | FAIL; |
| 1782 | /* LITERAL implies PREFIX */ |
| 1783 | if ((flags & SRE_INFO_LITERAL) && |
| 1784 | !(flags & SRE_INFO_PREFIX)) |
| 1785 | FAIL; |
| 1786 | /* Validate the prefix */ |
| 1787 | if (flags & SRE_INFO_PREFIX) { |
Ross Lagerwall | 88748d7 | 2012-03-06 21:48:57 +0200 | [diff] [blame] | 1788 | SRE_CODE prefix_len; |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1789 | GET_ARG; prefix_len = arg; |
Ross Lagerwall | 88748d7 | 2012-03-06 21:48:57 +0200 | [diff] [blame] | 1790 | GET_ARG; |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1791 | /* Here comes the prefix string */ |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1792 | if (prefix_len > (Py_uintptr_t)(newcode - code)) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1793 | FAIL; |
| 1794 | code += prefix_len; |
| 1795 | /* And here comes the overlap table */ |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1796 | if (prefix_len > (Py_uintptr_t)(newcode - code)) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1797 | FAIL; |
| 1798 | /* Each overlap value should be < prefix_len */ |
| 1799 | for (i = 0; i < prefix_len; i++) { |
| 1800 | if (code[i] >= prefix_len) |
| 1801 | FAIL; |
| 1802 | } |
| 1803 | code += prefix_len; |
| 1804 | } |
| 1805 | /* Validate the charset */ |
| 1806 | if (flags & SRE_INFO_CHARSET) { |
| 1807 | if (!_validate_charset(code, newcode-1)) |
| 1808 | FAIL; |
| 1809 | if (newcode[-1] != SRE_OP_FAILURE) |
| 1810 | FAIL; |
| 1811 | code = newcode; |
| 1812 | } |
| 1813 | else if (code != newcode) { |
| 1814 | VTRACE(("code=%p, newcode=%p\n", code, newcode)); |
| 1815 | FAIL; |
| 1816 | } |
| 1817 | } |
| 1818 | break; |
| 1819 | |
| 1820 | case SRE_OP_BRANCH: |
| 1821 | { |
| 1822 | SRE_CODE *target = NULL; |
| 1823 | for (;;) { |
| 1824 | GET_SKIP; |
| 1825 | if (skip == 0) |
| 1826 | break; |
| 1827 | /* Stop 2 before the end; we check the JUMP below */ |
| 1828 | if (!_validate_inner(code, code+skip-3, groups)) |
| 1829 | FAIL; |
| 1830 | code += skip-3; |
| 1831 | /* Check that it ends with a JUMP, and that each JUMP |
| 1832 | has the same target */ |
| 1833 | GET_OP; |
| 1834 | if (op != SRE_OP_JUMP) |
| 1835 | FAIL; |
| 1836 | GET_SKIP; |
| 1837 | if (target == NULL) |
| 1838 | target = code+skip-1; |
| 1839 | else if (code+skip-1 != target) |
| 1840 | FAIL; |
| 1841 | } |
| 1842 | } |
| 1843 | break; |
| 1844 | |
| 1845 | case SRE_OP_REPEAT_ONE: |
| 1846 | case SRE_OP_MIN_REPEAT_ONE: |
| 1847 | { |
| 1848 | SRE_CODE min, max; |
| 1849 | GET_SKIP; |
| 1850 | GET_ARG; min = arg; |
| 1851 | GET_ARG; max = arg; |
| 1852 | if (min > max) |
| 1853 | FAIL; |
Serhiy Storchaka | 70ca021 | 2013-02-16 16:47:47 +0200 | [diff] [blame] | 1854 | if (max > SRE_MAXREPEAT) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1855 | FAIL; |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1856 | if (!_validate_inner(code, code+skip-4, groups)) |
| 1857 | FAIL; |
| 1858 | code += skip-4; |
| 1859 | GET_OP; |
| 1860 | if (op != SRE_OP_SUCCESS) |
| 1861 | FAIL; |
| 1862 | } |
| 1863 | break; |
| 1864 | |
| 1865 | case SRE_OP_REPEAT: |
| 1866 | { |
| 1867 | SRE_CODE min, max; |
| 1868 | GET_SKIP; |
| 1869 | GET_ARG; min = arg; |
| 1870 | GET_ARG; max = arg; |
| 1871 | if (min > max) |
| 1872 | FAIL; |
Serhiy Storchaka | 70ca021 | 2013-02-16 16:47:47 +0200 | [diff] [blame] | 1873 | if (max > SRE_MAXREPEAT) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1874 | FAIL; |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1875 | if (!_validate_inner(code, code+skip-3, groups)) |
| 1876 | FAIL; |
| 1877 | code += skip-3; |
| 1878 | GET_OP; |
| 1879 | if (op != SRE_OP_MAX_UNTIL && op != SRE_OP_MIN_UNTIL) |
| 1880 | FAIL; |
| 1881 | } |
| 1882 | break; |
| 1883 | |
| 1884 | case SRE_OP_GROUPREF: |
| 1885 | case SRE_OP_GROUPREF_IGNORE: |
| 1886 | GET_ARG; |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1887 | if (arg >= (size_t)groups) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1888 | FAIL; |
| 1889 | break; |
| 1890 | |
| 1891 | case SRE_OP_GROUPREF_EXISTS: |
| 1892 | /* The regex syntax for this is: '(?(group)then|else)', where |
| 1893 | 'group' is either an integer group number or a group name, |
| 1894 | 'then' and 'else' are sub-regexes, and 'else' is optional. */ |
| 1895 | GET_ARG; |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1896 | if (arg >= (size_t)groups) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1897 | FAIL; |
Guido van Rossum | 92f8f3e | 2008-09-10 14:30:50 +0000 | [diff] [blame] | 1898 | GET_SKIP_ADJ(1); |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1899 | code--; /* The skip is relative to the first arg! */ |
| 1900 | /* There are two possibilities here: if there is both a 'then' |
| 1901 | part and an 'else' part, the generated code looks like: |
| 1902 | |
| 1903 | GROUPREF_EXISTS |
| 1904 | <group> |
| 1905 | <skipyes> |
| 1906 | ...then part... |
| 1907 | JUMP |
| 1908 | <skipno> |
| 1909 | (<skipyes> jumps here) |
| 1910 | ...else part... |
| 1911 | (<skipno> jumps here) |
| 1912 | |
| 1913 | If there is only a 'then' part, it looks like: |
| 1914 | |
| 1915 | GROUPREF_EXISTS |
| 1916 | <group> |
| 1917 | <skip> |
| 1918 | ...then part... |
| 1919 | (<skip> jumps here) |
| 1920 | |
| 1921 | There is no direct way to decide which it is, and we don't want |
| 1922 | to allow arbitrary jumps anywhere in the code; so we just look |
| 1923 | for a JUMP opcode preceding our skip target. |
| 1924 | */ |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1925 | if (skip >= 3 && skip-3 < (Py_uintptr_t)(end - code) && |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1926 | code[skip-3] == SRE_OP_JUMP) |
| 1927 | { |
| 1928 | VTRACE(("both then and else parts present\n")); |
| 1929 | if (!_validate_inner(code+1, code+skip-3, groups)) |
| 1930 | FAIL; |
| 1931 | code += skip-2; /* Position after JUMP, at <skipno> */ |
| 1932 | GET_SKIP; |
| 1933 | if (!_validate_inner(code, code+skip-1, groups)) |
| 1934 | FAIL; |
| 1935 | code += skip-1; |
| 1936 | } |
| 1937 | else { |
| 1938 | VTRACE(("only a then part present\n")); |
| 1939 | if (!_validate_inner(code+1, code+skip-1, groups)) |
| 1940 | FAIL; |
| 1941 | code += skip-1; |
| 1942 | } |
| 1943 | break; |
| 1944 | |
| 1945 | case SRE_OP_ASSERT: |
| 1946 | case SRE_OP_ASSERT_NOT: |
| 1947 | GET_SKIP; |
| 1948 | GET_ARG; /* 0 for lookahead, width for lookbehind */ |
| 1949 | code--; /* Back up over arg to simplify math below */ |
| 1950 | if (arg & 0x80000000) |
| 1951 | FAIL; /* Width too large */ |
| 1952 | /* Stop 1 before the end; we check the SUCCESS below */ |
| 1953 | if (!_validate_inner(code+1, code+skip-2, groups)) |
| 1954 | FAIL; |
| 1955 | code += skip-2; |
| 1956 | GET_OP; |
| 1957 | if (op != SRE_OP_SUCCESS) |
| 1958 | FAIL; |
| 1959 | break; |
| 1960 | |
| 1961 | default: |
| 1962 | FAIL; |
| 1963 | |
| 1964 | } |
| 1965 | } |
| 1966 | |
| 1967 | VTRACE(("okay\n")); |
| 1968 | return 1; |
| 1969 | } |
| 1970 | |
| 1971 | static int |
| 1972 | _validate_outer(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups) |
| 1973 | { |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 1974 | if (groups < 0 || (size_t)groups > SRE_MAXGROUPS || |
| 1975 | code >= end || end[-1] != SRE_OP_SUCCESS) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1976 | FAIL; |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1977 | return _validate_inner(code, end-1, groups); |
| 1978 | } |
| 1979 | |
| 1980 | static int |
| 1981 | _validate(PatternObject *self) |
| 1982 | { |
| 1983 | if (!_validate_outer(self->code, self->code+self->codesize, self->groups)) |
| 1984 | { |
| 1985 | PyErr_SetString(PyExc_RuntimeError, "invalid SRE code"); |
| 1986 | return 0; |
| 1987 | } |
| 1988 | else |
| 1989 | VTRACE(("Success!\n")); |
| 1990 | return 1; |
| 1991 | } |
| 1992 | |
| 1993 | /* -------------------------------------------------------------------- */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1994 | /* match methods */ |
| 1995 | |
| 1996 | static void |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1997 | match_dealloc(MatchObject* self) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1998 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1999 | Py_XDECREF(self->regs); |
| 2000 | Py_XDECREF(self->string); |
| 2001 | Py_DECREF(self->pattern); |
| 2002 | PyObject_DEL(self); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | static PyObject* |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2006 | match_getslice_by_index(MatchObject* self, Py_ssize_t index, PyObject* def) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2007 | { |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 2008 | Py_ssize_t length; |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 2009 | int isbytes, charsize; |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 2010 | Py_buffer view; |
| 2011 | PyObject *result; |
| 2012 | void* ptr; |
| 2013 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2014 | if (index < 0 || index >= self->groups) { |
| 2015 | /* raise IndexError if we were given a bad group number */ |
| 2016 | PyErr_SetString( |
| 2017 | PyExc_IndexError, |
| 2018 | "no such group" |
| 2019 | ); |
| 2020 | return NULL; |
| 2021 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2022 | |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2023 | index *= 2; |
| 2024 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2025 | if (self->string == Py_None || self->mark[index] < 0) { |
| 2026 | /* return default value if the string or group is undefined */ |
| 2027 | Py_INCREF(def); |
| 2028 | return def; |
| 2029 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2030 | |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 2031 | ptr = getstring(self->string, &length, &isbytes, &charsize, &view); |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 2032 | if (ptr == NULL) |
| 2033 | return NULL; |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 2034 | result = getslice(isbytes, ptr, |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 2035 | self->string, self->mark[index], self->mark[index+1]); |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 2036 | if (isbytes && view.buf != NULL) |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 2037 | PyBuffer_Release(&view); |
| 2038 | return result; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2041 | static Py_ssize_t |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2042 | match_getindex(MatchObject* self, PyObject* index) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2043 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2044 | Py_ssize_t i; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2045 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2046 | if (index == NULL) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2047 | /* Default value */ |
| 2048 | return 0; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2049 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2050 | if (PyLong_Check(index)) |
| 2051 | return PyLong_AsSsize_t(index); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2052 | |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2053 | i = -1; |
| 2054 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2055 | if (self->pattern->groupindex) { |
| 2056 | index = PyObject_GetItem(self->pattern->groupindex, index); |
| 2057 | if (index) { |
Neal Norwitz | 1fe5f38 | 2007-08-31 04:32:55 +0000 | [diff] [blame] | 2058 | if (PyLong_Check(index)) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2059 | i = PyLong_AsSsize_t(index); |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2060 | Py_DECREF(index); |
| 2061 | } else |
| 2062 | PyErr_Clear(); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2063 | } |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2064 | |
| 2065 | return i; |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | static PyObject* |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 2069 | match_getslice(MatchObject* self, PyObject* index, PyObject* def) |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2070 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2071 | return match_getslice_by_index(self, match_getindex(self, index), def); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | static PyObject* |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2075 | match_expand(MatchObject* self, PyObject* ptemplate) |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 2076 | { |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 2077 | /* delegate to Python code */ |
| 2078 | return call( |
Thomas Wouters | 9ada3d6 | 2006-04-21 09:47:09 +0000 | [diff] [blame] | 2079 | SRE_PY_MODULE, "_expand", |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2080 | PyTuple_Pack(3, self->pattern, self, ptemplate) |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 2081 | ); |
| 2082 | } |
| 2083 | |
| 2084 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2085 | match_group(MatchObject* self, PyObject* args) |
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 | PyObject* result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2088 | Py_ssize_t i, size; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2089 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2090 | size = PyTuple_GET_SIZE(args); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2091 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2092 | switch (size) { |
| 2093 | case 0: |
| 2094 | result = match_getslice(self, Py_False, Py_None); |
| 2095 | break; |
| 2096 | case 1: |
| 2097 | result = match_getslice(self, PyTuple_GET_ITEM(args, 0), Py_None); |
| 2098 | break; |
| 2099 | default: |
| 2100 | /* fetch multiple items */ |
| 2101 | result = PyTuple_New(size); |
| 2102 | if (!result) |
| 2103 | return NULL; |
| 2104 | for (i = 0; i < size; i++) { |
| 2105 | PyObject* item = match_getslice( |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 2106 | self, PyTuple_GET_ITEM(args, i), Py_None |
| 2107 | ); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2108 | if (!item) { |
| 2109 | Py_DECREF(result); |
| 2110 | return NULL; |
| 2111 | } |
| 2112 | PyTuple_SET_ITEM(result, i, item); |
| 2113 | } |
| 2114 | break; |
| 2115 | } |
| 2116 | return result; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2117 | } |
| 2118 | |
| 2119 | static PyObject* |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 2120 | match_groups(MatchObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2121 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2122 | PyObject* result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2123 | Py_ssize_t index; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2124 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2125 | PyObject* def = Py_None; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 2126 | static char* kwlist[] = { "default", NULL }; |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 2127 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:groups", kwlist, &def)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2128 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2129 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2130 | result = PyTuple_New(self->groups-1); |
| 2131 | if (!result) |
| 2132 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2133 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2134 | for (index = 1; index < self->groups; index++) { |
| 2135 | PyObject* item; |
| 2136 | item = match_getslice_by_index(self, index, def); |
| 2137 | if (!item) { |
| 2138 | Py_DECREF(result); |
| 2139 | return NULL; |
| 2140 | } |
| 2141 | PyTuple_SET_ITEM(result, index-1, item); |
| 2142 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2143 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2144 | return result; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
| 2147 | static PyObject* |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 2148 | match_groupdict(MatchObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2149 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2150 | PyObject* result; |
| 2151 | PyObject* keys; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2152 | Py_ssize_t index; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2153 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2154 | PyObject* def = Py_None; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 2155 | static char* kwlist[] = { "default", NULL }; |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2156 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:groupdict", kwlist, &def)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2157 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2158 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2159 | result = PyDict_New(); |
| 2160 | if (!result || !self->pattern->groupindex) |
| 2161 | return result; |
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 | keys = PyMapping_Keys(self->pattern->groupindex); |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2164 | if (!keys) |
| 2165 | goto failed; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2166 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2167 | for (index = 0; index < PyList_GET_SIZE(keys); index++) { |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2168 | int status; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2169 | PyObject* key; |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2170 | PyObject* value; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2171 | key = PyList_GET_ITEM(keys, index); |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2172 | if (!key) |
| 2173 | goto failed; |
| 2174 | value = match_getslice(self, key, def); |
| 2175 | if (!value) { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2176 | Py_DECREF(key); |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2177 | goto failed; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2178 | } |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2179 | status = PyDict_SetItem(result, key, value); |
| 2180 | Py_DECREF(value); |
| 2181 | if (status < 0) |
| 2182 | goto failed; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 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 | Py_DECREF(keys); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2186 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2187 | return result; |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2188 | |
| 2189 | failed: |
Neal Norwitz | 60da316 | 2006-03-07 04:48:24 +0000 | [diff] [blame] | 2190 | Py_XDECREF(keys); |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2191 | Py_DECREF(result); |
| 2192 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
| 2195 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2196 | match_start(MatchObject* self, PyObject* args) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2197 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2198 | Py_ssize_t index; |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2199 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2200 | PyObject* index_ = NULL; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2201 | if (!PyArg_UnpackTuple(args, "start", 0, 1, &index_)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2202 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2203 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2204 | index = match_getindex(self, index_); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2205 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2206 | if (index < 0 || index >= self->groups) { |
| 2207 | PyErr_SetString( |
| 2208 | PyExc_IndexError, |
| 2209 | "no such group" |
| 2210 | ); |
| 2211 | return NULL; |
| 2212 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2213 | |
Fredrik Lundh | 510c97b | 2000-09-02 16:36:57 +0000 | [diff] [blame] | 2214 | /* mark is -1 if group is undefined */ |
Antoine Pitrou | 43fb54c | 2012-12-02 12:52:36 +0100 | [diff] [blame] | 2215 | return PyLong_FromSsize_t(self->mark[index*2]); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2219 | match_end(MatchObject* self, PyObject* args) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2220 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2221 | Py_ssize_t index; |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2222 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2223 | PyObject* index_ = NULL; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2224 | if (!PyArg_UnpackTuple(args, "end", 0, 1, &index_)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2225 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2226 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2227 | index = match_getindex(self, index_); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2228 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2229 | if (index < 0 || index >= self->groups) { |
| 2230 | PyErr_SetString( |
| 2231 | PyExc_IndexError, |
| 2232 | "no such group" |
| 2233 | ); |
| 2234 | return NULL; |
| 2235 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2236 | |
Fredrik Lundh | 510c97b | 2000-09-02 16:36:57 +0000 | [diff] [blame] | 2237 | /* mark is -1 if group is undefined */ |
Antoine Pitrou | 43fb54c | 2012-12-02 12:52:36 +0100 | [diff] [blame] | 2238 | return PyLong_FromSsize_t(self->mark[index*2+1]); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
| 2241 | LOCAL(PyObject*) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2242 | _pair(Py_ssize_t i1, Py_ssize_t i2) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2243 | { |
| 2244 | PyObject* pair; |
| 2245 | PyObject* item; |
| 2246 | |
| 2247 | pair = PyTuple_New(2); |
| 2248 | if (!pair) |
| 2249 | return NULL; |
| 2250 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2251 | item = PyLong_FromSsize_t(i1); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2252 | if (!item) |
| 2253 | goto error; |
| 2254 | PyTuple_SET_ITEM(pair, 0, item); |
| 2255 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2256 | item = PyLong_FromSsize_t(i2); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2257 | if (!item) |
| 2258 | goto error; |
| 2259 | PyTuple_SET_ITEM(pair, 1, item); |
| 2260 | |
| 2261 | return pair; |
| 2262 | |
| 2263 | error: |
| 2264 | Py_DECREF(pair); |
| 2265 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
| 2268 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2269 | match_span(MatchObject* self, PyObject* args) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2270 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2271 | Py_ssize_t index; |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2272 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2273 | PyObject* index_ = NULL; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2274 | if (!PyArg_UnpackTuple(args, "span", 0, 1, &index_)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2275 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2276 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2277 | index = match_getindex(self, index_); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2278 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2279 | if (index < 0 || index >= self->groups) { |
| 2280 | PyErr_SetString( |
| 2281 | PyExc_IndexError, |
| 2282 | "no such group" |
| 2283 | ); |
| 2284 | return NULL; |
| 2285 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2286 | |
Fredrik Lundh | 510c97b | 2000-09-02 16:36:57 +0000 | [diff] [blame] | 2287 | /* marks are -1 if group is undefined */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2288 | return _pair(self->mark[index*2], self->mark[index*2+1]); |
| 2289 | } |
| 2290 | |
| 2291 | static PyObject* |
| 2292 | match_regs(MatchObject* self) |
| 2293 | { |
| 2294 | PyObject* regs; |
| 2295 | PyObject* item; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2296 | Py_ssize_t index; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2297 | |
| 2298 | regs = PyTuple_New(self->groups); |
| 2299 | if (!regs) |
| 2300 | return NULL; |
| 2301 | |
| 2302 | for (index = 0; index < self->groups; index++) { |
| 2303 | item = _pair(self->mark[index*2], self->mark[index*2+1]); |
| 2304 | if (!item) { |
| 2305 | Py_DECREF(regs); |
| 2306 | return NULL; |
| 2307 | } |
| 2308 | PyTuple_SET_ITEM(regs, index, item); |
| 2309 | } |
| 2310 | |
| 2311 | Py_INCREF(regs); |
| 2312 | self->regs = regs; |
| 2313 | |
| 2314 | return regs; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2315 | } |
| 2316 | |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2317 | static PyObject* |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2318 | match_copy(MatchObject* self, PyObject *unused) |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2319 | { |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2320 | #ifdef USE_BUILTIN_COPY |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2321 | MatchObject* copy; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2322 | Py_ssize_t slots, offset; |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 2323 | |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2324 | slots = 2 * (self->pattern->groups+1); |
| 2325 | |
| 2326 | copy = PyObject_NEW_VAR(MatchObject, &Match_Type, slots); |
| 2327 | if (!copy) |
| 2328 | return NULL; |
| 2329 | |
| 2330 | /* this value a constant, but any compiler should be able to |
| 2331 | figure that out all by itself */ |
| 2332 | offset = offsetof(MatchObject, string); |
| 2333 | |
| 2334 | Py_XINCREF(self->pattern); |
| 2335 | Py_XINCREF(self->string); |
| 2336 | Py_XINCREF(self->regs); |
| 2337 | |
| 2338 | memcpy((char*) copy + offset, (char*) self + offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2339 | sizeof(MatchObject) + slots * sizeof(Py_ssize_t) - offset); |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2340 | |
| 2341 | return (PyObject*) copy; |
| 2342 | #else |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2343 | PyErr_SetString(PyExc_TypeError, "cannot copy this match object"); |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2344 | return NULL; |
| 2345 | #endif |
| 2346 | } |
| 2347 | |
| 2348 | static PyObject* |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2349 | match_deepcopy(MatchObject* self, PyObject* memo) |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2350 | { |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2351 | #ifdef USE_BUILTIN_COPY |
| 2352 | MatchObject* copy; |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 2353 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2354 | copy = (MatchObject*) match_copy(self); |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2355 | if (!copy) |
| 2356 | return NULL; |
| 2357 | |
| 2358 | if (!deepcopy((PyObject**) ©->pattern, memo) || |
| 2359 | !deepcopy(©->string, memo) || |
| 2360 | !deepcopy(©->regs, memo)) { |
| 2361 | Py_DECREF(copy); |
| 2362 | return NULL; |
| 2363 | } |
| 2364 | |
| 2365 | #else |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2366 | PyErr_SetString(PyExc_TypeError, "cannot deepcopy this match object"); |
| 2367 | return NULL; |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2368 | #endif |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2369 | } |
| 2370 | |
Andrew Svetlov | 56ad5ed | 2012-12-23 19:23:07 +0200 | [diff] [blame] | 2371 | PyDoc_STRVAR(match_doc, |
| 2372 | "The result of re.match() and re.search().\n\ |
| 2373 | Match objects always have a boolean value of True."); |
| 2374 | |
| 2375 | PyDoc_STRVAR(match_group_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 2376 | "group([group1, ...]) -> str or tuple.\n\ |
Andrew Svetlov | 56ad5ed | 2012-12-23 19:23:07 +0200 | [diff] [blame] | 2377 | Return subgroup(s) of the match by indices or names.\n\ |
| 2378 | For 0 returns the entire match."); |
| 2379 | |
| 2380 | PyDoc_STRVAR(match_start_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 2381 | "start([group=0]) -> int.\n\ |
Andrew Svetlov | 56ad5ed | 2012-12-23 19:23:07 +0200 | [diff] [blame] | 2382 | Return index of the start of the substring matched by group."); |
| 2383 | |
| 2384 | PyDoc_STRVAR(match_end_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 2385 | "end([group=0]) -> int.\n\ |
Andrew Svetlov | 56ad5ed | 2012-12-23 19:23:07 +0200 | [diff] [blame] | 2386 | Return index of the end of the substring matched by group."); |
| 2387 | |
| 2388 | PyDoc_STRVAR(match_span_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 2389 | "span([group]) -> tuple.\n\ |
Andrew Svetlov | 56ad5ed | 2012-12-23 19:23:07 +0200 | [diff] [blame] | 2390 | For MatchObject m, return the 2-tuple (m.start(group), m.end(group))."); |
| 2391 | |
| 2392 | PyDoc_STRVAR(match_groups_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 2393 | "groups([default=None]) -> tuple.\n\ |
Andrew Svetlov | 56ad5ed | 2012-12-23 19:23:07 +0200 | [diff] [blame] | 2394 | Return a tuple containing all the subgroups of the match, from 1.\n\ |
| 2395 | The default argument is used for groups\n\ |
| 2396 | that did not participate in the match"); |
| 2397 | |
| 2398 | PyDoc_STRVAR(match_groupdict_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 2399 | "groupdict([default=None]) -> dict.\n\ |
Andrew Svetlov | 56ad5ed | 2012-12-23 19:23:07 +0200 | [diff] [blame] | 2400 | Return a dictionary containing all the named subgroups of the match,\n\ |
| 2401 | keyed by the subgroup name. The default argument is used for groups\n\ |
| 2402 | that did not participate in the match"); |
| 2403 | |
| 2404 | PyDoc_STRVAR(match_expand_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 2405 | "expand(template) -> str.\n\ |
Andrew Svetlov | 56ad5ed | 2012-12-23 19:23:07 +0200 | [diff] [blame] | 2406 | Return the string obtained by doing backslash substitution\n\ |
| 2407 | on the string template, as done by the sub() method."); |
| 2408 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2409 | static PyMethodDef match_methods[] = { |
Andrew Svetlov | 56ad5ed | 2012-12-23 19:23:07 +0200 | [diff] [blame] | 2410 | {"group", (PyCFunction) match_group, METH_VARARGS, match_group_doc}, |
| 2411 | {"start", (PyCFunction) match_start, METH_VARARGS, match_start_doc}, |
| 2412 | {"end", (PyCFunction) match_end, METH_VARARGS, match_end_doc}, |
| 2413 | {"span", (PyCFunction) match_span, METH_VARARGS, match_span_doc}, |
| 2414 | {"groups", (PyCFunction) match_groups, METH_VARARGS|METH_KEYWORDS, |
| 2415 | match_groups_doc}, |
| 2416 | {"groupdict", (PyCFunction) match_groupdict, METH_VARARGS|METH_KEYWORDS, |
| 2417 | match_groupdict_doc}, |
| 2418 | {"expand", (PyCFunction) match_expand, METH_O, match_expand_doc}, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2419 | {"__copy__", (PyCFunction) match_copy, METH_NOARGS}, |
| 2420 | {"__deepcopy__", (PyCFunction) match_deepcopy, METH_O}, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2421 | {NULL, NULL} |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2422 | }; |
| 2423 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 2424 | static PyObject * |
| 2425 | match_lastindex_get(MatchObject *self) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2426 | { |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 2427 | if (self->lastindex >= 0) |
Antoine Pitrou | 43fb54c | 2012-12-02 12:52:36 +0100 | [diff] [blame] | 2428 | return PyLong_FromSsize_t(self->lastindex); |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 2429 | Py_INCREF(Py_None); |
| 2430 | return Py_None; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2431 | } |
| 2432 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 2433 | static PyObject * |
| 2434 | match_lastgroup_get(MatchObject *self) |
| 2435 | { |
| 2436 | if (self->pattern->indexgroup && self->lastindex >= 0) { |
| 2437 | PyObject* result = PySequence_GetItem( |
| 2438 | self->pattern->indexgroup, self->lastindex |
| 2439 | ); |
| 2440 | if (result) |
| 2441 | return result; |
| 2442 | PyErr_Clear(); |
| 2443 | } |
| 2444 | Py_INCREF(Py_None); |
| 2445 | return Py_None; |
| 2446 | } |
| 2447 | |
| 2448 | static PyObject * |
| 2449 | match_regs_get(MatchObject *self) |
| 2450 | { |
| 2451 | if (self->regs) { |
| 2452 | Py_INCREF(self->regs); |
| 2453 | return self->regs; |
| 2454 | } else |
| 2455 | return match_regs(self); |
| 2456 | } |
| 2457 | |
Serhiy Storchaka | 36af10c | 2013-10-20 13:13:31 +0300 | [diff] [blame] | 2458 | static PyObject * |
| 2459 | match_repr(MatchObject *self) |
| 2460 | { |
| 2461 | PyObject *result; |
| 2462 | PyObject *group0 = match_getslice_by_index(self, 0, Py_None); |
| 2463 | if (group0 == NULL) |
| 2464 | return NULL; |
| 2465 | result = PyUnicode_FromFormat( |
| 2466 | "<%s object; span=(%d, %d), match=%.50R>", |
| 2467 | Py_TYPE(self)->tp_name, |
| 2468 | self->mark[0], self->mark[1], group0); |
| 2469 | Py_DECREF(group0); |
| 2470 | return result; |
| 2471 | } |
| 2472 | |
| 2473 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 2474 | static PyGetSetDef match_getset[] = { |
| 2475 | {"lastindex", (getter)match_lastindex_get, (setter)NULL}, |
| 2476 | {"lastgroup", (getter)match_lastgroup_get, (setter)NULL}, |
| 2477 | {"regs", (getter)match_regs_get, (setter)NULL}, |
| 2478 | {NULL} |
| 2479 | }; |
| 2480 | |
| 2481 | #define MATCH_OFF(x) offsetof(MatchObject, x) |
| 2482 | static PyMemberDef match_members[] = { |
| 2483 | {"string", T_OBJECT, MATCH_OFF(string), READONLY}, |
| 2484 | {"re", T_OBJECT, MATCH_OFF(pattern), READONLY}, |
| 2485 | {"pos", T_PYSSIZET, MATCH_OFF(pos), READONLY}, |
| 2486 | {"endpos", T_PYSSIZET, MATCH_OFF(endpos), READONLY}, |
| 2487 | {NULL} |
| 2488 | }; |
| 2489 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2490 | /* FIXME: implement setattr("string", None) as a special case (to |
| 2491 | detach the associated string, if any */ |
| 2492 | |
Neal Norwitz | 57c179c | 2006-03-22 07:18:02 +0000 | [diff] [blame] | 2493 | static PyTypeObject Match_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2494 | PyVarObject_HEAD_INIT(NULL,0) |
| 2495 | "_" SRE_MODULE ".SRE_Match", |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2496 | sizeof(MatchObject), sizeof(Py_ssize_t), |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2497 | (destructor)match_dealloc, /* tp_dealloc */ |
| 2498 | 0, /* tp_print */ |
| 2499 | 0, /* tp_getattr */ |
| 2500 | 0, /* tp_setattr */ |
| 2501 | 0, /* tp_reserved */ |
Serhiy Storchaka | 36af10c | 2013-10-20 13:13:31 +0300 | [diff] [blame] | 2502 | (reprfunc)match_repr, /* tp_repr */ |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2503 | 0, /* tp_as_number */ |
| 2504 | 0, /* tp_as_sequence */ |
| 2505 | 0, /* tp_as_mapping */ |
| 2506 | 0, /* tp_hash */ |
| 2507 | 0, /* tp_call */ |
| 2508 | 0, /* tp_str */ |
| 2509 | 0, /* tp_getattro */ |
| 2510 | 0, /* tp_setattro */ |
| 2511 | 0, /* tp_as_buffer */ |
| 2512 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 2513 | match_doc, /* tp_doc */ |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2514 | 0, /* tp_traverse */ |
| 2515 | 0, /* tp_clear */ |
| 2516 | 0, /* tp_richcompare */ |
| 2517 | 0, /* tp_weaklistoffset */ |
| 2518 | 0, /* tp_iter */ |
| 2519 | 0, /* tp_iternext */ |
| 2520 | match_methods, /* tp_methods */ |
| 2521 | match_members, /* tp_members */ |
| 2522 | match_getset, /* tp_getset */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2523 | }; |
| 2524 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2525 | static PyObject* |
Victor Stinner | f558778 | 2013-11-15 23:21:11 +0100 | [diff] [blame] | 2526 | pattern_new_match(PatternObject* pattern, SRE_STATE* state, Py_ssize_t status) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2527 | { |
| 2528 | /* create match object (from state object) */ |
| 2529 | |
| 2530 | MatchObject* match; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2531 | Py_ssize_t i, j; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2532 | char* base; |
| 2533 | int n; |
| 2534 | |
| 2535 | if (status > 0) { |
| 2536 | |
| 2537 | /* create match object (with room for extra group marks) */ |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 2538 | /* coverity[ampersand_in_size] */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2539 | match = PyObject_NEW_VAR(MatchObject, &Match_Type, |
| 2540 | 2*(pattern->groups+1)); |
| 2541 | if (!match) |
| 2542 | return NULL; |
| 2543 | |
| 2544 | Py_INCREF(pattern); |
| 2545 | match->pattern = pattern; |
| 2546 | |
| 2547 | Py_INCREF(state->string); |
| 2548 | match->string = state->string; |
| 2549 | |
| 2550 | match->regs = NULL; |
| 2551 | match->groups = pattern->groups+1; |
| 2552 | |
| 2553 | /* fill in group slices */ |
| 2554 | |
| 2555 | base = (char*) state->beginning; |
| 2556 | n = state->charsize; |
| 2557 | |
| 2558 | match->mark[0] = ((char*) state->start - base) / n; |
| 2559 | match->mark[1] = ((char*) state->ptr - base) / n; |
| 2560 | |
| 2561 | for (i = j = 0; i < pattern->groups; i++, j+=2) |
| 2562 | if (j+1 <= state->lastmark && state->mark[j] && state->mark[j+1]) { |
| 2563 | match->mark[j+2] = ((char*) state->mark[j] - base) / n; |
| 2564 | match->mark[j+3] = ((char*) state->mark[j+1] - base) / n; |
| 2565 | } else |
| 2566 | match->mark[j+2] = match->mark[j+3] = -1; /* undefined */ |
| 2567 | |
| 2568 | match->pos = state->pos; |
| 2569 | match->endpos = state->endpos; |
| 2570 | |
| 2571 | match->lastindex = state->lastindex; |
| 2572 | |
| 2573 | return (PyObject*) match; |
| 2574 | |
| 2575 | } else if (status == 0) { |
| 2576 | |
| 2577 | /* no match */ |
| 2578 | Py_INCREF(Py_None); |
| 2579 | return Py_None; |
| 2580 | |
| 2581 | } |
| 2582 | |
| 2583 | /* internal error */ |
| 2584 | pattern_error(status); |
| 2585 | return NULL; |
| 2586 | } |
| 2587 | |
| 2588 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2589 | /* -------------------------------------------------------------------- */ |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 2590 | /* scanner methods (experimental) */ |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2591 | |
| 2592 | static void |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 2593 | scanner_dealloc(ScannerObject* self) |
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 | state_fini(&self->state); |
Antoine Pitrou | 82feb1f | 2010-01-14 17:34:48 +0000 | [diff] [blame] | 2596 | Py_XDECREF(self->pattern); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2597 | PyObject_DEL(self); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2598 | } |
| 2599 | |
| 2600 | static PyObject* |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2601 | scanner_match(ScannerObject* self, PyObject *unused) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2602 | { |
| 2603 | SRE_STATE* state = &self->state; |
| 2604 | PyObject* match; |
Victor Stinner | 7a6d7cf | 2012-10-31 00:37:41 +0100 | [diff] [blame] | 2605 | Py_ssize_t status; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2606 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 2607 | state_reset(state); |
| 2608 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2609 | state->ptr = state->start; |
| 2610 | |
Serhiy Storchaka | 429b59e | 2014-05-14 21:48:17 +0300 | [diff] [blame] | 2611 | status = sre_match(state, PatternObject_GetCode(self->pattern), 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2612 | if (PyErr_Occurred()) |
| 2613 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2614 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2615 | match = pattern_new_match((PatternObject*) self->pattern, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2616 | state, status); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2617 | |
Gustavo Niemeyer | 0506c64 | 2004-09-03 18:11:59 +0000 | [diff] [blame] | 2618 | if (status == 0 || state->ptr == state->start) |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2619 | state->start = (void*) ((char*) state->ptr + state->charsize); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2620 | else |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2621 | state->start = state->ptr; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2622 | |
| 2623 | return match; |
| 2624 | } |
| 2625 | |
| 2626 | |
| 2627 | static PyObject* |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2628 | scanner_search(ScannerObject* self, PyObject *unused) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2629 | { |
| 2630 | SRE_STATE* state = &self->state; |
| 2631 | PyObject* match; |
Victor Stinner | 7a6d7cf | 2012-10-31 00:37:41 +0100 | [diff] [blame] | 2632 | Py_ssize_t status; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2633 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 2634 | state_reset(state); |
| 2635 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2636 | state->ptr = state->start; |
| 2637 | |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 2638 | status = sre_search(state, PatternObject_GetCode(self->pattern)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2639 | if (PyErr_Occurred()) |
| 2640 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2641 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2642 | match = pattern_new_match((PatternObject*) self->pattern, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2643 | state, status); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2644 | |
Gustavo Niemeyer | 0506c64 | 2004-09-03 18:11:59 +0000 | [diff] [blame] | 2645 | if (status == 0 || state->ptr == state->start) |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 2646 | state->start = (void*) ((char*) state->ptr + state->charsize); |
| 2647 | else |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2648 | state->start = state->ptr; |
| 2649 | |
| 2650 | return match; |
| 2651 | } |
| 2652 | |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 2653 | static PyMethodDef scanner_methods[] = { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2654 | {"match", (PyCFunction) scanner_match, METH_NOARGS}, |
| 2655 | {"search", (PyCFunction) scanner_search, METH_NOARGS}, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2656 | {NULL, NULL} |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2657 | }; |
| 2658 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 2659 | #define SCAN_OFF(x) offsetof(ScannerObject, x) |
| 2660 | static PyMemberDef scanner_members[] = { |
Ezio Melotti | 7c8c1ea | 2011-09-29 01:00:19 +0300 | [diff] [blame] | 2661 | {"pattern", T_OBJECT, SCAN_OFF(pattern), READONLY}, |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 2662 | {NULL} /* Sentinel */ |
| 2663 | }; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2664 | |
Neal Norwitz | 57c179c | 2006-03-22 07:18:02 +0000 | [diff] [blame] | 2665 | static PyTypeObject Scanner_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2666 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2667 | "_" SRE_MODULE ".SRE_Scanner", |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2668 | sizeof(ScannerObject), 0, |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 2669 | (destructor)scanner_dealloc,/* tp_dealloc */ |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2670 | 0, /* tp_print */ |
| 2671 | 0, /* tp_getattr */ |
| 2672 | 0, /* tp_setattr */ |
| 2673 | 0, /* tp_reserved */ |
| 2674 | 0, /* tp_repr */ |
| 2675 | 0, /* tp_as_number */ |
| 2676 | 0, /* tp_as_sequence */ |
| 2677 | 0, /* tp_as_mapping */ |
| 2678 | 0, /* tp_hash */ |
| 2679 | 0, /* tp_call */ |
| 2680 | 0, /* tp_str */ |
| 2681 | 0, /* tp_getattro */ |
| 2682 | 0, /* tp_setattro */ |
| 2683 | 0, /* tp_as_buffer */ |
| 2684 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 2685 | 0, /* tp_doc */ |
| 2686 | 0, /* tp_traverse */ |
| 2687 | 0, /* tp_clear */ |
| 2688 | 0, /* tp_richcompare */ |
| 2689 | 0, /* tp_weaklistoffset */ |
| 2690 | 0, /* tp_iter */ |
| 2691 | 0, /* tp_iternext */ |
| 2692 | scanner_methods, /* tp_methods */ |
| 2693 | scanner_members, /* tp_members */ |
| 2694 | 0, /* tp_getset */ |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2695 | }; |
| 2696 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2697 | static PyObject* |
Sean Reifschneider | 7b3c975 | 2012-03-12 18:22:38 -0600 | [diff] [blame] | 2698 | pattern_scanner(PatternObject* pattern, PyObject* args, PyObject* kw) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2699 | { |
| 2700 | /* create search state object */ |
| 2701 | |
| 2702 | ScannerObject* self; |
| 2703 | |
Serhiy Storchaka | ccdf352 | 2014-03-06 11:28:32 +0200 | [diff] [blame] | 2704 | PyObject *string = NULL, *string2 = NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2705 | Py_ssize_t start = 0; |
| 2706 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Serhiy Storchaka | ccdf352 | 2014-03-06 11:28:32 +0200 | [diff] [blame] | 2707 | static char* kwlist[] = { "string", "pos", "endpos", "source", NULL }; |
| 2708 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|Onn$O:scanner", kwlist, |
| 2709 | &string, &start, &end, &string2)) |
| 2710 | return NULL; |
| 2711 | |
| 2712 | string = fix_string_param(string, string2, "source"); |
| 2713 | if (!string) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2714 | return NULL; |
| 2715 | |
| 2716 | /* create scanner object */ |
| 2717 | self = PyObject_NEW(ScannerObject, &Scanner_Type); |
| 2718 | if (!self) |
| 2719 | return NULL; |
Antoine Pitrou | 82feb1f | 2010-01-14 17:34:48 +0000 | [diff] [blame] | 2720 | self->pattern = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2721 | |
| 2722 | string = state_init(&self->state, pattern, string, start, end); |
| 2723 | if (!string) { |
Antoine Pitrou | 82feb1f | 2010-01-14 17:34:48 +0000 | [diff] [blame] | 2724 | Py_DECREF(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2725 | return NULL; |
| 2726 | } |
| 2727 | |
| 2728 | Py_INCREF(pattern); |
| 2729 | self->pattern = (PyObject*) pattern; |
| 2730 | |
| 2731 | return (PyObject*) self; |
| 2732 | } |
| 2733 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2734 | static PyMethodDef _functions[] = { |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 2735 | {"compile", _compile, METH_VARARGS}, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2736 | {"getcodesize", sre_codesize, METH_NOARGS}, |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 2737 | {"getlower", sre_getlower, METH_VARARGS}, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2738 | {NULL, NULL} |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2739 | }; |
| 2740 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2741 | static struct PyModuleDef sremodule = { |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2742 | PyModuleDef_HEAD_INIT, |
| 2743 | "_" SRE_MODULE, |
| 2744 | NULL, |
| 2745 | -1, |
| 2746 | _functions, |
| 2747 | NULL, |
| 2748 | NULL, |
| 2749 | NULL, |
| 2750 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2751 | }; |
| 2752 | |
| 2753 | PyMODINIT_FUNC PyInit__sre(void) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2754 | { |
Fredrik Lundh | b35ffc0 | 2001-01-15 12:46:09 +0000 | [diff] [blame] | 2755 | PyObject* m; |
| 2756 | PyObject* d; |
Barry Warsaw | 214a0b13 | 2001-08-16 20:33:48 +0000 | [diff] [blame] | 2757 | PyObject* x; |
Fredrik Lundh | b35ffc0 | 2001-01-15 12:46:09 +0000 | [diff] [blame] | 2758 | |
Benjamin Peterson | 08bf91c | 2010-04-11 16:12:57 +0000 | [diff] [blame] | 2759 | /* Patch object types */ |
| 2760 | if (PyType_Ready(&Pattern_Type) || PyType_Ready(&Match_Type) || |
| 2761 | PyType_Ready(&Scanner_Type)) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2762 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2763 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2764 | m = PyModule_Create(&sremodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 2765 | if (m == NULL) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2766 | return NULL; |
Fredrik Lundh | b35ffc0 | 2001-01-15 12:46:09 +0000 | [diff] [blame] | 2767 | d = PyModule_GetDict(m); |
| 2768 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2769 | x = PyLong_FromLong(SRE_MAGIC); |
Fredrik Lundh | 21009b9 | 2001-09-18 18:47:09 +0000 | [diff] [blame] | 2770 | if (x) { |
| 2771 | PyDict_SetItemString(d, "MAGIC", x); |
| 2772 | Py_DECREF(x); |
| 2773 | } |
Fredrik Lundh | 9c7eab8 | 2001-04-15 19:00:58 +0000 | [diff] [blame] | 2774 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2775 | x = PyLong_FromLong(sizeof(SRE_CODE)); |
Martin v. Löwis | 78e2f06 | 2003-04-19 12:56:08 +0000 | [diff] [blame] | 2776 | if (x) { |
| 2777 | PyDict_SetItemString(d, "CODESIZE", x); |
| 2778 | Py_DECREF(x); |
| 2779 | } |
| 2780 | |
Serhiy Storchaka | 70ca021 | 2013-02-16 16:47:47 +0200 | [diff] [blame] | 2781 | x = PyLong_FromUnsignedLong(SRE_MAXREPEAT); |
| 2782 | if (x) { |
| 2783 | PyDict_SetItemString(d, "MAXREPEAT", x); |
| 2784 | Py_DECREF(x); |
| 2785 | } |
| 2786 | |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 2787 | x = PyLong_FromUnsignedLong(SRE_MAXGROUPS); |
| 2788 | if (x) { |
| 2789 | PyDict_SetItemString(d, "MAXGROUPS", x); |
| 2790 | Py_DECREF(x); |
| 2791 | } |
| 2792 | |
Neal Norwitz | fe53713 | 2007-08-26 03:55:15 +0000 | [diff] [blame] | 2793 | x = PyUnicode_FromString(copyright); |
Fredrik Lundh | 21009b9 | 2001-09-18 18:47:09 +0000 | [diff] [blame] | 2794 | if (x) { |
| 2795 | PyDict_SetItemString(d, "copyright", x); |
| 2796 | Py_DECREF(x); |
| 2797 | } |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2798 | return m; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2799 | } |
| 2800 | |
Gustavo Niemeyer | be733ee | 2003-04-20 07:35:44 +0000 | [diff] [blame] | 2801 | /* vim:ts=4:sw=4:et |
| 2802 | */ |