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