MCPcopy Create free account
hub / github.com/DFHack/dfhack / str_find_aux

Function str_find_aux

depends/lua/src/lstrlib.c:607–651  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

605
606
607static 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
654static int str_find (lua_State *L) {

Callers 2

str_findFunction · 0.85
str_matchFunction · 0.85

Calls 12

luaL_checklstringFunction · 0.85
posrelatFunction · 0.85
luaL_optintegerFunction · 0.85
lua_pushnilFunction · 0.85
lua_tobooleanFunction · 0.85
nospecialsFunction · 0.85
lmemfindFunction · 0.85
lua_pushintegerFunction · 0.85
prepstateFunction · 0.85
reprepstateFunction · 0.85
push_capturesFunction · 0.85
matchFunction · 0.70

Tested by

no test coverage detected