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