static int str_find (lua_State *L) { size_t l1, l2; const char *s = luaL_check_lstr(L, 1, &l1); const char *p = luaL_check_lstr(L, 2, &l2); long init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1; struct Capture cap; luaL_arg_check(L, 0 <= init && (size_t)init <= l1, 3, "out of range"); if (lua_gettop(L) > 3 || /* extra argument? */ strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */ const char *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 { int anchor = (*p == '^') ? (p++, 1) : 0; const char *s1=s+init; cap.src_end = s+l1; do { const char *res; cap.level = 0; if ((res=match(L, s1, p, &cap)) != NULL) { lua_pushnumber(L, s1-s+1); /* start */ lua_pushnumber(L, res-s); /* end */ return push_captures(L, &cap) + 2; } } while (s1++<cap.src_end && !anchor); } lua_pushnil(L); /* not found */ return 1; }
static int str_find_aux(struct match_state *ms, const char *pattern, const char *string, struct str_find *sm, size_t nsm, off_t init) { size_t ls = strlen(string); size_t lp = strlen(pattern); const char *s = string; const char *p = pattern; const char *s1, *s2; int anchor, i; if (init < 0) init = 0; else if (init > (off_t)ls) return match_error(ms, "starting after string's end"); s1 = s + init; if (nospecials(p, lp)) { /* do a plain search */ s2 = lmemfind(s1, ls - (size_t)init, p, lp); if (s2 == NULL) return (0); i = 0; sm[i].sm_so = 0; sm[i].sm_eo = (off_t)ls; if (nsm > 1) { i++; sm[i].sm_so = s2 - s; sm[i].sm_eo = (off_t)((s2 - s) + (off_t)lp); } return (i + 1); } anchor = (*p == '^'); if (anchor) { p++; lp--; /* skip anchor character */ } ms->maxcaptures = (int)((nsm > MAXCAPTURES ? MAXCAPTURES : nsm) - 1); ms->matchdepth = MAXCCALLS; ms->repetitioncounter = MAXREPETITION; ms->src_init = s; ms->src_end = s + ls; ms->p_end = p + lp; do { const char *res; ms->level = 0; if ((res = match(ms, s1, p)) != NULL) { sm->sm_so = 0; sm->sm_eo = (off_t)ls; return push_captures(ms, s1, res, sm + 1, nsm - 1) + 1; } else if (ms->error != NULL) { return 0; } } while (s1++ < ms->src_end && !anchor); return 0; }
bool str_find(const char *s, size_t l1, const char *p, size_t l2, bool explicit, int32_t init_pos, size_t *num_of_cap, struct capture *capture, size_t len) { ptrdiff_t init = posrelat(init_pos, l1) - 1; if (init < 0) init = 0; else if ((size_t) (init) > l1) init = (ptrdiff_t) l1; if ((explicit || /* explicit request? */ strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */ /* do a plain search */ const char *s2 = lmemfind(s + init, l1 - (size_t)init, p, l2); if (s2) return 2; } else {
static int str_replace(lua_State *L) { size_t l1, l2, l3; const char *src = luaL_checklstring(L, 1, &l1); const char *p = luaL_checklstring(L, 2, &l2); const char *p2 = luaL_checklstring(L, 3, &l3); int init = luaL_optinteger(L, 4, 1) - 1; const char *s2; int n = 0; TBuffer b; buffer_init (&b, l1, L); if (init > 0) { buffer_addlstring(&b, src, init); } while (1) { s2 = lmemfind(src+init, l1-init, p, l2); if (s2) { buffer_addlstring(&b, src+init, s2-(src+init)); buffer_addlstring(&b, p2, l3); init = init + (s2-(src+init)) + l2; n++; } else { buffer_addlstring(&b, src+init, l1-init); break; } } buffer_pushresult(&b); buffer_free (&b); lua_pushnumber(L, (lua_Number)n); /* number of substitutions */ return 2; }
static int str_find_aux(LuaMatchState *ms, int find, const char *s, size_t ls,const char *p, size_t lp, size_t init, int raw_find) { int result = 0; // not found ms->error = NULL; init = posrelat(init, ls); if ((int)init < 0) init = 0; else if (init > ls + 1) { // start after string's end? return 0; // cannot find anything } // explicit request or no special characters? if (find && (raw_find || nospecials(p, lp))) { // do a plain search const char *s2 = lmemfind(s + init, ls - init + 1, p, lp); if (s2) { long start_pos = ((long)(s2 - s)); ms->level = 1; ms->capture[0].len = CAP_POSITION; ms->capture[0].init = (const char *)start_pos; result = (int)(start_pos + lp); } } else { const char *s1 = s + init; int anchor = (*p == '^'); if (anchor) p++, lp--; // skip anchor character ms->src_init = s; ms->src_end = s + ls; ms->p_end = p + lp; do { const char *res; ms->level = 0; if ((res=match(ms, s1, p)) != NULL) { result = (int)(res - s); goto eofunc; } } while (s1++ < ms->src_end && !anchor); } eofunc: if(result) { int i; for(i=0; i<ms->level; ++i) { if(ms->capture[i].len == CAP_UNFINISHED) { ms->error = "unfinished capture"; return 0; } } } return result; }
static int str_find_aux (lua_State *L, int find) { size_t ls, lp; const char *s = luaL_checklstring(L, 1, &ls); const char *p = luaL_checklstring(L, 2, &lp); size_t init = posrelat(luaL_optinteger(L, 3, 1), ls); if (init < 1) init = 1; else if (init > ls + 1) { /* start after string's end? */ lua_pushnil(L); /* cannot find anything */ return 1; } /* explicit request or no special characters? */ if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) { /* do a plain search */ const char *s2 = lmemfind(s + init - 1, ls - init + 1, p, lp); if (s2) { lua_pushinteger(L, s2 - s + 1); lua_pushinteger(L, s2 - s + lp); return 2; } } else { MatchState ms; const char *s1 = s + init - 1; int anchor = (*p == '^'); if (anchor) { p++; lp--; /* skip anchor character */ } ms.L = L; ms.matchdepth = MAXCCALLS; ms.src_init = s; ms.src_end = s + ls; ms.p_end = p + lp; do { const char *res; ms.level = 0; lua_assert(ms.matchdepth == MAXCCALLS); if ((res=match(&ms, s1, p)) != NULL) { if (find) { lua_pushinteger(L, s1 - s + 1); /* start */ lua_pushinteger(L, res - s); /* end */ return push_captures(&ms, NULL, 0) + 2; } else return push_captures(&ms, s1, res); } } while (s1++ < ms.src_end && !anchor); } lua_pushnil(L); /* not found */ return 1; }
//mod by nirenr static int gfind_aux (lua_State *L) { size_t ls, lp; const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls); const char *p = lua_tolstring(L, lua_upvalueindex(2), &lp); lua_Integer init = posrelat(luaL_optinteger(L, lua_upvalueindex(3), 1), ls); if (init < 1) init = 1; else if (init > (lua_Integer)ls + 1) { /* start after string's end? */ return 0; /* cannot find anything */ } /* explicit request or no special characters? */ if (lua_toboolean(L, lua_upvalueindex(4)) || nospecials(p, lp)) { /* do a plain search */ const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp); if (s2) { lua_pushinteger(L, (s2 - s) + 1); lua_pushinteger(L, (s2 - s) + lp); lua_pushinteger(L,(s2 - s) + lp + 1); lua_replace(L, lua_upvalueindex(3)); return 2; } } else { MatchState ms; const char *s1 = s + init - 1; int anchor = (*p == '^'); if (anchor) { p++; lp--; /* skip anchor character */ } ms.L = L; ms.matchdepth = MAXCCALLS; ms.src_init = s; ms.src_end = s + ls; ms.p_end = p + lp; do { const char *res; ms.level = 0; lua_assert(ms.matchdepth == MAXCCALLS); if ((res=match(&ms, s1, p)) != NULL) { lua_pushinteger(L, (s1 - s) + 1); /* start */ lua_pushinteger(L, res - s); /* end */ lua_pushinteger(L, res - s + 1); lua_replace(L, lua_upvalueindex(3)); return push_captures(&ms, NULL, 0) + 2; } } while (s1++ < ms.src_end && !anchor); } return 0; /* not found */ }
static int str_find_aux (lua_State *L, int mode) { /* EXT */ size_t ls, lp; const char *s = luaL_checklstring(L, 1, &ls); const char *p = luaL_checklstring(L, 2, &lp); lua_Integer init = posrelat(luaL_optinteger(L, 3, 1), ls); if (init < 1) init = 1; else if (init > (lua_Integer)ls + 1) { /* start after string's end? */ lua_pushnil(L); /* cannot find anything */ return 1; } /* explicit request or no special characters? */ /* EXT */ if (mode == MODE_FIND && (lua_toboolean(L, 4) || nospecials(p, lp))) { /* do a plain search */ const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp); if (s2) { lua_pushinteger(L, (s2 - s) + 1); lua_pushinteger(L, (s2 - s) + lp); return 2; } } else { MatchState ms; const char *s1 = s + init - 1; int anchor = (*p == '^'); prepstate(&ms, L, s, ls, p, lp); /* EXT (moved before anchor check) */ if (anchor) p++; /* skip anchor character */ /* EXT */ do { const char *res; reprepstate(&ms); if ((res=match(&ms, s1, p)) != NULL) { if (mode == MODE_FIND) { /* EXT */ lua_pushinteger(L, (s1 - s) + 1); /* start */ lua_pushinteger(L, res - s); /* end */ return push_captures(&ms, NULL, 0) + 2; } else if (mode == MODE_MATCH) /* EXT */ return push_captures(&ms, s1, res); else { /* EXT */ return build_result_table(&ms, s1, res); } } } while (s1++ < ms.src_end && !anchor); } lua_pushnil(L); /* not found */ return 1; }
static int str_find_aux (lua_State *L, int find) { size_t l1, l2; const char *s = luaL_checklstring(L, 1, &l1); const char *p = luaL_checklstring(L, 2, &l2); ptrdiff_t init = 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? */ strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */ /* do a plain search */ const char *s2 = lmemfind(s+init, l1-init, p, l2); if (s2) { lua_pushinteger(L, s2-s+1); lua_pushinteger(L, s2-s+l2); return 2; } } else { MatchState ms; int anchor = (*p == '^') ? (p++, 1) : 0; const char *s1=s+init; ms.L = L; ms.src_init = s; ms.src_end = s+l1; do { const char *res; ms.level = 0; if ((res=match(&ms, s1, p)) != NULL) { if (find) { lua_pushinteger(L, s1-s+1); /* start */ lua_pushinteger(L, res-s); /* end */ return push_captures(&ms, NULL, 0) + 2; } else return push_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; }