static int str_gsub (lua_State *L) { size_t srcl; const char *src = luaL_check_lstr(L, 1, &srcl); const char *p = luaL_check_string(L, 2); int max_s = luaL_opt_int(L, 4, srcl+1); int anchor = (*p == '^') ? (p++, 1) : 0; int n = 0; struct Capture cap; luaL_Buffer b; luaL_arg_check(L, lua_gettop(L) >= 3 && (lua_isstring(L, 3) || lua_isfunction(L, 3)), 3, "string or function expected"); luaL_buffinit(L, &b); cap.src_end = src+srcl; while (n < max_s) { const char *e; cap.level = 0; e = match(L, src, p, &cap); if (e) { n++; add_s(L, &b, &cap); } if (e && e>src) /* non empty match? */ src = e; /* skip it */ else if (src < cap.src_end) luaL_putchar(&b, *src++); else break; if (anchor) break; } luaL_addlstring(&b, src, cap.src_end-src); luaL_pushresult(&b); lua_pushnumber(L, n); /* number of substitutions */ return 2; }
static int luaB_tonumber (lua_State *L) { int base = luaL_opt_int(L, 2, 10); if (base == 10) { /* standard conversion */ luaL_checkany(L, 1); if (lua_isnumber(L, 1)) { lua_pushnumber(L, lua_tonumber(L, 1)); return 1; } } else { const char *s1 = luaL_check_string(L, 1); char *s2; unsigned long n; luaL_arg_check(L, 2 <= base && base <= 36, 2, "base out of range"); n = strtoul(s1, &s2, base); if (s1 != s2) { /* at least one valid digit? */ while (isspace((unsigned char)*s2)) s2++; /* skip trailing spaces */ if (*s2 == '\0') { /* no invalid trailing characters? */ lua_pushnumber(L, n); return 1; } } } lua_pushnil(L); /* else not a number */ return 1; }
static void math_random (void) { /* the '%' avoids the (rare) case of r==1, and is needed also because on some systems (SunOS!) "rand()" may return a value bigger than RAND_MAX */ double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX; int l = luaL_opt_int(1, 0); if (l == 0) lua_pushnumber(r); else { int u = luaL_opt_int(2, 0); if (u == 0) { u = l; l = 1; } luaL_arg_check(l<=u, 1, "interval is empty"); lua_pushnumber((int)(r*(u-l+1))+l); } }
static void luaB_tremove (void) { Hash *a = gethash(1); int n = (int)getnarg(a); int pos = luaL_opt_int(2, n); if (n <= 0) return; /* table is "empty" */ luaA_pushobject(luaH_getint(a, pos)); /* result = a[pos] */ for ( ;pos<n; pos++) luaH_move(a, pos+1, pos); /* a[pos] = a[pos+1] */ luaV_setn(a, n-1); /* a.n = n-1 */ luaH_setint(a, n, &luaO_nilobject); /* a[n] = nil */ }
static void luaB_tonumber (void) { int base = luaL_opt_int(2, 10); if (base == 10) { /* standard conversion */ lua_Object o = lua_getparam(1); if (lua_isnumber(o)) lua_pushnumber(lua_getnumber(o)); else lua_pushnil(); /* not a number */ } else { char *s = luaL_check_string(1); long n; luaL_arg_check(0 <= base && base <= 36, 2, "base out of range"); n = strtol(s, &s, base); while (isspace((unsigned char)*s)) s++; /* skip trailing spaces */ if (*s) lua_pushnil(); /* invalid format: return nil */ else lua_pushnumber(n); } }
static int luaB_tremove (lua_State *L) { int pos, n; luaL_checktype(L, 1, LUA_TTABLE); n = lua_getn(L, 1); pos = luaL_opt_int(L, 2, n); if (n <= 0) return 0; /* table is "empty" */ lua_rawgeti(L, 1, pos); /* result = t[pos] */ for ( ;pos<n; pos++) { lua_rawgeti(L, 1, pos+1); lua_rawseti(L, 1, pos); /* a[pos] = a[pos+1] */ } lua_pushstring(L, "n"); lua_pushnumber(L, n-1); lua_rawset(L, 1); /* t.n = n-1 */ lua_pushnil(L); lua_rawseti(L, 1, n); /* t[n] = nil */ return 1; }
static int io_exit (lua_State *L) { exit(luaL_opt_int(L, 1, EXIT_SUCCESS)); return 0; /* to avoid warnings */ }
static int luaB_collectgarbage (lua_State *L) { lua_setgcthreshold(L, luaL_opt_int(L, 1, 0)); return 0; }
static void luaB_collectgarbage (void) { lua_pushnumber(lua_collectgarbage(luaL_opt_int(1, 0))); }
static void Popup(void) { lua_pushnumber(IupPopup(iuplua_checkihandle(1), luaL_opt_int(2, IUP_CURRENT), luaL_opt_int(3, IUP_CURRENT))); }
static void ShowXY(void) { lua_pushnumber(IupShowXY(iuplua_checkihandle(1), luaL_opt_int(2, IUP_CURRENT), luaL_opt_int(3, IUP_CURRENT))); }