| 493 | |
| 494 | |
| 495 | static int str_find_aux (lua_State *L, int find) { |
| 496 | size_t l1, l2; |
| 497 | const char *s = luaL_checklstring(L, 1, &l1); |
| 498 | const char *p = luaL_checklstring(L, 2, &l2); |
| 499 | ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1; |
| 500 | if (init < 0) init = 0; |
| 501 | else if ((size_t)(init) > l1) init = (ptrdiff_t)l1; |
| 502 | if (find && (lua_toboolean(L, 4) || /* explicit request? */ |
| 503 | strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */ |
| 504 | /* do a plain search */ |
| 505 | const char *s2 = lmemfind(s+init, l1-init, p, l2); |
| 506 | if (s2) { |
| 507 | lua_pushinteger(L, s2-s+1); |
| 508 | lua_pushinteger(L, s2-s+l2); |
| 509 | return 2; |
| 510 | } |
| 511 | } |
| 512 | else { |
| 513 | MatchState ms; |
| 514 | int anchor = (*p == '^') ? (p++, 1) : 0; |
| 515 | const char *s1=s+init; |
| 516 | ms.L = L; |
| 517 | ms.src_init = s; |
| 518 | ms.src_end = s+l1; |
| 519 | do { |
| 520 | const char *res; |
| 521 | ms.level = 0; |
| 522 | if ((res=match(&ms, s1, p)) != NULL) { |
| 523 | if (find) { |
| 524 | lua_pushinteger(L, s1-s+1); /* start */ |
| 525 | lua_pushinteger(L, res-s); /* end */ |
| 526 | return push_captures(&ms, NULL, 0) + 2; |
| 527 | } |
| 528 | else |
| 529 | return push_captures(&ms, s1, res); |
| 530 | } |
| 531 | } while (s1++ < ms.src_end && !anchor); |
| 532 | } |
| 533 | lua_pushnil(L); /* not found */ |
| 534 | return 1; |
| 535 | } |
| 536 | |
| 537 | |
| 538 | static int str_find (lua_State *L) { |
no test coverage detected