| 605 | |
| 606 | |
| 607 | static int str_find_aux (lua_State *L, int find) { |
| 608 | size_t ls, lp; |
| 609 | const char *s = luaL_checklstring(L, 1, &ls); |
| 610 | const char *p = luaL_checklstring(L, 2, &lp); |
| 611 | lua_Integer init = posrelat(luaL_optinteger(L, 3, 1), ls); |
| 612 | if (init < 1) init = 1; |
| 613 | else if (init > (lua_Integer)ls + 1) { /* start after string's end? */ |
| 614 | lua_pushnil(L); /* cannot find anything */ |
| 615 | return 1; |
| 616 | } |
| 617 | /* explicit request or no special characters? */ |
| 618 | if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) { |
| 619 | /* do a plain search */ |
| 620 | const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp); |
| 621 | if (s2) { |
| 622 | lua_pushinteger(L, (s2 - s) + 1); |
| 623 | lua_pushinteger(L, (s2 - s) + lp); |
| 624 | return 2; |
| 625 | } |
| 626 | } |
| 627 | else { |
| 628 | MatchState ms; |
| 629 | const char *s1 = s + init - 1; |
| 630 | int anchor = (*p == '^'); |
| 631 | if (anchor) { |
| 632 | p++; lp--; /* skip anchor character */ |
| 633 | } |
| 634 | prepstate(&ms, L, s, ls, p, lp); |
| 635 | do { |
| 636 | const char *res; |
| 637 | reprepstate(&ms); |
| 638 | if ((res=match(&ms, s1, p)) != NULL) { |
| 639 | if (find) { |
| 640 | lua_pushinteger(L, (s1 - s) + 1); /* start */ |
| 641 | lua_pushinteger(L, res - s); /* end */ |
| 642 | return push_captures(&ms, NULL, 0) + 2; |
| 643 | } |
| 644 | else |
| 645 | return push_captures(&ms, s1, res); |
| 646 | } |
| 647 | } while (s1++ < ms.src_end && !anchor); |
| 648 | } |
| 649 | lua_pushnil(L); /* not found */ |
| 650 | return 1; |
| 651 | } |
| 652 | |
| 653 | |
| 654 | static int str_find (lua_State *L) { |
no test coverage detected