static int wstr_find_aux (lua_State *L, int find) { size_t l1, l2; const lua_WChar *s = luaL_checklwstring(L, 1, &l1); const lua_WChar *p = luaL_checklwstring(L, 2, &l2); ptrdiff_t init = wstr_posrelat(luaL_optinteger(L, 3, 1), l1) - 1; if (init < 0) init = 0; else if ((size_t)(init) > l1) init = (ptrdiff_t)l1; if (find && (lua_toboolean(L, 4) || /* explicit request? */ lua_WChar_pbrk(p, WSPECIALS) == NULL)) { /* or no special characters? */ /* do a plain search */ const lua_WChar *s2 = wlmemfind(s+init, l1-init, p, l2); if (s2) { lua_pushinteger(L, s2-s+1); lua_pushinteger(L, s2-s+l2); return 2; } } else { WMatchState ms; int anchor = (*p == '^') ? (p++, 1) : 0; const lua_WChar *s1=s+init; ms.L = L; ms.src_init = s; ms.src_end = s+l1; do { const lua_WChar *res; ms.level = 0; if ((res=wmatch(&ms, s1, p)) != NULL) { if (find) { lua_pushinteger(L, s1-s+1); /* start */ lua_pushinteger(L, res-s); /* end */ return wpush_captures(&ms, NULL, 0) + 2; } else return wpush_captures(&ms, s1, res); } } while (s1++ < ms.src_end && !anchor); } lua_pushnil(L); /* not found */ return 1; }
static int str_find (lua_State *L) { size_t l1, l2; const lua_WChar *s = luaL_checklwstring(L, 1, &l1); const lua_WChar *p = luaL_checklwstring(L, 2, &l2); sint32 init = posrelat(luaL_optlong(L, 3, 1), l1) - 1; luaL_argcheck(L, 0 <= init && (size_t)(init) <= l1, 3, "out of range"); if (lua_toboolean(L, 4) || /* explicit request? */ lua_WChar_pbrk(p, SPECIALS) == NULL) { /* or no special characters? */ /* do a plain search */ const lua_WChar *s2 = lmemfind(s+init, l1-init, p, l2); if (s2) { lua_pushnumber(L, s2-s+1); lua_pushnumber(L, s2-s+l2); return 2; } } else { MatchState ms; int anchor = (*p == '^') ? (p++, 1) : 0; const lua_WChar *s1=s+init; ms.L = L; ms.src_init = s; ms.src_end = s+l1; do { const lua_WChar *res; ms.level = 0; if ((res=match(&ms, s1, p)) != NULL) { lua_pushnumber(L, s1-s+1); /* start */ lua_pushnumber(L, res-s); /* end */ return push_captures(&ms, NULL, 0) + 2; } } while (s1++<ms.src_end && !anchor); } lua_pushnil(L); /* not found */ return 1; }