| 1 |
/* |
| 2 |
* Secret Labs' Regular Expression Engine |
| 3 |
* |
| 4 |
* regular expression matching engine |
| 5 |
* |
| 6 |
* Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved. |
| 7 |
* |
| 8 |
* See the sre.c file for information on usage and redistribution. |
| 9 |
*/ |
| 10 |
|
| 11 |
/* String matching engine */ |
| 12 |
|
| 13 |
/* This file is included three times, with different character settings */ |
| 14 |
|
| 15 |
LOCAL(int) |
| 16 |
SRE(at)(SRE_STATE* state, const SRE_CHAR* ptr, SRE_CODE at) |
| 17 |
{ |
| 18 |
/* check if pointer is at given position */ |
| 19 |
|
| 20 |
Py_ssize_t thisp, thatp; |
| 21 |
|
| 22 |
switch (at) { |
| 23 |
|
| 24 |
case SRE_AT_BEGINNING: |
| 25 |
case SRE_AT_BEGINNING_STRING: |
| 26 |
return ((void*) ptr == state->beginning); |
| 27 |
|
| 28 |
case SRE_AT_BEGINNING_LINE: |
| 29 |
return ((void*) ptr == state->beginning || |
| 30 |
SRE_IS_LINEBREAK((int) ptr[-1])); |
| 31 |
|
| 32 |
case SRE_AT_END: |
| 33 |
return (((SRE_CHAR *)state->end - ptr == 1 && |
| 34 |
SRE_IS_LINEBREAK((int) ptr[0])) || |
| 35 |
((void*) ptr == state->end)); |
| 36 |
|
| 37 |
case SRE_AT_END_LINE: |
| 38 |
return ((void*) ptr == state->end || |
| 39 |
SRE_IS_LINEBREAK((int) ptr[0])); |
| 40 |
|
| 41 |
case SRE_AT_END_STRING: |
| 42 |
return ((void*) ptr == state->end); |
| 43 |
|
| 44 |
case SRE_AT_BOUNDARY: |
| 45 |
thatp = ((void*) ptr > state->beginning) ? |
| 46 |
SRE_IS_WORD((int) ptr[-1]) : 0; |
| 47 |
thisp = ((void*) ptr < state->end) ? |
| 48 |
SRE_IS_WORD((int) ptr[0]) : 0; |
| 49 |
return thisp != thatp; |
| 50 |
|
| 51 |
case SRE_AT_NON_BOUNDARY: |
| 52 |
thatp = ((void*) ptr > state->beginning) ? |
| 53 |
SRE_IS_WORD((int) ptr[-1]) : 0; |
| 54 |
thisp = ((void*) ptr < state->end) ? |
| 55 |
SRE_IS_WORD((int) ptr[0]) : 0; |
| 56 |
return thisp == thatp; |
| 57 |
|
| 58 |
case SRE_AT_LOC_BOUNDARY: |
| 59 |
thatp = ((void*) ptr > state->beginning) ? |
| 60 |
SRE_LOC_IS_WORD((int) ptr[-1]) : 0; |
| 61 |
thisp = ((void*) ptr < state->end) ? |
| 62 |
SRE_LOC_IS_WORD((int) ptr[0]) : 0; |
| 63 |
return thisp != thatp; |
| 64 |
|
| 65 |
case SRE_AT_LOC_NON_BOUNDARY: |
| 66 |
thatp = ((void*) ptr > state->beginning) ? |
| 67 |
SRE_LOC_IS_WORD((int) ptr[-1]) : 0; |
| 68 |
thisp = ((void*) ptr < state->end) ? |
| 69 |
SRE_LOC_IS_WORD((int) ptr[0]) : 0; |
| 70 |
return thisp == thatp; |
| 71 |
|
| 72 |
case SRE_AT_UNI_BOUNDARY: |
| 73 |
thatp = ((void*) ptr > state->beginning) ? |
| 74 |
SRE_UNI_IS_WORD((int) ptr[-1]) : 0; |
| 75 |
thisp = ((void*) ptr < state->end) ? |
| 76 |
SRE_UNI_IS_WORD((int) ptr[0]) : 0; |
| 77 |
return thisp != thatp; |
| 78 |
|
| 79 |
case SRE_AT_UNI_NON_BOUNDARY: |
| 80 |
thatp = ((void*) ptr > state->beginning) ? |
| 81 |
SRE_UNI_IS_WORD((int) ptr[-1]) : 0; |
| 82 |
thisp = ((void*) ptr < state->end) ? |
| 83 |
SRE_UNI_IS_WORD((int) ptr[0]) : 0; |
| 84 |
return thisp == thatp; |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
LOCAL(int) |
| 92 |
SRE(charset)(SRE_STATE* state, const SRE_CODE* set, SRE_CODE ch) |
| 93 |
{ |
| 94 |
/* check if character is a member of the given set */ |
| 95 |
|
| 96 |
int ok = 1; |
| 97 |
|
| 98 |
for (;;) { |
| 99 |
switch (*set++) { |
| 100 |
|
| 101 |
case SRE_OP_FAILURE: |
| 102 |
return !ok; |
| 103 |
|
| 104 |
case SRE_OP_LITERAL: |
| 105 |
/* <LITERAL> <code> */ |
| 106 |
if (ch == set[0]) |
| 107 |
return ok; |
| 108 |
set++; |
| 109 |
break; |
| 110 |
|
| 111 |
case SRE_OP_CATEGORY: |
| 112 |
/* <CATEGORY> <code> */ |
| 113 |
if (sre_category(set[0], (int) ch)) |
| 114 |
return ok; |
| 115 |
set++; |
| 116 |
break; |
| 117 |
|
| 118 |
case SRE_OP_CHARSET: |
| 119 |
/* <CHARSET> <bitmap> */ |
| 120 |
if (ch < 256 && |
| 121 |
(set[ch/SRE_CODE_BITS] & (1u << (ch & (SRE_CODE_BITS-1))))) |
| 122 |
return ok; |
| 123 |
set += 256/SRE_CODE_BITS; |
| 124 |
break; |
| 125 |
|
| 126 |
case SRE_OP_RANGE: |
| 127 |
/* <RANGE> <lower> <upper> */ |
| 128 |
if (set[0] <= ch && ch <= set[1]) |
| 129 |
return ok; |
| 130 |
set += 2; |
| 131 |
break; |
| 132 |
|
| 133 |
case SRE_OP_RANGE_UNI_IGNORE: |
| 134 |
/* <RANGE_UNI_IGNORE> <lower> <upper> */ |
| 135 |
{ |
| 136 |
SRE_CODE uch; |
| 137 |
/* ch is already lower cased */ |
| 138 |
if (set[0] <= ch && ch <= set[1]) |
| 139 |
return ok; |
| 140 |
uch = sre_upper_unicode(ch); |
| 141 |
if (set[0] <= uch && uch <= set[1]) |
| 142 |
return ok; |
| 143 |
set += 2; |
| 144 |
break; |
| 145 |
} |
| 146 |
|
| 147 |
case SRE_OP_NEGATE: |
| 148 |
ok = !ok; |
| 149 |
break; |
| 150 |
|
| 151 |
case SRE_OP_BIGCHARSET: |
| 152 |
/* <BIGCHARSET> <blockcount> <256 blockindices> <blocks> */ |
| 153 |
{ |
| 154 |
Py_ssize_t count, block; |
| 155 |
count = *(set++); |
| 156 |
|
| 157 |
if (ch < 0x10000u) |
| 158 |
block = ((unsigned char*)set)[ch >> 8]; |
| 159 |
else |
| 160 |
block = -1; |
| 161 |
set += 256/sizeof(SRE_CODE); |
| 162 |
if (block >=0 && |
| 163 |
(set[(block * 256 + (ch & 255))/SRE_CODE_BITS] & |
| 164 |
(1u << (ch & (SRE_CODE_BITS-1))))) |
| 165 |
return ok; |
| 166 |
set += count * (256/SRE_CODE_BITS); |
| 167 |
break; |
| 168 |
} |
| 169 |
|
| 170 |
default: |
| 171 |
/* internal error -- there's not much we can do about it |
| 172 |
here, so let's just pretend it didn't match... */ |
| 173 |
return 0; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
LOCAL(int) |
| 179 |
SRE(charset_loc_ignore)(SRE_STATE* state, const SRE_CODE* set, SRE_CODE ch) |
| 180 |
{ |
| 181 |
SRE_CODE lo, up; |
| 182 |
lo = sre_lower_locale(ch); |
| 183 |
if (SRE(charset)(state, set, lo)) |
| 184 |
return 1; |
| 185 |
|
| 186 |
up = sre_upper_locale(ch); |
| 187 |
return up != lo && SRE(charset)(state, set, up); |
| 188 |
} |
| 189 |
|
| 190 |
LOCAL(Py_ssize_t) SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel); |
| 191 |
|
| 192 |
LOCAL(Py_ssize_t) |
| 193 |
SRE(count)(SRE_STATE* state, const SRE_CODE* pattern, Py_ssize_t maxcount) |
| 194 |
{ |
| 195 |
SRE_CODE chr; |
| 196 |
SRE_CHAR c; |
| 197 |
const SRE_CHAR* ptr = (const SRE_CHAR *)state->ptr; |
| 198 |
const SRE_CHAR* end = (const SRE_CHAR *)state->end; |
| 199 |
Py_ssize_t i; |
| 200 |
INIT_TRACE(state); |
| 201 |
|
| 202 |
/* adjust end */ |
| 203 |
if (maxcount < end - ptr && maxcount != SRE_MAXREPEAT) |
| 204 |
end = ptr + maxcount; |
| 205 |
|
| 206 |
switch (pattern[0]) { |
| 207 |
|
| 208 |
case SRE_OP_IN: |
| 209 |
/* repeated set */ |
| 210 |
TRACE(("|%p|%p|COUNT IN\n", pattern, ptr)); |
| 211 |
while (ptr < end && SRE(charset)(state, pattern + 2, *ptr)) |
| 212 |
ptr++; |
| 213 |
break; |
| 214 |
|
| 215 |
case SRE_OP_ANY: |
| 216 |
/* repeated dot wildcard. */ |
| 217 |
TRACE(("|%p|%p|COUNT ANY\n", pattern, ptr)); |
| 218 |
while (ptr < end && !SRE_IS_LINEBREAK(*ptr)) |
| 219 |
ptr++; |
| 220 |
break; |
| 221 |
|
| 222 |
case SRE_OP_ANY_ALL: |
| 223 |
/* repeated dot wildcard. skip to the end of the target |
| 224 |
string, and backtrack from there */ |
| 225 |
TRACE(("|%p|%p|COUNT ANY_ALL\n", pattern, ptr)); |
| 226 |
ptr = end; |
| 227 |
break; |
| 228 |
|
| 229 |
case SRE_OP_LITERAL: |
| 230 |
/* repeated literal */ |
| 231 |
chr = pattern[1]; |
| 232 |
TRACE(("|%p|%p|COUNT LITERAL %d\n", pattern, ptr, chr)); |
| 233 |
c = (SRE_CHAR) chr; |
| 234 |
#if SIZEOF_SRE_CHAR < 4 |
| 235 |
if ((SRE_CODE) c != chr) |
| 236 |
; /* literal can't match: doesn't fit in char width */ |
| 237 |
else |
| 238 |
#endif |
| 239 |
while (ptr < end && *ptr == c) |
| 240 |
ptr++; |
| 241 |
break; |
| 242 |
|
| 243 |
case SRE_OP_LITERAL_IGNORE: |
| 244 |
/* repeated literal */ |
| 245 |
chr = pattern[1]; |
| 246 |
TRACE(("|%p|%p|COUNT LITERAL_IGNORE %d\n", pattern, ptr, chr)); |
| 247 |
while (ptr < end && (SRE_CODE) sre_lower_ascii(*ptr) == chr) |
| 248 |
ptr++; |
| 249 |
break; |
| 250 |
|
| 251 |
case SRE_OP_LITERAL_UNI_IGNORE: |
| 252 |
/* repeated literal */ |
| 253 |
chr = pattern[1]; |
| 254 |
TRACE(("|%p|%p|COUNT LITERAL_UNI_IGNORE %d\n", pattern, ptr, chr)); |
| 255 |
while (ptr < end && (SRE_CODE) sre_lower_unicode(*ptr) == chr) |
| 256 |
ptr++; |
| 257 |
break; |
| 258 |
|
| 259 |
case SRE_OP_LITERAL_LOC_IGNORE: |
| 260 |
/* repeated literal */ |
| 261 |
chr = pattern[1]; |
| 262 |
TRACE(("|%p|%p|COUNT LITERAL_LOC_IGNORE %d\n", pattern, ptr, chr)); |
| 263 |
while (ptr < end && char_loc_ignore(chr, *ptr)) |
| 264 |
ptr++; |
| 265 |
break; |
| 266 |
|
| 267 |
case SRE_OP_NOT_LITERAL: |
| 268 |
/* repeated non-literal */ |
| 269 |
chr = pattern[1]; |
| 270 |
TRACE(("|%p|%p|COUNT NOT_LITERAL %d\n", pattern, ptr, chr)); |
| 271 |
c = (SRE_CHAR) chr; |
| 272 |
#if SIZEOF_SRE_CHAR < 4 |
| 273 |
if ((SRE_CODE) c != chr) |
| 274 |
ptr = end; /* literal can't match: doesn't fit in char width */ |
| 275 |
else |
| 276 |
#endif |
| 277 |
while (ptr < end && *ptr != c) |
| 278 |
ptr++; |
| 279 |
break; |
| 280 |
|
| 281 |
case SRE_OP_NOT_LITERAL_IGNORE: |
| 282 |
/* repeated non-literal */ |
| 283 |
chr = pattern[1]; |
| 284 |
TRACE(("|%p|%p|COUNT NOT_LITERAL_IGNORE %d\n", pattern, ptr, chr)); |
| 285 |
while (ptr < end && (SRE_CODE) sre_lower_ascii(*ptr) != chr) |
| 286 |
ptr++; |
| 287 |
break; |
| 288 |
|
| 289 |
case SRE_OP_NOT_LITERAL_UNI_IGNORE: |
| 290 |
/* repeated non-literal */ |
| 291 |
chr = pattern[1]; |
| 292 |
TRACE(("|%p|%p|COUNT NOT_LITERAL_UNI_IGNORE %d\n", pattern, ptr, chr)); |
| 293 |
while (ptr < end && (SRE_CODE) sre_lower_unicode(*ptr) != chr) |
| 294 |
ptr++; |
| 295 |
break; |
| 296 |
|
| 297 |
case SRE_OP_NOT_LITERAL_LOC_IGNORE: |
| 298 |
/* repeated non-literal */ |
| 299 |
chr = pattern[1]; |
| 300 |
TRACE(("|%p|%p|COUNT NOT_LITERAL_LOC_IGNORE %d\n", pattern, ptr, chr)); |
| 301 |
while (ptr < end && !char_loc_ignore(chr, *ptr)) |
| 302 |
ptr++; |
| 303 |
break; |
| 304 |
|
| 305 |
default: |
| 306 |
/* repeated single character pattern */ |
| 307 |
TRACE(("|%p|%p|COUNT SUBPATTERN\n", pattern, ptr)); |
| 308 |
while ((SRE_CHAR*) state->ptr < end) { |
| 309 |
i = SRE(match)(state, pattern, 0); |
| 310 |
if (i < 0) |
| 311 |
return i; |
| 312 |
if (!i) |
| 313 |
break; |
| 314 |
} |
| 315 |
TRACE(("|%p|%p|COUNT %zd\n", pattern, ptr, |
| 316 |
(SRE_CHAR*) state->ptr - ptr)); |
| 317 |
return (SRE_CHAR*) state->ptr - ptr; |
| 318 |
} |
| 319 |
|
| 320 |
TRACE(("|%p|%p|COUNT %zd\n", pattern, ptr, |
| 321 |
ptr - (SRE_CHAR*) state->ptr)); |
| 322 |
return ptr - (SRE_CHAR*) state->ptr; |
| 323 |
} |
| 324 |
|
| 325 |
/* The macros below should be used to protect recursive SRE(match)() |
| 326 |
* calls that *failed* and do *not* return immediately (IOW, those |
| 327 |
* that will backtrack). Explaining: |
| 328 |
* |
| 329 |
* - Recursive SRE(match)() returned true: that's usually a success |
| 330 |
* (besides atypical cases like ASSERT_NOT), therefore there's no |
| 331 |
* reason to restore lastmark; |
| 332 |
* |
| 333 |
* - Recursive SRE(match)() returned false but the current SRE(match)() |
| 334 |
* is returning to the caller: If the current SRE(match)() is the |
| 335 |
* top function of the recursion, returning false will be a matching |
| 336 |
* failure, and it doesn't matter where lastmark is pointing to. |
| 337 |
* If it's *not* the top function, it will be a recursive SRE(match)() |
| 338 |
* failure by itself, and the calling SRE(match)() will have to deal |
| 339 |
* with the failure by the same rules explained here (it will restore |
| 340 |
* lastmark by itself if necessary); |
| 341 |
* |
| 342 |
* - Recursive SRE(match)() returned false, and will continue the |
| 343 |
* outside 'for' loop: must be protected when breaking, since the next |
| 344 |
* OP could potentially depend on lastmark; |
| 345 |
* |
| 346 |
* - Recursive SRE(match)() returned false, and will be called again |
| 347 |
* inside a local for/while loop: must be protected between each |
| 348 |
* loop iteration, since the recursive SRE(match)() could do anything, |
| 349 |
* and could potentially depend on lastmark. |
| 350 |
* |
| 351 |
* For more information, check the discussion at SF patch #712900. |
| 352 |
*/ |
| 353 |
#define LASTMARK_SAVE() \ |
| 354 |
do { \ |
| 355 |
ctx->lastmark = state->lastmark; \ |
| 356 |
ctx->lastindex = state->lastindex; \ |
| 357 |
} while (0) |
| 358 |
#define LASTMARK_RESTORE() \ |
| 359 |
do { \ |
| 360 |
state->lastmark = ctx->lastmark; \ |
| 361 |
state->lastindex = ctx->lastindex; \ |
| 362 |
} while (0) |
| 363 |
|
| 364 |
#define LAST_PTR_PUSH() \ |
| 365 |
do { \ |
| 366 |
TRACE(("push last_ptr: %zd", \ |
| 367 |
PTR_TO_INDEX(ctx->u.rep->last_ptr))); \ |
| 368 |
DATA_PUSH(&ctx->u.rep->last_ptr); \ |
| 369 |
} while (0) |
| 370 |
#define LAST_PTR_POP() \ |
| 371 |
do { \ |
| 372 |
DATA_POP(&ctx->u.rep->last_ptr); \ |
| 373 |
TRACE(("pop last_ptr: %zd", \ |
| 374 |
PTR_TO_INDEX(ctx->u.rep->last_ptr))); \ |
| 375 |
} while (0) |
| 376 |
|
| 377 |
#define RETURN_ERROR(i) do { return i; } while(0) |
| 378 |
#define RETURN_FAILURE do { ret = 0; goto exit; } while(0) |
| 379 |
#define RETURN_SUCCESS do { ret = 1; goto exit; } while(0) |
| 380 |
|
| 381 |
#define RETURN_ON_ERROR(i) \ |
| 382 |
do { if (i < 0) RETURN_ERROR(i); } while (0) |
| 383 |
#define RETURN_ON_SUCCESS(i) \ |
| 384 |
do { RETURN_ON_ERROR(i); if (i > 0) RETURN_SUCCESS; } while (0) |
| 385 |
#define RETURN_ON_FAILURE(i) \ |
| 386 |
do { RETURN_ON_ERROR(i); if (i == 0) RETURN_FAILURE; } while (0) |
| 387 |
|
| 388 |
#define DATA_STACK_ALLOC(state, type, ptr) \ |
| 389 |
do { \ |
| 390 |
alloc_pos = state->data_stack_base; \ |
| 391 |
TRACE(("allocating %s in %zd (%zd)\n", \ |
| 392 |
Py_STRINGIFY(type), alloc_pos, sizeof(type))); \ |
| 393 |
if (sizeof(type) > state->data_stack_size - alloc_pos) { \ |
| 394 |
int j = data_stack_grow(state, sizeof(type)); \ |
| 395 |
if (j < 0) return j; \ |
| 396 |
if (ctx_pos != -1) \ |
| 397 |
DATA_STACK_LOOKUP_AT(state, SRE(match_context), ctx, ctx_pos); \ |
| 398 |
} \ |
| 399 |
ptr = (type*)(state->data_stack+alloc_pos); \ |
| 400 |
state->data_stack_base += sizeof(type); \ |
| 401 |
} while (0) |
| 402 |
|
| 403 |
#define DATA_STACK_LOOKUP_AT(state, type, ptr, pos) \ |
| 404 |
do { \ |
| 405 |
TRACE(("looking up %s at %zd\n", Py_STRINGIFY(type), pos)); \ |
| 406 |
ptr = (type*)(state->data_stack+pos); \ |
| 407 |
} while (0) |
| 408 |
|
| 409 |
#define DATA_STACK_PUSH(state, data, size) \ |
| 410 |
do { \ |
| 411 |
TRACE(("copy data in %p to %zd (%zd)\n", \ |
| 412 |
data, state->data_stack_base, size)); \ |
| 413 |
if (size > state->data_stack_size - state->data_stack_base) { \ |
| 414 |
int j = data_stack_grow(state, size); \ |
| 415 |
if (j < 0) return j; \ |
| 416 |
if (ctx_pos != -1) \ |
| 417 |
DATA_STACK_LOOKUP_AT(state, SRE(match_context), ctx, ctx_pos); \ |
| 418 |
} \ |
| 419 |
memcpy(state->data_stack+state->data_stack_base, data, size); \ |
| 420 |
state->data_stack_base += size; \ |
| 421 |
} while (0) |
| 422 |
|
| 423 |
/* We add an explicit cast to memcpy here because MSVC has a bug when |
| 424 |
compiling C code where it believes that `const void**` cannot be |
| 425 |
safely casted to `void*`, see bpo-39943 for details. */ |
| 426 |
#define DATA_STACK_POP(state, data, size, discard) \ |
| 427 |
do { \ |
| 428 |
TRACE(("copy data to %p from %zd (%zd)\n", \ |
| 429 |
data, state->data_stack_base-size, size)); \ |
| 430 |
memcpy((void*) data, state->data_stack+state->data_stack_base-size, size); \ |
| 431 |
if (discard) \ |
| 432 |
state->data_stack_base -= size; \ |
| 433 |
} while (0) |
| 434 |
|
| 435 |
#define DATA_STACK_POP_DISCARD(state, size) \ |
| 436 |
do { \ |
| 437 |
TRACE(("discard data from %zd (%zd)\n", \ |
| 438 |
state->data_stack_base-size, size)); \ |
| 439 |
state->data_stack_base -= size; \ |
| 440 |
} while(0) |
| 441 |
|
| 442 |
#define DATA_PUSH(x) \ |
| 443 |
DATA_STACK_PUSH(state, (x), sizeof(*(x))) |
| 444 |
#define DATA_POP(x) \ |
| 445 |
DATA_STACK_POP(state, (x), sizeof(*(x)), 1) |
| 446 |
#define DATA_POP_DISCARD(x) \ |
| 447 |
DATA_STACK_POP_DISCARD(state, sizeof(*(x))) |
| 448 |
#define DATA_ALLOC(t,p) \ |
| 449 |
DATA_STACK_ALLOC(state, t, p) |
| 450 |
#define DATA_LOOKUP_AT(t,p,pos) \ |
| 451 |
DATA_STACK_LOOKUP_AT(state,t,p,pos) |
| 452 |
|
| 453 |
#define PTR_TO_INDEX(ptr) \ |
| 454 |
((ptr) ? ((char*)(ptr) - (char*)state->beginning) / state->charsize : -1) |
| 455 |
|
| 456 |
#if VERBOSE |
| 457 |
# define MARK_TRACE(label, lastmark) \ |
| 458 |
do if (DO_TRACE) { \ |
| 459 |
TRACE(("%s %d marks:", (label), (lastmark)+1)); \ |
| 460 |
for (int j = 0; j <= (lastmark); j++) { \ |
| 461 |
if (j && (j & 1) == 0) { \ |
| 462 |
TRACE((" ")); \ |
| 463 |
} \ |
| 464 |
TRACE((" %zd", PTR_TO_INDEX(state->mark[j]))); \ |
| 465 |
} \ |
| 466 |
TRACE(("\n")); \ |
| 467 |
} while (0) |
| 468 |
#else |
| 469 |
# define MARK_TRACE(label, lastmark) |
| 470 |
#endif |
| 471 |
#define MARK_PUSH(lastmark) \ |
| 472 |
do if (lastmark >= 0) { \ |
| 473 |
MARK_TRACE("push", (lastmark)); \ |
| 474 |
size_t _marks_size = (lastmark+1) * sizeof(void*); \ |
| 475 |
DATA_STACK_PUSH(state, state->mark, _marks_size); \ |
| 476 |
} while (0) |
| 477 |
#define MARK_POP(lastmark) \ |
| 478 |
do if (lastmark >= 0) { \ |
| 479 |
size_t _marks_size = (lastmark+1) * sizeof(void*); \ |
| 480 |
DATA_STACK_POP(state, state->mark, _marks_size, 1); \ |
| 481 |
MARK_TRACE("pop", (lastmark)); \ |
| 482 |
} while (0) |
| 483 |
#define MARK_POP_KEEP(lastmark) \ |
| 484 |
do if (lastmark >= 0) { \ |
| 485 |
size_t _marks_size = (lastmark+1) * sizeof(void*); \ |
| 486 |
DATA_STACK_POP(state, state->mark, _marks_size, 0); \ |
| 487 |
MARK_TRACE("pop keep", (lastmark)); \ |
| 488 |
} while (0) |
| 489 |
#define MARK_POP_DISCARD(lastmark) \ |
| 490 |
do if (lastmark >= 0) { \ |
| 491 |
size_t _marks_size = (lastmark+1) * sizeof(void*); \ |
| 492 |
DATA_STACK_POP_DISCARD(state, _marks_size); \ |
| 493 |
MARK_TRACE("pop discard", (lastmark)); \ |
| 494 |
} while (0) |
| 495 |
|
| 496 |
#define JUMP_NONE 0 |
| 497 |
#define JUMP_MAX_UNTIL_1 1 |
| 498 |
#define JUMP_MAX_UNTIL_2 2 |
| 499 |
#define JUMP_MAX_UNTIL_3 3 |
| 500 |
#define JUMP_MIN_UNTIL_1 4 |
| 501 |
#define JUMP_MIN_UNTIL_2 5 |
| 502 |
#define JUMP_MIN_UNTIL_3 6 |
| 503 |
#define JUMP_REPEAT 7 |
| 504 |
#define JUMP_REPEAT_ONE_1 8 |
| 505 |
#define JUMP_REPEAT_ONE_2 9 |
| 506 |
#define JUMP_MIN_REPEAT_ONE 10 |
| 507 |
#define JUMP_BRANCH 11 |
| 508 |
#define JUMP_ASSERT 12 |
| 509 |
#define JUMP_ASSERT_NOT 13 |
| 510 |
#define JUMP_POSS_REPEAT_1 14 |
| 511 |
#define JUMP_POSS_REPEAT_2 15 |
| 512 |
#define JUMP_ATOMIC_GROUP 16 |
| 513 |
|
| 514 |
#define DO_JUMPX(jumpvalue, jumplabel, nextpattern, toplevel_) \ |
| 515 |
ctx->pattern = pattern; \ |
| 516 |
ctx->ptr = ptr; \ |
| 517 |
DATA_ALLOC(SRE(match_context), nextctx); \ |
| 518 |
nextctx->pattern = nextpattern; \ |
| 519 |
nextctx->toplevel = toplevel_; \ |
| 520 |
nextctx->jump = jumpvalue; \ |
| 521 |
nextctx->last_ctx_pos = ctx_pos; \ |
| 522 |
pattern = nextpattern; \ |
| 523 |
ctx_pos = alloc_pos; \ |
| 524 |
ctx = nextctx; \ |
| 525 |
goto entrance; \ |
| 526 |
jumplabel: \ |
| 527 |
pattern = ctx->pattern; \ |
| 528 |
ptr = ctx->ptr; |
| 529 |
|
| 530 |
#define DO_JUMP(jumpvalue, jumplabel, nextpattern) \ |
| 531 |
DO_JUMPX(jumpvalue, jumplabel, nextpattern, ctx->toplevel) |
| 532 |
|
| 533 |
#define DO_JUMP0(jumpvalue, jumplabel, nextpattern) \ |
| 534 |
DO_JUMPX(jumpvalue, jumplabel, nextpattern, 0) |
| 535 |
|
| 536 |
typedef struct { |
| 537 |
Py_ssize_t count; |
| 538 |
union { |
| 539 |
SRE_CODE chr; |
| 540 |
SRE_REPEAT* rep; |
| 541 |
} u; |
| 542 |
int lastmark; |
| 543 |
int lastindex; |
| 544 |
const SRE_CODE* pattern; |
| 545 |
const SRE_CHAR* ptr; |
| 546 |
int toplevel; |
| 547 |
int jump; |
| 548 |
Py_ssize_t last_ctx_pos; |
| 549 |
} SRE(match_context); |
| 550 |
|
| 551 |
#define _MAYBE_CHECK_SIGNALS \ |
| 552 |
do { \ |
| 553 |
if ((0 == (++sigcount & 0xfff)) && PyErr_CheckSignals()) { \ |
| 554 |
RETURN_ERROR(SRE_ERROR_INTERRUPTED); \ |
| 555 |
} \ |
| 556 |
} while (0) |
| 557 |
|
| 558 |
#ifdef Py_DEBUG |
| 559 |
# define MAYBE_CHECK_SIGNALS \ |
| 560 |
do { \ |
| 561 |
_MAYBE_CHECK_SIGNALS; \ |
| 562 |
if (state->fail_after_count >= 0) { \ |
| 563 |
if (state->fail_after_count-- == 0) { \ |
| 564 |
PyErr_SetNone(state->fail_after_exc); \ |
| 565 |
RETURN_ERROR(SRE_ERROR_INTERRUPTED); \ |
| 566 |
} \ |
| 567 |
} \ |
| 568 |
} while (0) |
| 569 |
#else |
| 570 |
# define MAYBE_CHECK_SIGNALS _MAYBE_CHECK_SIGNALS |
| 571 |
#endif /* Py_DEBUG */ |
| 572 |
|
| 573 |
#ifdef HAVE_COMPUTED_GOTOS |
| 574 |
#ifndef USE_COMPUTED_GOTOS |
| 575 |
#define USE_COMPUTED_GOTOS 1 |
| 576 |
#endif |
| 577 |
#elif defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS |
| 578 |
#error "Computed gotos are not supported on this compiler." |
| 579 |
#else |
| 580 |
#undef USE_COMPUTED_GOTOS |
| 581 |
#define USE_COMPUTED_GOTOS 0 |
| 582 |
#endif |
| 583 |
|
| 584 |
#if USE_COMPUTED_GOTOS |
| 585 |
#define TARGET(OP) TARGET_ ## OP |
| 586 |
#define DISPATCH \ |
| 587 |
do { \ |
| 588 |
MAYBE_CHECK_SIGNALS; \ |
| 589 |
goto *sre_targets[*pattern++]; \ |
| 590 |
} while (0) |
| 591 |
#else |
| 592 |
#define TARGET(OP) case OP |
| 593 |
#define DISPATCH goto dispatch |
| 594 |
#endif |
| 595 |
|
| 596 |
/* check if string matches the given pattern. returns <0 for |
| 597 |
error, 0 for failure, and 1 for success */ |
| 598 |
LOCAL(Py_ssize_t) |
| 599 |
SRE(match)(SRE_STATE* state, const SRE_CODE* pattern, int toplevel) |
| 600 |
{ |
| 601 |
const SRE_CHAR* end = (const SRE_CHAR *)state->end; |
| 602 |
Py_ssize_t alloc_pos, ctx_pos = -1; |
| 603 |
Py_ssize_t ret = 0; |
| 604 |
int jump; |
| 605 |
unsigned int sigcount = state->sigcount; |
| 606 |
|
| 607 |
SRE(match_context)* ctx; |
| 608 |
SRE(match_context)* nextctx; |
| 609 |
INIT_TRACE(state); |
| 610 |
|
| 611 |
TRACE(("|%p|%p|ENTER\n", pattern, state->ptr)); |
| 612 |
|
| 613 |
DATA_ALLOC(SRE(match_context), ctx); |
| 614 |
ctx->last_ctx_pos = -1; |
| 615 |
ctx->jump = JUMP_NONE; |
| 616 |
ctx->toplevel = toplevel; |
| 617 |
ctx_pos = alloc_pos; |
| 618 |
|
| 619 |
#if USE_COMPUTED_GOTOS |
| 620 |
#include "sre_targets.h" |
| 621 |
#endif |
| 622 |
|
| 623 |
entrance: |
| 624 |
|
| 625 |
; // Fashion statement. |
| 626 |
const SRE_CHAR *ptr = (SRE_CHAR *)state->ptr; |
| 627 |
|
| 628 |
if (pattern[0] == SRE_OP_INFO) { |
| 629 |
/* optimization info block */ |
| 630 |
/* <INFO> <1=skip> <2=flags> <3=min> ... */ |
| 631 |
if (pattern[3] && (uintptr_t)(end - ptr) < pattern[3]) { |
| 632 |
TRACE(("reject (got %tu chars, need %zu)\n", |
| 633 |
end - ptr, (size_t) pattern[3])); |
| 634 |
RETURN_FAILURE; |
| 635 |
} |
| 636 |
pattern += pattern[1] + 1; |
| 637 |
} |
| 638 |
|
| 639 |
#if USE_COMPUTED_GOTOS |
| 640 |
DISPATCH; |
| 641 |
#else |
| 642 |
dispatch: |
| 643 |
MAYBE_CHECK_SIGNALS; |
| 644 |
switch (*pattern++) |
| 645 |
#endif |
| 646 |
{ |
| 647 |
|
| 648 |
TARGET(SRE_OP_MARK): |
| 649 |
/* set mark */ |
| 650 |
/* <MARK> <gid> */ |
| 651 |
TRACE(("|%p|%p|MARK %d\n", pattern, |
| 652 |
ptr, pattern[0])); |
| 653 |
{ |
| 654 |
int i = pattern[0]; |
| 655 |
if (i & 1) |
| 656 |
state->lastindex = i/2 + 1; |
| 657 |
if (i > state->lastmark) { |
| 658 |
/* state->lastmark is the highest valid index in the |
| 659 |
state->mark array. If it is increased by more than 1, |
| 660 |
the intervening marks must be set to NULL to signal |
| 661 |
that these marks have not been encountered. */ |
| 662 |
int j = state->lastmark + 1; |
| 663 |
while (j < i) |
| 664 |
state->mark[j++] = NULL; |
| 665 |
state->lastmark = i; |
| 666 |
} |
| 667 |
state->mark[i] = ptr; |
| 668 |
} |
| 669 |
pattern++; |
| 670 |
DISPATCH; |
| 671 |
|
| 672 |
TARGET(SRE_OP_LITERAL): |
| 673 |
/* match literal string */ |
| 674 |
/* <LITERAL> <code> */ |
| 675 |
TRACE(("|%p|%p|LITERAL %d\n", pattern, |
| 676 |
ptr, *pattern)); |
| 677 |
if (ptr >= end || (SRE_CODE) ptr[0] != pattern[0]) |
| 678 |
RETURN_FAILURE; |
| 679 |
pattern++; |
| 680 |
ptr++; |
| 681 |
DISPATCH; |
| 682 |
|
| 683 |
TARGET(SRE_OP_NOT_LITERAL): |
| 684 |
/* match anything that is not literal character */ |
| 685 |
/* <NOT_LITERAL> <code> */ |
| 686 |
TRACE(("|%p|%p|NOT_LITERAL %d\n", pattern, |
| 687 |
ptr, *pattern)); |
| 688 |
if (ptr >= end || (SRE_CODE) ptr[0] == pattern[0]) |
| 689 |
RETURN_FAILURE; |
| 690 |
pattern++; |
| 691 |
ptr++; |
| 692 |
DISPATCH; |
| 693 |
|
| 694 |
TARGET(SRE_OP_SUCCESS): |
| 695 |
/* end of pattern */ |
| 696 |
TRACE(("|%p|%p|SUCCESS\n", pattern, ptr)); |
| 697 |
if (ctx->toplevel && |
| 698 |
((state->match_all && ptr != state->end) || |
| 699 |
(state->must_advance && ptr == state->start))) |
| 700 |
{ |
| 701 |
RETURN_FAILURE; |
| 702 |
} |
| 703 |
state->ptr = ptr; |
| 704 |
RETURN_SUCCESS; |
| 705 |
|
| 706 |
TARGET(SRE_OP_AT): |
| 707 |
/* match at given position */ |
| 708 |
/* <AT> <code> */ |
| 709 |
TRACE(("|%p|%p|AT %d\n", pattern, ptr, *pattern)); |
| 710 |
if (!SRE(at)(state, ptr, *pattern)) |
| 711 |
RETURN_FAILURE; |
| 712 |
pattern++; |
| 713 |
DISPATCH; |
| 714 |
|
| 715 |
TARGET(SRE_OP_CATEGORY): |
| 716 |
/* match at given category */ |
| 717 |
/* <CATEGORY> <code> */ |
| 718 |
TRACE(("|%p|%p|CATEGORY %d\n", pattern, |
| 719 |
ptr, *pattern)); |
| 720 |
if (ptr >= end || !sre_category(pattern[0], ptr[0])) |
| 721 |
RETURN_FAILURE; |
| 722 |
pattern++; |
| 723 |
ptr++; |
| 724 |
DISPATCH; |
| 725 |
|
| 726 |
TARGET(SRE_OP_ANY): |
| 727 |
/* match anything (except a newline) */ |
| 728 |
/* <ANY> */ |
| 729 |
TRACE(("|%p|%p|ANY\n", pattern, ptr)); |
| 730 |
if (ptr >= end || SRE_IS_LINEBREAK(ptr[0])) |
| 731 |
RETURN_FAILURE; |
| 732 |
ptr++; |
| 733 |
DISPATCH; |
| 734 |
|
| 735 |
TARGET(SRE_OP_ANY_ALL): |
| 736 |
/* match anything */ |
| 737 |
/* <ANY_ALL> */ |
| 738 |
TRACE(("|%p|%p|ANY_ALL\n", pattern, ptr)); |
| 739 |
if (ptr >= end) |
| 740 |
RETURN_FAILURE; |
| 741 |
ptr++; |
| 742 |
DISPATCH; |
| 743 |
|
| 744 |
TARGET(SRE_OP_IN): |
| 745 |
/* match set member (or non_member) */ |
| 746 |
/* <IN> <skip> <set> */ |
| 747 |
TRACE(("|%p|%p|IN\n", pattern, ptr)); |
| 748 |
if (ptr >= end || |
| 749 |
!SRE(charset)(state, pattern + 1, *ptr)) |
| 750 |
RETURN_FAILURE; |
| 751 |
pattern += pattern[0]; |
| 752 |
ptr++; |
| 753 |
DISPATCH; |
| 754 |
|
| 755 |
TARGET(SRE_OP_LITERAL_IGNORE): |
| 756 |
TRACE(("|%p|%p|LITERAL_IGNORE %d\n", |
| 757 |
pattern, ptr, pattern[0])); |
| 758 |
if (ptr >= end || |
| 759 |
sre_lower_ascii(*ptr) != *pattern) |
| 760 |
RETURN_FAILURE; |
| 761 |
pattern++; |
| 762 |
ptr++; |
| 763 |
DISPATCH; |
| 764 |
|
| 765 |
TARGET(SRE_OP_LITERAL_UNI_IGNORE): |
| 766 |
TRACE(("|%p|%p|LITERAL_UNI_IGNORE %d\n", |
| 767 |
pattern, ptr, pattern[0])); |
| 768 |
if (ptr >= end || |
| 769 |
sre_lower_unicode(*ptr) != *pattern) |
| 770 |
RETURN_FAILURE; |
| 771 |
pattern++; |
| 772 |
ptr++; |
| 773 |
DISPATCH; |
| 774 |
|
| 775 |
TARGET(SRE_OP_LITERAL_LOC_IGNORE): |
| 776 |
TRACE(("|%p|%p|LITERAL_LOC_IGNORE %d\n", |
| 777 |
pattern, ptr, pattern[0])); |
| 778 |
if (ptr >= end |
| 779 |
|| !char_loc_ignore(*pattern, *ptr)) |
| 780 |
RETURN_FAILURE; |
| 781 |
pattern++; |
| 782 |
ptr++; |
| 783 |
DISPATCH; |
| 784 |
|
| 785 |
TARGET(SRE_OP_NOT_LITERAL_IGNORE): |
| 786 |
TRACE(("|%p|%p|NOT_LITERAL_IGNORE %d\n", |
| 787 |
pattern, ptr, *pattern)); |
| 788 |
if (ptr >= end || |
| 789 |
sre_lower_ascii(*ptr) == *pattern) |
| 790 |
RETURN_FAILURE; |
| 791 |
pattern++; |
| 792 |
ptr++; |
| 793 |
DISPATCH; |
| 794 |
|
| 795 |
TARGET(SRE_OP_NOT_LITERAL_UNI_IGNORE): |
| 796 |
TRACE(("|%p|%p|NOT_LITERAL_UNI_IGNORE %d\n", |
| 797 |
pattern, ptr, *pattern)); |
| 798 |
if (ptr >= end || |
| 799 |
sre_lower_unicode(*ptr) == *pattern) |
| 800 |
RETURN_FAILURE; |
| 801 |
pattern++; |
| 802 |
ptr++; |
| 803 |
DISPATCH; |
| 804 |
|
| 805 |
TARGET(SRE_OP_NOT_LITERAL_LOC_IGNORE): |
| 806 |
TRACE(("|%p|%p|NOT_LITERAL_LOC_IGNORE %d\n", |
| 807 |
pattern, ptr, *pattern)); |
| 808 |
if (ptr >= end |
| 809 |
|| char_loc_ignore(*pattern, *ptr)) |
| 810 |
RETURN_FAILURE; |
| 811 |
pattern++; |
| 812 |
ptr++; |
| 813 |
DISPATCH; |
| 814 |
|
| 815 |
TARGET(SRE_OP_IN_IGNORE): |
| 816 |
TRACE(("|%p|%p|IN_IGNORE\n", pattern, ptr)); |
| 817 |
if (ptr >= end |
| 818 |
|| !SRE(charset)(state, pattern+1, |
| 819 |
(SRE_CODE)sre_lower_ascii(*ptr))) |
| 820 |
RETURN_FAILURE; |
| 821 |
pattern += pattern[0]; |
| 822 |
ptr++; |
| 823 |
DISPATCH; |
| 824 |
|
| 825 |
TARGET(SRE_OP_IN_UNI_IGNORE): |
| 826 |
TRACE(("|%p|%p|IN_UNI_IGNORE\n", pattern, ptr)); |
| 827 |
if (ptr >= end |
| 828 |
|| !SRE(charset)(state, pattern+1, |
| 829 |
(SRE_CODE)sre_lower_unicode(*ptr))) |
| 830 |
RETURN_FAILURE; |
| 831 |
pattern += pattern[0]; |
| 832 |
ptr++; |
| 833 |
DISPATCH; |
| 834 |
|
| 835 |
TARGET(SRE_OP_IN_LOC_IGNORE): |
| 836 |
TRACE(("|%p|%p|IN_LOC_IGNORE\n", pattern, ptr)); |
| 837 |
if (ptr >= end |
| 838 |
|| !SRE(charset_loc_ignore)(state, pattern+1, *ptr)) |
| 839 |
RETURN_FAILURE; |
| 840 |
pattern += pattern[0]; |
| 841 |
ptr++; |
| 842 |
DISPATCH; |
| 843 |
|
| 844 |
TARGET(SRE_OP_JUMP): |
| 845 |
TARGET(SRE_OP_INFO): |
| 846 |
/* jump forward */ |
| 847 |
/* <JUMP> <offset> */ |
| 848 |
TRACE(("|%p|%p|JUMP %d\n", pattern, |
| 849 |
ptr, pattern[0])); |
| 850 |
pattern += pattern[0]; |
| 851 |
DISPATCH; |
| 852 |
|
| 853 |
TARGET(SRE_OP_BRANCH): |
| 854 |
/* alternation */ |
| 855 |
/* <BRANCH> <0=skip> code <JUMP> ... <NULL> */ |
| 856 |
TRACE(("|%p|%p|BRANCH\n", pattern, ptr)); |
| 857 |
LASTMARK_SAVE(); |
| 858 |
if (state->repeat) |
| 859 |
MARK_PUSH(ctx->lastmark); |
| 860 |
for (; pattern[0]; pattern += pattern[0]) { |
| 861 |
if (pattern[1] == SRE_OP_LITERAL && |
| 862 |
(ptr >= end || |
| 863 |
(SRE_CODE) *ptr != pattern[2])) |
| 864 |
continue; |
| 865 |
if (pattern[1] == SRE_OP_IN && |
| 866 |
(ptr >= end || |
| 867 |
!SRE(charset)(state, pattern + 3, |
| 868 |
(SRE_CODE) *ptr))) |
| 869 |
continue; |
| 870 |
state->ptr = ptr; |
| 871 |
DO_JUMP(JUMP_BRANCH, jump_branch, pattern+1); |
| 872 |
if (ret) { |
| 873 |
if (state->repeat) |
| 874 |
MARK_POP_DISCARD(ctx->lastmark); |
| 875 |
RETURN_ON_ERROR(ret); |
| 876 |
RETURN_SUCCESS; |
| 877 |
} |
| 878 |
if (state->repeat) |
| 879 |
MARK_POP_KEEP(ctx->lastmark); |
| 880 |
LASTMARK_RESTORE(); |
| 881 |
} |
| 882 |
if (state->repeat) |
| 883 |
MARK_POP_DISCARD(ctx->lastmark); |
| 884 |
RETURN_FAILURE; |
| 885 |
|
| 886 |
TARGET(SRE_OP_REPEAT_ONE): |
| 887 |
/* match repeated sequence (maximizing regexp) */ |
| 888 |
|
| 889 |
/* this operator only works if the repeated item is |
| 890 |
exactly one character wide, and we're not already |
| 891 |
collecting backtracking points. for other cases, |
| 892 |
use the MAX_REPEAT operator */ |
| 893 |
|
| 894 |
/* <REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */ |
| 895 |
|
| 896 |
TRACE(("|%p|%p|REPEAT_ONE %d %d\n", pattern, ptr, |
| 897 |
pattern[1], pattern[2])); |
| 898 |
|
| 899 |
if ((Py_ssize_t) pattern[1] > end - ptr) |
| 900 |
RETURN_FAILURE; /* cannot match */ |
| 901 |
|
| 902 |
state->ptr = ptr; |
| 903 |
|
| 904 |
ret = SRE(count)(state, pattern+3, pattern[2]); |
| 905 |
RETURN_ON_ERROR(ret); |
| 906 |
DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos); |
| 907 |
ctx->count = ret; |
| 908 |
ptr += ctx->count; |
| 909 |
|
| 910 |
/* when we arrive here, count contains the number of |
| 911 |
matches, and ptr points to the tail of the target |
| 912 |
string. check if the rest of the pattern matches, |
| 913 |
and backtrack if not. */ |
| 914 |
|
| 915 |
if (ctx->count < (Py_ssize_t) pattern[1]) |
| 916 |
RETURN_FAILURE; |
| 917 |
|
| 918 |
if (pattern[pattern[0]] == SRE_OP_SUCCESS && |
| 919 |
ptr == state->end && |
| 920 |
!(ctx->toplevel && state->must_advance && ptr == state->start)) |
| 921 |
{ |
| 922 |
/* tail is empty. we're finished */ |
| 923 |
state->ptr = ptr; |
| 924 |
RETURN_SUCCESS; |
| 925 |
} |
| 926 |
|
| 927 |
LASTMARK_SAVE(); |
| 928 |
if (state->repeat) |
| 929 |
MARK_PUSH(ctx->lastmark); |
| 930 |
|
| 931 |
if (pattern[pattern[0]] == SRE_OP_LITERAL) { |
| 932 |
/* tail starts with a literal. skip positions where |
| 933 |
the rest of the pattern cannot possibly match */ |
| 934 |
ctx->u.chr = pattern[pattern[0]+1]; |
| 935 |
for (;;) { |
| 936 |
while (ctx->count >= (Py_ssize_t) pattern[1] && |
| 937 |
(ptr >= end || *ptr != ctx->u.chr)) { |
| 938 |
ptr--; |
| 939 |
ctx->count--; |
| 940 |
} |
| 941 |
if (ctx->count < (Py_ssize_t) pattern[1]) |
| 942 |
break; |
| 943 |
state->ptr = ptr; |
| 944 |
DO_JUMP(JUMP_REPEAT_ONE_1, jump_repeat_one_1, |
| 945 |
pattern+pattern[0]); |
| 946 |
if (ret) { |
| 947 |
if (state->repeat) |
| 948 |
MARK_POP_DISCARD(ctx->lastmark); |
| 949 |
RETURN_ON_ERROR(ret); |
| 950 |
RETURN_SUCCESS; |
| 951 |
} |
| 952 |
if (state->repeat) |
| 953 |
MARK_POP_KEEP(ctx->lastmark); |
| 954 |
LASTMARK_RESTORE(); |
| 955 |
|
| 956 |
ptr--; |
| 957 |
ctx->count--; |
| 958 |
} |
| 959 |
if (state->repeat) |
| 960 |
MARK_POP_DISCARD(ctx->lastmark); |
| 961 |
} else { |
| 962 |
/* general case */ |
| 963 |
while (ctx->count >= (Py_ssize_t) pattern[1]) { |
| 964 |
state->ptr = ptr; |
| 965 |
DO_JUMP(JUMP_REPEAT_ONE_2, jump_repeat_one_2, |
| 966 |
pattern+pattern[0]); |
| 967 |
if (ret) { |
| 968 |
if (state->repeat) |
| 969 |
MARK_POP_DISCARD(ctx->lastmark); |
| 970 |
RETURN_ON_ERROR(ret); |
| 971 |
RETURN_SUCCESS; |
| 972 |
} |
| 973 |
if (state->repeat) |
| 974 |
MARK_POP_KEEP(ctx->lastmark); |
| 975 |
LASTMARK_RESTORE(); |
| 976 |
|
| 977 |
ptr--; |
| 978 |
ctx->count--; |
| 979 |
} |
| 980 |
if (state->repeat) |
| 981 |
MARK_POP_DISCARD(ctx->lastmark); |
| 982 |
} |
| 983 |
RETURN_FAILURE; |
| 984 |
|
| 985 |
TARGET(SRE_OP_MIN_REPEAT_ONE): |
| 986 |
/* match repeated sequence (minimizing regexp) */ |
| 987 |
|
| 988 |
/* this operator only works if the repeated item is |
| 989 |
exactly one character wide, and we're not already |
| 990 |
collecting backtracking points. for other cases, |
| 991 |
use the MIN_REPEAT operator */ |
| 992 |
|
| 993 |
/* <MIN_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail */ |
| 994 |
|
| 995 |
TRACE(("|%p|%p|MIN_REPEAT_ONE %d %d\n", pattern, ptr, |
| 996 |
pattern[1], pattern[2])); |
| 997 |
|
| 998 |
if ((Py_ssize_t) pattern[1] > end - ptr) |
| 999 |
RETURN_FAILURE; /* cannot match */ |
| 1000 |
|
| 1001 |
state->ptr = ptr; |
| 1002 |
|
| 1003 |
if (pattern[1] == 0) |
| 1004 |
ctx->count = 0; |
| 1005 |
else { |
| 1006 |
/* count using pattern min as the maximum */ |
| 1007 |
ret = SRE(count)(state, pattern+3, pattern[1]); |
| 1008 |
RETURN_ON_ERROR(ret); |
| 1009 |
DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos); |
| 1010 |
if (ret < (Py_ssize_t) pattern[1]) |
| 1011 |
/* didn't match minimum number of times */ |
| 1012 |
RETURN_FAILURE; |
| 1013 |
/* advance past minimum matches of repeat */ |
| 1014 |
ctx->count = ret; |
| 1015 |
ptr += ctx->count; |
| 1016 |
} |
| 1017 |
|
| 1018 |
if (pattern[pattern[0]] == SRE_OP_SUCCESS && |
| 1019 |
!(ctx->toplevel && |
| 1020 |
((state->match_all && ptr != state->end) || |
| 1021 |
(state->must_advance && ptr == state->start)))) |
| 1022 |
{ |
| 1023 |
/* tail is empty. we're finished */ |
| 1024 |
state->ptr = ptr; |
| 1025 |
RETURN_SUCCESS; |
| 1026 |
|
| 1027 |
} else { |
| 1028 |
/* general case */ |
| 1029 |
LASTMARK_SAVE(); |
| 1030 |
if (state->repeat) |
| 1031 |
MARK_PUSH(ctx->lastmark); |
| 1032 |
|
| 1033 |
while ((Py_ssize_t)pattern[2] == SRE_MAXREPEAT |
| 1034 |
|| ctx->count <= (Py_ssize_t)pattern[2]) { |
| 1035 |
state->ptr = ptr; |
| 1036 |
DO_JUMP(JUMP_MIN_REPEAT_ONE,jump_min_repeat_one, |
| 1037 |
pattern+pattern[0]); |
| 1038 |
if (ret) { |
| 1039 |
if (state->repeat) |
| 1040 |
MARK_POP_DISCARD(ctx->lastmark); |
| 1041 |
RETURN_ON_ERROR(ret); |
| 1042 |
RETURN_SUCCESS; |
| 1043 |
} |
| 1044 |
if (state->repeat) |
| 1045 |
MARK_POP_KEEP(ctx->lastmark); |
| 1046 |
LASTMARK_RESTORE(); |
| 1047 |
|
| 1048 |
state->ptr = ptr; |
| 1049 |
ret = SRE(count)(state, pattern+3, 1); |
| 1050 |
RETURN_ON_ERROR(ret); |
| 1051 |
DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos); |
| 1052 |
if (ret == 0) |
| 1053 |
break; |
| 1054 |
assert(ret == 1); |
| 1055 |
ptr++; |
| 1056 |
ctx->count++; |
| 1057 |
} |
| 1058 |
if (state->repeat) |
| 1059 |
MARK_POP_DISCARD(ctx->lastmark); |
| 1060 |
} |
| 1061 |
RETURN_FAILURE; |
| 1062 |
|
| 1063 |
TARGET(SRE_OP_POSSESSIVE_REPEAT_ONE): |
| 1064 |
/* match repeated sequence (maximizing regexp) without |
| 1065 |
backtracking */ |
| 1066 |
|
| 1067 |
/* this operator only works if the repeated item is |
| 1068 |
exactly one character wide, and we're not already |
| 1069 |
collecting backtracking points. for other cases, |
| 1070 |
use the MAX_REPEAT operator */ |
| 1071 |
|
| 1072 |
/* <POSSESSIVE_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> |
| 1073 |
tail */ |
| 1074 |
|
| 1075 |
TRACE(("|%p|%p|POSSESSIVE_REPEAT_ONE %d %d\n", pattern, |
| 1076 |
ptr, pattern[1], pattern[2])); |
| 1077 |
|
| 1078 |
if (ptr + pattern[1] > end) { |
| 1079 |
RETURN_FAILURE; /* cannot match */ |
| 1080 |
} |
| 1081 |
|
| 1082 |
state->ptr = ptr; |
| 1083 |
|
| 1084 |
ret = SRE(count)(state, pattern + 3, pattern[2]); |
| 1085 |
RETURN_ON_ERROR(ret); |
| 1086 |
DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos); |
| 1087 |
ctx->count = ret; |
| 1088 |
ptr += ctx->count; |
| 1089 |
|
| 1090 |
/* when we arrive here, count contains the number of |
| 1091 |
matches, and ptr points to the tail of the target |
| 1092 |
string. check if the rest of the pattern matches, |
| 1093 |
and fail if not. */ |
| 1094 |
|
| 1095 |
/* Test for not enough repetitions in match */ |
| 1096 |
if (ctx->count < (Py_ssize_t) pattern[1]) { |
| 1097 |
RETURN_FAILURE; |
| 1098 |
} |
| 1099 |
|
| 1100 |
/* Update the pattern to point to the next op code */ |
| 1101 |
pattern += pattern[0]; |
| 1102 |
|
| 1103 |
/* Let the tail be evaluated separately and consider this |
| 1104 |
match successful. */ |
| 1105 |
if (*pattern == SRE_OP_SUCCESS && |
| 1106 |
ptr == state->end && |
| 1107 |
!(ctx->toplevel && state->must_advance && ptr == state->start)) |
| 1108 |
{ |
| 1109 |
/* tail is empty. we're finished */ |
| 1110 |
state->ptr = ptr; |
| 1111 |
RETURN_SUCCESS; |
| 1112 |
} |
| 1113 |
|
| 1114 |
/* Attempt to match the rest of the string */ |
| 1115 |
DISPATCH; |
| 1116 |
|
| 1117 |
TARGET(SRE_OP_REPEAT): |
| 1118 |
/* create repeat context. all the hard work is done |
| 1119 |
by the UNTIL operator (MAX_UNTIL, MIN_UNTIL) */ |
| 1120 |
/* <REPEAT> <skip> <1=min> <2=max> |
| 1121 |
<3=repeat_index> item <UNTIL> tail */ |
| 1122 |
TRACE(("|%p|%p|REPEAT %d %d\n", pattern, ptr, |
| 1123 |
pattern[1], pattern[2])); |
| 1124 |
|
| 1125 |
/* install new repeat context */ |
| 1126 |
ctx->u.rep = repeat_pool_malloc(state); |
| 1127 |
if (!ctx->u.rep) { |
| 1128 |
RETURN_ERROR(SRE_ERROR_MEMORY); |
| 1129 |
} |
| 1130 |
ctx->u.rep->count = -1; |
| 1131 |
ctx->u.rep->pattern = pattern; |
| 1132 |
ctx->u.rep->prev = state->repeat; |
| 1133 |
ctx->u.rep->last_ptr = NULL; |
| 1134 |
state->repeat = ctx->u.rep; |
| 1135 |
|
| 1136 |
state->ptr = ptr; |
| 1137 |
DO_JUMP(JUMP_REPEAT, jump_repeat, pattern+pattern[0]); |
| 1138 |
state->repeat = ctx->u.rep->prev; |
| 1139 |
repeat_pool_free(state, ctx->u.rep); |
| 1140 |
|
| 1141 |
if (ret) { |
| 1142 |
RETURN_ON_ERROR(ret); |
| 1143 |
RETURN_SUCCESS; |
| 1144 |
} |
| 1145 |
RETURN_FAILURE; |
| 1146 |
|
| 1147 |
TARGET(SRE_OP_MAX_UNTIL): |
| 1148 |
/* maximizing repeat */ |
| 1149 |
/* <REPEAT> <skip> <1=min> <2=max> item <MAX_UNTIL> tail */ |
| 1150 |
|
| 1151 |
/* FIXME: we probably need to deal with zero-width |
| 1152 |
matches in here... */ |
| 1153 |
|
| 1154 |
ctx->u.rep = state->repeat; |
| 1155 |
if (!ctx->u.rep) |
| 1156 |
RETURN_ERROR(SRE_ERROR_STATE); |
| 1157 |
|
| 1158 |
state->ptr = ptr; |
| 1159 |
|
| 1160 |
ctx->count = ctx->u.rep->count+1; |
| 1161 |
|
| 1162 |
TRACE(("|%p|%p|MAX_UNTIL %zd\n", pattern, |
| 1163 |
ptr, ctx->count)); |
| 1164 |
|
| 1165 |
if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) { |
| 1166 |
/* not enough matches */ |
| 1167 |
ctx->u.rep->count = ctx->count; |
| 1168 |
DO_JUMP(JUMP_MAX_UNTIL_1, jump_max_until_1, |
| 1169 |
ctx->u.rep->pattern+3); |
| 1170 |
if (ret) { |
| 1171 |
RETURN_ON_ERROR(ret); |
| 1172 |
RETURN_SUCCESS; |
| 1173 |
} |
| 1174 |
ctx->u.rep->count = ctx->count-1; |
| 1175 |
state->ptr = ptr; |
| 1176 |
RETURN_FAILURE; |
| 1177 |
} |
| 1178 |
|
| 1179 |
if ((ctx->count < (Py_ssize_t) ctx->u.rep->pattern[2] || |
| 1180 |
ctx->u.rep->pattern[2] == SRE_MAXREPEAT) && |
| 1181 |
state->ptr != ctx->u.rep->last_ptr) { |
| 1182 |
/* we may have enough matches, but if we can |
| 1183 |
match another item, do so */ |
| 1184 |
ctx->u.rep->count = ctx->count; |
| 1185 |
LASTMARK_SAVE(); |
| 1186 |
MARK_PUSH(ctx->lastmark); |
| 1187 |
/* zero-width match protection */ |
| 1188 |
LAST_PTR_PUSH(); |
| 1189 |
ctx->u.rep->last_ptr = state->ptr; |
| 1190 |
DO_JUMP(JUMP_MAX_UNTIL_2, jump_max_until_2, |
| 1191 |
ctx->u.rep->pattern+3); |
| 1192 |
LAST_PTR_POP(); |
| 1193 |
if (ret) { |
| 1194 |
MARK_POP_DISCARD(ctx->lastmark); |
| 1195 |
RETURN_ON_ERROR(ret); |
| 1196 |
RETURN_SUCCESS; |
| 1197 |
} |
| 1198 |
MARK_POP(ctx->lastmark); |
| 1199 |
LASTMARK_RESTORE(); |
| 1200 |
ctx->u.rep->count = ctx->count-1; |
| 1201 |
state->ptr = ptr; |
| 1202 |
} |
| 1203 |
|
| 1204 |
/* cannot match more repeated items here. make sure the |
| 1205 |
tail matches */ |
| 1206 |
state->repeat = ctx->u.rep->prev; |
| 1207 |
DO_JUMP(JUMP_MAX_UNTIL_3, jump_max_until_3, pattern); |
| 1208 |
state->repeat = ctx->u.rep; // restore repeat before return |
| 1209 |
|
| 1210 |
RETURN_ON_SUCCESS(ret); |
| 1211 |
state->ptr = ptr; |
| 1212 |
RETURN_FAILURE; |
| 1213 |
|
| 1214 |
TARGET(SRE_OP_MIN_UNTIL): |
| 1215 |
/* minimizing repeat */ |
| 1216 |
/* <REPEAT> <skip> <1=min> <2=max> item <MIN_UNTIL> tail */ |
| 1217 |
|
| 1218 |
ctx->u.rep = state->repeat; |
| 1219 |
if (!ctx->u.rep) |
| 1220 |
RETURN_ERROR(SRE_ERROR_STATE); |
| 1221 |
|
| 1222 |
state->ptr = ptr; |
| 1223 |
|
| 1224 |
ctx->count = ctx->u.rep->count+1; |
| 1225 |
|
| 1226 |
TRACE(("|%p|%p|MIN_UNTIL %zd %p\n", pattern, |
| 1227 |
ptr, ctx->count, ctx->u.rep->pattern)); |
| 1228 |
|
| 1229 |
if (ctx->count < (Py_ssize_t) ctx->u.rep->pattern[1]) { |
| 1230 |
/* not enough matches */ |
| 1231 |
ctx->u.rep->count = ctx->count; |
| 1232 |
DO_JUMP(JUMP_MIN_UNTIL_1, jump_min_until_1, |
| 1233 |
ctx->u.rep->pattern+3); |
| 1234 |
if (ret) { |
| 1235 |
RETURN_ON_ERROR(ret); |
| 1236 |
RETURN_SUCCESS; |
| 1237 |
} |
| 1238 |
ctx->u.rep->count = ctx->count-1; |
| 1239 |
state->ptr = ptr; |
| 1240 |
RETURN_FAILURE; |
| 1241 |
} |
| 1242 |
|
| 1243 |
/* see if the tail matches */ |
| 1244 |
state->repeat = ctx->u.rep->prev; |
| 1245 |
|
| 1246 |
LASTMARK_SAVE(); |
| 1247 |
if (state->repeat) |
| 1248 |
MARK_PUSH(ctx->lastmark); |
| 1249 |
|
| 1250 |
DO_JUMP(JUMP_MIN_UNTIL_2, jump_min_until_2, pattern); |
| 1251 |
SRE_REPEAT *repeat_of_tail = state->repeat; |
| 1252 |
state->repeat = ctx->u.rep; // restore repeat before return |
| 1253 |
|
| 1254 |
if (ret) { |
| 1255 |
if (repeat_of_tail) |
| 1256 |
MARK_POP_DISCARD(ctx->lastmark); |
| 1257 |
RETURN_ON_ERROR(ret); |
| 1258 |
RETURN_SUCCESS; |
| 1259 |
} |
| 1260 |
if (repeat_of_tail) |
| 1261 |
MARK_POP(ctx->lastmark); |
| 1262 |
LASTMARK_RESTORE(); |
| 1263 |
|
| 1264 |
state->ptr = ptr; |
| 1265 |
|
| 1266 |
if ((ctx->count >= (Py_ssize_t) ctx->u.rep->pattern[2] |
| 1267 |
&& ctx->u.rep->pattern[2] != SRE_MAXREPEAT) || |
| 1268 |
state->ptr == ctx->u.rep->last_ptr) |
| 1269 |
RETURN_FAILURE; |
| 1270 |
|
| 1271 |
ctx->u.rep->count = ctx->count; |
| 1272 |
/* zero-width match protection */ |
| 1273 |
LAST_PTR_PUSH(); |
| 1274 |
ctx->u.rep->last_ptr = state->ptr; |
| 1275 |
DO_JUMP(JUMP_MIN_UNTIL_3,jump_min_until_3, |
| 1276 |
ctx->u.rep->pattern+3); |
| 1277 |
LAST_PTR_POP(); |
| 1278 |
if (ret) { |
| 1279 |
RETURN_ON_ERROR(ret); |
| 1280 |
RETURN_SUCCESS; |
| 1281 |
} |
| 1282 |
ctx->u.rep->count = ctx->count-1; |
| 1283 |
state->ptr = ptr; |
| 1284 |
RETURN_FAILURE; |
| 1285 |
|
| 1286 |
TARGET(SRE_OP_POSSESSIVE_REPEAT): |
| 1287 |
/* create possessive repeat contexts. */ |
| 1288 |
/* <POSSESSIVE_REPEAT> <skip> <1=min> <2=max> pattern |
| 1289 |
<SUCCESS> tail */ |
| 1290 |
TRACE(("|%p|%p|POSSESSIVE_REPEAT %d %d\n", pattern, |
| 1291 |
ptr, pattern[1], pattern[2])); |
| 1292 |
|
| 1293 |
/* Set the global Input pointer to this context's Input |
| 1294 |
pointer */ |
| 1295 |
state->ptr = ptr; |
| 1296 |
|
| 1297 |
/* Set state->repeat to non-NULL */ |
| 1298 |
ctx->u.rep = repeat_pool_malloc(state); |
| 1299 |
if (!ctx->u.rep) { |
| 1300 |
RETURN_ERROR(SRE_ERROR_MEMORY); |
| 1301 |
} |
| 1302 |
ctx->u.rep->count = -1; |
| 1303 |
ctx->u.rep->pattern = NULL; |
| 1304 |
ctx->u.rep->prev = state->repeat; |
| 1305 |
ctx->u.rep->last_ptr = NULL; |
| 1306 |
state->repeat = ctx->u.rep; |
| 1307 |
|
| 1308 |
/* Initialize Count to 0 */ |
| 1309 |
ctx->count = 0; |
| 1310 |
|
| 1311 |
/* Check for minimum required matches. */ |
| 1312 |
while (ctx->count < (Py_ssize_t)pattern[1]) { |
| 1313 |
/* not enough matches */ |
| 1314 |
DO_JUMP0(JUMP_POSS_REPEAT_1, jump_poss_repeat_1, |
| 1315 |
&pattern[3]); |
| 1316 |
if (ret) { |
| 1317 |
RETURN_ON_ERROR(ret); |
| 1318 |
ctx->count++; |
| 1319 |
} |
| 1320 |
else { |
| 1321 |
state->ptr = ptr; |
| 1322 |
/* Restore state->repeat */ |
| 1323 |
state->repeat = ctx->u.rep->prev; |
| 1324 |
repeat_pool_free(state, ctx->u.rep); |
| 1325 |
RETURN_FAILURE; |
| 1326 |
} |
| 1327 |
} |
| 1328 |
|
| 1329 |
/* Clear the context's Input stream pointer so that it |
| 1330 |
doesn't match the global state so that the while loop can |
| 1331 |
be entered. */ |
| 1332 |
ptr = NULL; |
| 1333 |
|
| 1334 |
/* Keep trying to parse the <pattern> sub-pattern until the |
| 1335 |
end is reached, creating a new context each time. */ |
| 1336 |
while ((ctx->count < (Py_ssize_t)pattern[2] || |
| 1337 |
(Py_ssize_t)pattern[2] == SRE_MAXREPEAT) && |
| 1338 |
state->ptr != ptr) { |
| 1339 |
/* Save the Capture Group Marker state into the current |
| 1340 |
Context and back up the current highest number |
| 1341 |
Capture Group marker. */ |
| 1342 |
LASTMARK_SAVE(); |
| 1343 |
MARK_PUSH(ctx->lastmark); |
| 1344 |
|
| 1345 |
/* zero-width match protection */ |
| 1346 |
/* Set the context's Input Stream pointer to be the |
| 1347 |
current Input Stream pointer from the global |
| 1348 |
state. When the loop reaches the next iteration, |
| 1349 |
the context will then store the last known good |
| 1350 |
position with the global state holding the Input |
| 1351 |
Input Stream position that has been updated with |
| 1352 |
the most recent match. Thus, if state's Input |
| 1353 |
stream remains the same as the one stored in the |
| 1354 |
current Context, we know we have successfully |
| 1355 |
matched an empty string and that all subsequent |
| 1356 |
matches will also be the empty string until the |
| 1357 |
maximum number of matches are counted, and because |
| 1358 |
of this, we could immediately stop at that point and |
| 1359 |
consider this match successful. */ |
| 1360 |
ptr = state->ptr; |
| 1361 |
|
| 1362 |
/* We have not reached the maximin matches, so try to |
| 1363 |
match once more. */ |
| 1364 |
DO_JUMP0(JUMP_POSS_REPEAT_2, jump_poss_repeat_2, |
| 1365 |
&pattern[3]); |
| 1366 |
|
| 1367 |
/* Check to see if the last attempted match |
| 1368 |
succeeded. */ |
| 1369 |
if (ret) { |
| 1370 |
/* Drop the saved highest number Capture Group |
| 1371 |
marker saved above and use the newly updated |
| 1372 |
value. */ |
| 1373 |
MARK_POP_DISCARD(ctx->lastmark); |
| 1374 |
RETURN_ON_ERROR(ret); |
| 1375 |
|
| 1376 |
/* Success, increment the count. */ |
| 1377 |
ctx->count++; |
| 1378 |
} |
| 1379 |
/* Last attempted match failed. */ |
| 1380 |
else { |
| 1381 |
/* Restore the previously saved highest number |
| 1382 |
Capture Group marker since the last iteration |
| 1383 |
did not match, then restore that to the global |
| 1384 |
state. */ |
| 1385 |
MARK_POP(ctx->lastmark); |
| 1386 |
LASTMARK_RESTORE(); |
| 1387 |
|
| 1388 |
/* Restore the global Input Stream pointer |
| 1389 |
since it can change after jumps. */ |
| 1390 |
state->ptr = ptr; |
| 1391 |
|
| 1392 |
/* We have sufficient matches, so exit loop. */ |
| 1393 |
break; |
| 1394 |
} |
| 1395 |
} |
| 1396 |
|
| 1397 |
/* Restore state->repeat */ |
| 1398 |
state->repeat = ctx->u.rep->prev; |
| 1399 |
repeat_pool_free(state, ctx->u.rep); |
| 1400 |
|
| 1401 |
/* Evaluate Tail */ |
| 1402 |
/* Jump to end of pattern indicated by skip, and then skip |
| 1403 |
the SUCCESS op code that follows it. */ |
| 1404 |
pattern += pattern[0] + 1; |
| 1405 |
ptr = state->ptr; |
| 1406 |
DISPATCH; |
| 1407 |
|
| 1408 |
TARGET(SRE_OP_ATOMIC_GROUP): |
| 1409 |
/* Atomic Group Sub Pattern */ |
| 1410 |
/* <ATOMIC_GROUP> <skip> pattern <SUCCESS> tail */ |
| 1411 |
TRACE(("|%p|%p|ATOMIC_GROUP\n", pattern, ptr)); |
| 1412 |
|
| 1413 |
/* Set the global Input pointer to this context's Input |
| 1414 |
pointer */ |
| 1415 |
state->ptr = ptr; |
| 1416 |
|
| 1417 |
/* Evaluate the Atomic Group in a new context, terminating |
| 1418 |
when the end of the group, represented by a SUCCESS op |
| 1419 |
code, is reached. */ |
| 1420 |
/* Group Pattern begins at an offset of 1 code. */ |
| 1421 |
DO_JUMP0(JUMP_ATOMIC_GROUP, jump_atomic_group, |
| 1422 |
&pattern[1]); |
| 1423 |
|
| 1424 |
/* Test Exit Condition */ |
| 1425 |
RETURN_ON_ERROR(ret); |
| 1426 |
|
| 1427 |
if (ret == 0) { |
| 1428 |
/* Atomic Group failed to Match. */ |
| 1429 |
state->ptr = ptr; |
| 1430 |
RETURN_FAILURE; |
| 1431 |
} |
| 1432 |
|
| 1433 |
/* Evaluate Tail */ |
| 1434 |
/* Jump to end of pattern indicated by skip, and then skip |
| 1435 |
the SUCCESS op code that follows it. */ |
| 1436 |
pattern += pattern[0]; |
| 1437 |
ptr = state->ptr; |
| 1438 |
DISPATCH; |
| 1439 |
|
| 1440 |
TARGET(SRE_OP_GROUPREF): |
| 1441 |
/* match backreference */ |
| 1442 |
TRACE(("|%p|%p|GROUPREF %d\n", pattern, |
| 1443 |
ptr, pattern[0])); |
| 1444 |
{ |
| 1445 |
int groupref = pattern[0] * 2; |
| 1446 |
if (groupref >= state->lastmark) { |
| 1447 |
RETURN_FAILURE; |
| 1448 |
} else { |
| 1449 |
SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref]; |
| 1450 |
SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1]; |
| 1451 |
if (!p || !e || e < p) |
| 1452 |
RETURN_FAILURE; |
| 1453 |
while (p < e) { |
| 1454 |
if (ptr >= end || *ptr != *p) |
| 1455 |
RETURN_FAILURE; |
| 1456 |
p++; |
| 1457 |
ptr++; |
| 1458 |
} |
| 1459 |
} |
| 1460 |
} |
| 1461 |
pattern++; |
| 1462 |
DISPATCH; |
| 1463 |
|
| 1464 |
TARGET(SRE_OP_GROUPREF_IGNORE): |
| 1465 |
/* match backreference */ |
| 1466 |
TRACE(("|%p|%p|GROUPREF_IGNORE %d\n", pattern, |
| 1467 |
ptr, pattern[0])); |
| 1468 |
{ |
| 1469 |
int groupref = pattern[0] * 2; |
| 1470 |
if (groupref >= state->lastmark) { |
| 1471 |
RETURN_FAILURE; |
| 1472 |
} else { |
| 1473 |
SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref]; |
| 1474 |
SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1]; |
| 1475 |
if (!p || !e || e < p) |
| 1476 |
RETURN_FAILURE; |
| 1477 |
while (p < e) { |
| 1478 |
if (ptr >= end || |
| 1479 |
sre_lower_ascii(*ptr) != sre_lower_ascii(*p)) |
| 1480 |
RETURN_FAILURE; |
| 1481 |
p++; |
| 1482 |
ptr++; |
| 1483 |
} |
| 1484 |
} |
| 1485 |
} |
| 1486 |
pattern++; |
| 1487 |
DISPATCH; |
| 1488 |
|
| 1489 |
TARGET(SRE_OP_GROUPREF_UNI_IGNORE): |
| 1490 |
/* match backreference */ |
| 1491 |
TRACE(("|%p|%p|GROUPREF_UNI_IGNORE %d\n", pattern, |
| 1492 |
ptr, pattern[0])); |
| 1493 |
{ |
| 1494 |
int groupref = pattern[0] * 2; |
| 1495 |
if (groupref >= state->lastmark) { |
| 1496 |
RETURN_FAILURE; |
| 1497 |
} else { |
| 1498 |
SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref]; |
| 1499 |
SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1]; |
| 1500 |
if (!p || !e || e < p) |
| 1501 |
RETURN_FAILURE; |
| 1502 |
while (p < e) { |
| 1503 |
if (ptr >= end || |
| 1504 |
sre_lower_unicode(*ptr) != sre_lower_unicode(*p)) |
| 1505 |
RETURN_FAILURE; |
| 1506 |
p++; |
| 1507 |
ptr++; |
| 1508 |
} |
| 1509 |
} |
| 1510 |
} |
| 1511 |
pattern++; |
| 1512 |
DISPATCH; |
| 1513 |
|
| 1514 |
TARGET(SRE_OP_GROUPREF_LOC_IGNORE): |
| 1515 |
/* match backreference */ |
| 1516 |
TRACE(("|%p|%p|GROUPREF_LOC_IGNORE %d\n", pattern, |
| 1517 |
ptr, pattern[0])); |
| 1518 |
{ |
| 1519 |
int groupref = pattern[0] * 2; |
| 1520 |
if (groupref >= state->lastmark) { |
| 1521 |
RETURN_FAILURE; |
| 1522 |
} else { |
| 1523 |
SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref]; |
| 1524 |
SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1]; |
| 1525 |
if (!p || !e || e < p) |
| 1526 |
RETURN_FAILURE; |
| 1527 |
while (p < e) { |
| 1528 |
if (ptr >= end || |
| 1529 |
sre_lower_locale(*ptr) != sre_lower_locale(*p)) |
| 1530 |
RETURN_FAILURE; |
| 1531 |
p++; |
| 1532 |
ptr++; |
| 1533 |
} |
| 1534 |
} |
| 1535 |
} |
| 1536 |
pattern++; |
| 1537 |
DISPATCH; |
| 1538 |
|
| 1539 |
TARGET(SRE_OP_GROUPREF_EXISTS): |
| 1540 |
TRACE(("|%p|%p|GROUPREF_EXISTS %d\n", pattern, |
| 1541 |
ptr, pattern[0])); |
| 1542 |
/* <GROUPREF_EXISTS> <group> <skip> codeyes <JUMP> codeno ... */ |
| 1543 |
{ |
| 1544 |
int groupref = pattern[0] * 2; |
| 1545 |
if (groupref >= state->lastmark) { |
| 1546 |
pattern += pattern[1]; |
| 1547 |
DISPATCH; |
| 1548 |
} else { |
| 1549 |
SRE_CHAR* p = (SRE_CHAR*) state->mark[groupref]; |
| 1550 |
SRE_CHAR* e = (SRE_CHAR*) state->mark[groupref+1]; |
| 1551 |
if (!p || !e || e < p) { |
| 1552 |
pattern += pattern[1]; |
| 1553 |
DISPATCH; |
| 1554 |
} |
| 1555 |
} |
| 1556 |
} |
| 1557 |
pattern += 2; |
| 1558 |
DISPATCH; |
| 1559 |
|
| 1560 |
TARGET(SRE_OP_ASSERT): |
| 1561 |
/* assert subpattern */ |
| 1562 |
/* <ASSERT> <skip> <back> <pattern> */ |
| 1563 |
TRACE(("|%p|%p|ASSERT %d\n", pattern, |
| 1564 |
ptr, pattern[1])); |
| 1565 |
if ((uintptr_t)(ptr - (SRE_CHAR *)state->beginning) < pattern[1]) |
| 1566 |
RETURN_FAILURE; |
| 1567 |
state->ptr = ptr - pattern[1]; |
| 1568 |
DO_JUMP0(JUMP_ASSERT, jump_assert, pattern+2); |
| 1569 |
RETURN_ON_FAILURE(ret); |
| 1570 |
pattern += pattern[0]; |
| 1571 |
DISPATCH; |
| 1572 |
|
| 1573 |
TARGET(SRE_OP_ASSERT_NOT): |
| 1574 |
/* assert not subpattern */ |
| 1575 |
/* <ASSERT_NOT> <skip> <back> <pattern> */ |
| 1576 |
TRACE(("|%p|%p|ASSERT_NOT %d\n", pattern, |
| 1577 |
ptr, pattern[1])); |
| 1578 |
if ((uintptr_t)(ptr - (SRE_CHAR *)state->beginning) >= pattern[1]) { |
| 1579 |
state->ptr = ptr - pattern[1]; |
| 1580 |
LASTMARK_SAVE(); |
| 1581 |
if (state->repeat) |
| 1582 |
MARK_PUSH(ctx->lastmark); |
| 1583 |
|
| 1584 |
DO_JUMP0(JUMP_ASSERT_NOT, jump_assert_not, pattern+2); |
| 1585 |
if (ret) { |
| 1586 |
if (state->repeat) |
| 1587 |
MARK_POP_DISCARD(ctx->lastmark); |
| 1588 |
RETURN_ON_ERROR(ret); |
| 1589 |
RETURN_FAILURE; |
| 1590 |
} |
| 1591 |
if (state->repeat) |
| 1592 |
MARK_POP(ctx->lastmark); |
| 1593 |
LASTMARK_RESTORE(); |
| 1594 |
} |
| 1595 |
pattern += pattern[0]; |
| 1596 |
DISPATCH; |
| 1597 |
|
| 1598 |
TARGET(SRE_OP_FAILURE): |
| 1599 |
/* immediate failure */ |
| 1600 |
TRACE(("|%p|%p|FAILURE\n", pattern, ptr)); |
| 1601 |
RETURN_FAILURE; |
| 1602 |
|
| 1603 |
#if !USE_COMPUTED_GOTOS |
| 1604 |
default: |
| 1605 |
#endif |
| 1606 |
// Also any unused opcodes: |
| 1607 |
TARGET(SRE_OP_RANGE_UNI_IGNORE): |
| 1608 |
TARGET(SRE_OP_SUBPATTERN): |
| 1609 |
TARGET(SRE_OP_RANGE): |
| 1610 |
TARGET(SRE_OP_NEGATE): |
| 1611 |
TARGET(SRE_OP_BIGCHARSET): |
| 1612 |
TARGET(SRE_OP_CHARSET): |
| 1613 |
TRACE(("|%p|%p|UNKNOWN %d\n", pattern, ptr, |
| 1614 |
pattern[-1])); |
| 1615 |
RETURN_ERROR(SRE_ERROR_ILLEGAL); |
| 1616 |
|
| 1617 |
} |
| 1618 |
|
| 1619 |
exit: |
| 1620 |
ctx_pos = ctx->last_ctx_pos; |
| 1621 |
jump = ctx->jump; |
| 1622 |
DATA_POP_DISCARD(ctx); |
| 1623 |
if (ctx_pos == -1) { |
| 1624 |
state->sigcount = sigcount; |
| 1625 |
return ret; |
| 1626 |
} |
| 1627 |
DATA_LOOKUP_AT(SRE(match_context), ctx, ctx_pos); |
| 1628 |
|
| 1629 |
switch (jump) { |
| 1630 |
case JUMP_MAX_UNTIL_2: |
| 1631 |
TRACE(("|%p|%p|JUMP_MAX_UNTIL_2\n", pattern, ptr)); |
| 1632 |
goto jump_max_until_2; |
| 1633 |
case JUMP_MAX_UNTIL_3: |
| 1634 |
TRACE(("|%p|%p|JUMP_MAX_UNTIL_3\n", pattern, ptr)); |
| 1635 |
goto jump_max_until_3; |
| 1636 |
case JUMP_MIN_UNTIL_2: |
| 1637 |
TRACE(("|%p|%p|JUMP_MIN_UNTIL_2\n", pattern, ptr)); |
| 1638 |
goto jump_min_until_2; |
| 1639 |
case JUMP_MIN_UNTIL_3: |
| 1640 |
TRACE(("|%p|%p|JUMP_MIN_UNTIL_3\n", pattern, ptr)); |
| 1641 |
goto jump_min_until_3; |
| 1642 |
case JUMP_BRANCH: |
| 1643 |
TRACE(("|%p|%p|JUMP_BRANCH\n", pattern, ptr)); |
| 1644 |
goto jump_branch; |
| 1645 |
case JUMP_MAX_UNTIL_1: |
| 1646 |
TRACE(("|%p|%p|JUMP_MAX_UNTIL_1\n", pattern, ptr)); |
| 1647 |
goto jump_max_until_1; |
| 1648 |
case JUMP_MIN_UNTIL_1: |
| 1649 |
TRACE(("|%p|%p|JUMP_MIN_UNTIL_1\n", pattern, ptr)); |
| 1650 |
goto jump_min_until_1; |
| 1651 |
case JUMP_POSS_REPEAT_1: |
| 1652 |
TRACE(("|%p|%p|JUMP_POSS_REPEAT_1\n", pattern, ptr)); |
| 1653 |
goto jump_poss_repeat_1; |
| 1654 |
case JUMP_POSS_REPEAT_2: |
| 1655 |
TRACE(("|%p|%p|JUMP_POSS_REPEAT_2\n", pattern, ptr)); |
| 1656 |
goto jump_poss_repeat_2; |
| 1657 |
case JUMP_REPEAT: |
| 1658 |
TRACE(("|%p|%p|JUMP_REPEAT\n", pattern, ptr)); |
| 1659 |
goto jump_repeat; |
| 1660 |
case JUMP_REPEAT_ONE_1: |
| 1661 |
TRACE(("|%p|%p|JUMP_REPEAT_ONE_1\n", pattern, ptr)); |
| 1662 |
goto jump_repeat_one_1; |
| 1663 |
case JUMP_REPEAT_ONE_2: |
| 1664 |
TRACE(("|%p|%p|JUMP_REPEAT_ONE_2\n", pattern, ptr)); |
| 1665 |
goto jump_repeat_one_2; |
| 1666 |
case JUMP_MIN_REPEAT_ONE: |
| 1667 |
TRACE(("|%p|%p|JUMP_MIN_REPEAT_ONE\n", pattern, ptr)); |
| 1668 |
goto jump_min_repeat_one; |
| 1669 |
case JUMP_ATOMIC_GROUP: |
| 1670 |
TRACE(("|%p|%p|JUMP_ATOMIC_GROUP\n", pattern, ptr)); |
| 1671 |
goto jump_atomic_group; |
| 1672 |
case JUMP_ASSERT: |
| 1673 |
TRACE(("|%p|%p|JUMP_ASSERT\n", pattern, ptr)); |
| 1674 |
goto jump_assert; |
| 1675 |
case JUMP_ASSERT_NOT: |
| 1676 |
TRACE(("|%p|%p|JUMP_ASSERT_NOT\n", pattern, ptr)); |
| 1677 |
goto jump_assert_not; |
| 1678 |
case JUMP_NONE: |
| 1679 |
TRACE(("|%p|%p|RETURN %zd\n", pattern, |
| 1680 |
ptr, ret)); |
| 1681 |
break; |
| 1682 |
} |
| 1683 |
|
| 1684 |
return ret; /* should never get here */ |
| 1685 |
} |
| 1686 |
|
| 1687 |
/* need to reset capturing groups between two SRE(match) callings in loops */ |
| 1688 |
#define RESET_CAPTURE_GROUP() \ |
| 1689 |
do { state->lastmark = state->lastindex = -1; } while (0) |
| 1690 |
|
| 1691 |
LOCAL(Py_ssize_t) |
| 1692 |
SRE(search)(SRE_STATE* state, SRE_CODE* pattern) |
| 1693 |
{ |
| 1694 |
SRE_CHAR* ptr = (SRE_CHAR *)state->start; |
| 1695 |
SRE_CHAR* end = (SRE_CHAR *)state->end; |
| 1696 |
Py_ssize_t status = 0; |
| 1697 |
Py_ssize_t prefix_len = 0; |
| 1698 |
Py_ssize_t prefix_skip = 0; |
| 1699 |
SRE_CODE* prefix = NULL; |
| 1700 |
SRE_CODE* charset = NULL; |
| 1701 |
SRE_CODE* overlap = NULL; |
| 1702 |
int flags = 0; |
| 1703 |
INIT_TRACE(state); |
| 1704 |
|
| 1705 |
if (ptr > end) |
| 1706 |
return 0; |
| 1707 |
|
| 1708 |
if (pattern[0] == SRE_OP_INFO) { |
| 1709 |
/* optimization info block */ |
| 1710 |
/* <INFO> <1=skip> <2=flags> <3=min> <4=max> <5=prefix info> */ |
| 1711 |
|
| 1712 |
flags = pattern[2]; |
| 1713 |
|
| 1714 |
if (pattern[3] && (uintptr_t)(end - ptr) < pattern[3]) { |
| 1715 |
TRACE(("reject (got %tu chars, need %zu)\n", |
| 1716 |
end - ptr, (size_t) pattern[3])); |
| 1717 |
return 0; |
| 1718 |
} |
| 1719 |
if (pattern[3] > 1) { |
| 1720 |
/* adjust end point (but make sure we leave at least one |
| 1721 |
character in there, so literal search will work) */ |
| 1722 |
end -= pattern[3] - 1; |
| 1723 |
if (end <= ptr) |
| 1724 |
end = ptr; |
| 1725 |
} |
| 1726 |
|
| 1727 |
if (flags & SRE_INFO_PREFIX) { |
| 1728 |
/* pattern starts with a known prefix */ |
| 1729 |
/* <length> <skip> <prefix data> <overlap data> */ |
| 1730 |
prefix_len = pattern[5]; |
| 1731 |
prefix_skip = pattern[6]; |
| 1732 |
prefix = pattern + 7; |
| 1733 |
overlap = prefix + prefix_len - 1; |
| 1734 |
} else if (flags & SRE_INFO_CHARSET) |
| 1735 |
/* pattern starts with a character from a known set */ |
| 1736 |
/* <charset> */ |
| 1737 |
charset = pattern + 5; |
| 1738 |
|
| 1739 |
pattern += 1 + pattern[1]; |
| 1740 |
} |
| 1741 |
|
| 1742 |
TRACE(("prefix = %p %zd %zd\n", |
| 1743 |
prefix, prefix_len, prefix_skip)); |
| 1744 |
TRACE(("charset = %p\n", charset)); |
| 1745 |
|
| 1746 |
if (prefix_len == 1) { |
| 1747 |
/* pattern starts with a literal character */ |
| 1748 |
SRE_CHAR c = (SRE_CHAR) prefix[0]; |
| 1749 |
#if SIZEOF_SRE_CHAR < 4 |
| 1750 |
if ((SRE_CODE) c != prefix[0]) |
| 1751 |
return 0; /* literal can't match: doesn't fit in char width */ |
| 1752 |
#endif |
| 1753 |
end = (SRE_CHAR *)state->end; |
| 1754 |
state->must_advance = 0; |
| 1755 |
while (ptr < end) { |
| 1756 |
while (*ptr != c) { |
| 1757 |
if (++ptr >= end) |
| 1758 |
return 0; |
| 1759 |
} |
| 1760 |
TRACE(("|%p|%p|SEARCH LITERAL\n", pattern, ptr)); |
| 1761 |
state->start = ptr; |
| 1762 |
state->ptr = ptr + prefix_skip; |
| 1763 |
if (flags & SRE_INFO_LITERAL) |
| 1764 |
return 1; /* we got all of it */ |
| 1765 |
status = SRE(match)(state, pattern + 2*prefix_skip, 0); |
| 1766 |
if (status != 0) |
| 1767 |
return status; |
| 1768 |
++ptr; |
| 1769 |
RESET_CAPTURE_GROUP(); |
| 1770 |
} |
| 1771 |
return 0; |
| 1772 |
} |
| 1773 |
|
| 1774 |
if (prefix_len > 1) { |
| 1775 |
/* pattern starts with a known prefix. use the overlap |
| 1776 |
table to skip forward as fast as we possibly can */ |
| 1777 |
Py_ssize_t i = 0; |
| 1778 |
|
| 1779 |
end = (SRE_CHAR *)state->end; |
| 1780 |
if (prefix_len > end - ptr) |
| 1781 |
return 0; |
| 1782 |
#if SIZEOF_SRE_CHAR < 4 |
| 1783 |
for (i = 0; i < prefix_len; i++) |
| 1784 |
if ((SRE_CODE)(SRE_CHAR) prefix[i] != prefix[i]) |
| 1785 |
return 0; /* literal can't match: doesn't fit in char width */ |
| 1786 |
#endif |
| 1787 |
while (ptr < end) { |
| 1788 |
SRE_CHAR c = (SRE_CHAR) prefix[0]; |
| 1789 |
while (*ptr++ != c) { |
| 1790 |
if (ptr >= end) |
| 1791 |
return 0; |
| 1792 |
} |
| 1793 |
if (ptr >= end) |
| 1794 |
return 0; |
| 1795 |
|
| 1796 |
i = 1; |
| 1797 |
state->must_advance = 0; |
| 1798 |
do { |
| 1799 |
if (*ptr == (SRE_CHAR) prefix[i]) { |
| 1800 |
if (++i != prefix_len) { |
| 1801 |
if (++ptr >= end) |
| 1802 |
return 0; |
| 1803 |
continue; |
| 1804 |
} |
| 1805 |
/* found a potential match */ |
| 1806 |
TRACE(("|%p|%p|SEARCH SCAN\n", pattern, ptr)); |
| 1807 |
state->start = ptr - (prefix_len - 1); |
| 1808 |
state->ptr = ptr - (prefix_len - prefix_skip - 1); |
| 1809 |
if (flags & SRE_INFO_LITERAL) |
| 1810 |
return 1; /* we got all of it */ |
| 1811 |
status = SRE(match)(state, pattern + 2*prefix_skip, 0); |
| 1812 |
if (status != 0) |
| 1813 |
return status; |
| 1814 |
/* close but no cigar -- try again */ |
| 1815 |
if (++ptr >= end) |
| 1816 |
return 0; |
| 1817 |
RESET_CAPTURE_GROUP(); |
| 1818 |
} |
| 1819 |
i = overlap[i]; |
| 1820 |
} while (i != 0); |
| 1821 |
} |
| 1822 |
return 0; |
| 1823 |
} |
| 1824 |
|
| 1825 |
if (charset) { |
| 1826 |
/* pattern starts with a character from a known set */ |
| 1827 |
end = (SRE_CHAR *)state->end; |
| 1828 |
state->must_advance = 0; |
| 1829 |
for (;;) { |
| 1830 |
while (ptr < end && !SRE(charset)(state, charset, *ptr)) |
| 1831 |
ptr++; |
| 1832 |
if (ptr >= end) |
| 1833 |
return 0; |
| 1834 |
TRACE(("|%p|%p|SEARCH CHARSET\n", pattern, ptr)); |
| 1835 |
state->start = ptr; |
| 1836 |
state->ptr = ptr; |
| 1837 |
status = SRE(match)(state, pattern, 0); |
| 1838 |
if (status != 0) |
| 1839 |
break; |
| 1840 |
ptr++; |
| 1841 |
RESET_CAPTURE_GROUP(); |
| 1842 |
} |
| 1843 |
} else { |
| 1844 |
/* general case */ |
| 1845 |
assert(ptr <= end); |
| 1846 |
TRACE(("|%p|%p|SEARCH\n", pattern, ptr)); |
| 1847 |
state->start = state->ptr = ptr; |
| 1848 |
status = SRE(match)(state, pattern, 1); |
| 1849 |
state->must_advance = 0; |
| 1850 |
if (status == 0 && pattern[0] == SRE_OP_AT && |
| 1851 |
(pattern[1] == SRE_AT_BEGINNING || |
| 1852 |
pattern[1] == SRE_AT_BEGINNING_STRING)) |
| 1853 |
{ |
| 1854 |
state->start = state->ptr = ptr = end; |
| 1855 |
return 0; |
| 1856 |
} |
| 1857 |
while (status == 0 && ptr < end) { |
| 1858 |
ptr++; |
| 1859 |
RESET_CAPTURE_GROUP(); |
| 1860 |
TRACE(("|%p|%p|SEARCH\n", pattern, ptr)); |
| 1861 |
state->start = state->ptr = ptr; |
| 1862 |
status = SRE(match)(state, pattern, 0); |
| 1863 |
} |
| 1864 |
} |
| 1865 |
|
| 1866 |
return status; |
| 1867 |
} |
| 1868 |
|
| 1869 |
#undef SRE_CHAR |
| 1870 |
#undef SIZEOF_SRE_CHAR |
| 1871 |
#undef SRE |
| 1872 |
|
| 1873 |
/* vim:ts=4:sw=4:et |
| 1874 |
*/ |