File: | ./Modules/_sre.c |
Location: | line 2971, column 26 |
Description: | Value stored to 'min' is never read |
1 | /* |
2 | * Secret Labs' Regular Expression Engine |
3 | * |
4 | * regular expression matching engine |
5 | * |
6 | * partial history: |
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 | * |
26 | * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved. |
27 | * |
28 | * This version of the SRE library can be redistributed under CNRI's |
29 | * Python 1.6 license. For any other use, please contact Secret Labs |
30 | * AB (info@pythonware.com). |
31 | * |
32 | * Portions of this engine have been developed in cooperation with |
33 | * CNRI. Hewlett-Packard provided funding for 1.6 integration and |
34 | * other compatibility work. |
35 | */ |
36 | |
37 | #ifndef SRE_RECURSIVE |
38 | |
39 | static char copyright[] = |
40 | " SRE 2.2.2 Copyright (c) 1997-2002 by Secret Labs AB "; |
41 | |
42 | #define PY_SSIZE_T_CLEAN |
43 | |
44 | #include "Python.h" |
45 | #include "structmember.h" /* offsetof */ |
46 | |
47 | #include "sre.h" |
48 | |
49 | #include <ctype.h> |
50 | |
51 | /* name of this module, minus the leading underscore */ |
52 | #if !defined(SRE_MODULE"sre") |
53 | #define SRE_MODULE"sre" "sre" |
54 | #endif |
55 | |
56 | #define SRE_PY_MODULE"re" "re" |
57 | |
58 | /* defining this one enables tracing */ |
59 | #undef VERBOSE |
60 | |
61 | /* defining this enables unicode support (default under 1.6a1 and later) */ |
62 | #define HAVE_UNICODE |
63 | |
64 | /* -------------------------------------------------------------------- */ |
65 | /* optional features */ |
66 | |
67 | /* enables fast searching */ |
68 | #define USE_FAST_SEARCH |
69 | |
70 | /* enables copy/deepcopy handling (work in progress) */ |
71 | #undef USE_BUILTIN_COPY |
72 | |
73 | #if PY_VERSION_HEX((3 << 24) | (2 << 16) | (0 << 8) | (0xC << 4) | (2 << 0)) < 0x01060000 |
74 | #define PyObject_DEL_PyObject_DebugFree(op) PyMem_DEL_PyMem_DebugFree((op)) |
75 | #endif |
76 | |
77 | /* -------------------------------------------------------------------- */ |
78 | |
79 | #if defined(_MSC_VER) |
80 | #pragma optimize("agtw", on) /* doesn't seem to make much difference... */ |
81 | #pragma warning(disable: 4710) /* who cares if functions are not inlined ;-) */ |
82 | /* fastest possible local call under MSVC */ |
83 | #define LOCAL(type)static inline type static __inline type __fastcall |
84 | #elif defined(USE_INLINE1) |
85 | #define LOCAL(type)static inline type static inline type |
86 | #else |
87 | #define LOCAL(type)static inline type static type |
88 | #endif |
89 | |
90 | /* error codes */ |
91 | #define SRE_ERROR_ILLEGAL-1 -1 /* illegal opcode */ |
92 | #define SRE_ERROR_STATE-2 -2 /* illegal state */ |
93 | #define SRE_ERROR_RECURSION_LIMIT-3 -3 /* runaway recursion */ |
94 | #define SRE_ERROR_MEMORY-9 -9 /* out of memory */ |
95 | #define SRE_ERROR_INTERRUPTED-10 -10 /* signal handler raised exception */ |
96 | |
97 | #if defined(VERBOSE) |
98 | #define TRACE(v) printf v |
99 | #else |
100 | #define TRACE(v) |
101 | #endif |
102 | |
103 | /* -------------------------------------------------------------------- */ |
104 | /* search engine state */ |
105 | |
106 | /* default character predicates (run sre_chars.py to regenerate tables) */ |
107 | |
108 | #define SRE_DIGIT_MASK1 1 |
109 | #define SRE_SPACE_MASK2 2 |
110 | #define SRE_LINEBREAK_MASK4 4 |
111 | #define SRE_ALNUM_MASK8 8 |
112 | #define SRE_WORD_MASK16 16 |
113 | |
114 | /* FIXME: this assumes ASCII. create tables in init_sre() instead */ |
115 | |
116 | static char sre_char_info[128] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 6, 2, |
117 | 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, |
118 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 25, 25, 25, 25, 25, 25, 25, |
119 | 25, 25, 0, 0, 0, 0, 0, 0, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, |
120 | 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 0, 0, |
121 | 0, 0, 16, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, |
122 | 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 0, 0, 0, 0, 0 }; |
123 | |
124 | static char sre_char_lower[128] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
125 | 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, |
126 | 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, |
127 | 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, |
128 | 61, 62, 63, 64, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, |
129 | 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, |
130 | 122, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, |
131 | 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, |
132 | 120, 121, 122, 123, 124, 125, 126, 127 }; |
133 | |
134 | #define SRE_IS_DIGIT(ch)((ch) < 128 ? (sre_char_info[(ch)] & 1) : 0)\ |
135 | ((ch) < 128 ? (sre_char_info[(ch)] & SRE_DIGIT_MASK1) : 0) |
136 | #define SRE_IS_SPACE(ch)((ch) < 128 ? (sre_char_info[(ch)] & 2) : 0)\ |
137 | ((ch) < 128 ? (sre_char_info[(ch)] & SRE_SPACE_MASK2) : 0) |
138 | #define SRE_IS_LINEBREAK(ch)((ch) < 128 ? (sre_char_info[(ch)] & 4) : 0)\ |
139 | ((ch) < 128 ? (sre_char_info[(ch)] & SRE_LINEBREAK_MASK4) : 0) |
140 | #define SRE_IS_ALNUM(ch)((ch) < 128 ? (sre_char_info[(ch)] & 8) : 0)\ |
141 | ((ch) < 128 ? (sre_char_info[(ch)] & SRE_ALNUM_MASK8) : 0) |
142 | #define SRE_IS_WORD(ch)((ch) < 128 ? (sre_char_info[(ch)] & 16) : 0)\ |
143 | ((ch) < 128 ? (sre_char_info[(ch)] & SRE_WORD_MASK16) : 0) |
144 | |
145 | static unsigned int sre_lower(unsigned int ch) |
146 | { |
147 | return ((ch) < 128 ? (unsigned int)sre_char_lower[ch] : ch); |
148 | } |
149 | |
150 | /* locale-specific character predicates */ |
151 | /* !(c & ~N) == (c < N+1) for any unsigned c, this avoids |
152 | * warnings when c's type supports only numbers < N+1 */ |
153 | #define SRE_LOC_IS_DIGIT(ch)(!((ch) & ~255) ? isdigit((ch)) : 0) (!((ch) & ~255) ? isdigit((ch)) : 0) |
154 | #define SRE_LOC_IS_SPACE(ch)(!((ch) & ~255) ? iswspace(btowc((ch))) : 0) (!((ch) & ~255) ? isspace((ch))iswspace(btowc((ch))) : 0) |
155 | #define SRE_LOC_IS_LINEBREAK(ch)((ch) == '\n') ((ch) == '\n') |
156 | #define SRE_LOC_IS_ALNUM(ch)(!((ch) & ~255) ? iswalnum(btowc((ch))) : 0) (!((ch) & ~255) ? isalnum((ch))iswalnum(btowc((ch))) : 0) |
157 | #define SRE_LOC_IS_WORD(ch)((!(((ch)) & ~255) ? iswalnum(btowc(((ch)))) : 0) || (ch) == '_') (SRE_LOC_IS_ALNUM((ch))(!(((ch)) & ~255) ? iswalnum(btowc(((ch)))) : 0) || (ch) == '_') |
158 | |
159 | static unsigned int sre_lower_locale(unsigned int ch) |
160 | { |
161 | return ((ch) < 256 ? (unsigned int)tolower((ch))towlower(btowc((ch))) : ch); |
162 | } |
163 | |
164 | /* unicode-specific character predicates */ |
165 | |
166 | #if defined(HAVE_UNICODE) |
167 | |
168 | #define SRE_UNI_IS_DIGIT(ch)_PyUnicode_IsDecimalDigit((Py_UNICODE)(ch)) Py_UNICODE_ISDECIMAL((Py_UNICODE)(ch))_PyUnicode_IsDecimalDigit((Py_UNICODE)(ch)) |
169 | #define SRE_UNI_IS_SPACE(ch)(((Py_UNICODE)(ch)) < 128U ? _Py_ascii_whitespace[((Py_UNICODE )(ch))] : _PyUnicode_IsWhitespace((Py_UNICODE)(ch))) Py_UNICODE_ISSPACE((Py_UNICODE)(ch))(((Py_UNICODE)(ch)) < 128U ? _Py_ascii_whitespace[((Py_UNICODE )(ch))] : _PyUnicode_IsWhitespace((Py_UNICODE)(ch))) |
170 | #define SRE_UNI_IS_LINEBREAK(ch)_PyUnicode_IsLinebreak((Py_UNICODE)(ch)) Py_UNICODE_ISLINEBREAK((Py_UNICODE)(ch))_PyUnicode_IsLinebreak((Py_UNICODE)(ch)) |
171 | #define SRE_UNI_IS_ALNUM(ch)(_PyUnicode_IsAlpha((Py_UNICODE)(ch)) || _PyUnicode_IsDecimalDigit ((Py_UNICODE)(ch)) || _PyUnicode_IsDigit((Py_UNICODE)(ch)) || _PyUnicode_IsNumeric((Py_UNICODE)(ch))) Py_UNICODE_ISALNUM((Py_UNICODE)(ch))(_PyUnicode_IsAlpha((Py_UNICODE)(ch)) || _PyUnicode_IsDecimalDigit ((Py_UNICODE)(ch)) || _PyUnicode_IsDigit((Py_UNICODE)(ch)) || _PyUnicode_IsNumeric((Py_UNICODE)(ch))) |
172 | #define SRE_UNI_IS_WORD(ch)((_PyUnicode_IsAlpha((Py_UNICODE)((ch))) || _PyUnicode_IsDecimalDigit ((Py_UNICODE)((ch))) || _PyUnicode_IsDigit((Py_UNICODE)((ch)) ) || _PyUnicode_IsNumeric((Py_UNICODE)((ch)))) || (ch) == '_' ) (SRE_UNI_IS_ALNUM((ch))(_PyUnicode_IsAlpha((Py_UNICODE)((ch))) || _PyUnicode_IsDecimalDigit ((Py_UNICODE)((ch))) || _PyUnicode_IsDigit((Py_UNICODE)((ch)) ) || _PyUnicode_IsNumeric((Py_UNICODE)((ch)))) || (ch) == '_') |
173 | |
174 | static unsigned int sre_lower_unicode(unsigned int ch) |
175 | { |
176 | return (unsigned int) Py_UNICODE_TOLOWER((Py_UNICODE)(ch))_PyUnicode_ToLowercase((Py_UNICODE)(ch)); |
177 | } |
178 | |
179 | #endif |
180 | |
181 | LOCAL(int)static inline int |
182 | sre_category(SRE_CODEunsigned short category, unsigned int ch) |
183 | { |
184 | switch (category) { |
185 | |
186 | case SRE_CATEGORY_DIGIT0: |
187 | return SRE_IS_DIGIT(ch)((ch) < 128 ? (sre_char_info[(ch)] & 1) : 0); |
188 | case SRE_CATEGORY_NOT_DIGIT1: |
189 | return !SRE_IS_DIGIT(ch)((ch) < 128 ? (sre_char_info[(ch)] & 1) : 0); |
190 | case SRE_CATEGORY_SPACE2: |
191 | return SRE_IS_SPACE(ch)((ch) < 128 ? (sre_char_info[(ch)] & 2) : 0); |
192 | case SRE_CATEGORY_NOT_SPACE3: |
193 | return !SRE_IS_SPACE(ch)((ch) < 128 ? (sre_char_info[(ch)] & 2) : 0); |
194 | case SRE_CATEGORY_WORD4: |
195 | return SRE_IS_WORD(ch)((ch) < 128 ? (sre_char_info[(ch)] & 16) : 0); |
196 | case SRE_CATEGORY_NOT_WORD5: |
197 | return !SRE_IS_WORD(ch)((ch) < 128 ? (sre_char_info[(ch)] & 16) : 0); |
198 | case SRE_CATEGORY_LINEBREAK6: |
199 | return SRE_IS_LINEBREAK(ch)((ch) < 128 ? (sre_char_info[(ch)] & 4) : 0); |
200 | case SRE_CATEGORY_NOT_LINEBREAK7: |
201 | return !SRE_IS_LINEBREAK(ch)((ch) < 128 ? (sre_char_info[(ch)] & 4) : 0); |
202 | |
203 | case SRE_CATEGORY_LOC_WORD8: |
204 | return SRE_LOC_IS_WORD(ch)((!(((ch)) & ~255) ? iswalnum(btowc(((ch)))) : 0) || (ch) == '_'); |
205 | case SRE_CATEGORY_LOC_NOT_WORD9: |
206 | return !SRE_LOC_IS_WORD(ch)((!(((ch)) & ~255) ? iswalnum(btowc(((ch)))) : 0) || (ch) == '_'); |
207 | |
208 | #if defined(HAVE_UNICODE) |
209 | case SRE_CATEGORY_UNI_DIGIT10: |
210 | return SRE_UNI_IS_DIGIT(ch)_PyUnicode_IsDecimalDigit((Py_UNICODE)(ch)); |
211 | case SRE_CATEGORY_UNI_NOT_DIGIT11: |
212 | return !SRE_UNI_IS_DIGIT(ch)_PyUnicode_IsDecimalDigit((Py_UNICODE)(ch)); |
213 | case SRE_CATEGORY_UNI_SPACE12: |
214 | return SRE_UNI_IS_SPACE(ch)(((Py_UNICODE)(ch)) < 128U ? _Py_ascii_whitespace[((Py_UNICODE )(ch))] : _PyUnicode_IsWhitespace((Py_UNICODE)(ch))); |
215 | case SRE_CATEGORY_UNI_NOT_SPACE13: |
216 | return !SRE_UNI_IS_SPACE(ch)(((Py_UNICODE)(ch)) < 128U ? _Py_ascii_whitespace[((Py_UNICODE )(ch))] : _PyUnicode_IsWhitespace((Py_UNICODE)(ch))); |
217 | case SRE_CATEGORY_UNI_WORD14: |
218 | return SRE_UNI_IS_WORD(ch)((_PyUnicode_IsAlpha((Py_UNICODE)((ch))) || _PyUnicode_IsDecimalDigit ((Py_UNICODE)((ch))) || _PyUnicode_IsDigit((Py_UNICODE)((ch)) ) || _PyUnicode_IsNumeric((Py_UNICODE)((ch)))) || (ch) == '_' ); |
219 | case SRE_CATEGORY_UNI_NOT_WORD15: |
220 | return !SRE_UNI_IS_WORD(ch)((_PyUnicode_IsAlpha((Py_UNICODE)((ch))) || _PyUnicode_IsDecimalDigit ((Py_UNICODE)((ch))) || _PyUnicode_IsDigit((Py_UNICODE)((ch)) ) || _PyUnicode_IsNumeric((Py_UNICODE)((ch)))) || (ch) == '_' ); |
221 | case SRE_CATEGORY_UNI_LINEBREAK16: |
222 | return SRE_UNI_IS_LINEBREAK(ch)_PyUnicode_IsLinebreak((Py_UNICODE)(ch)); |
223 | case SRE_CATEGORY_UNI_NOT_LINEBREAK17: |
224 | return !SRE_UNI_IS_LINEBREAK(ch)_PyUnicode_IsLinebreak((Py_UNICODE)(ch)); |
225 | #else |
226 | case SRE_CATEGORY_UNI_DIGIT10: |
227 | return SRE_IS_DIGIT(ch)((ch) < 128 ? (sre_char_info[(ch)] & 1) : 0); |
228 | case SRE_CATEGORY_UNI_NOT_DIGIT11: |
229 | return !SRE_IS_DIGIT(ch)((ch) < 128 ? (sre_char_info[(ch)] & 1) : 0); |
230 | case SRE_CATEGORY_UNI_SPACE12: |
231 | return SRE_IS_SPACE(ch)((ch) < 128 ? (sre_char_info[(ch)] & 2) : 0); |
232 | case SRE_CATEGORY_UNI_NOT_SPACE13: |
233 | return !SRE_IS_SPACE(ch)((ch) < 128 ? (sre_char_info[(ch)] & 2) : 0); |
234 | case SRE_CATEGORY_UNI_WORD14: |
235 | return SRE_LOC_IS_WORD(ch)((!(((ch)) & ~255) ? iswalnum(btowc(((ch)))) : 0) || (ch) == '_'); |
236 | case SRE_CATEGORY_UNI_NOT_WORD15: |
237 | return !SRE_LOC_IS_WORD(ch)((!(((ch)) & ~255) ? iswalnum(btowc(((ch)))) : 0) || (ch) == '_'); |
238 | case SRE_CATEGORY_UNI_LINEBREAK16: |
239 | return SRE_IS_LINEBREAK(ch)((ch) < 128 ? (sre_char_info[(ch)] & 4) : 0); |
240 | case SRE_CATEGORY_UNI_NOT_LINEBREAK17: |
241 | return !SRE_IS_LINEBREAK(ch)((ch) < 128 ? (sre_char_info[(ch)] & 4) : 0); |
242 | #endif |
243 | } |
244 | return 0; |
245 | } |
246 | |
247 | /* helpers */ |
248 | |
249 | static void |
250 | data_stack_dealloc(SRE_STATE* state) |
251 | { |
252 | if (state->data_stack) { |
253 | PyMem_FREE_PyMem_DebugFree(state->data_stack); |
254 | state->data_stack = NULL((void*)0); |
255 | } |
256 | state->data_stack_size = state->data_stack_base = 0; |
257 | } |
258 | |
259 | static int |
260 | data_stack_grow(SRE_STATE* state, Py_ssize_t size) |
261 | { |
262 | Py_ssize_t minsize, cursize; |
263 | minsize = state->data_stack_base+size; |
264 | cursize = state->data_stack_size; |
265 | if (cursize < minsize) { |
266 | void* stack; |
267 | cursize = minsize+minsize/4+1024; |
268 | TRACE(("allocate/grow stack %d\n", cursize)); |
269 | stack = PyMem_REALLOC_PyMem_DebugRealloc(state->data_stack, cursize); |
270 | if (!stack) { |
271 | data_stack_dealloc(state); |
272 | return SRE_ERROR_MEMORY-9; |
273 | } |
274 | state->data_stack = (char *)stack; |
275 | state->data_stack_size = cursize; |
276 | } |
277 | return 0; |
278 | } |
279 | |
280 | /* generate 8-bit version */ |
281 | |
282 | #define SRE_CHARPy_UNICODE unsigned char |
283 | #define SRE_ATsre_uat sre_at |
284 | #define SRE_COUNTsre_ucount sre_count |
285 | #define SRE_CHARSETsre_ucharset sre_charset |
286 | #define SRE_INFOsre_uinfo sre_info |
287 | #define SRE_MATCHsre_umatch sre_match |
288 | #define SRE_MATCH_CONTEXTsre_umatch_context sre_match_context |
289 | #define SRE_SEARCHsre_usearch sre_search |
290 | #define SRE_LITERAL_TEMPLATEsre_uliteral_template sre_literal_template |
291 | |
292 | #if defined(HAVE_UNICODE) |
293 | |
294 | #define SRE_RECURSIVE |
295 | #include "_sre.c" |
296 | #undef SRE_RECURSIVE |
297 | |
298 | #undef SRE_LITERAL_TEMPLATEsre_uliteral_template |
299 | #undef SRE_SEARCHsre_usearch |
300 | #undef SRE_MATCHsre_umatch |
301 | #undef SRE_MATCH_CONTEXTsre_umatch_context |
302 | #undef SRE_INFOsre_uinfo |
303 | #undef SRE_CHARSETsre_ucharset |
304 | #undef SRE_COUNTsre_ucount |
305 | #undef SRE_ATsre_uat |
306 | #undef SRE_CHARPy_UNICODE |
307 | |
308 | /* generate 16-bit unicode version */ |
309 | |
310 | #define SRE_CHARPy_UNICODE Py_UNICODE |
311 | #define SRE_ATsre_uat sre_uat |
312 | #define SRE_COUNTsre_ucount sre_ucount |
313 | #define SRE_CHARSETsre_ucharset sre_ucharset |
314 | #define SRE_INFOsre_uinfo sre_uinfo |
315 | #define SRE_MATCHsre_umatch sre_umatch |
316 | #define SRE_MATCH_CONTEXTsre_umatch_context sre_umatch_context |
317 | #define SRE_SEARCHsre_usearch sre_usearch |
318 | #define SRE_LITERAL_TEMPLATEsre_uliteral_template sre_uliteral_template |
319 | #endif |
320 | |
321 | #endif /* SRE_RECURSIVE */ |
322 | |
323 | /* -------------------------------------------------------------------- */ |
324 | /* String matching engine */ |
325 | |
326 | /* the following section is compiled twice, with different character |
327 | settings */ |
328 | |
329 | LOCAL(int)static inline int |
330 | SRE_ATsre_uat(SRE_STATE* state, SRE_CHARPy_UNICODE* ptr, SRE_CODEunsigned short at) |
331 | { |
332 | /* check if pointer is at given position */ |
333 | |
334 | Py_ssize_t thisp, thatp; |
335 | |
336 | switch (at) { |
337 | |
338 | case SRE_AT_BEGINNING0: |
339 | case SRE_AT_BEGINNING_STRING2: |
340 | return ((void*) ptr == state->beginning); |
341 | |
342 | case SRE_AT_BEGINNING_LINE1: |
343 | return ((void*) ptr == state->beginning || |
344 | SRE_IS_LINEBREAK((int) ptr[-1])(((int) ptr[-1]) < 128 ? (sre_char_info[((int) ptr[-1])] & 4) : 0)); |
345 | |
346 | case SRE_AT_END5: |
347 | return (((void*) (ptr+1) == state->end && |
348 | SRE_IS_LINEBREAK((int) ptr[0])(((int) ptr[0]) < 128 ? (sre_char_info[((int) ptr[0])] & 4) : 0)) || |
349 | ((void*) ptr == state->end)); |
350 | |
351 | case SRE_AT_END_LINE6: |
352 | return ((void*) ptr == state->end || |
353 | SRE_IS_LINEBREAK((int) ptr[0])(((int) ptr[0]) < 128 ? (sre_char_info[((int) ptr[0])] & 4) : 0)); |
354 | |
355 | case SRE_AT_END_STRING7: |
356 | return ((void*) ptr == state->end); |
357 | |
358 | case SRE_AT_BOUNDARY3: |
359 | if (state->beginning == state->end) |
360 | return 0; |
361 | thatp = ((void*) ptr > state->beginning) ? |
362 | SRE_IS_WORD((int) ptr[-1])(((int) ptr[-1]) < 128 ? (sre_char_info[((int) ptr[-1])] & 16) : 0) : 0; |
363 | thisp = ((void*) ptr < state->end) ? |
364 | SRE_IS_WORD((int) ptr[0])(((int) ptr[0]) < 128 ? (sre_char_info[((int) ptr[0])] & 16) : 0) : 0; |
365 | return thisp != thatp; |
366 | |
367 | case SRE_AT_NON_BOUNDARY4: |
368 | if (state->beginning == state->end) |
369 | return 0; |
370 | thatp = ((void*) ptr > state->beginning) ? |
371 | SRE_IS_WORD((int) ptr[-1])(((int) ptr[-1]) < 128 ? (sre_char_info[((int) ptr[-1])] & 16) : 0) : 0; |
372 | thisp = ((void*) ptr < state->end) ? |
373 | SRE_IS_WORD((int) ptr[0])(((int) ptr[0]) < 128 ? (sre_char_info[((int) ptr[0])] & 16) : 0) : 0; |
374 | return thisp == thatp; |
375 | |
376 | case SRE_AT_LOC_BOUNDARY8: |
377 | if (state->beginning == state->end) |
378 | return 0; |
379 | thatp = ((void*) ptr > state->beginning) ? |
380 | SRE_LOC_IS_WORD((int) ptr[-1])((!((((int) ptr[-1])) & ~255) ? iswalnum(btowc((((int) ptr [-1])))) : 0) || ((int) ptr[-1]) == '_') : 0; |
381 | thisp = ((void*) ptr < state->end) ? |
382 | SRE_LOC_IS_WORD((int) ptr[0])((!((((int) ptr[0])) & ~255) ? iswalnum(btowc((((int) ptr [0])))) : 0) || ((int) ptr[0]) == '_') : 0; |
383 | return thisp != thatp; |
384 | |
385 | case SRE_AT_LOC_NON_BOUNDARY9: |
386 | if (state->beginning == state->end) |
387 | return 0; |
388 | thatp = ((void*) ptr > state->beginning) ? |
389 | SRE_LOC_IS_WORD((int) ptr[-1])((!((((int) ptr[-1])) & ~255) ? iswalnum(btowc((((int) ptr [-1])))) : 0) || ((int) ptr[-1]) == '_') : 0; |
390 | thisp = ((void*) ptr < state->end) ? |
391 | SRE_LOC_IS_WORD((int) ptr[0])((!((((int) ptr[0])) & ~255) ? iswalnum(btowc((((int) ptr [0])))) : 0) || ((int) ptr[0]) == '_') : 0; |
392 | return thisp == thatp; |
393 | |
394 | #if defined(HAVE_UNICODE) |
395 | case SRE_AT_UNI_BOUNDARY10: |
396 | if (state->beginning == state->end) |
397 | return 0; |
398 | thatp = ((void*) ptr > state->beginning) ? |
399 | SRE_UNI_IS_WORD((int) ptr[-1])((_PyUnicode_IsAlpha((Py_UNICODE)(((int) ptr[-1]))) || _PyUnicode_IsDecimalDigit ((Py_UNICODE)(((int) ptr[-1]))) || _PyUnicode_IsDigit((Py_UNICODE )(((int) ptr[-1]))) || _PyUnicode_IsNumeric((Py_UNICODE)(((int ) ptr[-1])))) || ((int) ptr[-1]) == '_') : 0; |
400 | thisp = ((void*) ptr < state->end) ? |
401 | SRE_UNI_IS_WORD((int) ptr[0])((_PyUnicode_IsAlpha((Py_UNICODE)(((int) ptr[0]))) || _PyUnicode_IsDecimalDigit ((Py_UNICODE)(((int) ptr[0]))) || _PyUnicode_IsDigit((Py_UNICODE )(((int) ptr[0]))) || _PyUnicode_IsNumeric((Py_UNICODE)(((int ) ptr[0])))) || ((int) ptr[0]) == '_') : 0; |
402 | return thisp != thatp; |
403 | |
404 | case SRE_AT_UNI_NON_BOUNDARY11: |
405 | if (state->beginning == state->end) |
406 | return 0; |
407 | thatp = ((void*) ptr > state->beginning) ? |
408 | SRE_UNI_IS_WORD((int) ptr[-1])((_PyUnicode_IsAlpha((Py_UNICODE)(((int) ptr[-1]))) || _PyUnicode_IsDecimalDigit ((Py_UNICODE)(((int) ptr[-1]))) || _PyUnicode_IsDigit((Py_UNICODE )(((int) ptr[-1]))) || _PyUnicode_IsNumeric((Py_UNICODE)(((int ) ptr[-1])))) || ((int) ptr[-1]) == '_') : 0; |
409 | thisp = ((void*) ptr < state->end) ? |
410 | SRE_UNI_IS_WORD((int) ptr[0])((_PyUnicode_IsAlpha((Py_UNICODE)(((int) ptr[0]))) || _PyUnicode_IsDecimalDigit ((Py_UNICODE)(((int) ptr[0]))) || _PyUnicode_IsDigit((Py_UNICODE )(((int) ptr[0]))) || _PyUnicode_IsNumeric((Py_UNICODE)(((int ) ptr[0])))) || ((int) ptr[0]) == '_') : 0; |
411 | return thisp == thatp; |
412 | #endif |
413 | |
414 | } |
415 | |
416 | return 0; |
417 | } |
418 | |
419 | LOCAL(int)static inline int |
420 | SRE_CHARSETsre_ucharset(SRE_CODEunsigned short* set, SRE_CODEunsigned short ch) |
421 | { |
422 | /* check if character is a member of the given set */ |
423 | |
424 | int ok = 1; |
425 | |
426 | for (;;) { |
427 | switch (*set++) { |
428 | |
429 | case SRE_OP_FAILURE0: |
430 | return !ok; |
431 | |
432 | case SRE_OP_LITERAL19: |
433 | /* <LITERAL> <code> */ |
434 | if (ch == set[0]) |
435 | return ok; |
436 | set++; |
437 | break; |
438 | |
439 | case SRE_OP_CATEGORY9: |
440 | /* <CATEGORY> <code> */ |
441 | if (sre_category(set[0], (int) ch)) |
442 | return ok; |
443 | set += 1; |
444 | break; |
445 | |
446 | case SRE_OP_CHARSET10: |
447 | if (sizeof(SRE_CODEunsigned short) == 2) { |
448 | /* <CHARSET> <bitmap> (16 bits per code word) */ |
449 | if (ch < 256 && (set[ch >> 4] & (1 << (ch & 15)))) |
450 | return ok; |
451 | set += 16; |
452 | } |
453 | else { |
454 | /* <CHARSET> <bitmap> (32 bits per code word) */ |
455 | if (ch < 256 && (set[ch >> 5] & (1 << (ch & 31)))) |
456 | return ok; |
457 | set += 8; |
458 | } |
459 | break; |
460 | |
461 | case SRE_OP_RANGE27: |
462 | /* <RANGE> <lower> <upper> */ |
463 | if (set[0] <= ch && ch <= set[1]) |
464 | return ok; |
465 | set += 2; |
466 | break; |
467 | |
468 | case SRE_OP_NEGATE26: |
469 | ok = !ok; |
470 | break; |
471 | |
472 | case SRE_OP_BIGCHARSET11: |
473 | /* <BIGCHARSET> <blockcount> <256 blockindices> <blocks> */ |
474 | { |
475 | Py_ssize_t count, block; |
476 | count = *(set++); |
477 | |
478 | if (sizeof(SRE_CODEunsigned short) == 2) { |
479 | block = ((unsigned char*)set)[ch >> 8]; |
480 | set += 128; |
481 | if (set[block*16 + ((ch & 255)>>4)] & (1 << (ch & 15))) |
482 | return ok; |
483 | set += count*16; |
484 | } |
485 | else { |
486 | /* !(c & ~N) == (c < N+1) for any unsigned c, this avoids |
487 | * warnings when c's type supports only numbers < N+1 */ |
488 | if (!(ch & ~65535)) |
489 | block = ((unsigned char*)set)[ch >> 8]; |
490 | else |
491 | block = -1; |
492 | set += 64; |
493 | if (block >=0 && |
494 | (set[block*8 + ((ch & 255)>>5)] & (1 << (ch & 31)))) |
495 | return ok; |
496 | set += count*8; |
497 | } |
498 | break; |
499 | } |
500 | |
501 | default: |
502 | /* internal error -- there's not much we can do about it |
503 | here, so let's just pretend it didn't match... */ |
504 | return 0; |
505 | } |
506 | } |
507 | } |
508 | |
509 | LOCAL(Py_ssize_t)static inline Py_ssize_t SRE_MATCHsre_umatch(SRE_STATE* state, SRE_CODEunsigned short* pattern); |
510 | |
511 | LOCAL(Py_ssize_t)static inline Py_ssize_t |
512 | SRE_COUNTsre_ucount(SRE_STATE* state, SRE_CODEunsigned short* pattern, Py_ssize_t maxcount) |
513 | { |
514 | SRE_CODEunsigned short chr; |
515 | SRE_CHARPy_UNICODE* ptr = (SRE_CHARPy_UNICODE *)state->ptr; |
516 | SRE_CHARPy_UNICODE* end = (SRE_CHARPy_UNICODE *)state->end; |
517 | Py_ssize_t i; |
518 | |
519 | /* adjust end */ |
520 | if (maxcount < end - ptr && maxcount != 65535) |
521 | end = ptr + maxcount; |
522 | |
523 | switch (pattern[0]) { |
524 | |
525 | case SRE_OP_IN15: |
526 | /* repeated set */ |
527 | TRACE(("|%p|%p|COUNT IN\n", pattern, ptr)); |
528 | while (ptr < end && SRE_CHARSETsre_ucharset(pattern + 2, *ptr)) |
529 | ptr++; |
530 | break; |
531 | |
532 | case SRE_OP_ANY2: |
533 | /* repeated dot wildcard. */ |
534 | TRACE(("|%p|%p|COUNT ANY\n", pattern, ptr)); |
535 | while (ptr < end && !SRE_IS_LINEBREAK(*ptr)((*ptr) < 128 ? (sre_char_info[(*ptr)] & 4) : 0)) |
536 | ptr++; |
537 | break; |
538 | |
539 | case SRE_OP_ANY_ALL3: |
540 | /* repeated dot wildcard. skip to the end of the target |
541 | string, and backtrack from there */ |
542 | TRACE(("|%p|%p|COUNT ANY_ALL\n", pattern, ptr)); |
543 | ptr = end; |
544 | break; |
545 | |
546 | case SRE_OP_LITERAL19: |
547 | /* repeated literal */ |
548 | chr = pattern[1]; |
549 | TRACE(("|%p|%p|COUNT LITERAL %d\n", pattern, ptr, chr)); |
550 | while (ptr < end && (SRE_CODEunsigned short) *ptr == chr) |
551 | ptr++; |
552 | break; |
553 | |
554 | case SRE_OP_LITERAL_IGNORE20: |
555 | /* repeated literal */ |
556 | chr = pattern[1]; |
557 | TRACE(("|%p|%p|COUNT LITERAL_IGNORE %d\n", pattern, ptr, chr)); |
558 | while (ptr < end && (SRE_CODEunsigned short) state->lower(*ptr) == chr) |
559 | ptr++; |
560 | break; |
561 | |
562 | case SRE_OP_NOT_LITERAL24: |
563 | /* repeated non-literal */ |
564 | chr = pattern[1]; |
565 | TRACE(("|%p|%p|COUNT NOT_LITERAL %d\n", pattern, ptr, chr)); |
566 | while (ptr < end && (SRE_CODEunsigned short) *ptr != chr) |
567 | ptr++; |
568 | break; |
569 | |
570 | case SRE_OP_NOT_LITERAL_IGNORE25: |
571 | /* repeated non-literal */ |
572 | chr = pattern[1]; |
573 | TRACE(("|%p|%p|COUNT NOT_LITERAL_IGNORE %d\n", pattern, ptr, chr)); |
574 | while (ptr < end && (SRE_CODEunsigned short) state->lower(*ptr) != chr) |
575 | ptr++; |
576 | break; |
577 | |
578 | default: |
579 | /* repeated single character pattern */ |
580 | TRACE(("|%p|%p|COUNT SUBPATTERN\n", pattern, ptr)); |
581 | while ((SRE_CHARPy_UNICODE*) state->ptr < end) { |
582 | i = SRE_MATCHsre_umatch(state, pattern); |
583 | if (i < 0) |
584 | return i; |
585 | if (!i) |
586 | break; |
587 | } |
588 | TRACE(("|%p|%p|COUNT %d\n", pattern, ptr, |
589 | (SRE_CHAR*) state->ptr - ptr)); |
590 | return (SRE_CHARPy_UNICODE*) state->ptr - ptr; |
591 | } |
592 | |
593 | TRACE(("|%p|%p|COUNT %d\n", pattern, ptr, ptr - (SRE_CHAR*) state->ptr)); |
594 | return ptr - (SRE_CHARPy_UNICODE*) state->ptr; |
595 | } |
596 | |
597 | #if 0 /* not used in this release */ |
598 | LOCAL(int)static inline int |
599 | SRE_INFOsre_uinfo(SRE_STATE* state, SRE_CODEunsigned short* pattern) |
600 | { |
601 | /* check if an SRE_OP_INFO block matches at the current position. |
602 | returns the number of SRE_CODE objects to skip if successful, 0 |
603 | if no match */ |
604 | |
605 | SRE_CHARPy_UNICODE* end = state->end; |
606 | SRE_CHARPy_UNICODE* ptr = state->ptr; |
607 | Py_ssize_t i; |
608 | |
609 | /* check minimal length */ |
610 | if (pattern[3] && (end - ptr) < pattern[3]) |
611 | return 0; |
612 | |
613 | /* check known prefix */ |
614 | if (pattern[2] & SRE_INFO_PREFIX1 && pattern[5] > 1) { |
615 | /* <length> <skip> <prefix data> <overlap data> */ |
616 | for (i = 0; i < pattern[5]; i++) |
617 | if ((SRE_CODEunsigned short) ptr[i] != pattern[7 + i]) |
618 | return 0; |
619 | return pattern[0] + 2 * pattern[6]; |
620 | } |
621 | return pattern[0]; |
622 | } |
623 | #endif |
624 | |
625 | /* The macros below should be used to protect recursive SRE_MATCH() |
626 | * calls that *failed* and do *not* return immediately (IOW, those |
627 | * that will backtrack). Explaining: |
628 | * |
629 | * - Recursive SRE_MATCH() returned true: that's usually a success |
630 | * (besides atypical cases like ASSERT_NOT), therefore there's no |
631 | * reason to restore lastmark; |
632 | * |
633 | * - Recursive SRE_MATCH() returned false but the current SRE_MATCH() |
634 | * is returning to the caller: If the current SRE_MATCH() is the |
635 | * top function of the recursion, returning false will be a matching |
636 | * failure, and it doesn't matter where lastmark is pointing to. |
637 | * If it's *not* the top function, it will be a recursive SRE_MATCH() |
638 | * failure by itself, and the calling SRE_MATCH() will have to deal |
639 | * with the failure by the same rules explained here (it will restore |
640 | * lastmark by itself if necessary); |
641 | * |
642 | * - Recursive SRE_MATCH() returned false, and will continue the |
643 | * outside 'for' loop: must be protected when breaking, since the next |
644 | * OP could potentially depend on lastmark; |
645 | * |
646 | * - Recursive SRE_MATCH() returned false, and will be called again |
647 | * inside a local for/while loop: must be protected between each |
648 | * loop iteration, since the recursive SRE_MATCH() could do anything, |
649 | * and could potentially depend on lastmark. |
650 | * |
651 | * For more information, check the discussion at SF patch #712900. |
652 | */ |
653 | #define LASTMARK_SAVE()do { ctx->lastmark = state->lastmark; ctx->lastindex = state->lastindex; } while (0) \ |
654 | do { \ |
655 | ctx->lastmark = state->lastmark; \ |
656 | ctx->lastindex = state->lastindex; \ |
657 | } while (0) |
658 | #define LASTMARK_RESTORE()do { state->lastmark = ctx->lastmark; state->lastindex = ctx->lastindex; } while (0) \ |
659 | do { \ |
660 | state->lastmark = ctx->lastmark; \ |
661 | state->lastindex = ctx->lastindex; \ |
662 | } while (0) |
663 | |
664 | #define RETURN_ERROR(i)do { return i; } while(0) do { return i; } while(0) |
665 | #define RETURN_FAILUREdo { ret = 0; goto exit; } while(0) do { ret = 0; goto exit; } while(0) |
666 | #define RETURN_SUCCESSdo { ret = 1; goto exit; } while(0) do { ret = 1; goto exit; } while(0) |
667 | |
668 | #define RETURN_ON_ERROR(i)do { if (i < 0) do { return i; } while(0); } while (0) \ |
669 | do { if (i < 0) RETURN_ERROR(i)do { return i; } while(0); } while (0) |
670 | #define RETURN_ON_SUCCESS(i)do { do { if (i < 0) do { return i; } while(0); } while (0 ); if (i > 0) do { ret = 1; goto exit; } while(0); } while (0) \ |
671 | do { RETURN_ON_ERROR(i)do { if (i < 0) do { return i; } while(0); } while (0); if (i > 0) RETURN_SUCCESSdo { ret = 1; goto exit; } while(0); } while (0) |
672 | #define RETURN_ON_FAILURE(i)do { do { if (i < 0) do { return i; } while(0); } while (0 ); if (i == 0) do { ret = 0; goto exit; } while(0); } while ( 0) \ |
673 | do { RETURN_ON_ERROR(i)do { if (i < 0) do { return i; } while(0); } while (0); if (i == 0) RETURN_FAILUREdo { ret = 0; goto exit; } while(0); } while (0) |
674 | |
675 | #define SFY(x)"x" #x |
676 | |
677 | #define DATA_STACK_ALLOC(state, type, ptr)do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(type)) { int j = data_stack_grow(state , sizeof(type)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state->data_stack+ctx_pos ); } while (0); } ptr = (type*)(state->data_stack+alloc_pos ); state->data_stack_base += sizeof(type); } while (0) \ |
678 | do { \ |
679 | alloc_pos = state->data_stack_base; \ |
680 | TRACE(("allocating %s in %d (%d)\n", \ |
681 | SFY(type), alloc_pos, sizeof(type))); \ |
682 | if (state->data_stack_size < alloc_pos+sizeof(type)) { \ |
683 | int j = data_stack_grow(state, sizeof(type)); \ |
684 | if (j < 0) return j; \ |
685 | if (ctx_pos != -1) \ |
686 | DATA_STACK_LOOKUP_AT(state, SRE_MATCH_CONTEXT, ctx, ctx_pos)do { ; ctx = (sre_umatch_context*)(state->data_stack+ctx_pos ); } while (0); \ |
687 | } \ |
688 | ptr = (type*)(state->data_stack+alloc_pos); \ |
689 | state->data_stack_base += sizeof(type); \ |
690 | } while (0) |
691 | |
692 | #define DATA_STACK_LOOKUP_AT(state, type, ptr, pos)do { ; ptr = (type*)(state->data_stack+pos); } while (0) \ |
693 | do { \ |
694 | TRACE(("looking up %s at %d\n", SFY(type), pos)); \ |
695 | ptr = (type*)(state->data_stack+pos); \ |
696 | } while (0) |
697 | |
698 | #define DATA_STACK_PUSH(state, data, size)do { ; if (state->data_stack_size < state->data_stack_base +size) { int j = data_stack_grow(state, size); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state ->data_stack+ctx_pos); } while (0); } ((__builtin_object_size (state->data_stack+state->data_stack_base, 0) != (size_t ) -1) ? __builtin___memcpy_chk (state->data_stack+state-> data_stack_base, data, size, __builtin_object_size (state-> data_stack+state->data_stack_base, 0)) : __inline_memcpy_chk (state->data_stack+state->data_stack_base, data, size) ); state->data_stack_base += size; } while (0) \ |
699 | do { \ |
700 | TRACE(("copy data in %p to %d (%d)\n", \ |
701 | data, state->data_stack_base, size)); \ |
702 | if (state->data_stack_size < state->data_stack_base+size) { \ |
703 | int j = data_stack_grow(state, size); \ |
704 | if (j < 0) return j; \ |
705 | if (ctx_pos != -1) \ |
706 | DATA_STACK_LOOKUP_AT(state, SRE_MATCH_CONTEXT, ctx, ctx_pos)do { ; ctx = (sre_umatch_context*)(state->data_stack+ctx_pos ); } while (0); \ |
707 | } \ |
708 | memcpy(state->data_stack+state->data_stack_base, data, size)((__builtin_object_size (state->data_stack+state->data_stack_base , 0) != (size_t) -1) ? __builtin___memcpy_chk (state->data_stack +state->data_stack_base, data, size, __builtin_object_size (state->data_stack+state->data_stack_base, 0)) : __inline_memcpy_chk (state->data_stack+state->data_stack_base, data, size) ); \ |
709 | state->data_stack_base += size; \ |
710 | } while (0) |
711 | |
712 | #define DATA_STACK_POP(state, data, size, discard)do { ; ((__builtin_object_size (data, 0) != (size_t) -1) ? __builtin___memcpy_chk (data, state->data_stack+state->data_stack_base-size, size , __builtin_object_size (data, 0)) : __inline_memcpy_chk (data , state->data_stack+state->data_stack_base-size, size)) ; if (discard) state->data_stack_base -= size; } while (0) \ |
713 | do { \ |
714 | TRACE(("copy data to %p from %d (%d)\n", \ |
715 | data, state->data_stack_base-size, size)); \ |
716 | memcpy(data, state->data_stack+state->data_stack_base-size, size)((__builtin_object_size (data, 0) != (size_t) -1) ? __builtin___memcpy_chk (data, state->data_stack+state->data_stack_base-size, size , __builtin_object_size (data, 0)) : __inline_memcpy_chk (data , state->data_stack+state->data_stack_base-size, size)); \ |
717 | if (discard) \ |
718 | state->data_stack_base -= size; \ |
719 | } while (0) |
720 | |
721 | #define DATA_STACK_POP_DISCARD(state, size)do { ; state->data_stack_base -= size; } while(0) \ |
722 | do { \ |
723 | TRACE(("discard data from %d (%d)\n", \ |
724 | state->data_stack_base-size, size)); \ |
725 | state->data_stack_base -= size; \ |
726 | } while(0) |
727 | |
728 | #define DATA_PUSH(x)do { ; if (state->data_stack_size < state->data_stack_base +sizeof(*(x))) { int j = data_stack_grow(state, sizeof(*(x))) ; if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context *)(state->data_stack+ctx_pos); } while (0); } ((__builtin_object_size (state->data_stack+state->data_stack_base, 0) != (size_t ) -1) ? __builtin___memcpy_chk (state->data_stack+state-> data_stack_base, (x), sizeof(*(x)), __builtin_object_size (state ->data_stack+state->data_stack_base, 0)) : __inline_memcpy_chk (state->data_stack+state->data_stack_base, (x), sizeof (*(x)))); state->data_stack_base += sizeof(*(x)); } while ( 0) \ |
729 | DATA_STACK_PUSH(state, (x), sizeof(*(x)))do { ; if (state->data_stack_size < state->data_stack_base +sizeof(*(x))) { int j = data_stack_grow(state, sizeof(*(x))) ; if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context *)(state->data_stack+ctx_pos); } while (0); } ((__builtin_object_size (state->data_stack+state->data_stack_base, 0) != (size_t ) -1) ? __builtin___memcpy_chk (state->data_stack+state-> data_stack_base, (x), sizeof(*(x)), __builtin_object_size (state ->data_stack+state->data_stack_base, 0)) : __inline_memcpy_chk (state->data_stack+state->data_stack_base, (x), sizeof (*(x)))); state->data_stack_base += sizeof(*(x)); } while ( 0) |
730 | #define DATA_POP(x)do { ; ((__builtin_object_size ((x), 0) != (size_t) -1) ? __builtin___memcpy_chk ((x), state->data_stack+state->data_stack_base-sizeof( *(x)), sizeof(*(x)), __builtin_object_size ((x), 0)) : __inline_memcpy_chk ((x), state->data_stack+state->data_stack_base-sizeof( *(x)), sizeof(*(x)))); if (1) state->data_stack_base -= sizeof (*(x)); } while (0) \ |
731 | DATA_STACK_POP(state, (x), sizeof(*(x)), 1)do { ; ((__builtin_object_size ((x), 0) != (size_t) -1) ? __builtin___memcpy_chk ((x), state->data_stack+state->data_stack_base-sizeof( *(x)), sizeof(*(x)), __builtin_object_size ((x), 0)) : __inline_memcpy_chk ((x), state->data_stack+state->data_stack_base-sizeof( *(x)), sizeof(*(x)))); if (1) state->data_stack_base -= sizeof (*(x)); } while (0) |
732 | #define DATA_POP_DISCARD(x)do { ; state->data_stack_base -= sizeof(*(x)); } while(0) \ |
733 | DATA_STACK_POP_DISCARD(state, sizeof(*(x)))do { ; state->data_stack_base -= sizeof(*(x)); } while(0) |
734 | #define DATA_ALLOC(t,p)do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(t)) { int j = data_stack_grow(state, sizeof (t)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state->data_stack+ctx_pos); } while (0); } p = (t*)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(t); } while (0) \ |
735 | DATA_STACK_ALLOC(state, t, p)do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(t)) { int j = data_stack_grow(state, sizeof (t)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state->data_stack+ctx_pos); } while (0); } p = (t*)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(t); } while (0) |
736 | #define DATA_LOOKUP_AT(t,p,pos)do { ; p = (t*)(state->data_stack+pos); } while (0) \ |
737 | DATA_STACK_LOOKUP_AT(state,t,p,pos)do { ; p = (t*)(state->data_stack+pos); } while (0) |
738 | |
739 | #define MARK_PUSH(lastmark)do if (lastmark > 0) { i = lastmark; do { ; if (state-> data_stack_size < state->data_stack_base+(i+1)*sizeof(void *)) { int j = data_stack_grow(state, (i+1)*sizeof(void*)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context *)(state->data_stack+ctx_pos); } while (0); } ((__builtin_object_size (state->data_stack+state->data_stack_base, 0) != (size_t ) -1) ? __builtin___memcpy_chk (state->data_stack+state-> data_stack_base, state->mark, (i+1)*sizeof(void*), __builtin_object_size (state->data_stack+state->data_stack_base, 0)) : __inline_memcpy_chk (state->data_stack+state->data_stack_base, state->mark , (i+1)*sizeof(void*))); state->data_stack_base += (i+1)*sizeof (void*); } while (0); } while (0) \ |
740 | do if (lastmark > 0) { \ |
741 | i = lastmark; /* ctx->lastmark may change if reallocated */ \ |
742 | DATA_STACK_PUSH(state, state->mark, (i+1)*sizeof(void*))do { ; if (state->data_stack_size < state->data_stack_base +(i+1)*sizeof(void*)) { int j = data_stack_grow(state, (i+1)* sizeof(void*)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state->data_stack+ctx_pos ); } while (0); } ((__builtin_object_size (state->data_stack +state->data_stack_base, 0) != (size_t) -1) ? __builtin___memcpy_chk (state->data_stack+state->data_stack_base, state->mark , (i+1)*sizeof(void*), __builtin_object_size (state->data_stack +state->data_stack_base, 0)) : __inline_memcpy_chk (state-> data_stack+state->data_stack_base, state->mark, (i+1)*sizeof (void*))); state->data_stack_base += (i+1)*sizeof(void*); } while (0); \ |
743 | } while (0) |
744 | #define MARK_POP(lastmark)do if (lastmark > 0) { do { ; ((__builtin_object_size (state ->mark, 0) != (size_t) -1) ? __builtin___memcpy_chk (state ->mark, state->data_stack+state->data_stack_base-(lastmark +1)*sizeof(void*), (lastmark+1)*sizeof(void*), __builtin_object_size (state->mark, 0)) : __inline_memcpy_chk (state->mark, state ->data_stack+state->data_stack_base-(lastmark+1)*sizeof (void*), (lastmark+1)*sizeof(void*))); if (1) state->data_stack_base -= (lastmark+1)*sizeof(void*); } while (0); } while (0) \ |
745 | do if (lastmark > 0) { \ |
746 | DATA_STACK_POP(state, state->mark, (lastmark+1)*sizeof(void*), 1)do { ; ((__builtin_object_size (state->mark, 0) != (size_t ) -1) ? __builtin___memcpy_chk (state->mark, state->data_stack +state->data_stack_base-(lastmark+1)*sizeof(void*), (lastmark +1)*sizeof(void*), __builtin_object_size (state->mark, 0)) : __inline_memcpy_chk (state->mark, state->data_stack+ state->data_stack_base-(lastmark+1)*sizeof(void*), (lastmark +1)*sizeof(void*))); if (1) state->data_stack_base -= (lastmark +1)*sizeof(void*); } while (0); \ |
747 | } while (0) |
748 | #define MARK_POP_KEEP(lastmark)do if (lastmark > 0) { do { ; ((__builtin_object_size (state ->mark, 0) != (size_t) -1) ? __builtin___memcpy_chk (state ->mark, state->data_stack+state->data_stack_base-(lastmark +1)*sizeof(void*), (lastmark+1)*sizeof(void*), __builtin_object_size (state->mark, 0)) : __inline_memcpy_chk (state->mark, state ->data_stack+state->data_stack_base-(lastmark+1)*sizeof (void*), (lastmark+1)*sizeof(void*))); if (0) state->data_stack_base -= (lastmark+1)*sizeof(void*); } while (0); } while (0) \ |
749 | do if (lastmark > 0) { \ |
750 | DATA_STACK_POP(state, state->mark, (lastmark+1)*sizeof(void*), 0)do { ; ((__builtin_object_size (state->mark, 0) != (size_t ) -1) ? __builtin___memcpy_chk (state->mark, state->data_stack +state->data_stack_base-(lastmark+1)*sizeof(void*), (lastmark +1)*sizeof(void*), __builtin_object_size (state->mark, 0)) : __inline_memcpy_chk (state->mark, state->data_stack+ state->data_stack_base-(lastmark+1)*sizeof(void*), (lastmark +1)*sizeof(void*))); if (0) state->data_stack_base -= (lastmark +1)*sizeof(void*); } while (0); \ |
751 | } while (0) |
752 | #define MARK_POP_DISCARD(lastmark)do if (lastmark > 0) { do { ; state->data_stack_base -= (lastmark+1)*sizeof(void*); } while(0); } while (0) \ |
753 | do if (lastmark > 0) { \ |
754 | DATA_STACK_POP_DISCARD(state, (lastmark+1)*sizeof(void*))do { ; state->data_stack_base -= (lastmark+1)*sizeof(void* ); } while(0); \ |
755 | } while (0) |
756 | |
757 | #define JUMP_NONE0 0 |
758 | #define JUMP_MAX_UNTIL_11 1 |
759 | #define JUMP_MAX_UNTIL_22 2 |
760 | #define JUMP_MAX_UNTIL_33 3 |
761 | #define JUMP_MIN_UNTIL_14 4 |
762 | #define JUMP_MIN_UNTIL_25 5 |
763 | #define JUMP_MIN_UNTIL_36 6 |
764 | #define JUMP_REPEAT7 7 |
765 | #define JUMP_REPEAT_ONE_18 8 |
766 | #define JUMP_REPEAT_ONE_29 9 |
767 | #define JUMP_MIN_REPEAT_ONE10 10 |
768 | #define JUMP_BRANCH11 11 |
769 | #define JUMP_ASSERT12 12 |
770 | #define JUMP_ASSERT_NOT13 13 |
771 | |
772 | #define DO_JUMP(jumpvalue, jumplabel, nextpattern)do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = jumpvalue; nextctx->pattern = nextpattern; ctx_pos = alloc_pos; ctx = nextctx; goto entrance ; jumplabel: while (0) \ |
773 | DATA_ALLOC(SRE_MATCH_CONTEXT, nextctx)do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); \ |
774 | nextctx->last_ctx_pos = ctx_pos; \ |
775 | nextctx->jump = jumpvalue; \ |
776 | nextctx->pattern = nextpattern; \ |
777 | ctx_pos = alloc_pos; \ |
778 | ctx = nextctx; \ |
779 | goto entrance; \ |
780 | jumplabel: \ |
781 | while (0) /* gcc doesn't like labels at end of scopes */ \ |
782 | |
783 | typedef struct { |
784 | Py_ssize_t last_ctx_pos; |
785 | Py_ssize_t jump; |
786 | SRE_CHARPy_UNICODE* ptr; |
787 | SRE_CODEunsigned short* pattern; |
788 | Py_ssize_t count; |
789 | Py_ssize_t lastmark; |
790 | Py_ssize_t lastindex; |
791 | union { |
792 | SRE_CODEunsigned short chr; |
793 | SRE_REPEAT* rep; |
794 | } u; |
795 | } SRE_MATCH_CONTEXTsre_umatch_context; |
796 | |
797 | /* check if string matches the given pattern. returns <0 for |
798 | error, 0 for failure, and 1 for success */ |
799 | LOCAL(Py_ssize_t)static inline Py_ssize_t |
800 | SRE_MATCHsre_umatch(SRE_STATE* state, SRE_CODEunsigned short* pattern) |
801 | { |
802 | SRE_CHARPy_UNICODE* end = (SRE_CHARPy_UNICODE *)state->end; |
803 | Py_ssize_t alloc_pos, ctx_pos = -1; |
804 | Py_ssize_t i, ret = 0; |
805 | Py_ssize_t jump; |
806 | unsigned int sigcount=0; |
807 | |
808 | SRE_MATCH_CONTEXTsre_umatch_context* ctx; |
809 | SRE_MATCH_CONTEXTsre_umatch_context* nextctx; |
810 | |
811 | TRACE(("|%p|%p|ENTER\n", pattern, state->ptr)); |
812 | |
813 | DATA_ALLOC(SRE_MATCH_CONTEXT, ctx)do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } ctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); |
814 | ctx->last_ctx_pos = -1; |
815 | ctx->jump = JUMP_NONE0; |
816 | ctx->pattern = pattern; |
817 | ctx_pos = alloc_pos; |
818 | |
819 | entrance: |
820 | |
821 | ctx->ptr = (SRE_CHARPy_UNICODE *)state->ptr; |
822 | |
823 | if (ctx->pattern[0] == SRE_OP_INFO17) { |
824 | /* optimization info block */ |
825 | /* <INFO> <1=skip> <2=flags> <3=min> ... */ |
826 | if (ctx->pattern[3] && (end - ctx->ptr) < ctx->pattern[3]) { |
827 | TRACE(("reject (got %d chars, need %d)\n", |
828 | (end - ctx->ptr), ctx->pattern[3])); |
829 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
830 | } |
831 | ctx->pattern += ctx->pattern[1] + 1; |
832 | } |
833 | |
834 | for (;;) { |
835 | ++sigcount; |
836 | if ((0 == (sigcount & 0xfff)) && PyErr_CheckSignals()) |
837 | RETURN_ERROR(SRE_ERROR_INTERRUPTED)do { return -10; } while(0); |
838 | |
839 | switch (*ctx->pattern++) { |
840 | |
841 | case SRE_OP_MARK21: |
842 | /* set mark */ |
843 | /* <MARK> <gid> */ |
844 | TRACE(("|%p|%p|MARK %d\n", ctx->pattern, |
845 | ctx->ptr, ctx->pattern[0])); |
846 | i = ctx->pattern[0]; |
847 | if (i & 1) |
848 | state->lastindex = i/2 + 1; |
849 | if (i > state->lastmark) { |
850 | /* state->lastmark is the highest valid index in the |
851 | state->mark array. If it is increased by more than 1, |
852 | the intervening marks must be set to NULL to signal |
853 | that these marks have not been encountered. */ |
854 | Py_ssize_t j = state->lastmark + 1; |
855 | while (j < i) |
856 | state->mark[j++] = NULL((void*)0); |
857 | state->lastmark = i; |
858 | } |
859 | state->mark[i] = ctx->ptr; |
860 | ctx->pattern++; |
861 | break; |
862 | |
863 | case SRE_OP_LITERAL19: |
864 | /* match literal string */ |
865 | /* <LITERAL> <code> */ |
866 | TRACE(("|%p|%p|LITERAL %d\n", ctx->pattern, |
867 | ctx->ptr, *ctx->pattern)); |
868 | if (ctx->ptr >= end || (SRE_CODEunsigned short) ctx->ptr[0] != ctx->pattern[0]) |
869 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
870 | ctx->pattern++; |
871 | ctx->ptr++; |
872 | break; |
873 | |
874 | case SRE_OP_NOT_LITERAL24: |
875 | /* match anything that is not literal character */ |
876 | /* <NOT_LITERAL> <code> */ |
877 | TRACE(("|%p|%p|NOT_LITERAL %d\n", ctx->pattern, |
878 | ctx->ptr, *ctx->pattern)); |
879 | if (ctx->ptr >= end || (SRE_CODEunsigned short) ctx->ptr[0] == ctx->pattern[0]) |
880 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
881 | ctx->pattern++; |
882 | ctx->ptr++; |
883 | break; |
884 | |
885 | case SRE_OP_SUCCESS1: |
886 | /* end of pattern */ |
887 | TRACE(("|%p|%p|SUCCESS\n", ctx->pattern, ctx->ptr)); |
888 | state->ptr = ctx->ptr; |
889 | RETURN_SUCCESSdo { ret = 1; goto exit; } while(0); |
890 | |
891 | case SRE_OP_AT6: |
892 | /* match at given position */ |
893 | /* <AT> <code> */ |
894 | TRACE(("|%p|%p|AT %d\n", ctx->pattern, ctx->ptr, *ctx->pattern)); |
895 | if (!SRE_ATsre_uat(state, ctx->ptr, *ctx->pattern)) |
896 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
897 | ctx->pattern++; |
898 | break; |
899 | |
900 | case SRE_OP_CATEGORY9: |
901 | /* match at given category */ |
902 | /* <CATEGORY> <code> */ |
903 | TRACE(("|%p|%p|CATEGORY %d\n", ctx->pattern, |
904 | ctx->ptr, *ctx->pattern)); |
905 | if (ctx->ptr >= end || !sre_category(ctx->pattern[0], ctx->ptr[0])) |
906 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
907 | ctx->pattern++; |
908 | ctx->ptr++; |
909 | break; |
910 | |
911 | case SRE_OP_ANY2: |
912 | /* match anything (except a newline) */ |
913 | /* <ANY> */ |
914 | TRACE(("|%p|%p|ANY\n", ctx->pattern, ctx->ptr)); |
915 | if (ctx->ptr >= end || SRE_IS_LINEBREAK(ctx->ptr[0])((ctx->ptr[0]) < 128 ? (sre_char_info[(ctx->ptr[0])] & 4) : 0)) |
916 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
917 | ctx->ptr++; |
918 | break; |
919 | |
920 | case SRE_OP_ANY_ALL3: |
921 | /* match anything */ |
922 | /* <ANY_ALL> */ |
923 | TRACE(("|%p|%p|ANY_ALL\n", ctx->pattern, ctx->ptr)); |
924 | if (ctx->ptr >= end) |
925 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
926 | ctx->ptr++; |
927 | break; |
928 | |
929 | case SRE_OP_IN15: |
930 | /* match set member (or non_member) */ |
931 | /* <IN> <skip> <set> */ |
932 | TRACE(("|%p|%p|IN\n", ctx->pattern, ctx->ptr)); |
933 | if (ctx->ptr >= end || !SRE_CHARSETsre_ucharset(ctx->pattern + 1, *ctx->ptr)) |
934 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
935 | ctx->pattern += ctx->pattern[0]; |
936 | ctx->ptr++; |
937 | break; |
938 | |
939 | case SRE_OP_LITERAL_IGNORE20: |
940 | TRACE(("|%p|%p|LITERAL_IGNORE %d\n", |
941 | ctx->pattern, ctx->ptr, ctx->pattern[0])); |
942 | if (ctx->ptr >= end || |
943 | state->lower(*ctx->ptr) != state->lower(*ctx->pattern)) |
944 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
945 | ctx->pattern++; |
946 | ctx->ptr++; |
947 | break; |
948 | |
949 | case SRE_OP_NOT_LITERAL_IGNORE25: |
950 | TRACE(("|%p|%p|NOT_LITERAL_IGNORE %d\n", |
951 | ctx->pattern, ctx->ptr, *ctx->pattern)); |
952 | if (ctx->ptr >= end || |
953 | state->lower(*ctx->ptr) == state->lower(*ctx->pattern)) |
954 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
955 | ctx->pattern++; |
956 | ctx->ptr++; |
957 | break; |
958 | |
959 | case SRE_OP_IN_IGNORE16: |
960 | TRACE(("|%p|%p|IN_IGNORE\n", ctx->pattern, ctx->ptr)); |
961 | if (ctx->ptr >= end |
962 | || !SRE_CHARSETsre_ucharset(ctx->pattern+1, |
963 | (SRE_CODEunsigned short)state->lower(*ctx->ptr))) |
964 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
965 | ctx->pattern += ctx->pattern[0]; |
966 | ctx->ptr++; |
967 | break; |
968 | |
969 | case SRE_OP_JUMP18: |
970 | case SRE_OP_INFO17: |
971 | /* jump forward */ |
972 | /* <JUMP> <offset> */ |
973 | TRACE(("|%p|%p|JUMP %d\n", ctx->pattern, |
974 | ctx->ptr, ctx->pattern[0])); |
975 | ctx->pattern += ctx->pattern[0]; |
976 | break; |
977 | |
978 | case SRE_OP_BRANCH7: |
979 | /* alternation */ |
980 | /* <BRANCH> <0=skip> code <JUMP> ... <NULL> */ |
981 | TRACE(("|%p|%p|BRANCH\n", ctx->pattern, ctx->ptr)); |
982 | LASTMARK_SAVE()do { ctx->lastmark = state->lastmark; ctx->lastindex = state->lastindex; } while (0); |
983 | ctx->u.rep = state->repeat; |
984 | if (ctx->u.rep) |
985 | MARK_PUSH(ctx->lastmark)do if (ctx->lastmark > 0) { i = ctx->lastmark; do { ; if (state->data_stack_size < state->data_stack_base +(i+1)*sizeof(void*)) { int j = data_stack_grow(state, (i+1)* sizeof(void*)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state->data_stack+ctx_pos ); } while (0); } ((__builtin_object_size (state->data_stack +state->data_stack_base, 0) != (size_t) -1) ? __builtin___memcpy_chk (state->data_stack+state->data_stack_base, state->mark , (i+1)*sizeof(void*), __builtin_object_size (state->data_stack +state->data_stack_base, 0)) : __inline_memcpy_chk (state-> data_stack+state->data_stack_base, state->mark, (i+1)*sizeof (void*))); state->data_stack_base += (i+1)*sizeof(void*); } while (0); } while (0); |
986 | for (; ctx->pattern[0]; ctx->pattern += ctx->pattern[0]) { |
987 | if (ctx->pattern[1] == SRE_OP_LITERAL19 && |
988 | (ctx->ptr >= end || |
989 | (SRE_CODEunsigned short) *ctx->ptr != ctx->pattern[2])) |
990 | continue; |
991 | if (ctx->pattern[1] == SRE_OP_IN15 && |
992 | (ctx->ptr >= end || |
993 | !SRE_CHARSETsre_ucharset(ctx->pattern + 3, (SRE_CODEunsigned short) *ctx->ptr))) |
994 | continue; |
995 | state->ptr = ctx->ptr; |
996 | DO_JUMP(JUMP_BRANCH, jump_branch, ctx->pattern+1)do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 11; nextctx->pattern = ctx-> pattern+1; ctx_pos = alloc_pos; ctx = nextctx; goto entrance; jump_branch: while (0); |
997 | if (ret) { |
998 | if (ctx->u.rep) |
999 | MARK_POP_DISCARD(ctx->lastmark)do if (ctx->lastmark > 0) { do { ; state->data_stack_base -= (ctx->lastmark+1)*sizeof(void*); } while(0); } while ( 0); |
1000 | RETURN_ON_ERROR(ret)do { if (ret < 0) do { return ret; } while(0); } while (0); |
1001 | RETURN_SUCCESSdo { ret = 1; goto exit; } while(0); |
1002 | } |
1003 | if (ctx->u.rep) |
1004 | MARK_POP_KEEP(ctx->lastmark)do if (ctx->lastmark > 0) { do { ; ((__builtin_object_size (state->mark, 0) != (size_t) -1) ? __builtin___memcpy_chk (state->mark, state->data_stack+state->data_stack_base -(ctx->lastmark+1)*sizeof(void*), (ctx->lastmark+1)*sizeof (void*), __builtin_object_size (state->mark, 0)) : __inline_memcpy_chk (state->mark, state->data_stack+state->data_stack_base -(ctx->lastmark+1)*sizeof(void*), (ctx->lastmark+1)*sizeof (void*))); if (0) state->data_stack_base -= (ctx->lastmark +1)*sizeof(void*); } while (0); } while (0); |
1005 | LASTMARK_RESTORE()do { state->lastmark = ctx->lastmark; state->lastindex = ctx->lastindex; } while (0); |
1006 | } |
1007 | if (ctx->u.rep) |
1008 | MARK_POP_DISCARD(ctx->lastmark)do if (ctx->lastmark > 0) { do { ; state->data_stack_base -= (ctx->lastmark+1)*sizeof(void*); } while(0); } while ( 0); |
1009 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1010 | |
1011 | case SRE_OP_REPEAT_ONE29: |
1012 | /* match repeated sequence (maximizing regexp) */ |
1013 | |
1014 | /* this operator only works if the repeated item is |
1015 | exactly one character wide, and we're not already |
1016 | collecting backtracking points. for other cases, |
1017 | use the MAX_REPEAT operator */ |
1018 | |
1019 | /* <REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */ |
1020 | |
1021 | TRACE(("|%p|%p|REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr, |
1022 | ctx->pattern[1], ctx->pattern[2])); |
1023 | |
1024 | if (ctx->ptr + ctx->pattern[1] > end) |
1025 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); /* cannot match */ |
1026 | |
1027 | state->ptr = ctx->ptr; |
1028 | |
1029 | ret = SRE_COUNTsre_ucount(state, ctx->pattern+3, ctx->pattern[2]); |
1030 | RETURN_ON_ERROR(ret)do { if (ret < 0) do { return ret; } while(0); } while (0); |
1031 | DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos)do { ; ctx = (sre_umatch_context*)(state->data_stack+ctx_pos ); } while (0); |
1032 | ctx->count = ret; |
1033 | ctx->ptr += ctx->count; |
1034 | |
1035 | /* when we arrive here, count contains the number of |
1036 | matches, and ctx->ptr points to the tail of the target |
1037 | string. check if the rest of the pattern matches, |
1038 | and backtrack if not. */ |
1039 | |
1040 | if (ctx->count < (Py_ssize_t) ctx->pattern[1]) |
1041 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1042 | |
1043 | if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS1) { |
1044 | /* tail is empty. we're finished */ |
1045 | state->ptr = ctx->ptr; |
1046 | RETURN_SUCCESSdo { ret = 1; goto exit; } while(0); |
1047 | } |
1048 | |
1049 | LASTMARK_SAVE()do { ctx->lastmark = state->lastmark; ctx->lastindex = state->lastindex; } while (0); |
1050 | |
1051 | if (ctx->pattern[ctx->pattern[0]] == SRE_OP_LITERAL19) { |
1052 | /* tail starts with a literal. skip positions where |
1053 | the rest of the pattern cannot possibly match */ |
1054 | ctx->u.chr = ctx->pattern[ctx->pattern[0]+1]; |
1055 | for (;;) { |
1056 | while (ctx->count >= (Py_ssize_t) ctx->pattern[1] && |
1057 | (ctx->ptr >= end || *ctx->ptr != ctx->u.chr)) { |
1058 | ctx->ptr--; |
1059 | ctx->count--; |
1060 | } |
1061 | if (ctx->count < (Py_ssize_t) ctx->pattern[1]) |
1062 | break; |
1063 | state->ptr = ctx->ptr; |
1064 | DO_JUMP(JUMP_REPEAT_ONE_1, jump_repeat_one_1,do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 8; nextctx->pattern = ctx-> pattern+ctx->pattern[0]; ctx_pos = alloc_pos; ctx = nextctx ; goto entrance; jump_repeat_one_1: while (0) |
1065 | ctx->pattern+ctx->pattern[0])do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 8; nextctx->pattern = ctx-> pattern+ctx->pattern[0]; ctx_pos = alloc_pos; ctx = nextctx ; goto entrance; jump_repeat_one_1: while (0); |
1066 | if (ret) { |
1067 | RETURN_ON_ERROR(ret)do { if (ret < 0) do { return ret; } while(0); } while (0); |
1068 | RETURN_SUCCESSdo { ret = 1; goto exit; } while(0); |
1069 | } |
1070 | |
1071 | LASTMARK_RESTORE()do { state->lastmark = ctx->lastmark; state->lastindex = ctx->lastindex; } while (0); |
1072 | |
1073 | ctx->ptr--; |
1074 | ctx->count--; |
1075 | } |
1076 | |
1077 | } else { |
1078 | /* general case */ |
1079 | while (ctx->count >= (Py_ssize_t) ctx->pattern[1]) { |
1080 | state->ptr = ctx->ptr; |
1081 | DO_JUMP(JUMP_REPEAT_ONE_2, jump_repeat_one_2,do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 9; nextctx->pattern = ctx-> pattern+ctx->pattern[0]; ctx_pos = alloc_pos; ctx = nextctx ; goto entrance; jump_repeat_one_2: while (0) |
1082 | ctx->pattern+ctx->pattern[0])do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 9; nextctx->pattern = ctx-> pattern+ctx->pattern[0]; ctx_pos = alloc_pos; ctx = nextctx ; goto entrance; jump_repeat_one_2: while (0); |
1083 | if (ret) { |
1084 | RETURN_ON_ERROR(ret)do { if (ret < 0) do { return ret; } while(0); } while (0); |
1085 | RETURN_SUCCESSdo { ret = 1; goto exit; } while(0); |
1086 | } |
1087 | ctx->ptr--; |
1088 | ctx->count--; |
1089 | LASTMARK_RESTORE()do { state->lastmark = ctx->lastmark; state->lastindex = ctx->lastindex; } while (0); |
1090 | } |
1091 | } |
1092 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1093 | |
1094 | case SRE_OP_MIN_REPEAT_ONE31: |
1095 | /* match repeated sequence (minimizing regexp) */ |
1096 | |
1097 | /* this operator only works if the repeated item is |
1098 | exactly one character wide, and we're not already |
1099 | collecting backtracking points. for other cases, |
1100 | use the MIN_REPEAT operator */ |
1101 | |
1102 | /* <MIN_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */ |
1103 | |
1104 | TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", ctx->pattern, ctx->ptr, |
1105 | ctx->pattern[1], ctx->pattern[2])); |
1106 | |
1107 | if (ctx->ptr + ctx->pattern[1] > end) |
1108 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); /* cannot match */ |
1109 | |
1110 | state->ptr = ctx->ptr; |
1111 | |
1112 | if (ctx->pattern[1] == 0) |
1113 | ctx->count = 0; |
1114 | else { |
1115 | /* count using pattern min as the maximum */ |
1116 | ret = SRE_COUNTsre_ucount(state, ctx->pattern+3, ctx->pattern[1]); |
1117 | RETURN_ON_ERROR(ret)do { if (ret < 0) do { return ret; } while(0); } while (0); |
1118 | DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos)do { ; ctx = (sre_umatch_context*)(state->data_stack+ctx_pos ); } while (0); |
1119 | if (ret < (Py_ssize_t) ctx->pattern[1]) |
1120 | /* didn't match minimum number of times */ |
1121 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1122 | /* advance past minimum matches of repeat */ |
1123 | ctx->count = ret; |
1124 | ctx->ptr += ctx->count; |
1125 | } |
1126 | |
1127 | if (ctx->pattern[ctx->pattern[0]] == SRE_OP_SUCCESS1) { |
1128 | /* tail is empty. we're finished */ |
1129 | state->ptr = ctx->ptr; |
1130 | RETURN_SUCCESSdo { ret = 1; goto exit; } while(0); |
1131 | |
1132 | } else { |
1133 | /* general case */ |
1134 | LASTMARK_SAVE()do { ctx->lastmark = state->lastmark; ctx->lastindex = state->lastindex; } while (0); |
1135 | while ((Py_ssize_t)ctx->pattern[2] == 65535 |
1136 | || ctx->count <= (Py_ssize_t)ctx->pattern[2]) { |
1137 | state->ptr = ctx->ptr; |
1138 | DO_JUMP(JUMP_MIN_REPEAT_ONE,jump_min_repeat_one,do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 10; nextctx->pattern = ctx-> pattern+ctx->pattern[0]; ctx_pos = alloc_pos; ctx = nextctx ; goto entrance; jump_min_repeat_one: while (0) |
1139 | ctx->pattern+ctx->pattern[0])do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 10; nextctx->pattern = ctx-> pattern+ctx->pattern[0]; ctx_pos = alloc_pos; ctx = nextctx ; goto entrance; jump_min_repeat_one: while (0); |
1140 | if (ret) { |
1141 | RETURN_ON_ERROR(ret)do { if (ret < 0) do { return ret; } while(0); } while (0); |
1142 | RETURN_SUCCESSdo { ret = 1; goto exit; } while(0); |
1143 | } |
1144 | state->ptr = ctx->ptr; |
1145 | ret = SRE_COUNTsre_ucount(state, ctx->pattern+3, 1); |
1146 | RETURN_ON_ERROR(ret)do { if (ret < 0) do { return ret; } while(0); } while (0); |
1147 | DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos)do { ; ctx = (sre_umatch_context*)(state->data_stack+ctx_pos ); } while (0); |
1148 | if (ret == 0) |
1149 | break; |
1150 | assert(ret == 1)(__builtin_expect(!(ret == 1), 0) ? __assert_rtn(__func__, "./Modules/_sre.c" , 1150, "ret == 1") : (void)0); |
1151 | ctx->ptr++; |
1152 | ctx->count++; |
1153 | LASTMARK_RESTORE()do { state->lastmark = ctx->lastmark; state->lastindex = ctx->lastindex; } while (0); |
1154 | } |
1155 | } |
1156 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1157 | |
1158 | case SRE_OP_REPEAT28: |
1159 | /* create repeat context. all the hard work is done |
1160 | by the UNTIL operator (MAX_UNTIL, MIN_UNTIL) */ |
1161 | /* <REPEAT> <skip> <1=min> <2=max> item <UNTIL> tail */ |
1162 | TRACE(("|%p|%p|REPEAT %d %d\n", ctx->pattern, ctx->ptr, |
1163 | ctx->pattern[1], ctx->pattern[2])); |
1164 | |
1165 | /* install new repeat context */ |
1166 | ctx->u.rep = (SRE_REPEAT*) PyObject_MALLOC_PyObject_DebugMalloc(sizeof(*ctx->u.rep)); |
1167 | if (!ctx->u.rep) { |
1168 | PyErr_NoMemory(); |
1169 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1170 | } |
1171 | ctx->u.rep->count = -1; |
1172 | ctx->u.rep->pattern = ctx->pattern; |
1173 | ctx->u.rep->prev = state->repeat; |
1174 | ctx->u.rep->last_ptr = NULL((void*)0); |
1175 | state->repeat = ctx->u.rep; |
1176 | |
1177 | state->ptr = ctx->ptr; |
1178 | DO_JUMP(JUMP_REPEAT, jump_repeat, ctx->pattern+ctx->pattern[0])do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 7; nextctx->pattern = ctx-> pattern+ctx->pattern[0]; ctx_pos = alloc_pos; ctx = nextctx ; goto entrance; jump_repeat: while (0); |
1179 | state->repeat = ctx->u.rep->prev; |
1180 | PyObject_FREE_PyObject_DebugFree(ctx->u.rep); |
1181 | |
1182 | if (ret) { |
1183 | RETURN_ON_ERROR(ret)do { if (ret < 0) do { return ret; } while(0); } while (0); |
1184 | RETURN_SUCCESSdo { ret = 1; goto exit; } while(0); |
1185 | } |
1186 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1187 | |
1188 | case SRE_OP_MAX_UNTIL22: |
1189 | /* maximizing repeat */ |
1190 | /* <REPEAT> <skip> <1=min> <2=max> item <MAX_UNTIL> tail */ |
1191 | |
1192 | /* FIXME: we probably need to deal with zero-width |
1193 | matches in here... */ |
1194 | |
1195 | ctx->u.rep = state->repeat; |
1196 | if (!ctx->u.rep) |
1197 | RETURN_ERROR(SRE_ERROR_STATE)do { return -2; } while(0); |
1198 | |
1199 | state->ptr = ctx->ptr; |
1200 | |
1201 | ctx->count = ctx->u.rep->count+1; |
1202 | |
1203 | TRACE(("|%p|%p|MAX_UNTIL %d\n", ctx->pattern, |
1204 | ctx->ptr, ctx->count)); |
1205 | |
1206 | if (ctx->count < ctx->u.rep->pattern[1]) { |
1207 | /* not enough matches */ |
1208 | ctx->u.rep->count = ctx->count; |
1209 | DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1,do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 1; nextctx->pattern = ctx-> u.rep->pattern+3; ctx_pos = alloc_pos; ctx = nextctx; goto entrance; jump_max_until_1: while (0) |
1210 | ctx->u.rep->pattern+3)do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 1; nextctx->pattern = ctx-> u.rep->pattern+3; ctx_pos = alloc_pos; ctx = nextctx; goto entrance; jump_max_until_1: while (0); |
1211 | if (ret) { |
1212 | RETURN_ON_ERROR(ret)do { if (ret < 0) do { return ret; } while(0); } while (0); |
1213 | RETURN_SUCCESSdo { ret = 1; goto exit; } while(0); |
1214 | } |
1215 | ctx->u.rep->count = ctx->count-1; |
1216 | state->ptr = ctx->ptr; |
1217 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1218 | } |
1219 | |
1220 | if ((ctx->count < ctx->u.rep->pattern[2] || |
1221 | ctx->u.rep->pattern[2] == 65535) && |
1222 | state->ptr != ctx->u.rep->last_ptr) { |
1223 | /* we may have enough matches, but if we can |
1224 | match another item, do so */ |
1225 | ctx->u.rep->count = ctx->count; |
1226 | LASTMARK_SAVE()do { ctx->lastmark = state->lastmark; ctx->lastindex = state->lastindex; } while (0); |
1227 | MARK_PUSH(ctx->lastmark)do if (ctx->lastmark > 0) { i = ctx->lastmark; do { ; if (state->data_stack_size < state->data_stack_base +(i+1)*sizeof(void*)) { int j = data_stack_grow(state, (i+1)* sizeof(void*)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state->data_stack+ctx_pos ); } while (0); } ((__builtin_object_size (state->data_stack +state->data_stack_base, 0) != (size_t) -1) ? __builtin___memcpy_chk (state->data_stack+state->data_stack_base, state->mark , (i+1)*sizeof(void*), __builtin_object_size (state->data_stack +state->data_stack_base, 0)) : __inline_memcpy_chk (state-> data_stack+state->data_stack_base, state->mark, (i+1)*sizeof (void*))); state->data_stack_base += (i+1)*sizeof(void*); } while (0); } while (0); |
1228 | /* zero-width match protection */ |
1229 | DATA_PUSH(&ctx->u.rep->last_ptr)do { ; if (state->data_stack_size < state->data_stack_base +sizeof(*(&ctx->u.rep->last_ptr))) { int j = data_stack_grow (state, sizeof(*(&ctx->u.rep->last_ptr))); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context *)(state->data_stack+ctx_pos); } while (0); } ((__builtin_object_size (state->data_stack+state->data_stack_base, 0) != (size_t ) -1) ? __builtin___memcpy_chk (state->data_stack+state-> data_stack_base, (&ctx->u.rep->last_ptr), sizeof(*( &ctx->u.rep->last_ptr)), __builtin_object_size (state ->data_stack+state->data_stack_base, 0)) : __inline_memcpy_chk (state->data_stack+state->data_stack_base, (&ctx-> u.rep->last_ptr), sizeof(*(&ctx->u.rep->last_ptr )))); state->data_stack_base += sizeof(*(&ctx->u.rep ->last_ptr)); } while (0); |
1230 | ctx->u.rep->last_ptr = state->ptr; |
1231 | DO_JUMP(JUMP_MAX_UNTIL_2, jump_max_until_2,do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 2; nextctx->pattern = ctx-> u.rep->pattern+3; ctx_pos = alloc_pos; ctx = nextctx; goto entrance; jump_max_until_2: while (0) |
1232 | ctx->u.rep->pattern+3)do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 2; nextctx->pattern = ctx-> u.rep->pattern+3; ctx_pos = alloc_pos; ctx = nextctx; goto entrance; jump_max_until_2: while (0); |
1233 | DATA_POP(&ctx->u.rep->last_ptr)do { ; ((__builtin_object_size ((&ctx->u.rep->last_ptr ), 0) != (size_t) -1) ? __builtin___memcpy_chk ((&ctx-> u.rep->last_ptr), state->data_stack+state->data_stack_base -sizeof(*(&ctx->u.rep->last_ptr)), sizeof(*(&ctx ->u.rep->last_ptr)), __builtin_object_size ((&ctx-> u.rep->last_ptr), 0)) : __inline_memcpy_chk ((&ctx-> u.rep->last_ptr), state->data_stack+state->data_stack_base -sizeof(*(&ctx->u.rep->last_ptr)), sizeof(*(&ctx ->u.rep->last_ptr)))); if (1) state->data_stack_base -= sizeof(*(&ctx->u.rep->last_ptr)); } while (0); |
1234 | if (ret) { |
1235 | MARK_POP_DISCARD(ctx->lastmark)do if (ctx->lastmark > 0) { do { ; state->data_stack_base -= (ctx->lastmark+1)*sizeof(void*); } while(0); } while ( 0); |
1236 | RETURN_ON_ERROR(ret)do { if (ret < 0) do { return ret; } while(0); } while (0); |
1237 | RETURN_SUCCESSdo { ret = 1; goto exit; } while(0); |
1238 | } |
1239 | MARK_POP(ctx->lastmark)do if (ctx->lastmark > 0) { do { ; ((__builtin_object_size (state->mark, 0) != (size_t) -1) ? __builtin___memcpy_chk (state->mark, state->data_stack+state->data_stack_base -(ctx->lastmark+1)*sizeof(void*), (ctx->lastmark+1)*sizeof (void*), __builtin_object_size (state->mark, 0)) : __inline_memcpy_chk (state->mark, state->data_stack+state->data_stack_base -(ctx->lastmark+1)*sizeof(void*), (ctx->lastmark+1)*sizeof (void*))); if (1) state->data_stack_base -= (ctx->lastmark +1)*sizeof(void*); } while (0); } while (0); |
1240 | LASTMARK_RESTORE()do { state->lastmark = ctx->lastmark; state->lastindex = ctx->lastindex; } while (0); |
1241 | ctx->u.rep->count = ctx->count-1; |
1242 | state->ptr = ctx->ptr; |
1243 | } |
1244 | |
1245 | /* cannot match more repeated items here. make sure the |
1246 | tail matches */ |
1247 | state->repeat = ctx->u.rep->prev; |
1248 | DO_JUMP(JUMP_MAX_UNTIL_3, jump_max_until_3, ctx->pattern)do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 3; nextctx->pattern = ctx-> pattern; ctx_pos = alloc_pos; ctx = nextctx; goto entrance; jump_max_until_3 : while (0); |
1249 | RETURN_ON_SUCCESS(ret)do { do { if (ret < 0) do { return ret; } while(0); } while (0); if (ret > 0) do { ret = 1; goto exit; } while(0); } while (0); |
1250 | state->repeat = ctx->u.rep; |
1251 | state->ptr = ctx->ptr; |
1252 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1253 | |
1254 | case SRE_OP_MIN_UNTIL23: |
1255 | /* minimizing repeat */ |
1256 | /* <REPEAT> <skip> <1=min> <2=max> item <MIN_UNTIL> tail */ |
1257 | |
1258 | ctx->u.rep = state->repeat; |
1259 | if (!ctx->u.rep) |
1260 | RETURN_ERROR(SRE_ERROR_STATE)do { return -2; } while(0); |
1261 | |
1262 | state->ptr = ctx->ptr; |
1263 | |
1264 | ctx->count = ctx->u.rep->count+1; |
1265 | |
1266 | TRACE(("|%p|%p|MIN_UNTIL %d %p\n", ctx->pattern, |
1267 | ctx->ptr, ctx->count, ctx->u.rep->pattern)); |
1268 | |
1269 | if (ctx->count < ctx->u.rep->pattern[1]) { |
1270 | /* not enough matches */ |
1271 | ctx->u.rep->count = ctx->count; |
1272 | DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1,do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 4; nextctx->pattern = ctx-> u.rep->pattern+3; ctx_pos = alloc_pos; ctx = nextctx; goto entrance; jump_min_until_1: while (0) |
1273 | ctx->u.rep->pattern+3)do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 4; nextctx->pattern = ctx-> u.rep->pattern+3; ctx_pos = alloc_pos; ctx = nextctx; goto entrance; jump_min_until_1: while (0); |
1274 | if (ret) { |
1275 | RETURN_ON_ERROR(ret)do { if (ret < 0) do { return ret; } while(0); } while (0); |
1276 | RETURN_SUCCESSdo { ret = 1; goto exit; } while(0); |
1277 | } |
1278 | ctx->u.rep->count = ctx->count-1; |
1279 | state->ptr = ctx->ptr; |
1280 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1281 | } |
1282 | |
1283 | LASTMARK_SAVE()do { ctx->lastmark = state->lastmark; ctx->lastindex = state->lastindex; } while (0); |
1284 | |
1285 | /* see if the tail matches */ |
1286 | state->repeat = ctx->u.rep->prev; |
1287 | DO_JUMP(JUMP_MIN_UNTIL_2, jump_min_until_2, ctx->pattern)do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 5; nextctx->pattern = ctx-> pattern; ctx_pos = alloc_pos; ctx = nextctx; goto entrance; jump_min_until_2 : while (0); |
1288 | if (ret) { |
1289 | RETURN_ON_ERROR(ret)do { if (ret < 0) do { return ret; } while(0); } while (0); |
1290 | RETURN_SUCCESSdo { ret = 1; goto exit; } while(0); |
1291 | } |
1292 | |
1293 | state->repeat = ctx->u.rep; |
1294 | state->ptr = ctx->ptr; |
1295 | |
1296 | LASTMARK_RESTORE()do { state->lastmark = ctx->lastmark; state->lastindex = ctx->lastindex; } while (0); |
1297 | |
1298 | if (ctx->count >= ctx->u.rep->pattern[2] |
1299 | && ctx->u.rep->pattern[2] != 65535) |
1300 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1301 | |
1302 | ctx->u.rep->count = ctx->count; |
1303 | DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3,do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 6; nextctx->pattern = ctx-> u.rep->pattern+3; ctx_pos = alloc_pos; ctx = nextctx; goto entrance; jump_min_until_3: while (0) |
1304 | ctx->u.rep->pattern+3)do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 6; nextctx->pattern = ctx-> u.rep->pattern+3; ctx_pos = alloc_pos; ctx = nextctx; goto entrance; jump_min_until_3: while (0); |
1305 | if (ret) { |
1306 | RETURN_ON_ERROR(ret)do { if (ret < 0) do { return ret; } while(0); } while (0); |
1307 | RETURN_SUCCESSdo { ret = 1; goto exit; } while(0); |
1308 | } |
1309 | ctx->u.rep->count = ctx->count-1; |
1310 | state->ptr = ctx->ptr; |
1311 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1312 | |
1313 | case SRE_OP_GROUPREF12: |
1314 | /* match backreference */ |
1315 | TRACE(("|%p|%p|GROUPREF %d\n", ctx->pattern, |
1316 | ctx->ptr, ctx->pattern[0])); |
1317 | i = ctx->pattern[0]; |
1318 | { |
1319 | Py_ssize_t groupref = i+i; |
1320 | if (groupref >= state->lastmark) { |
1321 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1322 | } else { |
1323 | SRE_CHARPy_UNICODE* p = (SRE_CHARPy_UNICODE*) state->mark[groupref]; |
1324 | SRE_CHARPy_UNICODE* e = (SRE_CHARPy_UNICODE*) state->mark[groupref+1]; |
1325 | if (!p || !e || e < p) |
1326 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1327 | while (p < e) { |
1328 | if (ctx->ptr >= end || *ctx->ptr != *p) |
1329 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1330 | p++; ctx->ptr++; |
1331 | } |
1332 | } |
1333 | } |
1334 | ctx->pattern++; |
1335 | break; |
1336 | |
1337 | case SRE_OP_GROUPREF_IGNORE14: |
1338 | /* match backreference */ |
1339 | TRACE(("|%p|%p|GROUPREF_IGNORE %d\n", ctx->pattern, |
1340 | ctx->ptr, ctx->pattern[0])); |
1341 | i = ctx->pattern[0]; |
1342 | { |
1343 | Py_ssize_t groupref = i+i; |
1344 | if (groupref >= state->lastmark) { |
1345 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1346 | } else { |
1347 | SRE_CHARPy_UNICODE* p = (SRE_CHARPy_UNICODE*) state->mark[groupref]; |
1348 | SRE_CHARPy_UNICODE* e = (SRE_CHARPy_UNICODE*) state->mark[groupref+1]; |
1349 | if (!p || !e || e < p) |
1350 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1351 | while (p < e) { |
1352 | if (ctx->ptr >= end || |
1353 | state->lower(*ctx->ptr) != state->lower(*p)) |
1354 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1355 | p++; ctx->ptr++; |
1356 | } |
1357 | } |
1358 | } |
1359 | ctx->pattern++; |
1360 | break; |
1361 | |
1362 | case SRE_OP_GROUPREF_EXISTS13: |
1363 | TRACE(("|%p|%p|GROUPREF_EXISTS %d\n", ctx->pattern, |
1364 | ctx->ptr, ctx->pattern[0])); |
1365 | /* <GROUPREF_EXISTS> <group> <skip> codeyes <JUMP> codeno ... */ |
1366 | i = ctx->pattern[0]; |
1367 | { |
1368 | Py_ssize_t groupref = i+i; |
1369 | if (groupref >= state->lastmark) { |
1370 | ctx->pattern += ctx->pattern[1]; |
1371 | break; |
1372 | } else { |
1373 | SRE_CHARPy_UNICODE* p = (SRE_CHARPy_UNICODE*) state->mark[groupref]; |
1374 | SRE_CHARPy_UNICODE* e = (SRE_CHARPy_UNICODE*) state->mark[groupref+1]; |
1375 | if (!p || !e || e < p) { |
1376 | ctx->pattern += ctx->pattern[1]; |
1377 | break; |
1378 | } |
1379 | } |
1380 | } |
1381 | ctx->pattern += 2; |
1382 | break; |
1383 | |
1384 | case SRE_OP_ASSERT4: |
1385 | /* assert subpattern */ |
1386 | /* <ASSERT> <skip> <back> <pattern> */ |
1387 | TRACE(("|%p|%p|ASSERT %d\n", ctx->pattern, |
1388 | ctx->ptr, ctx->pattern[1])); |
1389 | state->ptr = ctx->ptr - ctx->pattern[1]; |
1390 | if (state->ptr < state->beginning) |
1391 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1392 | DO_JUMP(JUMP_ASSERT, jump_assert, ctx->pattern+2)do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 12; nextctx->pattern = ctx-> pattern+2; ctx_pos = alloc_pos; ctx = nextctx; goto entrance; jump_assert: while (0); |
1393 | RETURN_ON_FAILURE(ret)do { do { if (ret < 0) do { return ret; } while(0); } while (0); if (ret == 0) do { ret = 0; goto exit; } while(0); } while (0); |
1394 | ctx->pattern += ctx->pattern[0]; |
1395 | break; |
1396 | |
1397 | case SRE_OP_ASSERT_NOT5: |
1398 | /* assert not subpattern */ |
1399 | /* <ASSERT_NOT> <skip> <back> <pattern> */ |
1400 | TRACE(("|%p|%p|ASSERT_NOT %d\n", ctx->pattern, |
1401 | ctx->ptr, ctx->pattern[1])); |
1402 | state->ptr = ctx->ptr - ctx->pattern[1]; |
1403 | if (state->ptr >= state->beginning) { |
1404 | DO_JUMP(JUMP_ASSERT_NOT, jump_assert_not, ctx->pattern+2)do { alloc_pos = state->data_stack_base; ; if (state->data_stack_size < alloc_pos+sizeof(sre_umatch_context)) { int j = data_stack_grow (state, sizeof(sre_umatch_context)); if (j < 0) return j; if (ctx_pos != -1) do { ; ctx = (sre_umatch_context*)(state-> data_stack+ctx_pos); } while (0); } nextctx = (sre_umatch_context *)(state->data_stack+alloc_pos); state->data_stack_base += sizeof(sre_umatch_context); } while (0); nextctx->last_ctx_pos = ctx_pos; nextctx->jump = 13; nextctx->pattern = ctx-> pattern+2; ctx_pos = alloc_pos; ctx = nextctx; goto entrance; jump_assert_not: while (0); |
1405 | if (ret) { |
1406 | RETURN_ON_ERROR(ret)do { if (ret < 0) do { return ret; } while(0); } while (0); |
1407 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1408 | } |
1409 | } |
1410 | ctx->pattern += ctx->pattern[0]; |
1411 | break; |
1412 | |
1413 | case SRE_OP_FAILURE0: |
1414 | /* immediate failure */ |
1415 | TRACE(("|%p|%p|FAILURE\n", ctx->pattern, ctx->ptr)); |
1416 | RETURN_FAILUREdo { ret = 0; goto exit; } while(0); |
1417 | |
1418 | default: |
1419 | TRACE(("|%p|%p|UNKNOWN %d\n", ctx->pattern, ctx->ptr, |
1420 | ctx->pattern[-1])); |
1421 | RETURN_ERROR(SRE_ERROR_ILLEGAL)do { return -1; } while(0); |
1422 | } |
1423 | } |
1424 | |
1425 | exit: |
1426 | ctx_pos = ctx->last_ctx_pos; |
1427 | jump = ctx->jump; |
1428 | DATA_POP_DISCARD(ctx)do { ; state->data_stack_base -= sizeof(*(ctx)); } while(0 ); |
1429 | if (ctx_pos == -1) |
1430 | return ret; |
1431 | DATA_LOOKUP_AT(SRE_MATCH_CONTEXT, ctx, ctx_pos)do { ; ctx = (sre_umatch_context*)(state->data_stack+ctx_pos ); } while (0); |
1432 | |
1433 | switch (jump) { |
1434 | case JUMP_MAX_UNTIL_22: |
1435 | TRACE(("|%p|%p|JUMP_MAX_UNTIL_2\n", ctx->pattern, ctx->ptr)); |
1436 | goto jump_max_until_2; |
1437 | case JUMP_MAX_UNTIL_33: |
1438 | TRACE(("|%p|%p|JUMP_MAX_UNTIL_3\n", ctx->pattern, ctx->ptr)); |
1439 | goto jump_max_until_3; |
1440 | case JUMP_MIN_UNTIL_25: |
1441 | TRACE(("|%p|%p|JUMP_MIN_UNTIL_2\n", ctx->pattern, ctx->ptr)); |
1442 | goto jump_min_until_2; |
1443 | case JUMP_MIN_UNTIL_36: |
1444 | TRACE(("|%p|%p|JUMP_MIN_UNTIL_3\n", ctx->pattern, ctx->ptr)); |
1445 | goto jump_min_until_3; |
1446 | case JUMP_BRANCH11: |
1447 | TRACE(("|%p|%p|JUMP_BRANCH\n", ctx->pattern, ctx->ptr)); |
1448 | goto jump_branch; |
1449 | case JUMP_MAX_UNTIL_11: |
1450 | TRACE(("|%p|%p|JUMP_MAX_UNTIL_1\n", ctx->pattern, ctx->ptr)); |
1451 | goto jump_max_until_1; |
1452 | case JUMP_MIN_UNTIL_14: |
1453 | TRACE(("|%p|%p|JUMP_MIN_UNTIL_1\n", ctx->pattern, ctx->ptr)); |
1454 | goto jump_min_until_1; |
1455 | case JUMP_REPEAT7: |
1456 | TRACE(("|%p|%p|JUMP_REPEAT\n", ctx->pattern, ctx->ptr)); |
1457 | goto jump_repeat; |
1458 | case JUMP_REPEAT_ONE_18: |
1459 | TRACE(("|%p|%p|JUMP_REPEAT_ONE_1\n", ctx->pattern, ctx->ptr)); |
1460 | goto jump_repeat_one_1; |
1461 | case JUMP_REPEAT_ONE_29: |
1462 | TRACE(("|%p|%p|JUMP_REPEAT_ONE_2\n", ctx->pattern, ctx->ptr)); |
1463 | goto jump_repeat_one_2; |
1464 | case JUMP_MIN_REPEAT_ONE10: |
1465 | TRACE(("|%p|%p|JUMP_MIN_REPEAT_ONE\n", ctx->pattern, ctx->ptr)); |
1466 | goto jump_min_repeat_one; |
1467 | case JUMP_ASSERT12: |
1468 | TRACE(("|%p|%p|JUMP_ASSERT\n", ctx->pattern, ctx->ptr)); |
1469 | goto jump_assert; |
1470 | case JUMP_ASSERT_NOT13: |
1471 | TRACE(("|%p|%p|JUMP_ASSERT_NOT\n", ctx->pattern, ctx->ptr)); |
1472 | goto jump_assert_not; |
1473 | case JUMP_NONE0: |
1474 | TRACE(("|%p|%p|RETURN %d\n", ctx->pattern, ctx->ptr, ret)); |
1475 | break; |
1476 | } |
1477 | |
1478 | return ret; /* should never get here */ |
1479 | } |
1480 | |
1481 | LOCAL(Py_ssize_t)static inline Py_ssize_t |
1482 | SRE_SEARCHsre_usearch(SRE_STATE* state, SRE_CODEunsigned short* pattern) |
1483 | { |
1484 | SRE_CHARPy_UNICODE* ptr = (SRE_CHARPy_UNICODE *)state->start; |
1485 | SRE_CHARPy_UNICODE* end = (SRE_CHARPy_UNICODE *)state->end; |
1486 | Py_ssize_t status = 0; |
1487 | Py_ssize_t prefix_len = 0; |
1488 | Py_ssize_t prefix_skip = 0; |
1489 | SRE_CODEunsigned short* prefix = NULL((void*)0); |
1490 | SRE_CODEunsigned short* charset = NULL((void*)0); |
1491 | SRE_CODEunsigned short* overlap = NULL((void*)0); |
1492 | int flags = 0; |
1493 | |
1494 | if (pattern[0] == SRE_OP_INFO17) { |
1495 | /* optimization info block */ |
1496 | /* <INFO> <1=skip> <2=flags> <3=min> <4=max> <5=prefix info> */ |
1497 | |
1498 | flags = pattern[2]; |
1499 | |
1500 | if (pattern[3] > 1) { |
1501 | /* adjust end point (but make sure we leave at least one |
1502 | character in there, so literal search will work) */ |
1503 | end -= pattern[3]-1; |
1504 | if (end <= ptr) |
1505 | end = ptr+1; |
1506 | } |
1507 | |
1508 | if (flags & SRE_INFO_PREFIX1) { |
1509 | /* pattern starts with a known prefix */ |
1510 | /* <length> <skip> <prefix data> <overlap data> */ |
1511 | prefix_len = pattern[5]; |
1512 | prefix_skip = pattern[6]; |
1513 | prefix = pattern + 7; |
1514 | overlap = prefix + prefix_len - 1; |
1515 | } else if (flags & SRE_INFO_CHARSET4) |
1516 | /* pattern starts with a character from a known set */ |
1517 | /* <charset> */ |
1518 | charset = pattern + 5; |
1519 | |
1520 | pattern += 1 + pattern[1]; |
1521 | } |
1522 | |
1523 | TRACE(("prefix = %p %d %d\n", prefix, prefix_len, prefix_skip)); |
1524 | TRACE(("charset = %p\n", charset)); |
1525 | |
1526 | #if defined(USE_FAST_SEARCH) |
1527 | if (prefix_len > 1) { |
1528 | /* pattern starts with a known prefix. use the overlap |
1529 | table to skip forward as fast as we possibly can */ |
1530 | Py_ssize_t i = 0; |
1531 | end = (SRE_CHARPy_UNICODE *)state->end; |
1532 | while (ptr < end) { |
1533 | for (;;) { |
1534 | if ((SRE_CODEunsigned short) ptr[0] != prefix[i]) { |
1535 | if (!i) |
1536 | break; |
1537 | else |
1538 | i = overlap[i]; |
1539 | } else { |
1540 | if (++i == prefix_len) { |
1541 | /* found a potential match */ |
1542 | TRACE(("|%p|%p|SEARCH SCAN\n", pattern, ptr)); |
1543 | state->start = ptr + 1 - prefix_len; |
1544 | state->ptr = ptr + 1 - prefix_len + prefix_skip; |
1545 | if (flags & SRE_INFO_LITERAL2) |
1546 | return 1; /* we got all of it */ |
1547 | status = SRE_MATCHsre_umatch(state, pattern + 2*prefix_skip); |
1548 | if (status != 0) |
1549 | return status; |
1550 | /* close but no cigar -- try again */ |
1551 | i = overlap[i]; |
1552 | } |
1553 | break; |
1554 | } |
1555 | } |
1556 | ptr++; |
1557 | } |
1558 | return 0; |
1559 | } |
1560 | #endif |
1561 | |
1562 | if (pattern[0] == SRE_OP_LITERAL19) { |
1563 | /* pattern starts with a literal character. this is used |
1564 | for short prefixes, and if fast search is disabled */ |
1565 | SRE_CODEunsigned short chr = pattern[1]; |
1566 | end = (SRE_CHARPy_UNICODE *)state->end; |
1567 | for (;;) { |
1568 | while (ptr < end && (SRE_CODEunsigned short) ptr[0] != chr) |
1569 | ptr++; |
1570 | if (ptr >= end) |
1571 | return 0; |
1572 | TRACE(("|%p|%p|SEARCH LITERAL\n", pattern, ptr)); |
1573 | state->start = ptr; |
1574 | state->ptr = ++ptr; |
1575 | if (flags & SRE_INFO_LITERAL2) |
1576 | return 1; /* we got all of it */ |
1577 | status = SRE_MATCHsre_umatch(state, pattern + 2); |
1578 | if (status != 0) |
1579 | break; |
1580 | } |
1581 | } else if (charset) { |
1582 | /* pattern starts with a character from a known set */ |
1583 | end = (SRE_CHARPy_UNICODE *)state->end; |
1584 | for (;;) { |
1585 | while (ptr < end && !SRE_CHARSETsre_ucharset(charset, ptr[0])) |
1586 | ptr++; |
1587 | if (ptr >= end) |
1588 | return 0; |
1589 | TRACE(("|%p|%p|SEARCH CHARSET\n", pattern, ptr)); |
1590 | state->start = ptr; |
1591 | state->ptr = ptr; |
1592 | status = SRE_MATCHsre_umatch(state, pattern); |
1593 | if (status != 0) |
1594 | break; |
1595 | ptr++; |
1596 | } |
1597 | } else |
1598 | /* general case */ |
1599 | while (ptr <= end) { |
1600 | TRACE(("|%p|%p|SEARCH\n", pattern, ptr)); |
1601 | state->start = state->ptr = ptr++; |
1602 | status = SRE_MATCHsre_umatch(state, pattern); |
1603 | if (status != 0) |
1604 | break; |
1605 | } |
1606 | |
1607 | return status; |
1608 | } |
1609 | |
1610 | LOCAL(int)static inline int |
1611 | SRE_LITERAL_TEMPLATEsre_uliteral_template(SRE_CHARPy_UNICODE* ptr, Py_ssize_t len) |
1612 | { |
1613 | /* check if given string is a literal template (i.e. no escapes) */ |
1614 | while (len-- > 0) |
1615 | if (*ptr++ == '\\') |
1616 | return 0; |
1617 | return 1; |
1618 | } |
1619 | |
1620 | #if !defined(SRE_RECURSIVE) |
1621 | |
1622 | /* -------------------------------------------------------------------- */ |
1623 | /* factories and destructors */ |
1624 | |
1625 | /* see sre.h for object declarations */ |
1626 | static PyObject*pattern_new_match(PatternObject*, SRE_STATE*, int); |
1627 | static PyObject*pattern_scanner(PatternObject*, PyObject*); |
1628 | |
1629 | static PyObject * |
1630 | sre_codesize(PyObject* self, PyObject *unused) |
1631 | { |
1632 | return Py_BuildValue_Py_BuildValue_SizeT("l", sizeof(SRE_CODEunsigned short)); |
1633 | } |
1634 | |
1635 | static PyObject * |
1636 | sre_getlower(PyObject* self, PyObject* args) |
1637 | { |
1638 | int character, flags; |
1639 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "ii", &character, &flags)) |
1640 | return NULL((void*)0); |
1641 | if (flags & SRE_FLAG_LOCALE4) |
1642 | return Py_BuildValue_Py_BuildValue_SizeT("i", sre_lower_locale(character)); |
1643 | if (flags & SRE_FLAG_UNICODE32) |
1644 | #if defined(HAVE_UNICODE) |
1645 | return Py_BuildValue_Py_BuildValue_SizeT("i", sre_lower_unicode(character)); |
1646 | #else |
1647 | return Py_BuildValue_Py_BuildValue_SizeT("i", sre_lower_locale(character)); |
1648 | #endif |
1649 | return Py_BuildValue_Py_BuildValue_SizeT("i", sre_lower(character)); |
1650 | } |
1651 | |
1652 | LOCAL(void)static inline void |
1653 | state_reset(SRE_STATE* state) |
1654 | { |
1655 | /* FIXME: dynamic! */ |
1656 | /*memset(state->mark, 0, sizeof(*state->mark) * SRE_MARK_SIZE);*/ |
1657 | |
1658 | state->lastmark = -1; |
1659 | state->lastindex = -1; |
1660 | |
1661 | state->repeat = NULL((void*)0); |
1662 | |
1663 | data_stack_dealloc(state); |
1664 | } |
1665 | |
1666 | static void* |
1667 | getstring(PyObject* string, Py_ssize_t* p_length, int* p_charsize) |
1668 | { |
1669 | /* given a python object, return a data pointer, a length (in |
1670 | characters), and a character size. return NULL if the object |
1671 | is not a string (or not compatible) */ |
1672 | |
1673 | PyBufferProcs *buffer; |
1674 | Py_ssize_t size, bytes; |
1675 | int charsize; |
1676 | void* ptr; |
1677 | Py_buffer view; |
1678 | |
1679 | /* Unicode objects do not support the buffer API. So, get the data |
1680 | directly instead. */ |
1681 | if (PyUnicode_Check(string)((((((PyObject*)(string))->ob_type))->tp_flags & (( 1L<<28))) != 0)) { |
1682 | ptr = (void *)PyUnicode_AS_DATA(string)((__builtin_expect(!(((((((PyObject*)(string))->ob_type))-> tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__ , "./Modules/_sre.c", 1682, "PyUnicode_Check(string)") : (void )0),((const char *)((PyUnicodeObject *)(string))->str)); |
1683 | *p_length = PyUnicode_GET_SIZE(string)((__builtin_expect(!(((((((PyObject*)(string))->ob_type))-> tp_flags & ((1L<<28))) != 0)), 0) ? __assert_rtn(__func__ , "./Modules/_sre.c", 1683, "PyUnicode_Check(string)") : (void )0),(((PyUnicodeObject *)(string))->length)); |
1684 | *p_charsize = sizeof(Py_UNICODE); |
1685 | return ptr; |
1686 | } |
1687 | |
1688 | /* get pointer to string buffer */ |
1689 | view.len = -1; |
1690 | buffer = Py_TYPE(string)(((PyObject*)(string))->ob_type)->tp_as_buffer; |
1691 | if (!buffer || !buffer->bf_getbuffer || |
1692 | (*buffer->bf_getbuffer)(string, &view, PyBUF_SIMPLE0) < 0) { |
1693 | PyErr_SetString(PyExc_TypeError, "expected string or buffer"); |
1694 | return NULL((void*)0); |
1695 | } |
1696 | |
1697 | /* determine buffer size */ |
1698 | bytes = view.len; |
1699 | ptr = view.buf; |
1700 | |
1701 | /* Release the buffer immediately --- possibly dangerous |
1702 | but doing something else would require some re-factoring |
1703 | */ |
1704 | PyBuffer_Release(&view); |
1705 | |
1706 | if (bytes < 0) { |
1707 | PyErr_SetString(PyExc_TypeError, "buffer has negative size"); |
1708 | return NULL((void*)0); |
1709 | } |
1710 | |
1711 | /* determine character size */ |
1712 | size = PyObject_Size(string); |
1713 | |
1714 | if (PyBytes_Check(string)((((((PyObject*)(string))->ob_type))->tp_flags & (( 1L<<27))) != 0) || bytes == size) |
1715 | charsize = 1; |
1716 | #if defined(HAVE_UNICODE) |
1717 | else if (bytes == (Py_ssize_t) (size * sizeof(Py_UNICODE))) |
1718 | charsize = sizeof(Py_UNICODE); |
1719 | #endif |
1720 | else { |
1721 | PyErr_SetString(PyExc_TypeError, "buffer size mismatch"); |
1722 | return NULL((void*)0); |
1723 | } |
1724 | |
1725 | *p_length = size; |
1726 | *p_charsize = charsize; |
1727 | |
1728 | if (ptr == NULL((void*)0)) { |
1729 | PyErr_SetString(PyExc_ValueError, |
1730 | "Buffer is NULL"); |
1731 | } |
1732 | return ptr; |
1733 | } |
1734 | |
1735 | LOCAL(PyObject*)static inline PyObject* |
1736 | state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string, |
1737 | Py_ssize_t start, Py_ssize_t end) |
1738 | { |
1739 | /* prepare state object */ |
1740 | |
1741 | Py_ssize_t length; |
1742 | int charsize; |
1743 | void* ptr; |
1744 | |
1745 | memset(state, 0, sizeof(SRE_STATE))((__builtin_object_size (state, 0) != (size_t) -1) ? __builtin___memset_chk (state, 0, sizeof(SRE_STATE), __builtin_object_size (state, 0 )) : __inline_memset_chk (state, 0, sizeof(SRE_STATE))); |
1746 | |
1747 | state->lastmark = -1; |
1748 | state->lastindex = -1; |
1749 | |
1750 | ptr = getstring(string, &length, &charsize); |
1751 | if (!ptr) |
1752 | return NULL((void*)0); |
1753 | |
1754 | if (charsize == 1 && pattern->charsize > 1) { |
1755 | PyErr_SetString(PyExc_TypeError, |
1756 | "can't use a string pattern on a bytes-like object"); |
1757 | return NULL((void*)0); |
1758 | } |
1759 | if (charsize > 1 && pattern->charsize == 1) { |
1760 | PyErr_SetString(PyExc_TypeError, |
1761 | "can't use a bytes pattern on a string-like object"); |
1762 | return NULL((void*)0); |
1763 | } |
1764 | |
1765 | /* adjust boundaries */ |
1766 | if (start < 0) |
1767 | start = 0; |
1768 | else if (start > length) |
1769 | start = length; |
1770 | |
1771 | if (end < 0) |
1772 | end = 0; |
1773 | else if (end > length) |
1774 | end = length; |
1775 | |
1776 | state->charsize = charsize; |
1777 | |
1778 | state->beginning = ptr; |
1779 | |
1780 | state->start = (void*) ((char*) ptr + start * state->charsize); |
1781 | state->end = (void*) ((char*) ptr + end * state->charsize); |
1782 | |
1783 | Py_INCREF(string)( _Py_RefTotal++ , ((PyObject*)(string))->ob_refcnt++); |
1784 | state->string = string; |
1785 | state->pos = start; |
1786 | state->endpos = end; |
1787 | |
1788 | if (pattern->flags & SRE_FLAG_LOCALE4) |
1789 | state->lower = sre_lower_locale; |
1790 | else if (pattern->flags & SRE_FLAG_UNICODE32) |
1791 | #if defined(HAVE_UNICODE) |
1792 | state->lower = sre_lower_unicode; |
1793 | #else |
1794 | state->lower = sre_lower_locale; |
1795 | #endif |
1796 | else |
1797 | state->lower = sre_lower; |
1798 | |
1799 | return string; |
1800 | } |
1801 | |
1802 | LOCAL(void)static inline void |
1803 | state_fini(SRE_STATE* state) |
1804 | { |
1805 | Py_XDECREF(state->string)do { if ((state->string) == ((void*)0)) ; else do { if (_Py_RefTotal -- , --((PyObject*)(state->string))->ob_refcnt != 0) { if (((PyObject*)state->string)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 1805, (PyObject *)(state->string)); } else _Py_Dealloc((PyObject *)(state->string)); } while (0 ); } while (0); |
1806 | data_stack_dealloc(state); |
1807 | } |
1808 | |
1809 | /* calculate offset from start of string */ |
1810 | #define STATE_OFFSET(state, member)(((char*)(member) - (char*)(state)->beginning) / (state)-> charsize)\ |
1811 | (((char*)(member) - (char*)(state)->beginning) / (state)->charsize) |
1812 | |
1813 | LOCAL(PyObject*)static inline PyObject* |
1814 | state_getslice(SRE_STATE* state, Py_ssize_t index, PyObject* string, int empty) |
1815 | { |
1816 | Py_ssize_t i, j; |
1817 | |
1818 | index = (index - 1) * 2; |
1819 | |
1820 | if (string == Py_None(&_Py_NoneStruct) || index >= state->lastmark || !state->mark[index] || !state->mark[index+1]) { |
1821 | if (empty) |
1822 | /* want empty string */ |
1823 | i = j = 0; |
1824 | else { |
1825 | Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt ++); |
1826 | return Py_None(&_Py_NoneStruct); |
1827 | } |
1828 | } else { |
1829 | i = STATE_OFFSET(state, state->mark[index])(((char*)(state->mark[index]) - (char*)(state)->beginning ) / (state)->charsize); |
1830 | j = STATE_OFFSET(state, state->mark[index+1])(((char*)(state->mark[index+1]) - (char*)(state)->beginning ) / (state)->charsize); |
1831 | } |
1832 | |
1833 | return PySequence_GetSlice(string, i, j); |
1834 | } |
1835 | |
1836 | static void |
1837 | pattern_error(int status) |
1838 | { |
1839 | switch (status) { |
1840 | case SRE_ERROR_RECURSION_LIMIT-3: |
1841 | PyErr_SetString( |
1842 | PyExc_RuntimeError, |
1843 | "maximum recursion limit exceeded" |
1844 | ); |
1845 | break; |
1846 | case SRE_ERROR_MEMORY-9: |
1847 | PyErr_NoMemory(); |
1848 | break; |
1849 | case SRE_ERROR_INTERRUPTED-10: |
1850 | /* An exception has already been raised, so let it fly */ |
1851 | break; |
1852 | default: |
1853 | /* other error codes indicate compiler/engine bugs */ |
1854 | PyErr_SetString( |
1855 | PyExc_RuntimeError, |
1856 | "internal error in regular expression engine" |
1857 | ); |
1858 | } |
1859 | } |
1860 | |
1861 | static void |
1862 | pattern_dealloc(PatternObject* self) |
1863 | { |
1864 | if (self->weakreflist != NULL((void*)0)) |
1865 | PyObject_ClearWeakRefs((PyObject *) self); |
1866 | Py_XDECREF(self->pattern)do { if ((self->pattern) == ((void*)0)) ; else do { if (_Py_RefTotal -- , --((PyObject*)(self->pattern))->ob_refcnt != 0) { if (((PyObject*)self->pattern)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 1866, (PyObject *)(self->pattern)); } else _Py_Dealloc((PyObject *)(self->pattern)); } while (0 ); } while (0); |
1867 | Py_XDECREF(self->groupindex)do { if ((self->groupindex) == ((void*)0)) ; else do { if ( _Py_RefTotal-- , --((PyObject*)(self->groupindex))->ob_refcnt != 0) { if (((PyObject*)self->groupindex)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_sre.c", 1867, (PyObject * )(self->groupindex)); } else _Py_Dealloc((PyObject *)(self ->groupindex)); } while (0); } while (0); |
1868 | Py_XDECREF(self->indexgroup)do { if ((self->indexgroup) == ((void*)0)) ; else do { if ( _Py_RefTotal-- , --((PyObject*)(self->indexgroup))->ob_refcnt != 0) { if (((PyObject*)self->indexgroup)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_sre.c", 1868, (PyObject * )(self->indexgroup)); } else _Py_Dealloc((PyObject *)(self ->indexgroup)); } while (0); } while (0); |
1869 | PyObject_DEL_PyObject_DebugFree(self); |
1870 | } |
1871 | |
1872 | static PyObject* |
1873 | pattern_match(PatternObject* self, PyObject* args, PyObject* kw) |
1874 | { |
1875 | SRE_STATE state; |
1876 | int status; |
1877 | |
1878 | PyObject* string; |
1879 | Py_ssize_t start = 0; |
1880 | Py_ssize_t end = PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)); |
1881 | static char* kwlist[] = { "pattern", "pos", "endpos", NULL((void*)0) }; |
1882 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kw, "O|nn:match", kwlist, |
1883 | &string, &start, &end)) |
1884 | return NULL((void*)0); |
1885 | |
1886 | string = state_init(&state, self, string, start, end); |
1887 | if (!string) |
1888 | return NULL((void*)0); |
1889 | |
1890 | state.ptr = state.start; |
1891 | |
1892 | TRACE(("|%p|%p|MATCH\n", PatternObject_GetCode(self), state.ptr)); |
1893 | |
1894 | if (state.charsize == 1) { |
1895 | status = sre_match(&state, PatternObject_GetCode(self)(((PatternObject*)(self))->code)); |
1896 | } else { |
1897 | #if defined(HAVE_UNICODE) |
1898 | status = sre_umatch(&state, PatternObject_GetCode(self)(((PatternObject*)(self))->code)); |
1899 | #endif |
1900 | } |
1901 | |
1902 | TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr)); |
1903 | if (PyErr_Occurred()) |
1904 | return NULL((void*)0); |
1905 | |
1906 | state_fini(&state); |
1907 | |
1908 | return pattern_new_match(self, &state, status); |
1909 | } |
1910 | |
1911 | static PyObject* |
1912 | pattern_search(PatternObject* self, PyObject* args, PyObject* kw) |
1913 | { |
1914 | SRE_STATE state; |
1915 | int status; |
1916 | |
1917 | PyObject* string; |
1918 | Py_ssize_t start = 0; |
1919 | Py_ssize_t end = PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)); |
1920 | static char* kwlist[] = { "pattern", "pos", "endpos", NULL((void*)0) }; |
1921 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kw, "O|nn:search", kwlist, |
1922 | &string, &start, &end)) |
1923 | return NULL((void*)0); |
1924 | |
1925 | string = state_init(&state, self, string, start, end); |
1926 | if (!string) |
1927 | return NULL((void*)0); |
1928 | |
1929 | TRACE(("|%p|%p|SEARCH\n", PatternObject_GetCode(self), state.ptr)); |
1930 | |
1931 | if (state.charsize == 1) { |
1932 | status = sre_search(&state, PatternObject_GetCode(self)(((PatternObject*)(self))->code)); |
1933 | } else { |
1934 | #if defined(HAVE_UNICODE) |
1935 | status = sre_usearch(&state, PatternObject_GetCode(self)(((PatternObject*)(self))->code)); |
1936 | #endif |
1937 | } |
1938 | |
1939 | TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr)); |
1940 | |
1941 | state_fini(&state); |
1942 | |
1943 | if (PyErr_Occurred()) |
1944 | return NULL((void*)0); |
1945 | |
1946 | return pattern_new_match(self, &state, status); |
1947 | } |
1948 | |
1949 | static PyObject* |
1950 | call(char* module, char* function, PyObject* args) |
1951 | { |
1952 | PyObject* name; |
1953 | PyObject* mod; |
1954 | PyObject* func; |
1955 | PyObject* result; |
1956 | |
1957 | if (!args) |
1958 | return NULL((void*)0); |
1959 | name = PyUnicode_FromStringPyUnicodeUCS2_FromString(module); |
1960 | if (!name) |
1961 | return NULL((void*)0); |
1962 | mod = PyImport_Import(name); |
1963 | Py_DECREF(name)do { if (_Py_RefTotal-- , --((PyObject*)(name))->ob_refcnt != 0) { if (((PyObject*)name)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 1963, (PyObject *)(name)); } else _Py_Dealloc ((PyObject *)(name)); } while (0); |
1964 | if (!mod) |
1965 | return NULL((void*)0); |
1966 | func = PyObject_GetAttrString(mod, function); |
1967 | Py_DECREF(mod)do { if (_Py_RefTotal-- , --((PyObject*)(mod))->ob_refcnt != 0) { if (((PyObject*)mod)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 1967, (PyObject *)(mod)); } else _Py_Dealloc ((PyObject *)(mod)); } while (0); |
1968 | if (!func) |
1969 | return NULL((void*)0); |
1970 | result = PyObject_CallObject(func, args); |
1971 | Py_DECREF(func)do { if (_Py_RefTotal-- , --((PyObject*)(func))->ob_refcnt != 0) { if (((PyObject*)func)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 1971, (PyObject *)(func)); } else _Py_Dealloc ((PyObject *)(func)); } while (0); |
1972 | Py_DECREF(args)do { if (_Py_RefTotal-- , --((PyObject*)(args))->ob_refcnt != 0) { if (((PyObject*)args)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 1972, (PyObject *)(args)); } else _Py_Dealloc ((PyObject *)(args)); } while (0); |
1973 | return result; |
1974 | } |
1975 | |
1976 | #ifdef USE_BUILTIN_COPY |
1977 | static int |
1978 | deepcopy(PyObject** object, PyObject* memo) |
1979 | { |
1980 | PyObject* copy; |
1981 | |
1982 | copy = call( |
1983 | "copy", "deepcopy", |
1984 | PyTuple_Pack(2, *object, memo) |
1985 | ); |
1986 | if (!copy) |
1987 | return 0; |
1988 | |
1989 | Py_DECREF(*object)do { if (_Py_RefTotal-- , --((PyObject*)(*object))->ob_refcnt != 0) { if (((PyObject*)*object)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 1989, (PyObject *)(*object)); } else _Py_Dealloc ((PyObject *)(*object)); } while (0); |
1990 | *object = copy; |
1991 | |
1992 | return 1; /* success */ |
1993 | } |
1994 | #endif |
1995 | |
1996 | static PyObject* |
1997 | join_list(PyObject* list, PyObject* string) |
1998 | { |
1999 | /* join list elements */ |
2000 | |
2001 | PyObject* joiner; |
2002 | #if PY_VERSION_HEX((3 << 24) | (2 << 16) | (0 << 8) | (0xC << 4) | (2 << 0)) >= 0x01060000 |
2003 | PyObject* function; |
2004 | PyObject* args; |
2005 | #endif |
2006 | PyObject* result; |
2007 | |
2008 | joiner = PySequence_GetSlice(string, 0, 0); |
2009 | if (!joiner) |
2010 | return NULL((void*)0); |
2011 | |
2012 | if (PyList_GET_SIZE(list)(((PyVarObject*)(list))->ob_size) == 0) { |
2013 | Py_DECREF(list)do { if (_Py_RefTotal-- , --((PyObject*)(list))->ob_refcnt != 0) { if (((PyObject*)list)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2013, (PyObject *)(list)); } else _Py_Dealloc ((PyObject *)(list)); } while (0); |
2014 | return joiner; |
2015 | } |
2016 | |
2017 | #if PY_VERSION_HEX((3 << 24) | (2 << 16) | (0 << 8) | (0xC << 4) | (2 << 0)) >= 0x01060000 |
2018 | function = PyObject_GetAttrString(joiner, "join"); |
2019 | if (!function) { |
2020 | Py_DECREF(joiner)do { if (_Py_RefTotal-- , --((PyObject*)(joiner))->ob_refcnt != 0) { if (((PyObject*)joiner)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2020, (PyObject *)(joiner)); } else _Py_Dealloc ((PyObject *)(joiner)); } while (0); |
2021 | return NULL((void*)0); |
2022 | } |
2023 | args = PyTuple_New(1); |
2024 | if (!args) { |
2025 | Py_DECREF(function)do { if (_Py_RefTotal-- , --((PyObject*)(function))->ob_refcnt != 0) { if (((PyObject*)function)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2025, (PyObject *)(function)); } else _Py_Dealloc ((PyObject *)(function)); } while (0); |
2026 | Py_DECREF(joiner)do { if (_Py_RefTotal-- , --((PyObject*)(joiner))->ob_refcnt != 0) { if (((PyObject*)joiner)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2026, (PyObject *)(joiner)); } else _Py_Dealloc ((PyObject *)(joiner)); } while (0); |
2027 | return NULL((void*)0); |
2028 | } |
2029 | PyTuple_SET_ITEM(args, 0, list)(((PyTupleObject *)(args))->ob_item[0] = list); |
2030 | result = PyObject_CallObject(function, args); |
2031 | Py_DECREF(args)do { if (_Py_RefTotal-- , --((PyObject*)(args))->ob_refcnt != 0) { if (((PyObject*)args)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2031, (PyObject *)(args)); } else _Py_Dealloc ((PyObject *)(args)); } while (0); /* also removes list */ |
2032 | Py_DECREF(function)do { if (_Py_RefTotal-- , --((PyObject*)(function))->ob_refcnt != 0) { if (((PyObject*)function)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2032, (PyObject *)(function)); } else _Py_Dealloc ((PyObject *)(function)); } while (0); |
2033 | #else |
2034 | result = call( |
2035 | "string", "join", |
2036 | PyTuple_Pack(2, list, joiner) |
2037 | ); |
2038 | #endif |
2039 | Py_DECREF(joiner)do { if (_Py_RefTotal-- , --((PyObject*)(joiner))->ob_refcnt != 0) { if (((PyObject*)joiner)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2039, (PyObject *)(joiner)); } else _Py_Dealloc ((PyObject *)(joiner)); } while (0); |
2040 | |
2041 | return result; |
2042 | } |
2043 | |
2044 | static PyObject* |
2045 | pattern_findall(PatternObject* self, PyObject* args, PyObject* kw) |
2046 | { |
2047 | SRE_STATE state; |
2048 | PyObject* list; |
2049 | int status; |
2050 | Py_ssize_t i, b, e; |
2051 | |
2052 | PyObject* string; |
2053 | Py_ssize_t start = 0; |
2054 | Py_ssize_t end = PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)); |
2055 | static char* kwlist[] = { "source", "pos", "endpos", NULL((void*)0) }; |
2056 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kw, "O|nn:findall", kwlist, |
2057 | &string, &start, &end)) |
2058 | return NULL((void*)0); |
2059 | |
2060 | string = state_init(&state, self, string, start, end); |
2061 | if (!string) |
2062 | return NULL((void*)0); |
2063 | |
2064 | list = PyList_New(0); |
2065 | if (!list) { |
2066 | state_fini(&state); |
2067 | return NULL((void*)0); |
2068 | } |
2069 | |
2070 | while (state.start <= state.end) { |
2071 | |
2072 | PyObject* item; |
2073 | |
2074 | state_reset(&state); |
2075 | |
2076 | state.ptr = state.start; |
2077 | |
2078 | if (state.charsize == 1) { |
2079 | status = sre_search(&state, PatternObject_GetCode(self)(((PatternObject*)(self))->code)); |
2080 | } else { |
2081 | #if defined(HAVE_UNICODE) |
2082 | status = sre_usearch(&state, PatternObject_GetCode(self)(((PatternObject*)(self))->code)); |
2083 | #endif |
2084 | } |
2085 | |
2086 | if (PyErr_Occurred()) |
2087 | goto error; |
2088 | |
2089 | if (status <= 0) { |
2090 | if (status == 0) |
2091 | break; |
2092 | pattern_error(status); |
2093 | goto error; |
2094 | } |
2095 | |
2096 | /* don't bother to build a match object */ |
2097 | switch (self->groups) { |
2098 | case 0: |
2099 | b = STATE_OFFSET(&state, state.start)(((char*)(state.start) - (char*)(&state)->beginning) / (&state)->charsize); |
2100 | e = STATE_OFFSET(&state, state.ptr)(((char*)(state.ptr) - (char*)(&state)->beginning) / ( &state)->charsize); |
2101 | item = PySequence_GetSlice(string, b, e); |
2102 | if (!item) |
2103 | goto error; |
2104 | break; |
2105 | case 1: |
2106 | item = state_getslice(&state, 1, string, 1); |
2107 | if (!item) |
2108 | goto error; |
2109 | break; |
2110 | default: |
2111 | item = PyTuple_New(self->groups); |
2112 | if (!item) |
2113 | goto error; |
2114 | for (i = 0; i < self->groups; i++) { |
2115 | PyObject* o = state_getslice(&state, i+1, string, 1); |
2116 | if (!o) { |
2117 | Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt != 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2117, (PyObject *)(item)); } else _Py_Dealloc ((PyObject *)(item)); } while (0); |
2118 | goto error; |
2119 | } |
2120 | PyTuple_SET_ITEM(item, i, o)(((PyTupleObject *)(item))->ob_item[i] = o); |
2121 | } |
2122 | break; |
2123 | } |
2124 | |
2125 | status = PyList_Append(list, item); |
2126 | Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt != 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2126, (PyObject *)(item)); } else _Py_Dealloc ((PyObject *)(item)); } while (0); |
2127 | if (status < 0) |
2128 | goto error; |
2129 | |
2130 | if (state.ptr == state.start) |
2131 | state.start = (void*) ((char*) state.ptr + state.charsize); |
2132 | else |
2133 | state.start = state.ptr; |
2134 | |
2135 | } |
2136 | |
2137 | state_fini(&state); |
2138 | return list; |
2139 | |
2140 | error: |
2141 | Py_DECREF(list)do { if (_Py_RefTotal-- , --((PyObject*)(list))->ob_refcnt != 0) { if (((PyObject*)list)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2141, (PyObject *)(list)); } else _Py_Dealloc ((PyObject *)(list)); } while (0); |
2142 | state_fini(&state); |
2143 | return NULL((void*)0); |
2144 | |
2145 | } |
2146 | |
2147 | #if PY_VERSION_HEX((3 << 24) | (2 << 16) | (0 << 8) | (0xC << 4) | (2 << 0)) >= 0x02020000 |
2148 | static PyObject* |
2149 | pattern_finditer(PatternObject* pattern, PyObject* args) |
2150 | { |
2151 | PyObject* scanner; |
2152 | PyObject* search; |
2153 | PyObject* iterator; |
2154 | |
2155 | scanner = pattern_scanner(pattern, args); |
2156 | if (!scanner) |
2157 | return NULL((void*)0); |
2158 | |
2159 | search = PyObject_GetAttrString(scanner, "search"); |
2160 | Py_DECREF(scanner)do { if (_Py_RefTotal-- , --((PyObject*)(scanner))->ob_refcnt != 0) { if (((PyObject*)scanner)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2160, (PyObject *)(scanner)); } else _Py_Dealloc ((PyObject *)(scanner)); } while (0); |
2161 | if (!search) |
2162 | return NULL((void*)0); |
2163 | |
2164 | iterator = PyCallIter_New(search, Py_None(&_Py_NoneStruct)); |
2165 | Py_DECREF(search)do { if (_Py_RefTotal-- , --((PyObject*)(search))->ob_refcnt != 0) { if (((PyObject*)search)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2165, (PyObject *)(search)); } else _Py_Dealloc ((PyObject *)(search)); } while (0); |
2166 | |
2167 | return iterator; |
2168 | } |
2169 | #endif |
2170 | |
2171 | static PyObject* |
2172 | pattern_split(PatternObject* self, PyObject* args, PyObject* kw) |
2173 | { |
2174 | SRE_STATE state; |
2175 | PyObject* list; |
2176 | PyObject* item; |
2177 | int status; |
2178 | Py_ssize_t n; |
2179 | Py_ssize_t i; |
2180 | void* last; |
2181 | |
2182 | PyObject* string; |
2183 | Py_ssize_t maxsplit = 0; |
2184 | static char* kwlist[] = { "source", "maxsplit", NULL((void*)0) }; |
2185 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kw, "O|n:split", kwlist, |
2186 | &string, &maxsplit)) |
2187 | return NULL((void*)0); |
2188 | |
2189 | string = state_init(&state, self, string, 0, PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1))); |
2190 | if (!string) |
2191 | return NULL((void*)0); |
2192 | |
2193 | list = PyList_New(0); |
2194 | if (!list) { |
2195 | state_fini(&state); |
2196 | return NULL((void*)0); |
2197 | } |
2198 | |
2199 | n = 0; |
2200 | last = state.start; |
2201 | |
2202 | while (!maxsplit || n < maxsplit) { |
2203 | |
2204 | state_reset(&state); |
2205 | |
2206 | state.ptr = state.start; |
2207 | |
2208 | if (state.charsize == 1) { |
2209 | status = sre_search(&state, PatternObject_GetCode(self)(((PatternObject*)(self))->code)); |
2210 | } else { |
2211 | #if defined(HAVE_UNICODE) |
2212 | status = sre_usearch(&state, PatternObject_GetCode(self)(((PatternObject*)(self))->code)); |
2213 | #endif |
2214 | } |
2215 | |
2216 | if (PyErr_Occurred()) |
2217 | goto error; |
2218 | |
2219 | if (status <= 0) { |
2220 | if (status == 0) |
2221 | break; |
2222 | pattern_error(status); |
2223 | goto error; |
2224 | } |
2225 | |
2226 | if (state.start == state.ptr) { |
2227 | if (last == state.end) |
2228 | break; |
2229 | /* skip one character */ |
2230 | state.start = (void*) ((char*) state.ptr + state.charsize); |
2231 | continue; |
2232 | } |
2233 | |
2234 | /* get segment before this match */ |
2235 | item = PySequence_GetSlice( |
2236 | string, STATE_OFFSET(&state, last)(((char*)(last) - (char*)(&state)->beginning) / (& state)->charsize), |
2237 | STATE_OFFSET(&state, state.start)(((char*)(state.start) - (char*)(&state)->beginning) / (&state)->charsize) |
2238 | ); |
2239 | if (!item) |
2240 | goto error; |
2241 | status = PyList_Append(list, item); |
2242 | Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt != 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2242, (PyObject *)(item)); } else _Py_Dealloc ((PyObject *)(item)); } while (0); |
2243 | if (status < 0) |
2244 | goto error; |
2245 | |
2246 | /* add groups (if any) */ |
2247 | for (i = 0; i < self->groups; i++) { |
2248 | item = state_getslice(&state, i+1, string, 0); |
2249 | if (!item) |
2250 | goto error; |
2251 | status = PyList_Append(list, item); |
2252 | Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt != 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2252, (PyObject *)(item)); } else _Py_Dealloc ((PyObject *)(item)); } while (0); |
2253 | if (status < 0) |
2254 | goto error; |
2255 | } |
2256 | |
2257 | n = n + 1; |
2258 | |
2259 | last = state.start = state.ptr; |
2260 | |
2261 | } |
2262 | |
2263 | /* get segment following last match (even if empty) */ |
2264 | item = PySequence_GetSlice( |
2265 | string, STATE_OFFSET(&state, last)(((char*)(last) - (char*)(&state)->beginning) / (& state)->charsize), state.endpos |
2266 | ); |
2267 | if (!item) |
2268 | goto error; |
2269 | status = PyList_Append(list, item); |
2270 | Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt != 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2270, (PyObject *)(item)); } else _Py_Dealloc ((PyObject *)(item)); } while (0); |
2271 | if (status < 0) |
2272 | goto error; |
2273 | |
2274 | state_fini(&state); |
2275 | return list; |
2276 | |
2277 | error: |
2278 | Py_DECREF(list)do { if (_Py_RefTotal-- , --((PyObject*)(list))->ob_refcnt != 0) { if (((PyObject*)list)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2278, (PyObject *)(list)); } else _Py_Dealloc ((PyObject *)(list)); } while (0); |
2279 | state_fini(&state); |
2280 | return NULL((void*)0); |
2281 | |
2282 | } |
2283 | |
2284 | static PyObject* |
2285 | pattern_subx(PatternObject* self, PyObject* ptemplate, PyObject* string, |
2286 | Py_ssize_t count, Py_ssize_t subn) |
2287 | { |
2288 | SRE_STATE state; |
2289 | PyObject* list; |
2290 | PyObject* item; |
2291 | PyObject* filter; |
2292 | PyObject* args; |
2293 | PyObject* match; |
2294 | void* ptr; |
2295 | int status; |
2296 | Py_ssize_t n; |
2297 | Py_ssize_t i, b, e; |
2298 | int bint; |
2299 | int filter_is_callable; |
2300 | |
2301 | if (PyCallable_Check(ptemplate)) { |
2302 | /* sub/subn takes either a function or a template */ |
2303 | filter = ptemplate; |
2304 | Py_INCREF(filter)( _Py_RefTotal++ , ((PyObject*)(filter))->ob_refcnt++); |
2305 | filter_is_callable = 1; |
2306 | } else { |
2307 | /* if not callable, check if it's a literal string */ |
2308 | int literal; |
2309 | ptr = getstring(ptemplate, &n, &bint); |
2310 | b = bint; |
2311 | if (ptr) { |
2312 | if (b == 1) { |
2313 | literal = sre_literal_template((unsigned char *)ptr, n); |
2314 | } else { |
2315 | #if defined(HAVE_UNICODE) |
2316 | literal = sre_uliteral_template((Py_UNICODE *)ptr, n); |
2317 | #endif |
2318 | } |
2319 | } else { |
2320 | PyErr_Clear(); |
2321 | literal = 0; |
2322 | } |
2323 | if (literal) { |
2324 | filter = ptemplate; |
2325 | Py_INCREF(filter)( _Py_RefTotal++ , ((PyObject*)(filter))->ob_refcnt++); |
2326 | filter_is_callable = 0; |
2327 | } else { |
2328 | /* not a literal; hand it over to the template compiler */ |
2329 | filter = call( |
2330 | SRE_PY_MODULE"re", "_subx", |
2331 | PyTuple_Pack(2, self, ptemplate) |
2332 | ); |
2333 | if (!filter) |
2334 | return NULL((void*)0); |
2335 | filter_is_callable = PyCallable_Check(filter); |
2336 | } |
2337 | } |
2338 | |
2339 | string = state_init(&state, self, string, 0, PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1))); |
2340 | if (!string) { |
2341 | Py_DECREF(filter)do { if (_Py_RefTotal-- , --((PyObject*)(filter))->ob_refcnt != 0) { if (((PyObject*)filter)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2341, (PyObject *)(filter)); } else _Py_Dealloc ((PyObject *)(filter)); } while (0); |
2342 | return NULL((void*)0); |
2343 | } |
2344 | |
2345 | list = PyList_New(0); |
2346 | if (!list) { |
2347 | Py_DECREF(filter)do { if (_Py_RefTotal-- , --((PyObject*)(filter))->ob_refcnt != 0) { if (((PyObject*)filter)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2347, (PyObject *)(filter)); } else _Py_Dealloc ((PyObject *)(filter)); } while (0); |
2348 | state_fini(&state); |
2349 | return NULL((void*)0); |
2350 | } |
2351 | |
2352 | n = i = 0; |
2353 | |
2354 | while (!count || n < count) { |
2355 | |
2356 | state_reset(&state); |
2357 | |
2358 | state.ptr = state.start; |
2359 | |
2360 | if (state.charsize == 1) { |
2361 | status = sre_search(&state, PatternObject_GetCode(self)(((PatternObject*)(self))->code)); |
2362 | } else { |
2363 | #if defined(HAVE_UNICODE) |
2364 | status = sre_usearch(&state, PatternObject_GetCode(self)(((PatternObject*)(self))->code)); |
2365 | #endif |
2366 | } |
2367 | |
2368 | if (PyErr_Occurred()) |
2369 | goto error; |
2370 | |
2371 | if (status <= 0) { |
2372 | if (status == 0) |
2373 | break; |
2374 | pattern_error(status); |
2375 | goto error; |
2376 | } |
2377 | |
2378 | b = STATE_OFFSET(&state, state.start)(((char*)(state.start) - (char*)(&state)->beginning) / (&state)->charsize); |
2379 | e = STATE_OFFSET(&state, state.ptr)(((char*)(state.ptr) - (char*)(&state)->beginning) / ( &state)->charsize); |
2380 | |
2381 | if (i < b) { |
2382 | /* get segment before this match */ |
2383 | item = PySequence_GetSlice(string, i, b); |
2384 | if (!item) |
2385 | goto error; |
2386 | status = PyList_Append(list, item); |
2387 | Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt != 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2387, (PyObject *)(item)); } else _Py_Dealloc ((PyObject *)(item)); } while (0); |
2388 | if (status < 0) |
2389 | goto error; |
2390 | |
2391 | } else if (i == b && i == e && n > 0) |
2392 | /* ignore empty match on latest position */ |
2393 | goto next; |
2394 | |
2395 | if (filter_is_callable) { |
2396 | /* pass match object through filter */ |
2397 | match = pattern_new_match(self, &state, 1); |
2398 | if (!match) |
2399 | goto error; |
2400 | args = PyTuple_Pack(1, match); |
2401 | if (!args) { |
2402 | Py_DECREF(match)do { if (_Py_RefTotal-- , --((PyObject*)(match))->ob_refcnt != 0) { if (((PyObject*)match)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2402, (PyObject *)(match)); } else _Py_Dealloc ((PyObject *)(match)); } while (0); |
2403 | goto error; |
2404 | } |
2405 | item = PyObject_CallObject(filter, args); |
2406 | Py_DECREF(args)do { if (_Py_RefTotal-- , --((PyObject*)(args))->ob_refcnt != 0) { if (((PyObject*)args)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2406, (PyObject *)(args)); } else _Py_Dealloc ((PyObject *)(args)); } while (0); |
2407 | Py_DECREF(match)do { if (_Py_RefTotal-- , --((PyObject*)(match))->ob_refcnt != 0) { if (((PyObject*)match)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2407, (PyObject *)(match)); } else _Py_Dealloc ((PyObject *)(match)); } while (0); |
2408 | if (!item) |
2409 | goto error; |
2410 | } else { |
2411 | /* filter is literal string */ |
2412 | item = filter; |
2413 | Py_INCREF(item)( _Py_RefTotal++ , ((PyObject*)(item))->ob_refcnt++); |
2414 | } |
2415 | |
2416 | /* add to list */ |
2417 | if (item != Py_None(&_Py_NoneStruct)) { |
2418 | status = PyList_Append(list, item); |
2419 | Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt != 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2419, (PyObject *)(item)); } else _Py_Dealloc ((PyObject *)(item)); } while (0); |
2420 | if (status < 0) |
2421 | goto error; |
2422 | } |
2423 | |
2424 | i = e; |
2425 | n = n + 1; |
2426 | |
2427 | next: |
2428 | /* move on */ |
2429 | if (state.ptr == state.start) |
2430 | state.start = (void*) ((char*) state.ptr + state.charsize); |
2431 | else |
2432 | state.start = state.ptr; |
2433 | |
2434 | } |
2435 | |
2436 | /* get segment following last match */ |
2437 | if (i < state.endpos) { |
2438 | item = PySequence_GetSlice(string, i, state.endpos); |
2439 | if (!item) |
2440 | goto error; |
2441 | status = PyList_Append(list, item); |
2442 | Py_DECREF(item)do { if (_Py_RefTotal-- , --((PyObject*)(item))->ob_refcnt != 0) { if (((PyObject*)item)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2442, (PyObject *)(item)); } else _Py_Dealloc ((PyObject *)(item)); } while (0); |
2443 | if (status < 0) |
2444 | goto error; |
2445 | } |
2446 | |
2447 | state_fini(&state); |
2448 | |
2449 | Py_DECREF(filter)do { if (_Py_RefTotal-- , --((PyObject*)(filter))->ob_refcnt != 0) { if (((PyObject*)filter)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2449, (PyObject *)(filter)); } else _Py_Dealloc ((PyObject *)(filter)); } while (0); |
2450 | |
2451 | /* convert list to single string (also removes list) */ |
2452 | item = join_list(list, string); |
2453 | |
2454 | if (!item) |
2455 | return NULL((void*)0); |
2456 | |
2457 | if (subn) |
2458 | return Py_BuildValue_Py_BuildValue_SizeT("Ni", item, n); |
2459 | |
2460 | return item; |
2461 | |
2462 | error: |
2463 | Py_DECREF(list)do { if (_Py_RefTotal-- , --((PyObject*)(list))->ob_refcnt != 0) { if (((PyObject*)list)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2463, (PyObject *)(list)); } else _Py_Dealloc ((PyObject *)(list)); } while (0); |
2464 | state_fini(&state); |
2465 | Py_DECREF(filter)do { if (_Py_RefTotal-- , --((PyObject*)(filter))->ob_refcnt != 0) { if (((PyObject*)filter)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2465, (PyObject *)(filter)); } else _Py_Dealloc ((PyObject *)(filter)); } while (0); |
2466 | return NULL((void*)0); |
2467 | |
2468 | } |
2469 | |
2470 | static PyObject* |
2471 | pattern_sub(PatternObject* self, PyObject* args, PyObject* kw) |
2472 | { |
2473 | PyObject* ptemplate; |
2474 | PyObject* string; |
2475 | Py_ssize_t count = 0; |
2476 | static char* kwlist[] = { "repl", "string", "count", NULL((void*)0) }; |
2477 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kw, "OO|n:sub", kwlist, |
2478 | &ptemplate, &string, &count)) |
2479 | return NULL((void*)0); |
2480 | |
2481 | return pattern_subx(self, ptemplate, string, count, 0); |
2482 | } |
2483 | |
2484 | static PyObject* |
2485 | pattern_subn(PatternObject* self, PyObject* args, PyObject* kw) |
2486 | { |
2487 | PyObject* ptemplate; |
2488 | PyObject* string; |
2489 | Py_ssize_t count = 0; |
2490 | static char* kwlist[] = { "repl", "string", "count", NULL((void*)0) }; |
2491 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kw, "OO|n:subn", kwlist, |
2492 | &ptemplate, &string, &count)) |
2493 | return NULL((void*)0); |
2494 | |
2495 | return pattern_subx(self, ptemplate, string, count, 1); |
2496 | } |
2497 | |
2498 | static PyObject* |
2499 | pattern_copy(PatternObject* self, PyObject *unused) |
2500 | { |
2501 | #ifdef USE_BUILTIN_COPY |
2502 | PatternObject* copy; |
2503 | int offset; |
2504 | |
2505 | copy = PyObject_NEW_VAR(PatternObject, &Pattern_Type, self->codesize)( (PatternObject *) PyObject_InitVar( (PyVarObject *) _PyObject_DebugMalloc ((size_t) ( ( ((&Pattern_Type))->tp_basicsize + ((self ->codesize))*((&Pattern_Type))->tp_itemsize + (8 - 1 ) ) & ~(8 - 1) ) ), (&Pattern_Type), (self->codesize )) ); |
2506 | if (!copy) |
2507 | return NULL((void*)0); |
2508 | |
2509 | offset = offsetof(PatternObject, groups)__builtin_offsetof(PatternObject, groups); |
2510 | |
2511 | Py_XINCREF(self->groupindex)do { if ((self->groupindex) == ((void*)0)) ; else ( _Py_RefTotal ++ , ((PyObject*)(self->groupindex))->ob_refcnt++); } while (0); |
2512 | Py_XINCREF(self->indexgroup)do { if ((self->indexgroup) == ((void*)0)) ; else ( _Py_RefTotal ++ , ((PyObject*)(self->indexgroup))->ob_refcnt++); } while (0); |
2513 | Py_XINCREF(self->pattern)do { if ((self->pattern) == ((void*)0)) ; else ( _Py_RefTotal ++ , ((PyObject*)(self->pattern))->ob_refcnt++); } while (0); |
2514 | |
2515 | memcpy((char*) copy + offset, (char*) self + offset,((__builtin_object_size ((char*) copy + offset, 0) != (size_t ) -1) ? __builtin___memcpy_chk ((char*) copy + offset, (char* ) self + offset, sizeof(PatternObject) + self->codesize * sizeof (unsigned short) - offset, __builtin_object_size ((char*) copy + offset, 0)) : __inline_memcpy_chk ((char*) copy + offset, ( char*) self + offset, sizeof(PatternObject) + self->codesize * sizeof(unsigned short) - offset)) |
2516 | sizeof(PatternObject) + self->codesize * sizeof(SRE_CODE) - offset)((__builtin_object_size ((char*) copy + offset, 0) != (size_t ) -1) ? __builtin___memcpy_chk ((char*) copy + offset, (char* ) self + offset, sizeof(PatternObject) + self->codesize * sizeof (unsigned short) - offset, __builtin_object_size ((char*) copy + offset, 0)) : __inline_memcpy_chk ((char*) copy + offset, ( char*) self + offset, sizeof(PatternObject) + self->codesize * sizeof(unsigned short) - offset)); |
2517 | copy->weakreflist = NULL((void*)0); |
2518 | |
2519 | return (PyObject*) copy; |
2520 | #else |
2521 | PyErr_SetString(PyExc_TypeError, "cannot copy this pattern object"); |
2522 | return NULL((void*)0); |
2523 | #endif |
2524 | } |
2525 | |
2526 | static PyObject* |
2527 | pattern_deepcopy(PatternObject* self, PyObject* memo) |
2528 | { |
2529 | #ifdef USE_BUILTIN_COPY |
2530 | PatternObject* copy; |
2531 | |
2532 | copy = (PatternObject*) pattern_copy(self); |
2533 | if (!copy) |
2534 | return NULL((void*)0); |
2535 | |
2536 | if (!deepcopy(©->groupindex, memo) || |
2537 | !deepcopy(©->indexgroup, memo) || |
2538 | !deepcopy(©->pattern, memo)) { |
2539 | Py_DECREF(copy)do { if (_Py_RefTotal-- , --((PyObject*)(copy))->ob_refcnt != 0) { if (((PyObject*)copy)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2539, (PyObject *)(copy)); } else _Py_Dealloc ((PyObject *)(copy)); } while (0); |
2540 | return NULL((void*)0); |
2541 | } |
2542 | |
2543 | #else |
2544 | PyErr_SetString(PyExc_TypeError, "cannot deepcopy this pattern object"); |
2545 | return NULL((void*)0); |
2546 | #endif |
2547 | } |
2548 | |
2549 | PyDoc_STRVAR(pattern_match_doc,static char pattern_match_doc[] = "match(string[, pos[, endpos]]) --> match object or None.\n Matches zero or more characters at the beginning of the string" |
2550 | "match(string[, pos[, endpos]]) --> match object or None.\n\static char pattern_match_doc[] = "match(string[, pos[, endpos]]) --> match object or None.\n Matches zero or more characters at the beginning of the string" |
2551 | Matches zero or more characters at the beginning of the string")static char pattern_match_doc[] = "match(string[, pos[, endpos]]) --> match object or None.\n Matches zero or more characters at the beginning of the string"; |
2552 | |
2553 | PyDoc_STRVAR(pattern_search_doc,static char pattern_search_doc[] = "search(string[, pos[, endpos]]) --> match object or None.\n Scan through string looking for a match, and return a corresponding\n MatchObject instance. Return None if no position in the string matches." |
2554 | "search(string[, pos[, endpos]]) --> match object or None.\n\static char pattern_search_doc[] = "search(string[, pos[, endpos]]) --> match object or None.\n Scan through string looking for a match, and return a corresponding\n MatchObject instance. Return None if no position in the string matches." |
2555 | Scan through string looking for a match, and return a corresponding\n\static char pattern_search_doc[] = "search(string[, pos[, endpos]]) --> match object or None.\n Scan through string looking for a match, and return a corresponding\n MatchObject instance. Return None if no position in the string matches." |
2556 | MatchObject instance. Return None if no position in the string matches.")static char pattern_search_doc[] = "search(string[, pos[, endpos]]) --> match object or None.\n Scan through string looking for a match, and return a corresponding\n MatchObject instance. Return None if no position in the string matches."; |
2557 | |
2558 | PyDoc_STRVAR(pattern_split_doc,static char pattern_split_doc[] = "split(string[, maxsplit = 0]) --> list.\n Split string by the occurrences of pattern." |
2559 | "split(string[, maxsplit = 0]) --> list.\n\static char pattern_split_doc[] = "split(string[, maxsplit = 0]) --> list.\n Split string by the occurrences of pattern." |
2560 | Split string by the occurrences of pattern.")static char pattern_split_doc[] = "split(string[, maxsplit = 0]) --> list.\n Split string by the occurrences of pattern."; |
2561 | |
2562 | PyDoc_STRVAR(pattern_findall_doc,static char pattern_findall_doc[] = "findall(string[, pos[, endpos]]) --> list.\n Return a list of all non-overlapping matches of pattern in string." |
2563 | "findall(string[, pos[, endpos]]) --> list.\n\static char pattern_findall_doc[] = "findall(string[, pos[, endpos]]) --> list.\n Return a list of all non-overlapping matches of pattern in string." |
2564 | Return a list of all non-overlapping matches of pattern in string.")static char pattern_findall_doc[] = "findall(string[, pos[, endpos]]) --> list.\n Return a list of all non-overlapping matches of pattern in string."; |
2565 | |
2566 | PyDoc_STRVAR(pattern_finditer_doc,static char pattern_finditer_doc[] = "finditer(string[, pos[, endpos]]) --> iterator.\n Return an iterator over all non-overlapping matches for the \n RE pattern in string. For each match, the iterator returns a\n match object." |
2567 | "finditer(string[, pos[, endpos]]) --> iterator.\n\static char pattern_finditer_doc[] = "finditer(string[, pos[, endpos]]) --> iterator.\n Return an iterator over all non-overlapping matches for the \n RE pattern in string. For each match, the iterator returns a\n match object." |
2568 | Return an iterator over all non-overlapping matches for the \n\static char pattern_finditer_doc[] = "finditer(string[, pos[, endpos]]) --> iterator.\n Return an iterator over all non-overlapping matches for the \n RE pattern in string. For each match, the iterator returns a\n match object." |
2569 | RE pattern in string. For each match, the iterator returns a\n\static char pattern_finditer_doc[] = "finditer(string[, pos[, endpos]]) --> iterator.\n Return an iterator over all non-overlapping matches for the \n RE pattern in string. For each match, the iterator returns a\n match object." |
2570 | match object.")static char pattern_finditer_doc[] = "finditer(string[, pos[, endpos]]) --> iterator.\n Return an iterator over all non-overlapping matches for the \n RE pattern in string. For each match, the iterator returns a\n match object."; |
2571 | |
2572 | PyDoc_STRVAR(pattern_sub_doc,static char pattern_sub_doc[] = "sub(repl, string[, count = 0]) --> newstring\n Return the string obtained by replacing the leftmost non-overlapping\n occurrences of pattern in string by the replacement repl." |
2573 | "sub(repl, string[, count = 0]) --> newstring\n\static char pattern_sub_doc[] = "sub(repl, string[, count = 0]) --> newstring\n Return the string obtained by replacing the leftmost non-overlapping\n occurrences of pattern in string by the replacement repl." |
2574 | Return the string obtained by replacing the leftmost non-overlapping\n\static char pattern_sub_doc[] = "sub(repl, string[, count = 0]) --> newstring\n Return the string obtained by replacing the leftmost non-overlapping\n occurrences of pattern in string by the replacement repl." |
2575 | occurrences of pattern in string by the replacement repl.")static char pattern_sub_doc[] = "sub(repl, string[, count = 0]) --> newstring\n Return the string obtained by replacing the leftmost non-overlapping\n occurrences of pattern in string by the replacement repl."; |
2576 | |
2577 | PyDoc_STRVAR(pattern_subn_doc,static char pattern_subn_doc[] = "subn(repl, string[, count = 0]) --> (newstring, number of subs)\n Return the tuple (new_string, number_of_subs_made) found by replacing\n the leftmost non-overlapping occurrences of pattern with the\n replacement repl." |
2578 | "subn(repl, string[, count = 0]) --> (newstring, number of subs)\n\static char pattern_subn_doc[] = "subn(repl, string[, count = 0]) --> (newstring, number of subs)\n Return the tuple (new_string, number_of_subs_made) found by replacing\n the leftmost non-overlapping occurrences of pattern with the\n replacement repl." |
2579 | Return the tuple (new_string, number_of_subs_made) found by replacing\n\static char pattern_subn_doc[] = "subn(repl, string[, count = 0]) --> (newstring, number of subs)\n Return the tuple (new_string, number_of_subs_made) found by replacing\n the leftmost non-overlapping occurrences of pattern with the\n replacement repl." |
2580 | the leftmost non-overlapping occurrences of pattern with the\n\static char pattern_subn_doc[] = "subn(repl, string[, count = 0]) --> (newstring, number of subs)\n Return the tuple (new_string, number_of_subs_made) found by replacing\n the leftmost non-overlapping occurrences of pattern with the\n replacement repl." |
2581 | replacement repl.")static char pattern_subn_doc[] = "subn(repl, string[, count = 0]) --> (newstring, number of subs)\n Return the tuple (new_string, number_of_subs_made) found by replacing\n the leftmost non-overlapping occurrences of pattern with the\n replacement repl."; |
2582 | |
2583 | PyDoc_STRVAR(pattern_doc, "Compiled regular expression objects")static char pattern_doc[] = "Compiled regular expression objects"; |
2584 | |
2585 | static PyMethodDef pattern_methods[] = { |
2586 | {"match", (PyCFunction) pattern_match, METH_VARARGS0x0001|METH_KEYWORDS0x0002, |
2587 | pattern_match_doc}, |
2588 | {"search", (PyCFunction) pattern_search, METH_VARARGS0x0001|METH_KEYWORDS0x0002, |
2589 | pattern_search_doc}, |
2590 | {"sub", (PyCFunction) pattern_sub, METH_VARARGS0x0001|METH_KEYWORDS0x0002, |
2591 | pattern_sub_doc}, |
2592 | {"subn", (PyCFunction) pattern_subn, METH_VARARGS0x0001|METH_KEYWORDS0x0002, |
2593 | pattern_subn_doc}, |
2594 | {"split", (PyCFunction) pattern_split, METH_VARARGS0x0001|METH_KEYWORDS0x0002, |
2595 | pattern_split_doc}, |
2596 | {"findall", (PyCFunction) pattern_findall, METH_VARARGS0x0001|METH_KEYWORDS0x0002, |
2597 | pattern_findall_doc}, |
2598 | #if PY_VERSION_HEX((3 << 24) | (2 << 16) | (0 << 8) | (0xC << 4) | (2 << 0)) >= 0x02020000 |
2599 | {"finditer", (PyCFunction) pattern_finditer, METH_VARARGS0x0001, |
2600 | pattern_finditer_doc}, |
2601 | #endif |
2602 | {"scanner", (PyCFunction) pattern_scanner, METH_VARARGS0x0001}, |
2603 | {"__copy__", (PyCFunction) pattern_copy, METH_NOARGS0x0004}, |
2604 | {"__deepcopy__", (PyCFunction) pattern_deepcopy, METH_O0x0008}, |
2605 | {NULL((void*)0), NULL((void*)0)} |
2606 | }; |
2607 | |
2608 | #define PAT_OFF(x)__builtin_offsetof(PatternObject, x) offsetof(PatternObject, x)__builtin_offsetof(PatternObject, x) |
2609 | static PyMemberDef pattern_members[] = { |
2610 | {"pattern", T_OBJECT6, PAT_OFF(pattern)__builtin_offsetof(PatternObject, pattern), READONLY1}, |
2611 | {"flags", T_INT1, PAT_OFF(flags)__builtin_offsetof(PatternObject, flags), READONLY1}, |
2612 | {"groups", T_PYSSIZET19, PAT_OFF(groups)__builtin_offsetof(PatternObject, groups), READONLY1}, |
2613 | {"groupindex", T_OBJECT6, PAT_OFF(groupindex)__builtin_offsetof(PatternObject, groupindex), READONLY1}, |
2614 | {NULL((void*)0)} /* Sentinel */ |
2615 | }; |
2616 | |
2617 | static PyTypeObject Pattern_Type = { |
2618 | PyVarObject_HEAD_INIT(NULL, 0){ { 0, 0, 1, ((void*)0) }, 0 }, |
2619 | "_" SRE_MODULE"sre" ".SRE_Pattern", |
2620 | sizeof(PatternObject), sizeof(SRE_CODEunsigned short), |
2621 | (destructor)pattern_dealloc, /* tp_dealloc */ |
2622 | 0, /* tp_print */ |
2623 | 0, /* tp_getattr */ |
2624 | 0, /* tp_setattr */ |
2625 | 0, /* tp_reserved */ |
2626 | 0, /* tp_repr */ |
2627 | 0, /* tp_as_number */ |
2628 | 0, /* tp_as_sequence */ |
2629 | 0, /* tp_as_mapping */ |
2630 | 0, /* tp_hash */ |
2631 | 0, /* tp_call */ |
2632 | 0, /* tp_str */ |
2633 | 0, /* tp_getattro */ |
2634 | 0, /* tp_setattro */ |
2635 | 0, /* tp_as_buffer */ |
2636 | Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0), /* tp_flags */ |
2637 | pattern_doc, /* tp_doc */ |
2638 | 0, /* tp_traverse */ |
2639 | 0, /* tp_clear */ |
2640 | 0, /* tp_richcompare */ |
2641 | offsetof(PatternObject, weakreflist)__builtin_offsetof(PatternObject, weakreflist), /* tp_weaklistoffset */ |
2642 | 0, /* tp_iter */ |
2643 | 0, /* tp_iternext */ |
2644 | pattern_methods, /* tp_methods */ |
2645 | pattern_members, /* tp_members */ |
2646 | }; |
2647 | |
2648 | static int _validate(PatternObject *self); /* Forward */ |
2649 | |
2650 | static PyObject * |
2651 | _compile(PyObject* self_, PyObject* args) |
2652 | { |
2653 | /* "compile" pattern descriptor to pattern object */ |
2654 | |
2655 | PatternObject* self; |
2656 | Py_ssize_t i, n; |
2657 | |
2658 | PyObject* pattern; |
2659 | int flags = 0; |
2660 | PyObject* code; |
2661 | Py_ssize_t groups = 0; |
2662 | PyObject* groupindex = NULL((void*)0); |
2663 | PyObject* indexgroup = NULL((void*)0); |
2664 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "OiO!|nOO", &pattern, &flags, |
2665 | &PyList_Type, &code, &groups, |
2666 | &groupindex, &indexgroup)) |
2667 | return NULL((void*)0); |
2668 | |
2669 | n = PyList_GET_SIZE(code)(((PyVarObject*)(code))->ob_size); |
2670 | /* coverity[ampersand_in_size] */ |
2671 | self = PyObject_NEW_VAR(PatternObject, &Pattern_Type, n)( (PatternObject *) PyObject_InitVar( (PyVarObject *) _PyObject_DebugMalloc ((size_t) ( ( ((&Pattern_Type))->tp_basicsize + ((n))* ((&Pattern_Type))->tp_itemsize + (8 - 1) ) & ~(8 - 1) ) ), (&Pattern_Type), (n)) ); |
2672 | if (!self) |
2673 | return NULL((void*)0); |
2674 | self->weakreflist = NULL((void*)0); |
2675 | self->pattern = NULL((void*)0); |
2676 | self->groupindex = NULL((void*)0); |
2677 | self->indexgroup = NULL((void*)0); |
2678 | |
2679 | self->codesize = n; |
2680 | |
2681 | for (i = 0; i < n; i++) { |
2682 | PyObject *o = PyList_GET_ITEM(code, i)(((PyListObject *)(code))->ob_item[i]); |
2683 | unsigned long value = PyLong_AsUnsignedLong(o); |
2684 | self->code[i] = (SRE_CODEunsigned short) value; |
2685 | if ((unsigned long) self->code[i] != value) { |
2686 | PyErr_SetString(PyExc_OverflowError, |
2687 | "regular expression code size limit exceeded"); |
2688 | break; |
2689 | } |
2690 | } |
2691 | |
2692 | if (PyErr_Occurred()) { |
2693 | Py_DECREF(self)do { if (_Py_RefTotal-- , --((PyObject*)(self))->ob_refcnt != 0) { if (((PyObject*)self)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2693, (PyObject *)(self)); } else _Py_Dealloc ((PyObject *)(self)); } while (0); |
2694 | return NULL((void*)0); |
2695 | } |
2696 | |
2697 | if (pattern == Py_None(&_Py_NoneStruct)) |
2698 | self->charsize = -1; |
2699 | else { |
2700 | Py_ssize_t p_length; |
2701 | if (!getstring(pattern, &p_length, &self->charsize)) { |
2702 | Py_DECREF(self)do { if (_Py_RefTotal-- , --((PyObject*)(self))->ob_refcnt != 0) { if (((PyObject*)self)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2702, (PyObject *)(self)); } else _Py_Dealloc ((PyObject *)(self)); } while (0); |
2703 | return NULL((void*)0); |
2704 | } |
2705 | } |
2706 | |
2707 | Py_INCREF(pattern)( _Py_RefTotal++ , ((PyObject*)(pattern))->ob_refcnt++); |
2708 | self->pattern = pattern; |
2709 | |
2710 | self->flags = flags; |
2711 | |
2712 | self->groups = groups; |
2713 | |
2714 | Py_XINCREF(groupindex)do { if ((groupindex) == ((void*)0)) ; else ( _Py_RefTotal++ , ((PyObject*)(groupindex))->ob_refcnt++); } while (0); |
2715 | self->groupindex = groupindex; |
2716 | |
2717 | Py_XINCREF(indexgroup)do { if ((indexgroup) == ((void*)0)) ; else ( _Py_RefTotal++ , ((PyObject*)(indexgroup))->ob_refcnt++); } while (0); |
2718 | self->indexgroup = indexgroup; |
2719 | |
2720 | self->weakreflist = NULL((void*)0); |
2721 | |
2722 | if (!_validate(self)) { |
2723 | Py_DECREF(self)do { if (_Py_RefTotal-- , --((PyObject*)(self))->ob_refcnt != 0) { if (((PyObject*)self)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 2723, (PyObject *)(self)); } else _Py_Dealloc ((PyObject *)(self)); } while (0); |
2724 | return NULL((void*)0); |
2725 | } |
2726 | |
2727 | return (PyObject*) self; |
2728 | } |
2729 | |
2730 | /* -------------------------------------------------------------------- */ |
2731 | /* Code validation */ |
2732 | |
2733 | /* To learn more about this code, have a look at the _compile() function in |
2734 | Lib/sre_compile.py. The validation functions below checks the code array |
2735 | for conformance with the code patterns generated there. |
2736 | |
2737 | The nice thing about the generated code is that it is position-independent: |
2738 | all jumps are relative jumps forward. Also, jumps don't cross each other: |
2739 | the target of a later jump is always earlier than the target of an earlier |
2740 | jump. IOW, this is okay: |
2741 | |
2742 | J---------J-------T--------T |
2743 | \ \_____/ / |
2744 | \______________________/ |
2745 | |
2746 | but this is not: |
2747 | |
2748 | J---------J-------T--------T |
2749 | \_________\_____/ / |
2750 | \____________/ |
2751 | |
2752 | It also helps that SRE_CODE is always an unsigned type, either 2 bytes or 4 |
2753 | bytes wide (the latter if Python is compiled for "wide" unicode support). |
2754 | */ |
2755 | |
2756 | /* Defining this one enables tracing of the validator */ |
2757 | #undef VVERBOSE |
2758 | |
2759 | /* Trace macro for the validator */ |
2760 | #if defined(VVERBOSE) |
2761 | #define VTRACE(v) printf v |
2762 | #else |
2763 | #define VTRACE(v) |
2764 | #endif |
2765 | |
2766 | /* Report failure */ |
2767 | #define FAILdo { ; return 0; } while (0) do { VTRACE(("FAIL: %d\n", __LINE__)); return 0; } while (0) |
2768 | |
2769 | /* Extract opcode, argument, or skip count from code array */ |
2770 | #define GET_OPdo { ; if (code >= end) do { ; return 0; } while (0); op = *code++; ; } while (0) \ |
2771 | do { \ |
2772 | VTRACE(("%p: ", code)); \ |
2773 | if (code >= end) FAILdo { ; return 0; } while (0); \ |
2774 | op = *code++; \ |
2775 | VTRACE(("%lu (op)\n", (unsigned long)op)); \ |
2776 | } while (0) |
2777 | #define GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0) \ |
2778 | do { \ |
2779 | VTRACE(("%p= ", code)); \ |
2780 | if (code >= end) FAILdo { ; return 0; } while (0); \ |
2781 | arg = *code++; \ |
2782 | VTRACE(("%lu (arg)\n", (unsigned long)arg)); \ |
2783 | } while (0) |
2784 | #define GET_SKIP_ADJ(adj)do { ; if (code >= end) do { ; return 0; } while (0); skip = *code; ; if (code+skip-adj < code || code+skip-adj > end) do { ; return 0; } while (0); code++; } while (0) \ |
2785 | do { \ |
2786 | VTRACE(("%p= ", code)); \ |
2787 | if (code >= end) FAILdo { ; return 0; } while (0); \ |
2788 | skip = *code; \ |
2789 | VTRACE(("%lu (skip to %p)\n", \ |
2790 | (unsigned long)skip, code+skip)); \ |
2791 | if (code+skip-adj < code || code+skip-adj > end)\ |
2792 | FAILdo { ; return 0; } while (0); \ |
2793 | code++; \ |
2794 | } while (0) |
2795 | #define GET_SKIPdo { ; if (code >= end) do { ; return 0; } while (0); skip = *code; ; if (code+skip-0 < code || code+skip-0 > end ) do { ; return 0; } while (0); code++; } while (0) GET_SKIP_ADJ(0)do { ; if (code >= end) do { ; return 0; } while (0); skip = *code; ; if (code+skip-0 < code || code+skip-0 > end ) do { ; return 0; } while (0); code++; } while (0) |
2796 | |
2797 | static int |
2798 | _validate_charset(SRE_CODEunsigned short *code, SRE_CODEunsigned short *end) |
2799 | { |
2800 | /* Some variables are manipulated by the macros above */ |
2801 | SRE_CODEunsigned short op; |
2802 | SRE_CODEunsigned short arg; |
2803 | SRE_CODEunsigned short offset; |
2804 | int i; |
2805 | |
2806 | while (code < end) { |
2807 | GET_OPdo { ; if (code >= end) do { ; return 0; } while (0); op = *code++; ; } while (0); |
2808 | switch (op) { |
2809 | |
2810 | case SRE_OP_NEGATE26: |
2811 | break; |
2812 | |
2813 | case SRE_OP_LITERAL19: |
2814 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); |
2815 | break; |
2816 | |
2817 | case SRE_OP_RANGE27: |
2818 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); |
2819 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); |
2820 | break; |
2821 | |
2822 | case SRE_OP_CHARSET10: |
2823 | offset = 32/sizeof(SRE_CODEunsigned short); /* 32-byte bitmap */ |
2824 | if (code+offset < code || code+offset > end) |
2825 | FAILdo { ; return 0; } while (0); |
2826 | code += offset; |
2827 | break; |
2828 | |
2829 | case SRE_OP_BIGCHARSET11: |
2830 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); /* Number of blocks */ |
2831 | offset = 256/sizeof(SRE_CODEunsigned short); /* 256-byte table */ |
2832 | if (code+offset < code || code+offset > end) |
2833 | FAILdo { ; return 0; } while (0); |
2834 | /* Make sure that each byte points to a valid block */ |
2835 | for (i = 0; i < 256; i++) { |
2836 | if (((unsigned char *)code)[i] >= arg) |
2837 | FAILdo { ; return 0; } while (0); |
2838 | } |
2839 | code += offset; |
2840 | offset = arg * 32/sizeof(SRE_CODEunsigned short); /* 32-byte bitmap times arg */ |
2841 | if (code+offset < code || code+offset > end) |
2842 | FAILdo { ; return 0; } while (0); |
2843 | code += offset; |
2844 | break; |
2845 | |
2846 | case SRE_OP_CATEGORY9: |
2847 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); |
2848 | switch (arg) { |
2849 | case SRE_CATEGORY_DIGIT0: |
2850 | case SRE_CATEGORY_NOT_DIGIT1: |
2851 | case SRE_CATEGORY_SPACE2: |
2852 | case SRE_CATEGORY_NOT_SPACE3: |
2853 | case SRE_CATEGORY_WORD4: |
2854 | case SRE_CATEGORY_NOT_WORD5: |
2855 | case SRE_CATEGORY_LINEBREAK6: |
2856 | case SRE_CATEGORY_NOT_LINEBREAK7: |
2857 | case SRE_CATEGORY_LOC_WORD8: |
2858 | case SRE_CATEGORY_LOC_NOT_WORD9: |
2859 | case SRE_CATEGORY_UNI_DIGIT10: |
2860 | case SRE_CATEGORY_UNI_NOT_DIGIT11: |
2861 | case SRE_CATEGORY_UNI_SPACE12: |
2862 | case SRE_CATEGORY_UNI_NOT_SPACE13: |
2863 | case SRE_CATEGORY_UNI_WORD14: |
2864 | case SRE_CATEGORY_UNI_NOT_WORD15: |
2865 | case SRE_CATEGORY_UNI_LINEBREAK16: |
2866 | case SRE_CATEGORY_UNI_NOT_LINEBREAK17: |
2867 | break; |
2868 | default: |
2869 | FAILdo { ; return 0; } while (0); |
2870 | } |
2871 | break; |
2872 | |
2873 | default: |
2874 | FAILdo { ; return 0; } while (0); |
2875 | |
2876 | } |
2877 | } |
2878 | |
2879 | return 1; |
2880 | } |
2881 | |
2882 | static int |
2883 | _validate_inner(SRE_CODEunsigned short *code, SRE_CODEunsigned short *end, Py_ssize_t groups) |
2884 | { |
2885 | /* Some variables are manipulated by the macros above */ |
2886 | SRE_CODEunsigned short op; |
2887 | SRE_CODEunsigned short arg; |
2888 | SRE_CODEunsigned short skip; |
2889 | |
2890 | VTRACE(("code=%p, end=%p\n", code, end)); |
2891 | |
2892 | if (code > end) |
2893 | FAILdo { ; return 0; } while (0); |
2894 | |
2895 | while (code < end) { |
2896 | GET_OPdo { ; if (code >= end) do { ; return 0; } while (0); op = *code++; ; } while (0); |
2897 | switch (op) { |
2898 | |
2899 | case SRE_OP_MARK21: |
2900 | /* We don't check whether marks are properly nested; the |
2901 | sre_match() code is robust even if they don't, and the worst |
2902 | you can get is nonsensical match results. */ |
2903 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); |
2904 | if (arg > 2*groups+1) { |
2905 | VTRACE(("arg=%d, groups=%d\n", (int)arg, (int)groups)); |
2906 | FAILdo { ; return 0; } while (0); |
2907 | } |
2908 | break; |
2909 | |
2910 | case SRE_OP_LITERAL19: |
2911 | case SRE_OP_NOT_LITERAL24: |
2912 | case SRE_OP_LITERAL_IGNORE20: |
2913 | case SRE_OP_NOT_LITERAL_IGNORE25: |
2914 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); |
2915 | /* The arg is just a character, nothing to check */ |
2916 | break; |
2917 | |
2918 | case SRE_OP_SUCCESS1: |
2919 | case SRE_OP_FAILURE0: |
2920 | /* Nothing to check; these normally end the matching process */ |
2921 | break; |
2922 | |
2923 | case SRE_OP_AT6: |
2924 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); |
2925 | switch (arg) { |
2926 | case SRE_AT_BEGINNING0: |
2927 | case SRE_AT_BEGINNING_STRING2: |
2928 | case SRE_AT_BEGINNING_LINE1: |
2929 | case SRE_AT_END5: |
2930 | case SRE_AT_END_LINE6: |
2931 | case SRE_AT_END_STRING7: |
2932 | case SRE_AT_BOUNDARY3: |
2933 | case SRE_AT_NON_BOUNDARY4: |
2934 | case SRE_AT_LOC_BOUNDARY8: |
2935 | case SRE_AT_LOC_NON_BOUNDARY9: |
2936 | case SRE_AT_UNI_BOUNDARY10: |
2937 | case SRE_AT_UNI_NON_BOUNDARY11: |
2938 | break; |
2939 | default: |
2940 | FAILdo { ; return 0; } while (0); |
2941 | } |
2942 | break; |
2943 | |
2944 | case SRE_OP_ANY2: |
2945 | case SRE_OP_ANY_ALL3: |
2946 | /* These have no operands */ |
2947 | break; |
2948 | |
2949 | case SRE_OP_IN15: |
2950 | case SRE_OP_IN_IGNORE16: |
2951 | GET_SKIPdo { ; if (code >= end) do { ; return 0; } while (0); skip = *code; ; if (code+skip-0 < code || code+skip-0 > end ) do { ; return 0; } while (0); code++; } while (0); |
2952 | /* Stop 1 before the end; we check the FAILURE below */ |
2953 | if (!_validate_charset(code, code+skip-2)) |
2954 | FAILdo { ; return 0; } while (0); |
2955 | if (code[skip-2] != SRE_OP_FAILURE0) |
2956 | FAILdo { ; return 0; } while (0); |
2957 | code += skip-1; |
2958 | break; |
2959 | |
2960 | case SRE_OP_INFO17: |
2961 | { |
2962 | /* A minimal info field is |
2963 | <INFO> <1=skip> <2=flags> <3=min> <4=max>; |
2964 | If SRE_INFO_PREFIX or SRE_INFO_CHARSET is in the flags, |
2965 | more follows. */ |
2966 | SRE_CODEunsigned short flags, min, max, i; |
2967 | SRE_CODEunsigned short *newcode; |
2968 | GET_SKIPdo { ; if (code >= end) do { ; return 0; } while (0); skip = *code; ; if (code+skip-0 < code || code+skip-0 > end ) do { ; return 0; } while (0); code++; } while (0); |
2969 | newcode = code+skip-1; |
2970 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); flags = arg; |
2971 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); min = arg; |
Value stored to 'min' is never read | |
2972 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); max = arg; |
2973 | /* Check that only valid flags are present */ |
2974 | if ((flags & ~(SRE_INFO_PREFIX1 | |
2975 | SRE_INFO_LITERAL2 | |
2976 | SRE_INFO_CHARSET4)) != 0) |
2977 | FAILdo { ; return 0; } while (0); |
2978 | /* PREFIX and CHARSET are mutually exclusive */ |
2979 | if ((flags & SRE_INFO_PREFIX1) && |
2980 | (flags & SRE_INFO_CHARSET4)) |
2981 | FAILdo { ; return 0; } while (0); |
2982 | /* LITERAL implies PREFIX */ |
2983 | if ((flags & SRE_INFO_LITERAL2) && |
2984 | !(flags & SRE_INFO_PREFIX1)) |
2985 | FAILdo { ; return 0; } while (0); |
2986 | /* Validate the prefix */ |
2987 | if (flags & SRE_INFO_PREFIX1) { |
2988 | SRE_CODEunsigned short prefix_len, prefix_skip; |
2989 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); prefix_len = arg; |
2990 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); prefix_skip = arg; |
2991 | /* Here comes the prefix string */ |
2992 | if (code+prefix_len < code || code+prefix_len > newcode) |
2993 | FAILdo { ; return 0; } while (0); |
2994 | code += prefix_len; |
2995 | /* And here comes the overlap table */ |
2996 | if (code+prefix_len < code || code+prefix_len > newcode) |
2997 | FAILdo { ; return 0; } while (0); |
2998 | /* Each overlap value should be < prefix_len */ |
2999 | for (i = 0; i < prefix_len; i++) { |
3000 | if (code[i] >= prefix_len) |
3001 | FAILdo { ; return 0; } while (0); |
3002 | } |
3003 | code += prefix_len; |
3004 | } |
3005 | /* Validate the charset */ |
3006 | if (flags & SRE_INFO_CHARSET4) { |
3007 | if (!_validate_charset(code, newcode-1)) |
3008 | FAILdo { ; return 0; } while (0); |
3009 | if (newcode[-1] != SRE_OP_FAILURE0) |
3010 | FAILdo { ; return 0; } while (0); |
3011 | code = newcode; |
3012 | } |
3013 | else if (code != newcode) { |
3014 | VTRACE(("code=%p, newcode=%p\n", code, newcode)); |
3015 | FAILdo { ; return 0; } while (0); |
3016 | } |
3017 | } |
3018 | break; |
3019 | |
3020 | case SRE_OP_BRANCH7: |
3021 | { |
3022 | SRE_CODEunsigned short *target = NULL((void*)0); |
3023 | for (;;) { |
3024 | GET_SKIPdo { ; if (code >= end) do { ; return 0; } while (0); skip = *code; ; if (code+skip-0 < code || code+skip-0 > end ) do { ; return 0; } while (0); code++; } while (0); |
3025 | if (skip == 0) |
3026 | break; |
3027 | /* Stop 2 before the end; we check the JUMP below */ |
3028 | if (!_validate_inner(code, code+skip-3, groups)) |
3029 | FAILdo { ; return 0; } while (0); |
3030 | code += skip-3; |
3031 | /* Check that it ends with a JUMP, and that each JUMP |
3032 | has the same target */ |
3033 | GET_OPdo { ; if (code >= end) do { ; return 0; } while (0); op = *code++; ; } while (0); |
3034 | if (op != SRE_OP_JUMP18) |
3035 | FAILdo { ; return 0; } while (0); |
3036 | GET_SKIPdo { ; if (code >= end) do { ; return 0; } while (0); skip = *code; ; if (code+skip-0 < code || code+skip-0 > end ) do { ; return 0; } while (0); code++; } while (0); |
3037 | if (target == NULL((void*)0)) |
3038 | target = code+skip-1; |
3039 | else if (code+skip-1 != target) |
3040 | FAILdo { ; return 0; } while (0); |
3041 | } |
3042 | } |
3043 | break; |
3044 | |
3045 | case SRE_OP_REPEAT_ONE29: |
3046 | case SRE_OP_MIN_REPEAT_ONE31: |
3047 | { |
3048 | SRE_CODEunsigned short min, max; |
3049 | GET_SKIPdo { ; if (code >= end) do { ; return 0; } while (0); skip = *code; ; if (code+skip-0 < code || code+skip-0 > end ) do { ; return 0; } while (0); code++; } while (0); |
3050 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); min = arg; |
3051 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); max = arg; |
3052 | if (min > max) |
3053 | FAILdo { ; return 0; } while (0); |
3054 | #ifdef Py_UNICODE_WIDE |
3055 | if (max > 65535) |
3056 | FAILdo { ; return 0; } while (0); |
3057 | #endif |
3058 | if (!_validate_inner(code, code+skip-4, groups)) |
3059 | FAILdo { ; return 0; } while (0); |
3060 | code += skip-4; |
3061 | GET_OPdo { ; if (code >= end) do { ; return 0; } while (0); op = *code++; ; } while (0); |
3062 | if (op != SRE_OP_SUCCESS1) |
3063 | FAILdo { ; return 0; } while (0); |
3064 | } |
3065 | break; |
3066 | |
3067 | case SRE_OP_REPEAT28: |
3068 | { |
3069 | SRE_CODEunsigned short min, max; |
3070 | GET_SKIPdo { ; if (code >= end) do { ; return 0; } while (0); skip = *code; ; if (code+skip-0 < code || code+skip-0 > end ) do { ; return 0; } while (0); code++; } while (0); |
3071 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); min = arg; |
3072 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); max = arg; |
3073 | if (min > max) |
3074 | FAILdo { ; return 0; } while (0); |
3075 | #ifdef Py_UNICODE_WIDE |
3076 | if (max > 65535) |
3077 | FAILdo { ; return 0; } while (0); |
3078 | #endif |
3079 | if (!_validate_inner(code, code+skip-3, groups)) |
3080 | FAILdo { ; return 0; } while (0); |
3081 | code += skip-3; |
3082 | GET_OPdo { ; if (code >= end) do { ; return 0; } while (0); op = *code++; ; } while (0); |
3083 | if (op != SRE_OP_MAX_UNTIL22 && op != SRE_OP_MIN_UNTIL23) |
3084 | FAILdo { ; return 0; } while (0); |
3085 | } |
3086 | break; |
3087 | |
3088 | case SRE_OP_GROUPREF12: |
3089 | case SRE_OP_GROUPREF_IGNORE14: |
3090 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); |
3091 | if (arg >= groups) |
3092 | FAILdo { ; return 0; } while (0); |
3093 | break; |
3094 | |
3095 | case SRE_OP_GROUPREF_EXISTS13: |
3096 | /* The regex syntax for this is: '(?(group)then|else)', where |
3097 | 'group' is either an integer group number or a group name, |
3098 | 'then' and 'else' are sub-regexes, and 'else' is optional. */ |
3099 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); |
3100 | if (arg >= groups) |
3101 | FAILdo { ; return 0; } while (0); |
3102 | GET_SKIP_ADJ(1)do { ; if (code >= end) do { ; return 0; } while (0); skip = *code; ; if (code+skip-1 < code || code+skip-1 > end ) do { ; return 0; } while (0); code++; } while (0); |
3103 | code--; /* The skip is relative to the first arg! */ |
3104 | /* There are two possibilities here: if there is both a 'then' |
3105 | part and an 'else' part, the generated code looks like: |
3106 | |
3107 | GROUPREF_EXISTS |
3108 | <group> |
3109 | <skipyes> |
3110 | ...then part... |
3111 | JUMP |
3112 | <skipno> |
3113 | (<skipyes> jumps here) |
3114 | ...else part... |
3115 | (<skipno> jumps here) |
3116 | |
3117 | If there is only a 'then' part, it looks like: |
3118 | |
3119 | GROUPREF_EXISTS |
3120 | <group> |
3121 | <skip> |
3122 | ...then part... |
3123 | (<skip> jumps here) |
3124 | |
3125 | There is no direct way to decide which it is, and we don't want |
3126 | to allow arbitrary jumps anywhere in the code; so we just look |
3127 | for a JUMP opcode preceding our skip target. |
3128 | */ |
3129 | if (skip >= 3 && code+skip-3 >= code && |
3130 | code[skip-3] == SRE_OP_JUMP18) |
3131 | { |
3132 | VTRACE(("both then and else parts present\n")); |
3133 | if (!_validate_inner(code+1, code+skip-3, groups)) |
3134 | FAILdo { ; return 0; } while (0); |
3135 | code += skip-2; /* Position after JUMP, at <skipno> */ |
3136 | GET_SKIPdo { ; if (code >= end) do { ; return 0; } while (0); skip = *code; ; if (code+skip-0 < code || code+skip-0 > end ) do { ; return 0; } while (0); code++; } while (0); |
3137 | if (!_validate_inner(code, code+skip-1, groups)) |
3138 | FAILdo { ; return 0; } while (0); |
3139 | code += skip-1; |
3140 | } |
3141 | else { |
3142 | VTRACE(("only a then part present\n")); |
3143 | if (!_validate_inner(code+1, code+skip-1, groups)) |
3144 | FAILdo { ; return 0; } while (0); |
3145 | code += skip-1; |
3146 | } |
3147 | break; |
3148 | |
3149 | case SRE_OP_ASSERT4: |
3150 | case SRE_OP_ASSERT_NOT5: |
3151 | GET_SKIPdo { ; if (code >= end) do { ; return 0; } while (0); skip = *code; ; if (code+skip-0 < code || code+skip-0 > end ) do { ; return 0; } while (0); code++; } while (0); |
3152 | GET_ARGdo { ; if (code >= end) do { ; return 0; } while (0); arg = *code++; ; } while (0); /* 0 for lookahead, width for lookbehind */ |
3153 | code--; /* Back up over arg to simplify math below */ |
3154 | if (arg & 0x80000000) |
3155 | FAILdo { ; return 0; } while (0); /* Width too large */ |
3156 | /* Stop 1 before the end; we check the SUCCESS below */ |
3157 | if (!_validate_inner(code+1, code+skip-2, groups)) |
3158 | FAILdo { ; return 0; } while (0); |
3159 | code += skip-2; |
3160 | GET_OPdo { ; if (code >= end) do { ; return 0; } while (0); op = *code++; ; } while (0); |
3161 | if (op != SRE_OP_SUCCESS1) |
3162 | FAILdo { ; return 0; } while (0); |
3163 | break; |
3164 | |
3165 | default: |
3166 | FAILdo { ; return 0; } while (0); |
3167 | |
3168 | } |
3169 | } |
3170 | |
3171 | VTRACE(("okay\n")); |
3172 | return 1; |
3173 | } |
3174 | |
3175 | static int |
3176 | _validate_outer(SRE_CODEunsigned short *code, SRE_CODEunsigned short *end, Py_ssize_t groups) |
3177 | { |
3178 | if (groups < 0 || groups > 100 || code >= end || end[-1] != SRE_OP_SUCCESS1) |
3179 | FAILdo { ; return 0; } while (0); |
3180 | if (groups == 0) /* fix for simplejson */ |
3181 | groups = 100; /* 100 groups should always be safe */ |
3182 | return _validate_inner(code, end-1, groups); |
3183 | } |
3184 | |
3185 | static int |
3186 | _validate(PatternObject *self) |
3187 | { |
3188 | if (!_validate_outer(self->code, self->code+self->codesize, self->groups)) |
3189 | { |
3190 | PyErr_SetString(PyExc_RuntimeError, "invalid SRE code"); |
3191 | return 0; |
3192 | } |
3193 | else |
3194 | VTRACE(("Success!\n")); |
3195 | return 1; |
3196 | } |
3197 | |
3198 | /* -------------------------------------------------------------------- */ |
3199 | /* match methods */ |
3200 | |
3201 | static void |
3202 | match_dealloc(MatchObject* self) |
3203 | { |
3204 | Py_XDECREF(self->regs)do { if ((self->regs) == ((void*)0)) ; else do { if (_Py_RefTotal -- , --((PyObject*)(self->regs))->ob_refcnt != 0) { if ( ((PyObject*)self->regs)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 3204, (PyObject *)(self->regs)); } else _Py_Dealloc((PyObject *)(self->regs)); } while (0); } while (0); |
3205 | Py_XDECREF(self->string)do { if ((self->string) == ((void*)0)) ; else do { if (_Py_RefTotal -- , --((PyObject*)(self->string))->ob_refcnt != 0) { if (((PyObject*)self->string)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 3205, (PyObject *)(self->string)); } else _Py_Dealloc((PyObject *)(self->string)); } while (0); } while (0); |
3206 | Py_DECREF(self->pattern)do { if (_Py_RefTotal-- , --((PyObject*)(self->pattern))-> ob_refcnt != 0) { if (((PyObject*)self->pattern)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_sre.c", 3206, (PyObject *)(self->pattern)); } else _Py_Dealloc((PyObject *)(self-> pattern)); } while (0); |
3207 | PyObject_DEL_PyObject_DebugFree(self); |
3208 | } |
3209 | |
3210 | static PyObject* |
3211 | match_getslice_by_index(MatchObject* self, Py_ssize_t index, PyObject* def) |
3212 | { |
3213 | if (index < 0 || index >= self->groups) { |
3214 | /* raise IndexError if we were given a bad group number */ |
3215 | PyErr_SetString( |
3216 | PyExc_IndexError, |
3217 | "no such group" |
3218 | ); |
3219 | return NULL((void*)0); |
3220 | } |
3221 | |
3222 | index *= 2; |
3223 | |
3224 | if (self->string == Py_None(&_Py_NoneStruct) || self->mark[index] < 0) { |
3225 | /* return default value if the string or group is undefined */ |
3226 | Py_INCREF(def)( _Py_RefTotal++ , ((PyObject*)(def))->ob_refcnt++); |
3227 | return def; |
3228 | } |
3229 | |
3230 | return PySequence_GetSlice( |
3231 | self->string, self->mark[index], self->mark[index+1] |
3232 | ); |
3233 | } |
3234 | |
3235 | static Py_ssize_t |
3236 | match_getindex(MatchObject* self, PyObject* index) |
3237 | { |
3238 | Py_ssize_t i; |
3239 | |
3240 | if (index == NULL((void*)0)) |
3241 | /* Default value */ |
3242 | return 0; |
3243 | |
3244 | if (PyLong_Check(index)((((((PyObject*)(index))->ob_type))->tp_flags & ((1L <<24))) != 0)) |
3245 | return PyLong_AsSsize_t(index); |
3246 | |
3247 | i = -1; |
3248 | |
3249 | if (self->pattern->groupindex) { |
3250 | index = PyObject_GetItem(self->pattern->groupindex, index); |
3251 | if (index) { |
3252 | if (PyLong_Check(index)((((((PyObject*)(index))->ob_type))->tp_flags & ((1L <<24))) != 0)) |
3253 | i = PyLong_AsSsize_t(index); |
3254 | Py_DECREF(index)do { if (_Py_RefTotal-- , --((PyObject*)(index))->ob_refcnt != 0) { if (((PyObject*)index)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 3254, (PyObject *)(index)); } else _Py_Dealloc ((PyObject *)(index)); } while (0); |
3255 | } else |
3256 | PyErr_Clear(); |
3257 | } |
3258 | |
3259 | return i; |
3260 | } |
3261 | |
3262 | static PyObject* |
3263 | match_getslice(MatchObject* self, PyObject* index, PyObject* def) |
3264 | { |
3265 | return match_getslice_by_index(self, match_getindex(self, index), def); |
3266 | } |
3267 | |
3268 | static PyObject* |
3269 | match_expand(MatchObject* self, PyObject* ptemplate) |
3270 | { |
3271 | /* delegate to Python code */ |
3272 | return call( |
3273 | SRE_PY_MODULE"re", "_expand", |
3274 | PyTuple_Pack(3, self->pattern, self, ptemplate) |
3275 | ); |
3276 | } |
3277 | |
3278 | static PyObject* |
3279 | match_group(MatchObject* self, PyObject* args) |
3280 | { |
3281 | PyObject* result; |
3282 | Py_ssize_t i, size; |
3283 | |
3284 | size = PyTuple_GET_SIZE(args)(((PyVarObject*)(args))->ob_size); |
3285 | |
3286 | switch (size) { |
3287 | case 0: |
3288 | result = match_getslice(self, Py_False((PyObject *) &_Py_FalseStruct), Py_None(&_Py_NoneStruct)); |
3289 | break; |
3290 | case 1: |
3291 | result = match_getslice(self, PyTuple_GET_ITEM(args, 0)(((PyTupleObject *)(args))->ob_item[0]), Py_None(&_Py_NoneStruct)); |
3292 | break; |
3293 | default: |
3294 | /* fetch multiple items */ |
3295 | result = PyTuple_New(size); |
3296 | if (!result) |
3297 | return NULL((void*)0); |
3298 | for (i = 0; i < size; i++) { |
3299 | PyObject* item = match_getslice( |
3300 | self, PyTuple_GET_ITEM(args, i)(((PyTupleObject *)(args))->ob_item[i]), Py_None(&_Py_NoneStruct) |
3301 | ); |
3302 | if (!item) { |
3303 | Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt != 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 3303, (PyObject *)(result)); } else _Py_Dealloc ((PyObject *)(result)); } while (0); |
3304 | return NULL((void*)0); |
3305 | } |
3306 | PyTuple_SET_ITEM(result, i, item)(((PyTupleObject *)(result))->ob_item[i] = item); |
3307 | } |
3308 | break; |
3309 | } |
3310 | return result; |
3311 | } |
3312 | |
3313 | static PyObject* |
3314 | match_groups(MatchObject* self, PyObject* args, PyObject* kw) |
3315 | { |
3316 | PyObject* result; |
3317 | Py_ssize_t index; |
3318 | |
3319 | PyObject* def = Py_None(&_Py_NoneStruct); |
3320 | static char* kwlist[] = { "default", NULL((void*)0) }; |
3321 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kw, "|O:groups", kwlist, &def)) |
3322 | return NULL((void*)0); |
3323 | |
3324 | result = PyTuple_New(self->groups-1); |
3325 | if (!result) |
3326 | return NULL((void*)0); |
3327 | |
3328 | for (index = 1; index < self->groups; index++) { |
3329 | PyObject* item; |
3330 | item = match_getslice_by_index(self, index, def); |
3331 | if (!item) { |
3332 | Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt != 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 3332, (PyObject *)(result)); } else _Py_Dealloc ((PyObject *)(result)); } while (0); |
3333 | return NULL((void*)0); |
3334 | } |
3335 | PyTuple_SET_ITEM(result, index-1, item)(((PyTupleObject *)(result))->ob_item[index-1] = item); |
3336 | } |
3337 | |
3338 | return result; |
3339 | } |
3340 | |
3341 | static PyObject* |
3342 | match_groupdict(MatchObject* self, PyObject* args, PyObject* kw) |
3343 | { |
3344 | PyObject* result; |
3345 | PyObject* keys; |
3346 | Py_ssize_t index; |
3347 | |
3348 | PyObject* def = Py_None(&_Py_NoneStruct); |
3349 | static char* kwlist[] = { "default", NULL((void*)0) }; |
3350 | if (!PyArg_ParseTupleAndKeywords_PyArg_ParseTupleAndKeywords_SizeT(args, kw, "|O:groupdict", kwlist, &def)) |
3351 | return NULL((void*)0); |
3352 | |
3353 | result = PyDict_New(); |
3354 | if (!result || !self->pattern->groupindex) |
3355 | return result; |
3356 | |
3357 | keys = PyMapping_Keys(self->pattern->groupindex); |
3358 | if (!keys) |
3359 | goto failed; |
3360 | |
3361 | for (index = 0; index < PyList_GET_SIZE(keys)(((PyVarObject*)(keys))->ob_size); index++) { |
3362 | int status; |
3363 | PyObject* key; |
3364 | PyObject* value; |
3365 | key = PyList_GET_ITEM(keys, index)(((PyListObject *)(keys))->ob_item[index]); |
3366 | if (!key) |
3367 | goto failed; |
3368 | value = match_getslice(self, key, def); |
3369 | if (!value) { |
3370 | Py_DECREF(key)do { if (_Py_RefTotal-- , --((PyObject*)(key))->ob_refcnt != 0) { if (((PyObject*)key)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 3370, (PyObject *)(key)); } else _Py_Dealloc ((PyObject *)(key)); } while (0); |
3371 | goto failed; |
3372 | } |
3373 | status = PyDict_SetItem(result, key, value); |
3374 | Py_DECREF(value)do { if (_Py_RefTotal-- , --((PyObject*)(value))->ob_refcnt != 0) { if (((PyObject*)value)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 3374, (PyObject *)(value)); } else _Py_Dealloc ((PyObject *)(value)); } while (0); |
3375 | if (status < 0) |
3376 | goto failed; |
3377 | } |
3378 | |
3379 | Py_DECREF(keys)do { if (_Py_RefTotal-- , --((PyObject*)(keys))->ob_refcnt != 0) { if (((PyObject*)keys)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 3379, (PyObject *)(keys)); } else _Py_Dealloc ((PyObject *)(keys)); } while (0); |
3380 | |
3381 | return result; |
3382 | |
3383 | failed: |
3384 | Py_XDECREF(keys)do { if ((keys) == ((void*)0)) ; else do { if (_Py_RefTotal-- , --((PyObject*)(keys))->ob_refcnt != 0) { if (((PyObject *)keys)->ob_refcnt < 0) _Py_NegativeRefcount("./Modules/_sre.c" , 3384, (PyObject *)(keys)); } else _Py_Dealloc((PyObject *)( keys)); } while (0); } while (0); |
3385 | Py_DECREF(result)do { if (_Py_RefTotal-- , --((PyObject*)(result))->ob_refcnt != 0) { if (((PyObject*)result)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 3385, (PyObject *)(result)); } else _Py_Dealloc ((PyObject *)(result)); } while (0); |
3386 | return NULL((void*)0); |
3387 | } |
3388 | |
3389 | static PyObject* |
3390 | match_start(MatchObject* self, PyObject* args) |
3391 | { |
3392 | Py_ssize_t index; |
3393 | |
3394 | PyObject* index_ = NULL((void*)0); |
3395 | if (!PyArg_UnpackTuple(args, "start", 0, 1, &index_)) |
3396 | return NULL((void*)0); |
3397 | |
3398 | index = match_getindex(self, index_); |
3399 | |
3400 | if (index < 0 || index >= self->groups) { |
3401 | PyErr_SetString( |
3402 | PyExc_IndexError, |
3403 | "no such group" |
3404 | ); |
3405 | return NULL((void*)0); |
3406 | } |
3407 | |
3408 | /* mark is -1 if group is undefined */ |
3409 | return Py_BuildValue_Py_BuildValue_SizeT("i", self->mark[index*2]); |
3410 | } |
3411 | |
3412 | static PyObject* |
3413 | match_end(MatchObject* self, PyObject* args) |
3414 | { |
3415 | Py_ssize_t index; |
3416 | |
3417 | PyObject* index_ = NULL((void*)0); |
3418 | if (!PyArg_UnpackTuple(args, "end", 0, 1, &index_)) |
3419 | return NULL((void*)0); |
3420 | |
3421 | index = match_getindex(self, index_); |
3422 | |
3423 | if (index < 0 || index >= self->groups) { |
3424 | PyErr_SetString( |
3425 | PyExc_IndexError, |
3426 | "no such group" |
3427 | ); |
3428 | return NULL((void*)0); |
3429 | } |
3430 | |
3431 | /* mark is -1 if group is undefined */ |
3432 | return Py_BuildValue_Py_BuildValue_SizeT("i", self->mark[index*2+1]); |
3433 | } |
3434 | |
3435 | LOCAL(PyObject*)static inline PyObject* |
3436 | _pair(Py_ssize_t i1, Py_ssize_t i2) |
3437 | { |
3438 | PyObject* pair; |
3439 | PyObject* item; |
3440 | |
3441 | pair = PyTuple_New(2); |
3442 | if (!pair) |
3443 | return NULL((void*)0); |
3444 | |
3445 | item = PyLong_FromSsize_t(i1); |
3446 | if (!item) |
3447 | goto error; |
3448 | PyTuple_SET_ITEM(pair, 0, item)(((PyTupleObject *)(pair))->ob_item[0] = item); |
3449 | |
3450 | item = PyLong_FromSsize_t(i2); |
3451 | if (!item) |
3452 | goto error; |
3453 | PyTuple_SET_ITEM(pair, 1, item)(((PyTupleObject *)(pair))->ob_item[1] = item); |
3454 | |
3455 | return pair; |
3456 | |
3457 | error: |
3458 | Py_DECREF(pair)do { if (_Py_RefTotal-- , --((PyObject*)(pair))->ob_refcnt != 0) { if (((PyObject*)pair)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 3458, (PyObject *)(pair)); } else _Py_Dealloc ((PyObject *)(pair)); } while (0); |
3459 | return NULL((void*)0); |
3460 | } |
3461 | |
3462 | static PyObject* |
3463 | match_span(MatchObject* self, PyObject* args) |
3464 | { |
3465 | Py_ssize_t index; |
3466 | |
3467 | PyObject* index_ = NULL((void*)0); |
3468 | if (!PyArg_UnpackTuple(args, "span", 0, 1, &index_)) |
3469 | return NULL((void*)0); |
3470 | |
3471 | index = match_getindex(self, index_); |
3472 | |
3473 | if (index < 0 || index >= self->groups) { |
3474 | PyErr_SetString( |
3475 | PyExc_IndexError, |
3476 | "no such group" |
3477 | ); |
3478 | return NULL((void*)0); |
3479 | } |
3480 | |
3481 | /* marks are -1 if group is undefined */ |
3482 | return _pair(self->mark[index*2], self->mark[index*2+1]); |
3483 | } |
3484 | |
3485 | static PyObject* |
3486 | match_regs(MatchObject* self) |
3487 | { |
3488 | PyObject* regs; |
3489 | PyObject* item; |
3490 | Py_ssize_t index; |
3491 | |
3492 | regs = PyTuple_New(self->groups); |
3493 | if (!regs) |
3494 | return NULL((void*)0); |
3495 | |
3496 | for (index = 0; index < self->groups; index++) { |
3497 | item = _pair(self->mark[index*2], self->mark[index*2+1]); |
3498 | if (!item) { |
3499 | Py_DECREF(regs)do { if (_Py_RefTotal-- , --((PyObject*)(regs))->ob_refcnt != 0) { if (((PyObject*)regs)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 3499, (PyObject *)(regs)); } else _Py_Dealloc ((PyObject *)(regs)); } while (0); |
3500 | return NULL((void*)0); |
3501 | } |
3502 | PyTuple_SET_ITEM(regs, index, item)(((PyTupleObject *)(regs))->ob_item[index] = item); |
3503 | } |
3504 | |
3505 | Py_INCREF(regs)( _Py_RefTotal++ , ((PyObject*)(regs))->ob_refcnt++); |
3506 | self->regs = regs; |
3507 | |
3508 | return regs; |
3509 | } |
3510 | |
3511 | static PyObject* |
3512 | match_copy(MatchObject* self, PyObject *unused) |
3513 | { |
3514 | #ifdef USE_BUILTIN_COPY |
3515 | MatchObject* copy; |
3516 | Py_ssize_t slots, offset; |
3517 | |
3518 | slots = 2 * (self->pattern->groups+1); |
3519 | |
3520 | copy = PyObject_NEW_VAR(MatchObject, &Match_Type, slots)( (MatchObject *) PyObject_InitVar( (PyVarObject *) _PyObject_DebugMalloc ((size_t) ( ( ((&Match_Type))->tp_basicsize + ((slots) )*((&Match_Type))->tp_itemsize + (8 - 1) ) & ~(8 - 1) ) ), (&Match_Type), (slots)) ); |
3521 | if (!copy) |
3522 | return NULL((void*)0); |
3523 | |
3524 | /* this value a constant, but any compiler should be able to |
3525 | figure that out all by itself */ |
3526 | offset = offsetof(MatchObject, string)__builtin_offsetof(MatchObject, string); |
3527 | |
3528 | Py_XINCREF(self->pattern)do { if ((self->pattern) == ((void*)0)) ; else ( _Py_RefTotal ++ , ((PyObject*)(self->pattern))->ob_refcnt++); } while (0); |
3529 | Py_XINCREF(self->string)do { if ((self->string) == ((void*)0)) ; else ( _Py_RefTotal ++ , ((PyObject*)(self->string))->ob_refcnt++); } while (0); |
3530 | Py_XINCREF(self->regs)do { if ((self->regs) == ((void*)0)) ; else ( _Py_RefTotal ++ , ((PyObject*)(self->regs))->ob_refcnt++); } while ( 0); |
3531 | |
3532 | memcpy((char*) copy + offset, (char*) self + offset,((__builtin_object_size ((char*) copy + offset, 0) != (size_t ) -1) ? __builtin___memcpy_chk ((char*) copy + offset, (char* ) self + offset, sizeof(MatchObject) + slots * sizeof(Py_ssize_t ) - offset, __builtin_object_size ((char*) copy + offset, 0)) : __inline_memcpy_chk ((char*) copy + offset, (char*) self + offset, sizeof(MatchObject) + slots * sizeof(Py_ssize_t) - offset )) |
3533 | sizeof(MatchObject) + slots * sizeof(Py_ssize_t) - offset)((__builtin_object_size ((char*) copy + offset, 0) != (size_t ) -1) ? __builtin___memcpy_chk ((char*) copy + offset, (char* ) self + offset, sizeof(MatchObject) + slots * sizeof(Py_ssize_t ) - offset, __builtin_object_size ((char*) copy + offset, 0)) : __inline_memcpy_chk ((char*) copy + offset, (char*) self + offset, sizeof(MatchObject) + slots * sizeof(Py_ssize_t) - offset )); |
3534 | |
3535 | return (PyObject*) copy; |
3536 | #else |
3537 | PyErr_SetString(PyExc_TypeError, "cannot copy this match object"); |
3538 | return NULL((void*)0); |
3539 | #endif |
3540 | } |
3541 | |
3542 | static PyObject* |
3543 | match_deepcopy(MatchObject* self, PyObject* memo) |
3544 | { |
3545 | #ifdef USE_BUILTIN_COPY |
3546 | MatchObject* copy; |
3547 | |
3548 | copy = (MatchObject*) match_copy(self); |
3549 | if (!copy) |
3550 | return NULL((void*)0); |
3551 | |
3552 | if (!deepcopy((PyObject**) ©->pattern, memo) || |
3553 | !deepcopy(©->string, memo) || |
3554 | !deepcopy(©->regs, memo)) { |
3555 | Py_DECREF(copy)do { if (_Py_RefTotal-- , --((PyObject*)(copy))->ob_refcnt != 0) { if (((PyObject*)copy)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 3555, (PyObject *)(copy)); } else _Py_Dealloc ((PyObject *)(copy)); } while (0); |
3556 | return NULL((void*)0); |
3557 | } |
3558 | |
3559 | #else |
3560 | PyErr_SetString(PyExc_TypeError, "cannot deepcopy this match object"); |
3561 | return NULL((void*)0); |
3562 | #endif |
3563 | } |
3564 | |
3565 | static PyMethodDef match_methods[] = { |
3566 | {"group", (PyCFunction) match_group, METH_VARARGS0x0001}, |
3567 | {"start", (PyCFunction) match_start, METH_VARARGS0x0001}, |
3568 | {"end", (PyCFunction) match_end, METH_VARARGS0x0001}, |
3569 | {"span", (PyCFunction) match_span, METH_VARARGS0x0001}, |
3570 | {"groups", (PyCFunction) match_groups, METH_VARARGS0x0001|METH_KEYWORDS0x0002}, |
3571 | {"groupdict", (PyCFunction) match_groupdict, METH_VARARGS0x0001|METH_KEYWORDS0x0002}, |
3572 | {"expand", (PyCFunction) match_expand, METH_O0x0008}, |
3573 | {"__copy__", (PyCFunction) match_copy, METH_NOARGS0x0004}, |
3574 | {"__deepcopy__", (PyCFunction) match_deepcopy, METH_O0x0008}, |
3575 | {NULL((void*)0), NULL((void*)0)} |
3576 | }; |
3577 | |
3578 | static PyObject * |
3579 | match_lastindex_get(MatchObject *self) |
3580 | { |
3581 | if (self->lastindex >= 0) |
3582 | return Py_BuildValue_Py_BuildValue_SizeT("i", self->lastindex); |
3583 | Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt ++); |
3584 | return Py_None(&_Py_NoneStruct); |
3585 | } |
3586 | |
3587 | static PyObject * |
3588 | match_lastgroup_get(MatchObject *self) |
3589 | { |
3590 | if (self->pattern->indexgroup && self->lastindex >= 0) { |
3591 | PyObject* result = PySequence_GetItem( |
3592 | self->pattern->indexgroup, self->lastindex |
3593 | ); |
3594 | if (result) |
3595 | return result; |
3596 | PyErr_Clear(); |
3597 | } |
3598 | Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt ++); |
3599 | return Py_None(&_Py_NoneStruct); |
3600 | } |
3601 | |
3602 | static PyObject * |
3603 | match_regs_get(MatchObject *self) |
3604 | { |
3605 | if (self->regs) { |
3606 | Py_INCREF(self->regs)( _Py_RefTotal++ , ((PyObject*)(self->regs))->ob_refcnt ++); |
3607 | return self->regs; |
3608 | } else |
3609 | return match_regs(self); |
3610 | } |
3611 | |
3612 | static PyGetSetDef match_getset[] = { |
3613 | {"lastindex", (getter)match_lastindex_get, (setter)NULL((void*)0)}, |
3614 | {"lastgroup", (getter)match_lastgroup_get, (setter)NULL((void*)0)}, |
3615 | {"regs", (getter)match_regs_get, (setter)NULL((void*)0)}, |
3616 | {NULL((void*)0)} |
3617 | }; |
3618 | |
3619 | #define MATCH_OFF(x)__builtin_offsetof(MatchObject, x) offsetof(MatchObject, x)__builtin_offsetof(MatchObject, x) |
3620 | static PyMemberDef match_members[] = { |
3621 | {"string", T_OBJECT6, MATCH_OFF(string)__builtin_offsetof(MatchObject, string), READONLY1}, |
3622 | {"re", T_OBJECT6, MATCH_OFF(pattern)__builtin_offsetof(MatchObject, pattern), READONLY1}, |
3623 | {"pos", T_PYSSIZET19, MATCH_OFF(pos)__builtin_offsetof(MatchObject, pos), READONLY1}, |
3624 | {"endpos", T_PYSSIZET19, MATCH_OFF(endpos)__builtin_offsetof(MatchObject, endpos), READONLY1}, |
3625 | {NULL((void*)0)} |
3626 | }; |
3627 | |
3628 | /* FIXME: implement setattr("string", None) as a special case (to |
3629 | detach the associated string, if any */ |
3630 | |
3631 | static PyTypeObject Match_Type = { |
3632 | PyVarObject_HEAD_INIT(NULL,0){ { 0, 0, 1, ((void*)0) }, 0 }, |
3633 | "_" SRE_MODULE"sre" ".SRE_Match", |
3634 | sizeof(MatchObject), sizeof(Py_ssize_t), |
3635 | (destructor)match_dealloc, /* tp_dealloc */ |
3636 | 0, /* tp_print */ |
3637 | 0, /* tp_getattr */ |
3638 | 0, /* tp_setattr */ |
3639 | 0, /* tp_reserved */ |
3640 | 0, /* tp_repr */ |
3641 | 0, /* tp_as_number */ |
3642 | 0, /* tp_as_sequence */ |
3643 | 0, /* tp_as_mapping */ |
3644 | 0, /* tp_hash */ |
3645 | 0, /* tp_call */ |
3646 | 0, /* tp_str */ |
3647 | 0, /* tp_getattro */ |
3648 | 0, /* tp_setattro */ |
3649 | 0, /* tp_as_buffer */ |
3650 | Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0), /* tp_flags */ |
3651 | 0, /* tp_doc */ |
3652 | 0, /* tp_traverse */ |
3653 | 0, /* tp_clear */ |
3654 | 0, /* tp_richcompare */ |
3655 | 0, /* tp_weaklistoffset */ |
3656 | 0, /* tp_iter */ |
3657 | 0, /* tp_iternext */ |
3658 | match_methods, /* tp_methods */ |
3659 | match_members, /* tp_members */ |
3660 | match_getset, /* tp_getset */ |
3661 | }; |
3662 | |
3663 | static PyObject* |
3664 | pattern_new_match(PatternObject* pattern, SRE_STATE* state, int status) |
3665 | { |
3666 | /* create match object (from state object) */ |
3667 | |
3668 | MatchObject* match; |
3669 | Py_ssize_t i, j; |
3670 | char* base; |
3671 | int n; |
3672 | |
3673 | if (status > 0) { |
3674 | |
3675 | /* create match object (with room for extra group marks) */ |
3676 | /* coverity[ampersand_in_size] */ |
3677 | match = PyObject_NEW_VAR(MatchObject, &Match_Type,( (MatchObject *) PyObject_InitVar( (PyVarObject *) _PyObject_DebugMalloc ((size_t) ( ( ((&Match_Type))->tp_basicsize + ((2*(pattern ->groups+1)))*((&Match_Type))->tp_itemsize + (8 - 1 ) ) & ~(8 - 1) ) ), (&Match_Type), (2*(pattern->groups +1))) ) |
3678 | 2*(pattern->groups+1))( (MatchObject *) PyObject_InitVar( (PyVarObject *) _PyObject_DebugMalloc ((size_t) ( ( ((&Match_Type))->tp_basicsize + ((2*(pattern ->groups+1)))*((&Match_Type))->tp_itemsize + (8 - 1 ) ) & ~(8 - 1) ) ), (&Match_Type), (2*(pattern->groups +1))) ); |
3679 | if (!match) |
3680 | return NULL((void*)0); |
3681 | |
3682 | Py_INCREF(pattern)( _Py_RefTotal++ , ((PyObject*)(pattern))->ob_refcnt++); |
3683 | match->pattern = pattern; |
3684 | |
3685 | Py_INCREF(state->string)( _Py_RefTotal++ , ((PyObject*)(state->string))->ob_refcnt ++); |
3686 | match->string = state->string; |
3687 | |
3688 | match->regs = NULL((void*)0); |
3689 | match->groups = pattern->groups+1; |
3690 | |
3691 | /* fill in group slices */ |
3692 | |
3693 | base = (char*) state->beginning; |
3694 | n = state->charsize; |
3695 | |
3696 | match->mark[0] = ((char*) state->start - base) / n; |
3697 | match->mark[1] = ((char*) state->ptr - base) / n; |
3698 | |
3699 | for (i = j = 0; i < pattern->groups; i++, j+=2) |
3700 | if (j+1 <= state->lastmark && state->mark[j] && state->mark[j+1]) { |
3701 | match->mark[j+2] = ((char*) state->mark[j] - base) / n; |
3702 | match->mark[j+3] = ((char*) state->mark[j+1] - base) / n; |
3703 | } else |
3704 | match->mark[j+2] = match->mark[j+3] = -1; /* undefined */ |
3705 | |
3706 | match->pos = state->pos; |
3707 | match->endpos = state->endpos; |
3708 | |
3709 | match->lastindex = state->lastindex; |
3710 | |
3711 | return (PyObject*) match; |
3712 | |
3713 | } else if (status == 0) { |
3714 | |
3715 | /* no match */ |
3716 | Py_INCREF(Py_None)( _Py_RefTotal++ , ((PyObject*)((&_Py_NoneStruct)))->ob_refcnt ++); |
3717 | return Py_None(&_Py_NoneStruct); |
3718 | |
3719 | } |
3720 | |
3721 | /* internal error */ |
3722 | pattern_error(status); |
3723 | return NULL((void*)0); |
3724 | } |
3725 | |
3726 | |
3727 | /* -------------------------------------------------------------------- */ |
3728 | /* scanner methods (experimental) */ |
3729 | |
3730 | static void |
3731 | scanner_dealloc(ScannerObject* self) |
3732 | { |
3733 | state_fini(&self->state); |
3734 | Py_XDECREF(self->pattern)do { if ((self->pattern) == ((void*)0)) ; else do { if (_Py_RefTotal -- , --((PyObject*)(self->pattern))->ob_refcnt != 0) { if (((PyObject*)self->pattern)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 3734, (PyObject *)(self->pattern)); } else _Py_Dealloc((PyObject *)(self->pattern)); } while (0 ); } while (0); |
3735 | PyObject_DEL_PyObject_DebugFree(self); |
3736 | } |
3737 | |
3738 | static PyObject* |
3739 | scanner_match(ScannerObject* self, PyObject *unused) |
3740 | { |
3741 | SRE_STATE* state = &self->state; |
3742 | PyObject* match; |
3743 | int status; |
3744 | |
3745 | state_reset(state); |
3746 | |
3747 | state->ptr = state->start; |
3748 | |
3749 | if (state->charsize == 1) { |
3750 | status = sre_match(state, PatternObject_GetCode(self->pattern)(((PatternObject*)(self->pattern))->code)); |
3751 | } else { |
3752 | #if defined(HAVE_UNICODE) |
3753 | status = sre_umatch(state, PatternObject_GetCode(self->pattern)(((PatternObject*)(self->pattern))->code)); |
3754 | #endif |
3755 | } |
3756 | if (PyErr_Occurred()) |
3757 | return NULL((void*)0); |
3758 | |
3759 | match = pattern_new_match((PatternObject*) self->pattern, |
3760 | state, status); |
3761 | |
3762 | if (status == 0 || state->ptr == state->start) |
3763 | state->start = (void*) ((char*) state->ptr + state->charsize); |
3764 | else |
3765 | state->start = state->ptr; |
3766 | |
3767 | return match; |
3768 | } |
3769 | |
3770 | |
3771 | static PyObject* |
3772 | scanner_search(ScannerObject* self, PyObject *unused) |
3773 | { |
3774 | SRE_STATE* state = &self->state; |
3775 | PyObject* match; |
3776 | int status; |
3777 | |
3778 | state_reset(state); |
3779 | |
3780 | state->ptr = state->start; |
3781 | |
3782 | if (state->charsize == 1) { |
3783 | status = sre_search(state, PatternObject_GetCode(self->pattern)(((PatternObject*)(self->pattern))->code)); |
3784 | } else { |
3785 | #if defined(HAVE_UNICODE) |
3786 | status = sre_usearch(state, PatternObject_GetCode(self->pattern)(((PatternObject*)(self->pattern))->code)); |
3787 | #endif |
3788 | } |
3789 | if (PyErr_Occurred()) |
3790 | return NULL((void*)0); |
3791 | |
3792 | match = pattern_new_match((PatternObject*) self->pattern, |
3793 | state, status); |
3794 | |
3795 | if (status == 0 || state->ptr == state->start) |
3796 | state->start = (void*) ((char*) state->ptr + state->charsize); |
3797 | else |
3798 | state->start = state->ptr; |
3799 | |
3800 | return match; |
3801 | } |
3802 | |
3803 | static PyMethodDef scanner_methods[] = { |
3804 | {"match", (PyCFunction) scanner_match, METH_NOARGS0x0004}, |
3805 | {"search", (PyCFunction) scanner_search, METH_NOARGS0x0004}, |
3806 | {NULL((void*)0), NULL((void*)0)} |
3807 | }; |
3808 | |
3809 | #define SCAN_OFF(x)__builtin_offsetof(ScannerObject, x) offsetof(ScannerObject, x)__builtin_offsetof(ScannerObject, x) |
3810 | static PyMemberDef scanner_members[] = { |
3811 | {"pattern", T_OBJECT6, SCAN_OFF(pattern)__builtin_offsetof(ScannerObject, pattern), READONLY1}, |
3812 | {NULL((void*)0)} /* Sentinel */ |
3813 | }; |
3814 | |
3815 | static PyTypeObject Scanner_Type = { |
3816 | PyVarObject_HEAD_INIT(NULL, 0){ { 0, 0, 1, ((void*)0) }, 0 }, |
3817 | "_" SRE_MODULE"sre" ".SRE_Scanner", |
3818 | sizeof(ScannerObject), 0, |
3819 | (destructor)scanner_dealloc,/* tp_dealloc */ |
3820 | 0, /* tp_print */ |
3821 | 0, /* tp_getattr */ |
3822 | 0, /* tp_setattr */ |
3823 | 0, /* tp_reserved */ |
3824 | 0, /* tp_repr */ |
3825 | 0, /* tp_as_number */ |
3826 | 0, /* tp_as_sequence */ |
3827 | 0, /* tp_as_mapping */ |
3828 | 0, /* tp_hash */ |
3829 | 0, /* tp_call */ |
3830 | 0, /* tp_str */ |
3831 | 0, /* tp_getattro */ |
3832 | 0, /* tp_setattro */ |
3833 | 0, /* tp_as_buffer */ |
3834 | Py_TPFLAGS_DEFAULT( 0 | (1L<<18) | 0), /* tp_flags */ |
3835 | 0, /* tp_doc */ |
3836 | 0, /* tp_traverse */ |
3837 | 0, /* tp_clear */ |
3838 | 0, /* tp_richcompare */ |
3839 | 0, /* tp_weaklistoffset */ |
3840 | 0, /* tp_iter */ |
3841 | 0, /* tp_iternext */ |
3842 | scanner_methods, /* tp_methods */ |
3843 | scanner_members, /* tp_members */ |
3844 | 0, /* tp_getset */ |
3845 | }; |
3846 | |
3847 | static PyObject* |
3848 | pattern_scanner(PatternObject* pattern, PyObject* args) |
3849 | { |
3850 | /* create search state object */ |
3851 | |
3852 | ScannerObject* self; |
3853 | |
3854 | PyObject* string; |
3855 | Py_ssize_t start = 0; |
3856 | Py_ssize_t end = PY_SSIZE_T_MAX((Py_ssize_t)(((size_t)-1)>>1)); |
3857 | if (!PyArg_ParseTuple_PyArg_ParseTuple_SizeT(args, "O|nn:scanner", &string, &start, &end)) |
3858 | return NULL((void*)0); |
3859 | |
3860 | /* create scanner object */ |
3861 | self = PyObject_NEW(ScannerObject, &Scanner_Type)( (ScannerObject *) PyObject_Init( (PyObject *) _PyObject_DebugMalloc ( ( (&Scanner_Type)->tp_basicsize ) ), (&Scanner_Type )) ); |
3862 | if (!self) |
3863 | return NULL((void*)0); |
3864 | self->pattern = NULL((void*)0); |
3865 | |
3866 | string = state_init(&self->state, pattern, string, start, end); |
3867 | if (!string) { |
3868 | Py_DECREF(self)do { if (_Py_RefTotal-- , --((PyObject*)(self))->ob_refcnt != 0) { if (((PyObject*)self)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 3868, (PyObject *)(self)); } else _Py_Dealloc ((PyObject *)(self)); } while (0); |
3869 | return NULL((void*)0); |
3870 | } |
3871 | |
3872 | Py_INCREF(pattern)( _Py_RefTotal++ , ((PyObject*)(pattern))->ob_refcnt++); |
3873 | self->pattern = (PyObject*) pattern; |
3874 | |
3875 | return (PyObject*) self; |
3876 | } |
3877 | |
3878 | static PyMethodDef _functions[] = { |
3879 | {"compile", _compile, METH_VARARGS0x0001}, |
3880 | {"getcodesize", sre_codesize, METH_NOARGS0x0004}, |
3881 | {"getlower", sre_getlower, METH_VARARGS0x0001}, |
3882 | {NULL((void*)0), NULL((void*)0)} |
3883 | }; |
3884 | |
3885 | static struct PyModuleDef sremodule = { |
3886 | PyModuleDef_HEAD_INIT{ { 0, 0, 1, ((void*)0) }, ((void*)0), 0, ((void*)0), }, |
3887 | "_" SRE_MODULE"sre", |
3888 | NULL((void*)0), |
3889 | -1, |
3890 | _functions, |
3891 | NULL((void*)0), |
3892 | NULL((void*)0), |
3893 | NULL((void*)0), |
3894 | NULL((void*)0) |
3895 | }; |
3896 | |
3897 | PyMODINIT_FUNCPyObject* PyInit__sre(void) |
3898 | { |
3899 | PyObject* m; |
3900 | PyObject* d; |
3901 | PyObject* x; |
3902 | |
3903 | /* Patch object types */ |
3904 | if (PyType_Ready(&Pattern_Type) || PyType_Ready(&Match_Type) || |
3905 | PyType_Ready(&Scanner_Type)) |
3906 | return NULL((void*)0); |
3907 | |
3908 | m = PyModule_Create(&sremodule)PyModule_Create2TraceRefs(&sremodule, 1013); |
3909 | if (m == NULL((void*)0)) |
3910 | return NULL((void*)0); |
3911 | d = PyModule_GetDict(m); |
3912 | |
3913 | x = PyLong_FromLong(SRE_MAGIC20031017); |
3914 | if (x) { |
3915 | PyDict_SetItemString(d, "MAGIC", x); |
3916 | Py_DECREF(x)do { if (_Py_RefTotal-- , --((PyObject*)(x))->ob_refcnt != 0) { if (((PyObject*)x)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 3916, (PyObject *)(x)); } else _Py_Dealloc ((PyObject *)(x)); } while (0); |
3917 | } |
3918 | |
3919 | x = PyLong_FromLong(sizeof(SRE_CODEunsigned short)); |
3920 | if (x) { |
3921 | PyDict_SetItemString(d, "CODESIZE", x); |
3922 | Py_DECREF(x)do { if (_Py_RefTotal-- , --((PyObject*)(x))->ob_refcnt != 0) { if (((PyObject*)x)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 3922, (PyObject *)(x)); } else _Py_Dealloc ((PyObject *)(x)); } while (0); |
3923 | } |
3924 | |
3925 | x = PyUnicode_FromStringPyUnicodeUCS2_FromString(copyright); |
3926 | if (x) { |
3927 | PyDict_SetItemString(d, "copyright", x); |
3928 | Py_DECREF(x)do { if (_Py_RefTotal-- , --((PyObject*)(x))->ob_refcnt != 0) { if (((PyObject*)x)->ob_refcnt < 0) _Py_NegativeRefcount ("./Modules/_sre.c", 3928, (PyObject *)(x)); } else _Py_Dealloc ((PyObject *)(x)); } while (0); |
3929 | } |
3930 | return m; |
3931 | } |
3932 | |
3933 | #endif /* !defined(SRE_RECURSIVE) */ |
3934 | |
3935 | /* vim:ts=4:sw=4:et |
3936 | */ |