| 565 | |
| 566 | |
| 567 | static const char *match (MatchState *ms, const char *s, const char *p) { |
| 568 | if (ms->matchdepth-- == 0) |
| 569 | luaL_error(ms->L, "pattern too complex"); |
| 570 | init: /* using goto's to optimize tail recursion */ |
| 571 | if (p != ms->p_end) { /* end of pattern? */ |
| 572 | switch (*p) { |
| 573 | case '(': { /* start capture */ |
| 574 | if (*(p + 1) == ')') /* position capture? */ |
| 575 | s = start_capture(ms, s, p + 2, CAP_POSITION); |
| 576 | else |
| 577 | s = start_capture(ms, s, p + 1, CAP_UNFINISHED); |
| 578 | break; |
| 579 | } |
| 580 | case ')': { /* end capture */ |
| 581 | s = end_capture(ms, s, p + 1); |
| 582 | break; |
| 583 | } |
| 584 | case '$': { |
| 585 | if ((p + 1) != ms->p_end) /* is the '$' the last char in pattern? */ |
| 586 | goto dflt; /* no; go to default */ |
| 587 | s = (s == ms->src_end) ? s : NULL; /* check end of string */ |
| 588 | break; |
| 589 | } |
| 590 | case L_ESC: { /* escaped sequences not in the format class[*+?-]? */ |
| 591 | switch (*(p + 1)) { |
| 592 | case 'b': { /* balanced string? */ |
| 593 | s = matchbalance(ms, s, p + 2); |
| 594 | if (s != NULL) { |
| 595 | p += 4; goto init; /* return match(ms, s, p + 4); */ |
| 596 | } /* else fail (s == NULL) */ |
| 597 | break; |
| 598 | } |
| 599 | case 'f': { /* frontier? */ |
| 600 | const char *ep; char previous; |
| 601 | p += 2; |
| 602 | if (*p != '[') |
| 603 | luaL_error(ms->L, "missing '[' after '%%f' in pattern"); |
| 604 | ep = classend(ms, p); /* points to what is next */ |
| 605 | previous = (s == ms->src_init) ? '\0' : *(s - 1); |
| 606 | if (!matchbracketclass(uchar(previous), p, ep - 1) && |
| 607 | matchbracketclass(uchar(*s), p, ep - 1)) { |
| 608 | p = ep; goto init; /* return match(ms, s, ep); */ |
| 609 | } |
| 610 | s = NULL; /* match failed */ |
| 611 | break; |
| 612 | } |
| 613 | case '0': case '1': case '2': case '3': |
| 614 | case '4': case '5': case '6': case '7': |
| 615 | case '8': case '9': { /* capture results (%0-%9)? */ |
| 616 | s = match_capture(ms, s, uchar(*(p + 1))); |
| 617 | if (s != NULL) { |
| 618 | p += 2; goto init; /* return match(ms, s, p + 2) */ |
| 619 | } |
| 620 | break; |
| 621 | } |
| 622 | default: goto dflt; |
| 623 | } |
| 624 | break; |
no test coverage detected