| 771 | |
| 772 | |
| 773 | static int str_find_aux (lua_State *L, int find) { |
| 774 | size_t ls, lp; |
| 775 | const char *s = luaL_checklstring(L, 1, &ls); |
| 776 | const char *p = luaL_checklstring(L, 2, &lp); |
| 777 | size_t init = posrelatI(luaL_optinteger(L, 3, 1), ls) - 1; |
| 778 | if (init > ls) { /* start after string's end? */ |
| 779 | luaL_pushfail(L); /* cannot find anything */ |
| 780 | return 1; |
| 781 | } |
| 782 | /* explicit request or no special characters? */ |
| 783 | if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) { |
| 784 | /* do a plain search */ |
| 785 | const char *s2 = lmemfind(s + init, ls - init, p, lp); |
| 786 | if (s2) { |
| 787 | lua_pushinteger(L, (s2 - s) + 1); |
| 788 | lua_pushinteger(L, (s2 - s) + lp); |
| 789 | return 2; |
| 790 | } |
| 791 | } |
| 792 | else { |
| 793 | MatchState ms; |
| 794 | const char *s1 = s + init; |
| 795 | int anchor = (*p == '^'); |
| 796 | if (anchor) { |
| 797 | p++; lp--; /* skip anchor character */ |
| 798 | } |
| 799 | prepstate(&ms, L, s, ls, p, lp); |
| 800 | do { |
| 801 | const char *res; |
| 802 | reprepstate(&ms); |
| 803 | if ((res=match(&ms, s1, p)) != NULL) { |
| 804 | if (find) { |
| 805 | lua_pushinteger(L, (s1 - s) + 1); /* start */ |
| 806 | lua_pushinteger(L, res - s); /* end */ |
| 807 | return push_captures(&ms, NULL, 0) + 2; |
| 808 | } |
| 809 | else |
| 810 | return push_captures(&ms, s1, res); |
| 811 | } |
| 812 | } while (s1++ < ms.src_end && !anchor); |
| 813 | } |
| 814 | luaL_pushfail(L); /* not found */ |
| 815 | return 1; |
| 816 | } |
| 817 | |
| 818 | |
| 819 | static int str_find (lua_State *L) { |
no test coverage detected