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_byte (lua_State *L) { size_t l; const char *s = luaL_check_lstr(L, 1, &l); long pos = posrelat(luaL_opt_long(L, 2, 1), l); luaL_arg_check(L, 0<pos && (size_t)pos<=l, 2, "out of range"); lua_pushnumber(L, (unsigned char)s[pos-1]); return 1; }
static int str_sub (lua_State *L) { size_t l; const char *s = luaL_check_lstr(L, 1, &l); long start = posrelat(luaL_check_long(L, 2), l); long end = posrelat(luaL_opt_long(L, 3, -1), l); if (start < 1) start = 1; if (end > (long)l) end = l; if (start <= end) lua_pushlstring(L, s+start-1, end-start+1); else lua_pushstring(L, ""); return 1; }
static int f_seek (lua_State *L) { static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; static const char *const modenames[] = {"set", "cur", "end", NULL}; FILE *f = tofile(L, 1); int op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames); long offset = luaL_opt_long(L, 3, 0); luaL_arg_check(L, op != -1, 2, "invalid mode"); op = fseek(f, offset, mode[op]); if (op) return pushresult(L, 0); /* error */ else { lua_pushnumber(L, ftell(f)); return 1; } }
static int io_seek (lua_State *L) { static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END}; static const char *const modenames[] = {"set", "cur", "end", NULL}; IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1); FILE *f; int op; long offset; lua_pop(L, 1); /* remove upvalue */ f = getnonullfile(L, ctrl, 1); op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames); offset = luaL_opt_long(L, 3, 0); luaL_arg_check(L, op != -1, 2, "invalid mode"); op = fseek(f, offset, mode[op]); if (op) return pushresult(L, 0); /* error */ else { lua_pushnumber(L, ftell(f)); return 1; } }