| 423 | |
| 424 | |
| 425 | static const char *match (MatchState *ms, const char *s, const char *p) { |
| 426 | if (ms->matchdepth-- == 0) |
| 427 | luaL_error(ms->L, "pattern too complex"); |
| 428 | init: /* using goto's to optimize tail recursion */ |
| 429 | if (p != ms->p_end) { /* end of pattern? */ |
| 430 | switch (*p) { |
| 431 | case '(': { /* start capture */ |
| 432 | if (*(p + 1) == ')') /* position capture? */ |
| 433 | s = start_capture(ms, s, p + 2, CAP_POSITION); |
| 434 | else |
| 435 | s = start_capture(ms, s, p + 1, CAP_UNFINISHED); |
| 436 | break; |
| 437 | } |
| 438 | case ')': { /* end capture */ |
| 439 | s = end_capture(ms, s, p + 1); |
| 440 | break; |
| 441 | } |
| 442 | case '$': { |
| 443 | if ((p + 1) != ms->p_end) /* is the '$' the last char in pattern? */ |
| 444 | goto dflt; /* no; go to default */ |
| 445 | s = (s == ms->src_end) ? s : NULL; /* check end of string */ |
| 446 | break; |
| 447 | } |
| 448 | case L_ESC: { /* escaped sequences not in the format class[*+?-]? */ |
| 449 | switch (*(p + 1)) { |
| 450 | case 'b': { /* balanced string? */ |
| 451 | s = matchbalance(ms, s, p + 2); |
| 452 | if (s != NULL) { |
| 453 | p += 4; goto init; /* return match(ms, s, p + 4); */ |
| 454 | } /* else fail (s == NULL) */ |
| 455 | break; |
| 456 | } |
| 457 | case 'f': { /* frontier? */ |
| 458 | const char *ep; char previous; |
| 459 | p += 2; |
| 460 | if (*p != '[') |
| 461 | luaL_error(ms->L, "missing '[' after '%%f' in pattern"); |
| 462 | ep = classend(ms, p); /* points to what is next */ |
| 463 | previous = (s == ms->src_init) ? '\0' : *(s - 1); |
| 464 | if (!matchbracketclass(uchar(previous), p, ep - 1) && |
| 465 | matchbracketclass(uchar(*s), p, ep - 1)) { |
| 466 | p = ep; goto init; /* return match(ms, s, ep); */ |
| 467 | } |
| 468 | s = NULL; /* match failed */ |
| 469 | break; |
| 470 | } |
| 471 | case '0': case '1': case '2': case '3': |
| 472 | case '4': case '5': case '6': case '7': |
| 473 | case '8': case '9': { /* capture results (%0-%9)? */ |
| 474 | s = match_capture(ms, s, uchar(*(p + 1))); |
| 475 | if (s != NULL) { |
| 476 | p += 2; goto init; /* return match(ms, s, p + 2) */ |
| 477 | } |
| 478 | break; |
| 479 | } |
| 480 | default: goto dflt; |
| 481 | } |
| 482 | break; |
no test coverage detected