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 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 1387 | #define PAT_OFF(x) offsetof(PatternObject, x) |
| 1388 | static PyMemberDef pattern_members[] = { |
| 1389 | {"pattern", T_OBJECT, PAT_OFF(pattern), READONLY}, |
| 1390 | {"flags", T_INT, PAT_OFF(flags), READONLY}, |
| 1391 | {"groups", T_PYSSIZET, PAT_OFF(groups), READONLY}, |
| 1392 | {"groupindex", T_OBJECT, PAT_OFF(groupindex), READONLY}, |
| 1393 | {NULL} /* Sentinel */ |
| 1394 | }; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1395 | |
Neal Norwitz | 57c179c | 2006-03-22 07:18:02 +0000 | [diff] [blame] | 1396 | static PyTypeObject Pattern_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 1397 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1398 | "_" SRE_MODULE ".SRE_Pattern", |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 1399 | sizeof(PatternObject), sizeof(SRE_CODE), |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 1400 | (destructor)pattern_dealloc, /* tp_dealloc */ |
| 1401 | 0, /* tp_print */ |
| 1402 | 0, /* tp_getattr */ |
| 1403 | 0, /* tp_setattr */ |
| 1404 | 0, /* tp_reserved */ |
Serhiy Storchaka | 5c24d0e | 2013-11-23 22:42:43 +0200 | [diff] [blame] | 1405 | (reprfunc)pattern_repr, /* tp_repr */ |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 1406 | 0, /* tp_as_number */ |
| 1407 | 0, /* tp_as_sequence */ |
| 1408 | 0, /* tp_as_mapping */ |
| 1409 | 0, /* tp_hash */ |
| 1410 | 0, /* tp_call */ |
| 1411 | 0, /* tp_str */ |
| 1412 | 0, /* tp_getattro */ |
| 1413 | 0, /* tp_setattro */ |
| 1414 | 0, /* tp_as_buffer */ |
| 1415 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 1416 | pattern_doc, /* tp_doc */ |
| 1417 | 0, /* tp_traverse */ |
| 1418 | 0, /* tp_clear */ |
| 1419 | 0, /* tp_richcompare */ |
| 1420 | offsetof(PatternObject, weakreflist), /* tp_weaklistoffset */ |
| 1421 | 0, /* tp_iter */ |
| 1422 | 0, /* tp_iternext */ |
| 1423 | pattern_methods, /* tp_methods */ |
| 1424 | pattern_members, /* tp_members */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1425 | }; |
| 1426 | |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1427 | static int _validate(PatternObject *self); /* Forward */ |
| 1428 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1429 | static PyObject * |
| 1430 | _compile(PyObject* self_, PyObject* args) |
| 1431 | { |
| 1432 | /* "compile" pattern descriptor to pattern object */ |
| 1433 | |
| 1434 | PatternObject* self; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1435 | Py_ssize_t i, n; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1436 | |
| 1437 | PyObject* pattern; |
| 1438 | int flags = 0; |
| 1439 | PyObject* code; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1440 | Py_ssize_t groups = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1441 | PyObject* groupindex = NULL; |
| 1442 | PyObject* indexgroup = NULL; |
Benjamin Peterson | e48944b | 2012-03-07 14:50:25 -0600 | [diff] [blame] | 1443 | |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 1444 | if (!PyArg_ParseTuple(args, "OiO!nOO", &pattern, &flags, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1445 | &PyList_Type, &code, &groups, |
| 1446 | &groupindex, &indexgroup)) |
| 1447 | return NULL; |
| 1448 | |
| 1449 | n = PyList_GET_SIZE(code); |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 1450 | /* coverity[ampersand_in_size] */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1451 | self = PyObject_NEW_VAR(PatternObject, &Pattern_Type, n); |
| 1452 | if (!self) |
| 1453 | return NULL; |
Antoine Pitrou | 82feb1f | 2010-01-14 17:34:48 +0000 | [diff] [blame] | 1454 | self->weakreflist = NULL; |
| 1455 | self->pattern = NULL; |
| 1456 | self->groupindex = NULL; |
| 1457 | self->indexgroup = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1458 | |
| 1459 | self->codesize = n; |
| 1460 | |
| 1461 | for (i = 0; i < n; i++) { |
| 1462 | PyObject *o = PyList_GET_ITEM(code, i); |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 1463 | unsigned long value = PyLong_AsUnsignedLong(o); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1464 | self->code[i] = (SRE_CODE) value; |
| 1465 | if ((unsigned long) self->code[i] != value) { |
| 1466 | PyErr_SetString(PyExc_OverflowError, |
| 1467 | "regular expression code size limit exceeded"); |
| 1468 | break; |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | if (PyErr_Occurred()) { |
Antoine Pitrou | 82feb1f | 2010-01-14 17:34:48 +0000 | [diff] [blame] | 1473 | Py_DECREF(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1474 | return NULL; |
| 1475 | } |
| 1476 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1477 | if (pattern == Py_None) { |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 1478 | self->isbytes = -1; |
Victor Stinner | 63ab875 | 2011-11-22 03:31:20 +0100 | [diff] [blame] | 1479 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1480 | else { |
| 1481 | Py_ssize_t p_length; |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 1482 | int charsize; |
| 1483 | Py_buffer view; |
| 1484 | view.buf = NULL; |
| 1485 | if (!getstring(pattern, &p_length, &self->isbytes, |
| 1486 | &charsize, &view)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1487 | Py_DECREF(self); |
| 1488 | return NULL; |
| 1489 | } |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 1490 | if (view.buf) |
| 1491 | PyBuffer_Release(&view); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1492 | } |
Antoine Pitrou | fd03645 | 2008-08-19 17:56:33 +0000 | [diff] [blame] | 1493 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1494 | Py_INCREF(pattern); |
| 1495 | self->pattern = pattern; |
| 1496 | |
| 1497 | self->flags = flags; |
| 1498 | |
| 1499 | self->groups = groups; |
| 1500 | |
| 1501 | Py_XINCREF(groupindex); |
| 1502 | self->groupindex = groupindex; |
| 1503 | |
| 1504 | Py_XINCREF(indexgroup); |
| 1505 | self->indexgroup = indexgroup; |
| 1506 | |
| 1507 | self->weakreflist = NULL; |
| 1508 | |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1509 | if (!_validate(self)) { |
| 1510 | Py_DECREF(self); |
| 1511 | return NULL; |
| 1512 | } |
| 1513 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1514 | return (PyObject*) self; |
| 1515 | } |
| 1516 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1517 | /* -------------------------------------------------------------------- */ |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1518 | /* Code validation */ |
| 1519 | |
| 1520 | /* To learn more about this code, have a look at the _compile() function in |
| 1521 | Lib/sre_compile.py. The validation functions below checks the code array |
| 1522 | for conformance with the code patterns generated there. |
| 1523 | |
| 1524 | The nice thing about the generated code is that it is position-independent: |
| 1525 | all jumps are relative jumps forward. Also, jumps don't cross each other: |
| 1526 | the target of a later jump is always earlier than the target of an earlier |
| 1527 | jump. IOW, this is okay: |
| 1528 | |
| 1529 | J---------J-------T--------T |
| 1530 | \ \_____/ / |
| 1531 | \______________________/ |
| 1532 | |
| 1533 | but this is not: |
| 1534 | |
| 1535 | J---------J-------T--------T |
| 1536 | \_________\_____/ / |
| 1537 | \____________/ |
| 1538 | |
Serhiy Storchaka | efa5a39 | 2013-10-27 08:04:58 +0200 | [diff] [blame] | 1539 | It also helps that SRE_CODE is always an unsigned type. |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1540 | */ |
| 1541 | |
| 1542 | /* Defining this one enables tracing of the validator */ |
| 1543 | #undef VVERBOSE |
| 1544 | |
| 1545 | /* Trace macro for the validator */ |
| 1546 | #if defined(VVERBOSE) |
| 1547 | #define VTRACE(v) printf v |
| 1548 | #else |
Senthil Kumaran | 202a3c4 | 2011-10-20 02:15:36 +0800 | [diff] [blame] | 1549 | #define VTRACE(v) do {} while(0) /* do nothing */ |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1550 | #endif |
| 1551 | |
| 1552 | /* Report failure */ |
| 1553 | #define FAIL do { VTRACE(("FAIL: %d\n", __LINE__)); return 0; } while (0) |
| 1554 | |
| 1555 | /* Extract opcode, argument, or skip count from code array */ |
| 1556 | #define GET_OP \ |
| 1557 | do { \ |
| 1558 | VTRACE(("%p: ", code)); \ |
| 1559 | if (code >= end) FAIL; \ |
| 1560 | op = *code++; \ |
| 1561 | VTRACE(("%lu (op)\n", (unsigned long)op)); \ |
| 1562 | } while (0) |
| 1563 | #define GET_ARG \ |
| 1564 | do { \ |
| 1565 | VTRACE(("%p= ", code)); \ |
| 1566 | if (code >= end) FAIL; \ |
| 1567 | arg = *code++; \ |
| 1568 | VTRACE(("%lu (arg)\n", (unsigned long)arg)); \ |
| 1569 | } while (0) |
Guido van Rossum | 92f8f3e | 2008-09-10 14:30:50 +0000 | [diff] [blame] | 1570 | #define GET_SKIP_ADJ(adj) \ |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1571 | do { \ |
| 1572 | VTRACE(("%p= ", code)); \ |
| 1573 | if (code >= end) FAIL; \ |
| 1574 | skip = *code; \ |
| 1575 | VTRACE(("%lu (skip to %p)\n", \ |
| 1576 | (unsigned long)skip, code+skip)); \ |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1577 | if (skip-adj > (Py_uintptr_t)(end - code)) \ |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1578 | FAIL; \ |
| 1579 | code++; \ |
| 1580 | } while (0) |
Guido van Rossum | 92f8f3e | 2008-09-10 14:30:50 +0000 | [diff] [blame] | 1581 | #define GET_SKIP GET_SKIP_ADJ(0) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1582 | |
| 1583 | static int |
| 1584 | _validate_charset(SRE_CODE *code, SRE_CODE *end) |
| 1585 | { |
| 1586 | /* Some variables are manipulated by the macros above */ |
| 1587 | SRE_CODE op; |
| 1588 | SRE_CODE arg; |
| 1589 | SRE_CODE offset; |
| 1590 | int i; |
| 1591 | |
| 1592 | while (code < end) { |
| 1593 | GET_OP; |
| 1594 | switch (op) { |
| 1595 | |
| 1596 | case SRE_OP_NEGATE: |
| 1597 | break; |
| 1598 | |
| 1599 | case SRE_OP_LITERAL: |
| 1600 | GET_ARG; |
| 1601 | break; |
| 1602 | |
| 1603 | case SRE_OP_RANGE: |
Serhiy Storchaka | 4b8f894 | 2014-10-31 12:36:56 +0200 | [diff] [blame] | 1604 | case SRE_OP_RANGE_IGNORE: |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1605 | GET_ARG; |
| 1606 | GET_ARG; |
| 1607 | break; |
| 1608 | |
| 1609 | case SRE_OP_CHARSET: |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 1610 | offset = 256/SRE_CODE_BITS; /* 256-bit bitmap */ |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1611 | if (offset > (Py_uintptr_t)(end - code)) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1612 | FAIL; |
| 1613 | code += offset; |
| 1614 | break; |
| 1615 | |
| 1616 | case SRE_OP_BIGCHARSET: |
| 1617 | GET_ARG; /* Number of blocks */ |
| 1618 | offset = 256/sizeof(SRE_CODE); /* 256-byte table */ |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1619 | if (offset > (Py_uintptr_t)(end - code)) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1620 | FAIL; |
| 1621 | /* Make sure that each byte points to a valid block */ |
| 1622 | for (i = 0; i < 256; i++) { |
| 1623 | if (((unsigned char *)code)[i] >= arg) |
| 1624 | FAIL; |
| 1625 | } |
| 1626 | code += offset; |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 1627 | offset = arg * (256/SRE_CODE_BITS); /* 256-bit bitmap times arg */ |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1628 | if (offset > (Py_uintptr_t)(end - code)) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1629 | FAIL; |
| 1630 | code += offset; |
| 1631 | break; |
| 1632 | |
| 1633 | case SRE_OP_CATEGORY: |
| 1634 | GET_ARG; |
| 1635 | switch (arg) { |
| 1636 | case SRE_CATEGORY_DIGIT: |
| 1637 | case SRE_CATEGORY_NOT_DIGIT: |
| 1638 | case SRE_CATEGORY_SPACE: |
| 1639 | case SRE_CATEGORY_NOT_SPACE: |
| 1640 | case SRE_CATEGORY_WORD: |
| 1641 | case SRE_CATEGORY_NOT_WORD: |
| 1642 | case SRE_CATEGORY_LINEBREAK: |
| 1643 | case SRE_CATEGORY_NOT_LINEBREAK: |
| 1644 | case SRE_CATEGORY_LOC_WORD: |
| 1645 | case SRE_CATEGORY_LOC_NOT_WORD: |
| 1646 | case SRE_CATEGORY_UNI_DIGIT: |
| 1647 | case SRE_CATEGORY_UNI_NOT_DIGIT: |
| 1648 | case SRE_CATEGORY_UNI_SPACE: |
| 1649 | case SRE_CATEGORY_UNI_NOT_SPACE: |
| 1650 | case SRE_CATEGORY_UNI_WORD: |
| 1651 | case SRE_CATEGORY_UNI_NOT_WORD: |
| 1652 | case SRE_CATEGORY_UNI_LINEBREAK: |
| 1653 | case SRE_CATEGORY_UNI_NOT_LINEBREAK: |
| 1654 | break; |
| 1655 | default: |
| 1656 | FAIL; |
| 1657 | } |
| 1658 | break; |
| 1659 | |
| 1660 | default: |
| 1661 | FAIL; |
| 1662 | |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | return 1; |
| 1667 | } |
| 1668 | |
| 1669 | static int |
| 1670 | _validate_inner(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups) |
| 1671 | { |
| 1672 | /* Some variables are manipulated by the macros above */ |
| 1673 | SRE_CODE op; |
| 1674 | SRE_CODE arg; |
| 1675 | SRE_CODE skip; |
| 1676 | |
| 1677 | VTRACE(("code=%p, end=%p\n", code, end)); |
| 1678 | |
| 1679 | if (code > end) |
| 1680 | FAIL; |
| 1681 | |
| 1682 | while (code < end) { |
| 1683 | GET_OP; |
| 1684 | switch (op) { |
| 1685 | |
| 1686 | case SRE_OP_MARK: |
| 1687 | /* We don't check whether marks are properly nested; the |
| 1688 | sre_match() code is robust even if they don't, and the worst |
| 1689 | you can get is nonsensical match results. */ |
| 1690 | GET_ARG; |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1691 | if (arg > 2 * (size_t)groups + 1) { |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1692 | VTRACE(("arg=%d, groups=%d\n", (int)arg, (int)groups)); |
| 1693 | FAIL; |
| 1694 | } |
| 1695 | break; |
| 1696 | |
| 1697 | case SRE_OP_LITERAL: |
| 1698 | case SRE_OP_NOT_LITERAL: |
| 1699 | case SRE_OP_LITERAL_IGNORE: |
| 1700 | case SRE_OP_NOT_LITERAL_IGNORE: |
| 1701 | GET_ARG; |
| 1702 | /* The arg is just a character, nothing to check */ |
| 1703 | break; |
| 1704 | |
| 1705 | case SRE_OP_SUCCESS: |
| 1706 | case SRE_OP_FAILURE: |
| 1707 | /* Nothing to check; these normally end the matching process */ |
| 1708 | break; |
| 1709 | |
| 1710 | case SRE_OP_AT: |
| 1711 | GET_ARG; |
| 1712 | switch (arg) { |
| 1713 | case SRE_AT_BEGINNING: |
| 1714 | case SRE_AT_BEGINNING_STRING: |
| 1715 | case SRE_AT_BEGINNING_LINE: |
| 1716 | case SRE_AT_END: |
| 1717 | case SRE_AT_END_LINE: |
| 1718 | case SRE_AT_END_STRING: |
| 1719 | case SRE_AT_BOUNDARY: |
| 1720 | case SRE_AT_NON_BOUNDARY: |
| 1721 | case SRE_AT_LOC_BOUNDARY: |
| 1722 | case SRE_AT_LOC_NON_BOUNDARY: |
| 1723 | case SRE_AT_UNI_BOUNDARY: |
| 1724 | case SRE_AT_UNI_NON_BOUNDARY: |
| 1725 | break; |
| 1726 | default: |
| 1727 | FAIL; |
| 1728 | } |
| 1729 | break; |
| 1730 | |
| 1731 | case SRE_OP_ANY: |
| 1732 | case SRE_OP_ANY_ALL: |
| 1733 | /* These have no operands */ |
| 1734 | break; |
| 1735 | |
| 1736 | case SRE_OP_IN: |
| 1737 | case SRE_OP_IN_IGNORE: |
| 1738 | GET_SKIP; |
| 1739 | /* Stop 1 before the end; we check the FAILURE below */ |
| 1740 | if (!_validate_charset(code, code+skip-2)) |
| 1741 | FAIL; |
| 1742 | if (code[skip-2] != SRE_OP_FAILURE) |
| 1743 | FAIL; |
| 1744 | code += skip-1; |
| 1745 | break; |
| 1746 | |
| 1747 | case SRE_OP_INFO: |
| 1748 | { |
| 1749 | /* A minimal info field is |
| 1750 | <INFO> <1=skip> <2=flags> <3=min> <4=max>; |
| 1751 | If SRE_INFO_PREFIX or SRE_INFO_CHARSET is in the flags, |
| 1752 | more follows. */ |
Ross Lagerwall | 88748d7 | 2012-03-06 21:48:57 +0200 | [diff] [blame] | 1753 | SRE_CODE flags, i; |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1754 | SRE_CODE *newcode; |
| 1755 | GET_SKIP; |
| 1756 | newcode = code+skip-1; |
| 1757 | GET_ARG; flags = arg; |
Ross Lagerwall | 88748d7 | 2012-03-06 21:48:57 +0200 | [diff] [blame] | 1758 | GET_ARG; |
| 1759 | GET_ARG; |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1760 | /* Check that only valid flags are present */ |
| 1761 | if ((flags & ~(SRE_INFO_PREFIX | |
| 1762 | SRE_INFO_LITERAL | |
| 1763 | SRE_INFO_CHARSET)) != 0) |
| 1764 | FAIL; |
| 1765 | /* PREFIX and CHARSET are mutually exclusive */ |
| 1766 | if ((flags & SRE_INFO_PREFIX) && |
| 1767 | (flags & SRE_INFO_CHARSET)) |
| 1768 | FAIL; |
| 1769 | /* LITERAL implies PREFIX */ |
| 1770 | if ((flags & SRE_INFO_LITERAL) && |
| 1771 | !(flags & SRE_INFO_PREFIX)) |
| 1772 | FAIL; |
| 1773 | /* Validate the prefix */ |
| 1774 | if (flags & SRE_INFO_PREFIX) { |
Ross Lagerwall | 88748d7 | 2012-03-06 21:48:57 +0200 | [diff] [blame] | 1775 | SRE_CODE prefix_len; |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1776 | GET_ARG; prefix_len = arg; |
Ross Lagerwall | 88748d7 | 2012-03-06 21:48:57 +0200 | [diff] [blame] | 1777 | GET_ARG; |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1778 | /* Here comes the prefix string */ |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1779 | if (prefix_len > (Py_uintptr_t)(newcode - code)) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1780 | FAIL; |
| 1781 | code += prefix_len; |
| 1782 | /* And here comes the overlap table */ |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1783 | if (prefix_len > (Py_uintptr_t)(newcode - code)) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1784 | FAIL; |
| 1785 | /* Each overlap value should be < prefix_len */ |
| 1786 | for (i = 0; i < prefix_len; i++) { |
| 1787 | if (code[i] >= prefix_len) |
| 1788 | FAIL; |
| 1789 | } |
| 1790 | code += prefix_len; |
| 1791 | } |
| 1792 | /* Validate the charset */ |
| 1793 | if (flags & SRE_INFO_CHARSET) { |
| 1794 | if (!_validate_charset(code, newcode-1)) |
| 1795 | FAIL; |
| 1796 | if (newcode[-1] != SRE_OP_FAILURE) |
| 1797 | FAIL; |
| 1798 | code = newcode; |
| 1799 | } |
| 1800 | else if (code != newcode) { |
| 1801 | VTRACE(("code=%p, newcode=%p\n", code, newcode)); |
| 1802 | FAIL; |
| 1803 | } |
| 1804 | } |
| 1805 | break; |
| 1806 | |
| 1807 | case SRE_OP_BRANCH: |
| 1808 | { |
| 1809 | SRE_CODE *target = NULL; |
| 1810 | for (;;) { |
| 1811 | GET_SKIP; |
| 1812 | if (skip == 0) |
| 1813 | break; |
| 1814 | /* Stop 2 before the end; we check the JUMP below */ |
| 1815 | if (!_validate_inner(code, code+skip-3, groups)) |
| 1816 | FAIL; |
| 1817 | code += skip-3; |
| 1818 | /* Check that it ends with a JUMP, and that each JUMP |
| 1819 | has the same target */ |
| 1820 | GET_OP; |
| 1821 | if (op != SRE_OP_JUMP) |
| 1822 | FAIL; |
| 1823 | GET_SKIP; |
| 1824 | if (target == NULL) |
| 1825 | target = code+skip-1; |
| 1826 | else if (code+skip-1 != target) |
| 1827 | FAIL; |
| 1828 | } |
| 1829 | } |
| 1830 | break; |
| 1831 | |
| 1832 | case SRE_OP_REPEAT_ONE: |
| 1833 | case SRE_OP_MIN_REPEAT_ONE: |
| 1834 | { |
| 1835 | SRE_CODE min, max; |
| 1836 | GET_SKIP; |
| 1837 | GET_ARG; min = arg; |
| 1838 | GET_ARG; max = arg; |
| 1839 | if (min > max) |
| 1840 | FAIL; |
Serhiy Storchaka | 70ca021 | 2013-02-16 16:47:47 +0200 | [diff] [blame] | 1841 | if (max > SRE_MAXREPEAT) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1842 | FAIL; |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1843 | if (!_validate_inner(code, code+skip-4, groups)) |
| 1844 | FAIL; |
| 1845 | code += skip-4; |
| 1846 | GET_OP; |
| 1847 | if (op != SRE_OP_SUCCESS) |
| 1848 | FAIL; |
| 1849 | } |
| 1850 | break; |
| 1851 | |
| 1852 | case SRE_OP_REPEAT: |
| 1853 | { |
| 1854 | SRE_CODE min, max; |
| 1855 | GET_SKIP; |
| 1856 | GET_ARG; min = arg; |
| 1857 | GET_ARG; max = arg; |
| 1858 | if (min > max) |
| 1859 | FAIL; |
Serhiy Storchaka | 70ca021 | 2013-02-16 16:47:47 +0200 | [diff] [blame] | 1860 | if (max > SRE_MAXREPEAT) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1861 | FAIL; |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1862 | if (!_validate_inner(code, code+skip-3, groups)) |
| 1863 | FAIL; |
| 1864 | code += skip-3; |
| 1865 | GET_OP; |
| 1866 | if (op != SRE_OP_MAX_UNTIL && op != SRE_OP_MIN_UNTIL) |
| 1867 | FAIL; |
| 1868 | } |
| 1869 | break; |
| 1870 | |
| 1871 | case SRE_OP_GROUPREF: |
| 1872 | case SRE_OP_GROUPREF_IGNORE: |
| 1873 | GET_ARG; |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1874 | if (arg >= (size_t)groups) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1875 | FAIL; |
| 1876 | break; |
| 1877 | |
| 1878 | case SRE_OP_GROUPREF_EXISTS: |
| 1879 | /* The regex syntax for this is: '(?(group)then|else)', where |
| 1880 | 'group' is either an integer group number or a group name, |
| 1881 | 'then' and 'else' are sub-regexes, and 'else' is optional. */ |
| 1882 | GET_ARG; |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1883 | if (arg >= (size_t)groups) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1884 | FAIL; |
Guido van Rossum | 92f8f3e | 2008-09-10 14:30:50 +0000 | [diff] [blame] | 1885 | GET_SKIP_ADJ(1); |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1886 | code--; /* The skip is relative to the first arg! */ |
| 1887 | /* There are two possibilities here: if there is both a 'then' |
| 1888 | part and an 'else' part, the generated code looks like: |
| 1889 | |
| 1890 | GROUPREF_EXISTS |
| 1891 | <group> |
| 1892 | <skipyes> |
| 1893 | ...then part... |
| 1894 | JUMP |
| 1895 | <skipno> |
| 1896 | (<skipyes> jumps here) |
| 1897 | ...else part... |
| 1898 | (<skipno> jumps here) |
| 1899 | |
| 1900 | If there is only a 'then' part, it looks like: |
| 1901 | |
| 1902 | GROUPREF_EXISTS |
| 1903 | <group> |
| 1904 | <skip> |
| 1905 | ...then part... |
| 1906 | (<skip> jumps here) |
| 1907 | |
| 1908 | There is no direct way to decide which it is, and we don't want |
| 1909 | to allow arbitrary jumps anywhere in the code; so we just look |
| 1910 | for a JUMP opcode preceding our skip target. |
| 1911 | */ |
Victor Stinner | 1fa174a | 2013-08-28 02:06:21 +0200 | [diff] [blame] | 1912 | if (skip >= 3 && skip-3 < (Py_uintptr_t)(end - code) && |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1913 | code[skip-3] == SRE_OP_JUMP) |
| 1914 | { |
| 1915 | VTRACE(("both then and else parts present\n")); |
| 1916 | if (!_validate_inner(code+1, code+skip-3, groups)) |
| 1917 | FAIL; |
| 1918 | code += skip-2; /* Position after JUMP, at <skipno> */ |
| 1919 | GET_SKIP; |
| 1920 | if (!_validate_inner(code, code+skip-1, groups)) |
| 1921 | FAIL; |
| 1922 | code += skip-1; |
| 1923 | } |
| 1924 | else { |
| 1925 | VTRACE(("only a then part present\n")); |
| 1926 | if (!_validate_inner(code+1, code+skip-1, groups)) |
| 1927 | FAIL; |
| 1928 | code += skip-1; |
| 1929 | } |
| 1930 | break; |
| 1931 | |
| 1932 | case SRE_OP_ASSERT: |
| 1933 | case SRE_OP_ASSERT_NOT: |
| 1934 | GET_SKIP; |
| 1935 | GET_ARG; /* 0 for lookahead, width for lookbehind */ |
| 1936 | code--; /* Back up over arg to simplify math below */ |
| 1937 | if (arg & 0x80000000) |
| 1938 | FAIL; /* Width too large */ |
| 1939 | /* Stop 1 before the end; we check the SUCCESS below */ |
| 1940 | if (!_validate_inner(code+1, code+skip-2, groups)) |
| 1941 | FAIL; |
| 1942 | code += skip-2; |
| 1943 | GET_OP; |
| 1944 | if (op != SRE_OP_SUCCESS) |
| 1945 | FAIL; |
| 1946 | break; |
| 1947 | |
| 1948 | default: |
| 1949 | FAIL; |
| 1950 | |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | VTRACE(("okay\n")); |
| 1955 | return 1; |
| 1956 | } |
| 1957 | |
| 1958 | static int |
| 1959 | _validate_outer(SRE_CODE *code, SRE_CODE *end, Py_ssize_t groups) |
| 1960 | { |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 1961 | if (groups < 0 || (size_t)groups > SRE_MAXGROUPS || |
| 1962 | code >= end || end[-1] != SRE_OP_SUCCESS) |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1963 | FAIL; |
Guido van Rossum | 10faf6a | 2008-08-06 19:29:14 +0000 | [diff] [blame] | 1964 | return _validate_inner(code, end-1, groups); |
| 1965 | } |
| 1966 | |
| 1967 | static int |
| 1968 | _validate(PatternObject *self) |
| 1969 | { |
| 1970 | if (!_validate_outer(self->code, self->code+self->codesize, self->groups)) |
| 1971 | { |
| 1972 | PyErr_SetString(PyExc_RuntimeError, "invalid SRE code"); |
| 1973 | return 0; |
| 1974 | } |
| 1975 | else |
| 1976 | VTRACE(("Success!\n")); |
| 1977 | return 1; |
| 1978 | } |
| 1979 | |
| 1980 | /* -------------------------------------------------------------------- */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1981 | /* match methods */ |
| 1982 | |
| 1983 | static void |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 1984 | match_dealloc(MatchObject* self) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1985 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 1986 | Py_XDECREF(self->regs); |
| 1987 | Py_XDECREF(self->string); |
| 1988 | Py_DECREF(self->pattern); |
| 1989 | PyObject_DEL(self); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | static PyObject* |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1993 | 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] | 1994 | { |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 1995 | Py_ssize_t length; |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 1996 | int isbytes, charsize; |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 1997 | Py_buffer view; |
| 1998 | PyObject *result; |
| 1999 | void* ptr; |
| 2000 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2001 | if (index < 0 || index >= self->groups) { |
| 2002 | /* raise IndexError if we were given a bad group number */ |
| 2003 | PyErr_SetString( |
| 2004 | PyExc_IndexError, |
| 2005 | "no such group" |
| 2006 | ); |
| 2007 | return NULL; |
| 2008 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2009 | |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2010 | index *= 2; |
| 2011 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2012 | if (self->string == Py_None || self->mark[index] < 0) { |
| 2013 | /* return default value if the string or group is undefined */ |
| 2014 | Py_INCREF(def); |
| 2015 | return def; |
| 2016 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2017 | |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 2018 | ptr = getstring(self->string, &length, &isbytes, &charsize, &view); |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 2019 | if (ptr == NULL) |
| 2020 | return NULL; |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 2021 | result = getslice(isbytes, ptr, |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 2022 | self->string, self->mark[index], self->mark[index+1]); |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 2023 | if (isbytes && view.buf != NULL) |
Serhiy Storchaka | 2532497 | 2013-10-16 12:46:28 +0300 | [diff] [blame] | 2024 | PyBuffer_Release(&view); |
| 2025 | return result; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2026 | } |
| 2027 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2028 | static Py_ssize_t |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2029 | match_getindex(MatchObject* self, PyObject* index) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2030 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2031 | Py_ssize_t i; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2032 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2033 | if (index == NULL) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2034 | /* Default value */ |
| 2035 | return 0; |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2036 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2037 | if (PyLong_Check(index)) |
| 2038 | return PyLong_AsSsize_t(index); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2039 | |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2040 | i = -1; |
| 2041 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2042 | if (self->pattern->groupindex) { |
| 2043 | index = PyObject_GetItem(self->pattern->groupindex, index); |
| 2044 | if (index) { |
Neal Norwitz | 1fe5f38 | 2007-08-31 04:32:55 +0000 | [diff] [blame] | 2045 | if (PyLong_Check(index)) |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2046 | i = PyLong_AsSsize_t(index); |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2047 | Py_DECREF(index); |
| 2048 | } else |
| 2049 | PyErr_Clear(); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2050 | } |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 2051 | |
| 2052 | return i; |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
| 2055 | static PyObject* |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 2056 | match_getslice(MatchObject* self, PyObject* index, PyObject* def) |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2057 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2058 | return match_getslice_by_index(self, match_getindex(self, index), def); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | static PyObject* |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2062 | match_expand(MatchObject* self, PyObject* ptemplate) |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 2063 | { |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 2064 | /* delegate to Python code */ |
| 2065 | return call( |
Thomas Wouters | 9ada3d6 | 2006-04-21 09:47:09 +0000 | [diff] [blame] | 2066 | SRE_PY_MODULE, "_expand", |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2067 | PyTuple_Pack(3, self->pattern, self, ptemplate) |
Fredrik Lundh | 5644b7f | 2000-09-21 17:03:25 +0000 | [diff] [blame] | 2068 | ); |
| 2069 | } |
| 2070 | |
| 2071 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2072 | match_group(MatchObject* self, PyObject* args) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2073 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2074 | PyObject* result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2075 | Py_ssize_t i, size; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2076 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2077 | size = PyTuple_GET_SIZE(args); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2078 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2079 | switch (size) { |
| 2080 | case 0: |
| 2081 | result = match_getslice(self, Py_False, Py_None); |
| 2082 | break; |
| 2083 | case 1: |
| 2084 | result = match_getslice(self, PyTuple_GET_ITEM(args, 0), Py_None); |
| 2085 | break; |
| 2086 | default: |
| 2087 | /* fetch multiple items */ |
| 2088 | result = PyTuple_New(size); |
| 2089 | if (!result) |
| 2090 | return NULL; |
| 2091 | for (i = 0; i < size; i++) { |
| 2092 | PyObject* item = match_getslice( |
Fredrik Lundh | df02d0b | 2000-06-30 07:08:20 +0000 | [diff] [blame] | 2093 | self, PyTuple_GET_ITEM(args, i), Py_None |
| 2094 | ); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2095 | if (!item) { |
| 2096 | Py_DECREF(result); |
| 2097 | return NULL; |
| 2098 | } |
| 2099 | PyTuple_SET_ITEM(result, i, item); |
| 2100 | } |
| 2101 | break; |
| 2102 | } |
| 2103 | return result; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | static PyObject* |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 2107 | match_groups(MatchObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2108 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2109 | PyObject* result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2110 | Py_ssize_t index; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2111 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2112 | PyObject* def = Py_None; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 2113 | static char* kwlist[] = { "default", NULL }; |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 2114 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:groups", kwlist, &def)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2115 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2116 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2117 | result = PyTuple_New(self->groups-1); |
| 2118 | if (!result) |
| 2119 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2120 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2121 | for (index = 1; index < self->groups; index++) { |
| 2122 | PyObject* item; |
| 2123 | item = match_getslice_by_index(self, index, def); |
| 2124 | if (!item) { |
| 2125 | Py_DECREF(result); |
| 2126 | return NULL; |
| 2127 | } |
| 2128 | PyTuple_SET_ITEM(result, index-1, item); |
| 2129 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2130 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2131 | return result; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | static PyObject* |
Fredrik Lundh | 562586e | 2000-10-03 20:43:34 +0000 | [diff] [blame] | 2135 | match_groupdict(MatchObject* self, PyObject* args, PyObject* kw) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2136 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2137 | PyObject* result; |
| 2138 | PyObject* keys; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2139 | Py_ssize_t index; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2140 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2141 | PyObject* def = Py_None; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 2142 | static char* kwlist[] = { "default", NULL }; |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2143 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|O:groupdict", kwlist, &def)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2144 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2145 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2146 | result = PyDict_New(); |
| 2147 | if (!result || !self->pattern->groupindex) |
| 2148 | return result; |
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 | keys = PyMapping_Keys(self->pattern->groupindex); |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2151 | if (!keys) |
| 2152 | goto failed; |
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 | for (index = 0; index < PyList_GET_SIZE(keys); index++) { |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2155 | int status; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2156 | PyObject* key; |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2157 | PyObject* value; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2158 | key = PyList_GET_ITEM(keys, index); |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2159 | if (!key) |
| 2160 | goto failed; |
| 2161 | value = match_getslice(self, key, def); |
| 2162 | if (!value) { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2163 | Py_DECREF(key); |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2164 | goto failed; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2165 | } |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2166 | status = PyDict_SetItem(result, key, value); |
| 2167 | Py_DECREF(value); |
| 2168 | if (status < 0) |
| 2169 | goto failed; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2170 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2171 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2172 | Py_DECREF(keys); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2173 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2174 | return result; |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2175 | |
| 2176 | failed: |
Neal Norwitz | 60da316 | 2006-03-07 04:48:24 +0000 | [diff] [blame] | 2177 | Py_XDECREF(keys); |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 2178 | Py_DECREF(result); |
| 2179 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
| 2182 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2183 | match_start(MatchObject* self, PyObject* args) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2184 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2185 | Py_ssize_t index; |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2186 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2187 | PyObject* index_ = NULL; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2188 | if (!PyArg_UnpackTuple(args, "start", 0, 1, &index_)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2189 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2190 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2191 | index = match_getindex(self, index_); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2192 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2193 | if (index < 0 || index >= self->groups) { |
| 2194 | PyErr_SetString( |
| 2195 | PyExc_IndexError, |
| 2196 | "no such group" |
| 2197 | ); |
| 2198 | return NULL; |
| 2199 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2200 | |
Fredrik Lundh | 510c97b | 2000-09-02 16:36:57 +0000 | [diff] [blame] | 2201 | /* mark is -1 if group is undefined */ |
Antoine Pitrou | 43fb54c | 2012-12-02 12:52:36 +0100 | [diff] [blame] | 2202 | return PyLong_FromSsize_t(self->mark[index*2]); |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2203 | } |
| 2204 | |
| 2205 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2206 | match_end(MatchObject* self, PyObject* args) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2207 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2208 | Py_ssize_t index; |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2209 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2210 | PyObject* index_ = NULL; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2211 | if (!PyArg_UnpackTuple(args, "end", 0, 1, &index_)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2212 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2213 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2214 | index = match_getindex(self, index_); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2215 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2216 | if (index < 0 || index >= self->groups) { |
| 2217 | PyErr_SetString( |
| 2218 | PyExc_IndexError, |
| 2219 | "no such group" |
| 2220 | ); |
| 2221 | return NULL; |
| 2222 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2223 | |
Fredrik Lundh | 510c97b | 2000-09-02 16:36:57 +0000 | [diff] [blame] | 2224 | /* mark is -1 if group is undefined */ |
Antoine Pitrou | 43fb54c | 2012-12-02 12:52:36 +0100 | [diff] [blame] | 2225 | return PyLong_FromSsize_t(self->mark[index*2+1]); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
| 2228 | LOCAL(PyObject*) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2229 | _pair(Py_ssize_t i1, Py_ssize_t i2) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2230 | { |
| 2231 | PyObject* pair; |
| 2232 | PyObject* item; |
| 2233 | |
| 2234 | pair = PyTuple_New(2); |
| 2235 | if (!pair) |
| 2236 | return NULL; |
| 2237 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2238 | item = PyLong_FromSsize_t(i1); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2239 | if (!item) |
| 2240 | goto error; |
| 2241 | PyTuple_SET_ITEM(pair, 0, item); |
| 2242 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2243 | item = PyLong_FromSsize_t(i2); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2244 | if (!item) |
| 2245 | goto error; |
| 2246 | PyTuple_SET_ITEM(pair, 1, item); |
| 2247 | |
| 2248 | return pair; |
| 2249 | |
| 2250 | error: |
| 2251 | Py_DECREF(pair); |
| 2252 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | static PyObject* |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2256 | match_span(MatchObject* self, PyObject* args) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2257 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2258 | Py_ssize_t index; |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2259 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 2260 | PyObject* index_ = NULL; |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2261 | if (!PyArg_UnpackTuple(args, "span", 0, 1, &index_)) |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2262 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2263 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2264 | index = match_getindex(self, index_); |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2265 | |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2266 | if (index < 0 || index >= self->groups) { |
| 2267 | PyErr_SetString( |
| 2268 | PyExc_IndexError, |
| 2269 | "no such group" |
| 2270 | ); |
| 2271 | return NULL; |
| 2272 | } |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2273 | |
Fredrik Lundh | 510c97b | 2000-09-02 16:36:57 +0000 | [diff] [blame] | 2274 | /* marks are -1 if group is undefined */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2275 | return _pair(self->mark[index*2], self->mark[index*2+1]); |
| 2276 | } |
| 2277 | |
| 2278 | static PyObject* |
| 2279 | match_regs(MatchObject* self) |
| 2280 | { |
| 2281 | PyObject* regs; |
| 2282 | PyObject* item; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2283 | Py_ssize_t index; |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2284 | |
| 2285 | regs = PyTuple_New(self->groups); |
| 2286 | if (!regs) |
| 2287 | return NULL; |
| 2288 | |
| 2289 | for (index = 0; index < self->groups; index++) { |
| 2290 | item = _pair(self->mark[index*2], self->mark[index*2+1]); |
| 2291 | if (!item) { |
| 2292 | Py_DECREF(regs); |
| 2293 | return NULL; |
| 2294 | } |
| 2295 | PyTuple_SET_ITEM(regs, index, item); |
| 2296 | } |
| 2297 | |
| 2298 | Py_INCREF(regs); |
| 2299 | self->regs = regs; |
| 2300 | |
| 2301 | return regs; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2304 | static PyObject* |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2305 | match_copy(MatchObject* self, PyObject *unused) |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2306 | { |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2307 | #ifdef USE_BUILTIN_COPY |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2308 | MatchObject* copy; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2309 | Py_ssize_t slots, offset; |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 2310 | |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2311 | slots = 2 * (self->pattern->groups+1); |
| 2312 | |
| 2313 | copy = PyObject_NEW_VAR(MatchObject, &Match_Type, slots); |
| 2314 | if (!copy) |
| 2315 | return NULL; |
| 2316 | |
| 2317 | /* this value a constant, but any compiler should be able to |
| 2318 | figure that out all by itself */ |
| 2319 | offset = offsetof(MatchObject, string); |
| 2320 | |
| 2321 | Py_XINCREF(self->pattern); |
| 2322 | Py_XINCREF(self->string); |
| 2323 | Py_XINCREF(self->regs); |
| 2324 | |
| 2325 | memcpy((char*) copy + offset, (char*) self + offset, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2326 | sizeof(MatchObject) + slots * sizeof(Py_ssize_t) - offset); |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2327 | |
| 2328 | return (PyObject*) copy; |
| 2329 | #else |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2330 | PyErr_SetString(PyExc_TypeError, "cannot copy this match object"); |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2331 | return NULL; |
| 2332 | #endif |
| 2333 | } |
| 2334 | |
| 2335 | static PyObject* |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2336 | match_deepcopy(MatchObject* self, PyObject* memo) |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2337 | { |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2338 | #ifdef USE_BUILTIN_COPY |
| 2339 | MatchObject* copy; |
Tim Peters | 3d56350 | 2006-01-21 02:47:53 +0000 | [diff] [blame] | 2340 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2341 | copy = (MatchObject*) match_copy(self); |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2342 | if (!copy) |
| 2343 | return NULL; |
| 2344 | |
| 2345 | if (!deepcopy((PyObject**) ©->pattern, memo) || |
| 2346 | !deepcopy(©->string, memo) || |
| 2347 | !deepcopy(©->regs, memo)) { |
| 2348 | Py_DECREF(copy); |
| 2349 | return NULL; |
| 2350 | } |
| 2351 | |
| 2352 | #else |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2353 | PyErr_SetString(PyExc_TypeError, "cannot deepcopy this match object"); |
| 2354 | return NULL; |
Fredrik Lundh | d89a2e7 | 2001-07-03 20:32:36 +0000 | [diff] [blame] | 2355 | #endif |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
Andrew Svetlov | 56ad5ed | 2012-12-23 19:23:07 +0200 | [diff] [blame] | 2358 | PyDoc_STRVAR(match_doc, |
| 2359 | "The result of re.match() and re.search().\n\ |
| 2360 | Match objects always have a boolean value of True."); |
| 2361 | |
| 2362 | PyDoc_STRVAR(match_group_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 2363 | "group([group1, ...]) -> str or tuple.\n\ |
Andrew Svetlov | 56ad5ed | 2012-12-23 19:23:07 +0200 | [diff] [blame] | 2364 | Return subgroup(s) of the match by indices or names.\n\ |
| 2365 | For 0 returns the entire match."); |
| 2366 | |
| 2367 | PyDoc_STRVAR(match_start_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 2368 | "start([group=0]) -> int.\n\ |
Andrew Svetlov | 56ad5ed | 2012-12-23 19:23:07 +0200 | [diff] [blame] | 2369 | Return index of the start of the substring matched by group."); |
| 2370 | |
| 2371 | PyDoc_STRVAR(match_end_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 2372 | "end([group=0]) -> int.\n\ |
Andrew Svetlov | 56ad5ed | 2012-12-23 19:23:07 +0200 | [diff] [blame] | 2373 | Return index of the end of the substring matched by group."); |
| 2374 | |
| 2375 | PyDoc_STRVAR(match_span_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 2376 | "span([group]) -> tuple.\n\ |
Andrew Svetlov | 56ad5ed | 2012-12-23 19:23:07 +0200 | [diff] [blame] | 2377 | For MatchObject m, return the 2-tuple (m.start(group), m.end(group))."); |
| 2378 | |
| 2379 | PyDoc_STRVAR(match_groups_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 2380 | "groups([default=None]) -> tuple.\n\ |
Andrew Svetlov | 56ad5ed | 2012-12-23 19:23:07 +0200 | [diff] [blame] | 2381 | Return a tuple containing all the subgroups of the match, from 1.\n\ |
| 2382 | The default argument is used for groups\n\ |
| 2383 | that did not participate in the match"); |
| 2384 | |
| 2385 | PyDoc_STRVAR(match_groupdict_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 2386 | "groupdict([default=None]) -> dict.\n\ |
Andrew Svetlov | 56ad5ed | 2012-12-23 19:23:07 +0200 | [diff] [blame] | 2387 | Return a dictionary containing all the named subgroups of the match,\n\ |
| 2388 | keyed by the subgroup name. The default argument is used for groups\n\ |
| 2389 | that did not participate in the match"); |
| 2390 | |
| 2391 | PyDoc_STRVAR(match_expand_doc, |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 2392 | "expand(template) -> str.\n\ |
Andrew Svetlov | 56ad5ed | 2012-12-23 19:23:07 +0200 | [diff] [blame] | 2393 | Return the string obtained by doing backslash substitution\n\ |
| 2394 | on the string template, as done by the sub() method."); |
| 2395 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2396 | static PyMethodDef match_methods[] = { |
Andrew Svetlov | 56ad5ed | 2012-12-23 19:23:07 +0200 | [diff] [blame] | 2397 | {"group", (PyCFunction) match_group, METH_VARARGS, match_group_doc}, |
| 2398 | {"start", (PyCFunction) match_start, METH_VARARGS, match_start_doc}, |
| 2399 | {"end", (PyCFunction) match_end, METH_VARARGS, match_end_doc}, |
| 2400 | {"span", (PyCFunction) match_span, METH_VARARGS, match_span_doc}, |
| 2401 | {"groups", (PyCFunction) match_groups, METH_VARARGS|METH_KEYWORDS, |
| 2402 | match_groups_doc}, |
| 2403 | {"groupdict", (PyCFunction) match_groupdict, METH_VARARGS|METH_KEYWORDS, |
| 2404 | match_groupdict_doc}, |
| 2405 | {"expand", (PyCFunction) match_expand, METH_O, match_expand_doc}, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2406 | {"__copy__", (PyCFunction) match_copy, METH_NOARGS}, |
| 2407 | {"__deepcopy__", (PyCFunction) match_deepcopy, METH_O}, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2408 | {NULL, NULL} |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2409 | }; |
| 2410 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 2411 | static PyObject * |
| 2412 | match_lastindex_get(MatchObject *self) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2413 | { |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 2414 | if (self->lastindex >= 0) |
Antoine Pitrou | 43fb54c | 2012-12-02 12:52:36 +0100 | [diff] [blame] | 2415 | return PyLong_FromSsize_t(self->lastindex); |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 2416 | Py_INCREF(Py_None); |
| 2417 | return Py_None; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2418 | } |
| 2419 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 2420 | static PyObject * |
| 2421 | match_lastgroup_get(MatchObject *self) |
| 2422 | { |
| 2423 | if (self->pattern->indexgroup && self->lastindex >= 0) { |
| 2424 | PyObject* result = PySequence_GetItem( |
| 2425 | self->pattern->indexgroup, self->lastindex |
| 2426 | ); |
| 2427 | if (result) |
| 2428 | return result; |
| 2429 | PyErr_Clear(); |
| 2430 | } |
| 2431 | Py_INCREF(Py_None); |
| 2432 | return Py_None; |
| 2433 | } |
| 2434 | |
| 2435 | static PyObject * |
| 2436 | match_regs_get(MatchObject *self) |
| 2437 | { |
| 2438 | if (self->regs) { |
| 2439 | Py_INCREF(self->regs); |
| 2440 | return self->regs; |
| 2441 | } else |
| 2442 | return match_regs(self); |
| 2443 | } |
| 2444 | |
Serhiy Storchaka | 36af10c | 2013-10-20 13:13:31 +0300 | [diff] [blame] | 2445 | static PyObject * |
| 2446 | match_repr(MatchObject *self) |
| 2447 | { |
| 2448 | PyObject *result; |
| 2449 | PyObject *group0 = match_getslice_by_index(self, 0, Py_None); |
| 2450 | if (group0 == NULL) |
| 2451 | return NULL; |
| 2452 | result = PyUnicode_FromFormat( |
| 2453 | "<%s object; span=(%d, %d), match=%.50R>", |
| 2454 | Py_TYPE(self)->tp_name, |
| 2455 | self->mark[0], self->mark[1], group0); |
| 2456 | Py_DECREF(group0); |
| 2457 | return result; |
| 2458 | } |
| 2459 | |
| 2460 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 2461 | static PyGetSetDef match_getset[] = { |
| 2462 | {"lastindex", (getter)match_lastindex_get, (setter)NULL}, |
| 2463 | {"lastgroup", (getter)match_lastgroup_get, (setter)NULL}, |
| 2464 | {"regs", (getter)match_regs_get, (setter)NULL}, |
| 2465 | {NULL} |
| 2466 | }; |
| 2467 | |
| 2468 | #define MATCH_OFF(x) offsetof(MatchObject, x) |
| 2469 | static PyMemberDef match_members[] = { |
| 2470 | {"string", T_OBJECT, MATCH_OFF(string), READONLY}, |
| 2471 | {"re", T_OBJECT, MATCH_OFF(pattern), READONLY}, |
| 2472 | {"pos", T_PYSSIZET, MATCH_OFF(pos), READONLY}, |
| 2473 | {"endpos", T_PYSSIZET, MATCH_OFF(endpos), READONLY}, |
| 2474 | {NULL} |
| 2475 | }; |
| 2476 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2477 | /* FIXME: implement setattr("string", None) as a special case (to |
| 2478 | detach the associated string, if any */ |
| 2479 | |
Neal Norwitz | 57c179c | 2006-03-22 07:18:02 +0000 | [diff] [blame] | 2480 | static PyTypeObject Match_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2481 | PyVarObject_HEAD_INIT(NULL,0) |
| 2482 | "_" SRE_MODULE ".SRE_Match", |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2483 | sizeof(MatchObject), sizeof(Py_ssize_t), |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2484 | (destructor)match_dealloc, /* tp_dealloc */ |
| 2485 | 0, /* tp_print */ |
| 2486 | 0, /* tp_getattr */ |
| 2487 | 0, /* tp_setattr */ |
| 2488 | 0, /* tp_reserved */ |
Serhiy Storchaka | 36af10c | 2013-10-20 13:13:31 +0300 | [diff] [blame] | 2489 | (reprfunc)match_repr, /* tp_repr */ |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2490 | 0, /* tp_as_number */ |
| 2491 | 0, /* tp_as_sequence */ |
| 2492 | 0, /* tp_as_mapping */ |
| 2493 | 0, /* tp_hash */ |
| 2494 | 0, /* tp_call */ |
| 2495 | 0, /* tp_str */ |
| 2496 | 0, /* tp_getattro */ |
| 2497 | 0, /* tp_setattro */ |
| 2498 | 0, /* tp_as_buffer */ |
| 2499 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Andrew Svetlov | 70dcef4 | 2012-12-23 19:59:27 +0200 | [diff] [blame] | 2500 | match_doc, /* tp_doc */ |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2501 | 0, /* tp_traverse */ |
| 2502 | 0, /* tp_clear */ |
| 2503 | 0, /* tp_richcompare */ |
| 2504 | 0, /* tp_weaklistoffset */ |
| 2505 | 0, /* tp_iter */ |
| 2506 | 0, /* tp_iternext */ |
| 2507 | match_methods, /* tp_methods */ |
| 2508 | match_members, /* tp_members */ |
| 2509 | match_getset, /* tp_getset */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2510 | }; |
| 2511 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2512 | static PyObject* |
Victor Stinner | f558778 | 2013-11-15 23:21:11 +0100 | [diff] [blame] | 2513 | pattern_new_match(PatternObject* pattern, SRE_STATE* state, Py_ssize_t status) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2514 | { |
| 2515 | /* create match object (from state object) */ |
| 2516 | |
| 2517 | MatchObject* match; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2518 | Py_ssize_t i, j; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2519 | char* base; |
| 2520 | int n; |
| 2521 | |
| 2522 | if (status > 0) { |
| 2523 | |
| 2524 | /* create match object (with room for extra group marks) */ |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 2525 | /* coverity[ampersand_in_size] */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2526 | match = PyObject_NEW_VAR(MatchObject, &Match_Type, |
| 2527 | 2*(pattern->groups+1)); |
| 2528 | if (!match) |
| 2529 | return NULL; |
| 2530 | |
| 2531 | Py_INCREF(pattern); |
| 2532 | match->pattern = pattern; |
| 2533 | |
| 2534 | Py_INCREF(state->string); |
| 2535 | match->string = state->string; |
| 2536 | |
| 2537 | match->regs = NULL; |
| 2538 | match->groups = pattern->groups+1; |
| 2539 | |
| 2540 | /* fill in group slices */ |
| 2541 | |
| 2542 | base = (char*) state->beginning; |
| 2543 | n = state->charsize; |
| 2544 | |
| 2545 | match->mark[0] = ((char*) state->start - base) / n; |
| 2546 | match->mark[1] = ((char*) state->ptr - base) / n; |
| 2547 | |
| 2548 | for (i = j = 0; i < pattern->groups; i++, j+=2) |
| 2549 | if (j+1 <= state->lastmark && state->mark[j] && state->mark[j+1]) { |
| 2550 | match->mark[j+2] = ((char*) state->mark[j] - base) / n; |
| 2551 | match->mark[j+3] = ((char*) state->mark[j+1] - base) / n; |
| 2552 | } else |
| 2553 | match->mark[j+2] = match->mark[j+3] = -1; /* undefined */ |
| 2554 | |
| 2555 | match->pos = state->pos; |
| 2556 | match->endpos = state->endpos; |
| 2557 | |
| 2558 | match->lastindex = state->lastindex; |
| 2559 | |
| 2560 | return (PyObject*) match; |
| 2561 | |
| 2562 | } else if (status == 0) { |
| 2563 | |
| 2564 | /* no match */ |
| 2565 | Py_INCREF(Py_None); |
| 2566 | return Py_None; |
| 2567 | |
| 2568 | } |
| 2569 | |
| 2570 | /* internal error */ |
| 2571 | pattern_error(status); |
| 2572 | return NULL; |
| 2573 | } |
| 2574 | |
| 2575 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2576 | /* -------------------------------------------------------------------- */ |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 2577 | /* scanner methods (experimental) */ |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2578 | |
| 2579 | static void |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 2580 | scanner_dealloc(ScannerObject* self) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2581 | { |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2582 | state_fini(&self->state); |
Antoine Pitrou | 82feb1f | 2010-01-14 17:34:48 +0000 | [diff] [blame] | 2583 | Py_XDECREF(self->pattern); |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2584 | PyObject_DEL(self); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | static PyObject* |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2588 | scanner_match(ScannerObject* self, PyObject *unused) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2589 | { |
| 2590 | SRE_STATE* state = &self->state; |
| 2591 | PyObject* match; |
Victor Stinner | 7a6d7cf | 2012-10-31 00:37:41 +0100 | [diff] [blame] | 2592 | Py_ssize_t status; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2593 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 2594 | state_reset(state); |
| 2595 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2596 | state->ptr = state->start; |
| 2597 | |
Serhiy Storchaka | 429b59e | 2014-05-14 21:48:17 +0300 | [diff] [blame] | 2598 | status = sre_match(state, PatternObject_GetCode(self->pattern), 0); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2599 | if (PyErr_Occurred()) |
| 2600 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2601 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2602 | match = pattern_new_match((PatternObject*) self->pattern, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2603 | state, status); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2604 | |
Gustavo Niemeyer | 0506c64 | 2004-09-03 18:11:59 +0000 | [diff] [blame] | 2605 | if (status == 0 || state->ptr == state->start) |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2606 | state->start = (void*) ((char*) state->ptr + state->charsize); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2607 | else |
Fredrik Lundh | 436c3d58 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 2608 | state->start = state->ptr; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2609 | |
| 2610 | return match; |
| 2611 | } |
| 2612 | |
| 2613 | |
| 2614 | static PyObject* |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2615 | scanner_search(ScannerObject* self, PyObject *unused) |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2616 | { |
| 2617 | SRE_STATE* state = &self->state; |
| 2618 | PyObject* match; |
Victor Stinner | 7a6d7cf | 2012-10-31 00:37:41 +0100 | [diff] [blame] | 2619 | Py_ssize_t status; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2620 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 2621 | state_reset(state); |
| 2622 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2623 | state->ptr = state->start; |
| 2624 | |
Serhiy Storchaka | 9eabac6 | 2013-10-26 10:45:48 +0300 | [diff] [blame] | 2625 | status = sre_search(state, PatternObject_GetCode(self->pattern)); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 2626 | if (PyErr_Occurred()) |
| 2627 | return NULL; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2628 | |
Fredrik Lundh | 75f2d67 | 2000-06-29 11:34:28 +0000 | [diff] [blame] | 2629 | match = pattern_new_match((PatternObject*) self->pattern, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2630 | state, status); |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2631 | |
Gustavo Niemeyer | 0506c64 | 2004-09-03 18:11:59 +0000 | [diff] [blame] | 2632 | if (status == 0 || state->ptr == state->start) |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 2633 | state->start = (void*) ((char*) state->ptr + state->charsize); |
| 2634 | else |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2635 | state->start = state->ptr; |
| 2636 | |
| 2637 | return match; |
| 2638 | } |
| 2639 | |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 2640 | static PyMethodDef scanner_methods[] = { |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2641 | {"match", (PyCFunction) scanner_match, METH_NOARGS}, |
| 2642 | {"search", (PyCFunction) scanner_search, METH_NOARGS}, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2643 | {NULL, NULL} |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2644 | }; |
| 2645 | |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 2646 | #define SCAN_OFF(x) offsetof(ScannerObject, x) |
| 2647 | static PyMemberDef scanner_members[] = { |
Ezio Melotti | 7c8c1ea | 2011-09-29 01:00:19 +0300 | [diff] [blame] | 2648 | {"pattern", T_OBJECT, SCAN_OFF(pattern), READONLY}, |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 2649 | {NULL} /* Sentinel */ |
| 2650 | }; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2651 | |
Neal Norwitz | 57c179c | 2006-03-22 07:18:02 +0000 | [diff] [blame] | 2652 | static PyTypeObject Scanner_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 2653 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2654 | "_" SRE_MODULE ".SRE_Scanner", |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2655 | sizeof(ScannerObject), 0, |
Amaury Forgeot d'Arc | e43d33a | 2008-07-02 20:50:16 +0000 | [diff] [blame] | 2656 | (destructor)scanner_dealloc,/* tp_dealloc */ |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2657 | 0, /* tp_print */ |
| 2658 | 0, /* tp_getattr */ |
| 2659 | 0, /* tp_setattr */ |
| 2660 | 0, /* tp_reserved */ |
| 2661 | 0, /* tp_repr */ |
| 2662 | 0, /* tp_as_number */ |
| 2663 | 0, /* tp_as_sequence */ |
| 2664 | 0, /* tp_as_mapping */ |
| 2665 | 0, /* tp_hash */ |
| 2666 | 0, /* tp_call */ |
| 2667 | 0, /* tp_str */ |
| 2668 | 0, /* tp_getattro */ |
| 2669 | 0, /* tp_setattro */ |
| 2670 | 0, /* tp_as_buffer */ |
| 2671 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 2672 | 0, /* tp_doc */ |
| 2673 | 0, /* tp_traverse */ |
| 2674 | 0, /* tp_clear */ |
| 2675 | 0, /* tp_richcompare */ |
| 2676 | 0, /* tp_weaklistoffset */ |
| 2677 | 0, /* tp_iter */ |
| 2678 | 0, /* tp_iternext */ |
| 2679 | scanner_methods, /* tp_methods */ |
| 2680 | scanner_members, /* tp_members */ |
| 2681 | 0, /* tp_getset */ |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 2682 | }; |
| 2683 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2684 | static PyObject* |
Sean Reifschneider | 7b3c975 | 2012-03-12 18:22:38 -0600 | [diff] [blame] | 2685 | pattern_scanner(PatternObject* pattern, PyObject* args, PyObject* kw) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2686 | { |
| 2687 | /* create search state object */ |
| 2688 | |
| 2689 | ScannerObject* self; |
| 2690 | |
Serhiy Storchaka | ccdf352 | 2014-03-06 11:28:32 +0200 | [diff] [blame] | 2691 | PyObject *string = NULL, *string2 = NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2692 | Py_ssize_t start = 0; |
| 2693 | Py_ssize_t end = PY_SSIZE_T_MAX; |
Serhiy Storchaka | ccdf352 | 2014-03-06 11:28:32 +0200 | [diff] [blame] | 2694 | static char* kwlist[] = { "string", "pos", "endpos", "source", NULL }; |
| 2695 | if (!PyArg_ParseTupleAndKeywords(args, kw, "|Onn$O:scanner", kwlist, |
| 2696 | &string, &start, &end, &string2)) |
| 2697 | return NULL; |
| 2698 | |
| 2699 | string = fix_string_param(string, string2, "source"); |
| 2700 | if (!string) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2701 | return NULL; |
| 2702 | |
| 2703 | /* create scanner object */ |
| 2704 | self = PyObject_NEW(ScannerObject, &Scanner_Type); |
| 2705 | if (!self) |
| 2706 | return NULL; |
Antoine Pitrou | 82feb1f | 2010-01-14 17:34:48 +0000 | [diff] [blame] | 2707 | self->pattern = NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2708 | |
| 2709 | string = state_init(&self->state, pattern, string, start, end); |
| 2710 | if (!string) { |
Antoine Pitrou | 82feb1f | 2010-01-14 17:34:48 +0000 | [diff] [blame] | 2711 | Py_DECREF(self); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 2712 | return NULL; |
| 2713 | } |
| 2714 | |
| 2715 | Py_INCREF(pattern); |
| 2716 | self->pattern = (PyObject*) pattern; |
| 2717 | |
| 2718 | return (PyObject*) self; |
| 2719 | } |
| 2720 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2721 | static PyMethodDef _functions[] = { |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 2722 | {"compile", _compile, METH_VARARGS}, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2723 | {"getcodesize", sre_codesize, METH_NOARGS}, |
Neal Norwitz | b049325 | 2002-03-31 14:44:22 +0000 | [diff] [blame] | 2724 | {"getlower", sre_getlower, METH_VARARGS}, |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 2725 | {NULL, NULL} |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2726 | }; |
| 2727 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2728 | static struct PyModuleDef sremodule = { |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2729 | PyModuleDef_HEAD_INIT, |
| 2730 | "_" SRE_MODULE, |
| 2731 | NULL, |
| 2732 | -1, |
| 2733 | _functions, |
| 2734 | NULL, |
| 2735 | NULL, |
| 2736 | NULL, |
| 2737 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2738 | }; |
| 2739 | |
| 2740 | PyMODINIT_FUNC PyInit__sre(void) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2741 | { |
Fredrik Lundh | b35ffc0 | 2001-01-15 12:46:09 +0000 | [diff] [blame] | 2742 | PyObject* m; |
| 2743 | PyObject* d; |
Barry Warsaw | 214a0b13 | 2001-08-16 20:33:48 +0000 | [diff] [blame] | 2744 | PyObject* x; |
Fredrik Lundh | b35ffc0 | 2001-01-15 12:46:09 +0000 | [diff] [blame] | 2745 | |
Benjamin Peterson | 08bf91c | 2010-04-11 16:12:57 +0000 | [diff] [blame] | 2746 | /* Patch object types */ |
| 2747 | if (PyType_Ready(&Pattern_Type) || PyType_Ready(&Match_Type) || |
| 2748 | PyType_Ready(&Scanner_Type)) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2749 | return NULL; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2750 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2751 | m = PyModule_Create(&sremodule); |
Neal Norwitz | 1ac754f | 2006-01-19 06:09:39 +0000 | [diff] [blame] | 2752 | if (m == NULL) |
Ezio Melotti | 2aa2b3b | 2011-09-29 00:58:57 +0300 | [diff] [blame] | 2753 | return NULL; |
Fredrik Lundh | b35ffc0 | 2001-01-15 12:46:09 +0000 | [diff] [blame] | 2754 | d = PyModule_GetDict(m); |
| 2755 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2756 | x = PyLong_FromLong(SRE_MAGIC); |
Fredrik Lundh | 21009b9 | 2001-09-18 18:47:09 +0000 | [diff] [blame] | 2757 | if (x) { |
| 2758 | PyDict_SetItemString(d, "MAGIC", x); |
| 2759 | Py_DECREF(x); |
| 2760 | } |
Fredrik Lundh | 9c7eab8 | 2001-04-15 19:00:58 +0000 | [diff] [blame] | 2761 | |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 2762 | x = PyLong_FromLong(sizeof(SRE_CODE)); |
Martin v. Löwis | 78e2f06 | 2003-04-19 12:56:08 +0000 | [diff] [blame] | 2763 | if (x) { |
| 2764 | PyDict_SetItemString(d, "CODESIZE", x); |
| 2765 | Py_DECREF(x); |
| 2766 | } |
| 2767 | |
Serhiy Storchaka | 70ca021 | 2013-02-16 16:47:47 +0200 | [diff] [blame] | 2768 | x = PyLong_FromUnsignedLong(SRE_MAXREPEAT); |
| 2769 | if (x) { |
| 2770 | PyDict_SetItemString(d, "MAXREPEAT", x); |
| 2771 | Py_DECREF(x); |
| 2772 | } |
| 2773 | |
Serhiy Storchaka | 9baa5b2 | 2014-09-29 22:49:23 +0300 | [diff] [blame] | 2774 | x = PyLong_FromUnsignedLong(SRE_MAXGROUPS); |
| 2775 | if (x) { |
| 2776 | PyDict_SetItemString(d, "MAXGROUPS", x); |
| 2777 | Py_DECREF(x); |
| 2778 | } |
| 2779 | |
Neal Norwitz | fe53713 | 2007-08-26 03:55:15 +0000 | [diff] [blame] | 2780 | x = PyUnicode_FromString(copyright); |
Fredrik Lundh | 21009b9 | 2001-09-18 18:47:09 +0000 | [diff] [blame] | 2781 | if (x) { |
| 2782 | PyDict_SetItemString(d, "copyright", x); |
| 2783 | Py_DECREF(x); |
| 2784 | } |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 2785 | return m; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
Gustavo Niemeyer | be733ee | 2003-04-20 07:35:44 +0000 | [diff] [blame] | 2788 | /* vim:ts=4:sw=4:et |
| 2789 | */ |