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