static void str_gsub (void) { char *src = luaL_check_string(1); char *p = luaL_check_string(2); lua_Object newp = lua_getparam(3); lua_Object table = lua_getparam(4); int max_s = (int)luaL_opt_number(lua_istable(table)?5:4, strlen(src)+1); int anchor = (*p == '^') ? (p++, 1) : 0; int n = 0; luaI_emptybuff(); while (n < max_s) { char *e = match(src, p, 0); if (e) { n++; add_s(newp, table, n); } if (e && e>src) /* non empty match? */ src = e; /* skip it */ else if (*src) luaI_addchar(*src++); else break; if (anchor) break; } addstr(src); lua_pushstring(luaI_addchar(0)); lua_pushnumber(n); /* number of substitutions */ }
/* ** get ascii value of a character in a string */ static void str_ascii (void) { char *s = luaL_check_string(1); long pos = (long)luaL_opt_number(2, 1) - 1; luaL_arg_check(0<=pos && pos<strlen(s), 2, "out of range"); lua_pushnumber((unsigned char)s[pos]); }
static void str_find (void) { char *s = luaL_check_string(1); char *p = luaL_check_string(2); long init = (long)luaL_opt_number(3, 1) - 1; luaL_arg_check(0 <= init && init <= strlen(s), 3, "out of range"); if (lua_getparam(4) != LUA_NOOBJECT || strpbrk(p, SPECIALS) == NULL) { /* no special caracters? */ char *s2 = strstr(s+init, p); if (s2) { lua_pushnumber(s2-s+1); lua_pushnumber(s2-s+strlen(p)); } } else { int anchor = (*p == '^') ? (p++, 1) : 0; char *s1=s+init; do { char *res; if ((res=match(s1, p, 0)) != NULL) { lua_pushnumber(s1-s+1); /* start */ lua_pushnumber(res-s); /* end */ push_captures(); return; } } while (*s1++ && !anchor); } }
static void str_gsub (void) { int32 srcl; const char *src = luaL_check_lstr(1, &srcl); const char *p = luaL_check_string(2); lua_Object newp = lua_getparam(3); int32 max_s = (int32)luaL_opt_number(4, srcl+1); int32 anchor = (*p == '^') ? (p++, 1) : 0; int32 n = 0; struct Capture cap; luaL_arg_check(lua_isstring(newp) || lua_isfunction(newp), 3, "string or function expected"); luaL_resetbuffer(); cap.src_end = src+srcl; while (n < max_s) { const char *e; cap.level = 0; e = match(src, p, &cap); if (e) { n++; add_s(newp, &cap); } if (e && e>src) /* non empty match? */ src = e; /* skip it */ else if (src < cap.src_end) luaL_addchar(*src++); else break; if (anchor) break; } addnchar(src, cap.src_end-src); closeandpush(); lua_pushnumber(n); /* number of substitutions */ }
static void str_find (void) { int32 l; const char *s = luaL_check_lstr(1, &l); const char *p = luaL_check_string(2); int32 init = posrelat((int32)luaL_opt_number(3, 1), l) - 1; struct Capture cap; luaL_arg_check(0 <= init && init <= l, 3, "out of range"); if (lua_getparam(4) != LUA_NOOBJECT || strpbrk(p, SPECIALS) == NULL) { /* no special characters? */ const char *s2 = strstr(s+init, p); if (s2) { lua_pushnumber(s2-s+1); lua_pushnumber(s2-s+strlen(p)); return; } } else { int32 anchor = (*p == '^') ? (p++, 1) : 0; const char *s1=s+init; cap.src_end = s+l; do { const char *res; cap.level = 0; if ((res=match(s1, p, &cap)) != NULL) { lua_pushnumber(s1-s+1); /* start */ lua_pushnumber(res-s); /* end */ push_captures(&cap); return; } } while (s1++<cap.src_end && !anchor); } lua_pushnil(); /* if arrives here, it didn't find */ }
static int io_date (lua_State *L) { const char *s = luaL_opt_string(L, 1, "%c"); time_t t = (time_t)(luaL_opt_number(L, 2, -1)); struct tm *stm; if (t == (time_t)(-1)) /* no time given? */ t = time(NULL); /* use current time */ if (*s == '!') { /* UTC? */ stm = gmtime(&t); s++; /* skip `!' */ } else stm = localtime(&t); if (stm == NULL) /* invalid date? */ lua_pushnil(L); else if (strcmp(s, "*t") == 0) { lua_newtable(L); setfield(L, "sec", stm->tm_sec); setfield(L, "min", stm->tm_min); setfield(L, "hour", stm->tm_hour); setfield(L, "day", stm->tm_mday); setfield(L, "month", stm->tm_mon+1); setfield(L, "year", stm->tm_year+1900); setfield(L, "wday", stm->tm_wday+1); setfield(L, "yday", stm->tm_yday+1); setfield(L, "isdst", stm->tm_isdst); } else { char b[256]; if (strftime(b, sizeof(b), s, stm)) lua_pushstring(L, b); else return luaL_verror(L, "invalid `date' format"); } return 1; }
static void math_random() { float r = (float)(rand() % RAND_MAX) / (float)RAND_MAX; float l = luaL_opt_number(1, 0); if (l == 0) lua_pushnumber(r); else lua_pushnumber((int32)(r * l) + 1); }
static void setloc (void) { static int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC, LC_TIME}; int op = (int)luaL_opt_number(2, 0); luaL_arg_check(0 <= op && op <= 5, 2, "invalid option"); lua_pushstring(setlocale(cat[op], luaL_check_string(1))); }
static void str_byte (void) { int32 l; const char *s = luaL_check_lstr(1, &l); int32 pos = posrelat((int32)luaL_opt_number(2, 1), l); luaL_arg_check(0<pos && pos<=l, 2, "out of range"); lua_pushnumber((byte)s[pos-1]); }
static void str_sub (void) { int32 l; const char *s = luaL_check_lstr(1, &l); int32 start = posrelat((int32)luaL_check_number(2), l); int32 end = posrelat((int32)luaL_opt_number(3, -1), l); if (1 <= start && start <= end && end <= l) lua_pushlstring(s+start-1, end-start+1); else lua_pushstring(""); }
static void math_random (void) { /* the '%' is needed because on some systems (SunOS!) "rand()" may */ /* return a value bigger than RAND_MAX... */ double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX; double l = luaL_opt_number(1, 0); if (l == 0) lua_pushnumber(r); else lua_pushnumber((int32)(r*l)+1); }
/* ** Return the substring of a string */ static void str_sub (void) { char *s = luaL_check_string(1); long l = strlen(s); long start = (long)luaL_check_number(2); long end = (long)luaL_opt_number(3, -1); if (start < 0) start = l+start+1; if (end < 0) end = l+end+1; if (1 <= start && start <= end && end <= l) { luaI_emptybuff(); addnchar(s+start-1, end-start+1); lua_pushstring(luaI_addchar(0)); } else lua_pushstring(""); }
static void tonumber() { int32 base = (int32)luaL_opt_number(2, 10); if (base == 10) { // standard conversion lua_Object o = lua_getparam(1); if (lua_isnumber(o)) lua_pushnumber(lua_getnumber(o)); } else { const char *s = luaL_check_string(1); char *e; int32 n; luaL_arg_check(0 <= base && base <= 36, 2, "base out of range"); n = (int32)strtol(s, &e, base); while (Common::isSpace(*e)) e++; // skip trailing spaces if (*e) return; // invalid format: return nil lua_pushnumber(n); } }
static void luaI_collectgarbage() { lua_pushnumber(lua_collectgarbage((int32)luaL_opt_number(1, 0))); }
static int io_difftime (lua_State *L) { lua_pushnumber(L, difftime((time_t)(luaL_check_number(L, 1)), (time_t)(luaL_opt_number(L, 2, 0)))); return 1; }