MCPcopy Create free account
hub / github.com/F-Stack/f-stack / luaB_tonumber

Function luaB_tonumber

freebsd/contrib/openzfs/module/lua/lbaselib.c:30–68  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28#define SPACECHARS " \f\n\r\t\v"
29
30static int luaB_tonumber (lua_State *L) {
31 if (lua_isnoneornil(L, 2)) { /* standard conversion */
32 int isnum;
33 lua_Number n = lua_tonumberx(L, 1, &isnum);
34 if (isnum) {
35 lua_pushnumber(L, n);
36 return 1;
37 } /* else not a number; must be something */
38 luaL_checkany(L, 1);
39 }
40 else {
41 size_t l;
42 const char *s = luaL_checklstring(L, 1, &l);
43 const char *e = s + l; /* end point for 's' */
44 int base = luaL_checkint(L, 2);
45 int neg = 0;
46 luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
47 s += strspn(s, SPACECHARS); /* skip initial spaces */
48 if (*s == '-') { s++; neg = 1; } /* handle signal */
49 else if (*s == '+') s++;
50 if (isalnum((unsigned char)*s)) {
51 lua_Number n = 0;
52 do {
53 int digit = (isdigit((unsigned char)*s)) ? *s - '0'
54 : toupper((unsigned char)*s) - 'A' + 10;
55 if (digit >= base) break; /* invalid numeral; force a fail */
56 n = n * (lua_Number)base + (lua_Number)digit;
57 s++;
58 } while (isalnum((unsigned char)*s));
59 s += strspn(s, SPACECHARS); /* skip trailing spaces */
60 if (s == e) { /* no invalid trailing characters? */
61 lua_pushnumber(L, (neg) ? -n : n);
62 return 1;
63 } /* else not a number */
64 } /* else not a number */
65 }
66 lua_pushnil(L); /* not a number */
67 return 1;
68}
69
70
71static int luaB_error (lua_State *L) {

Callers

nothing calls this directly

Calls 8

lua_tonumberxFunction · 0.85
strspnFunction · 0.85
isdigitFunction · 0.85
toupperFunction · 0.85
lua_pushnumberFunction · 0.70
luaL_checkanyFunction · 0.70
luaL_checklstringFunction · 0.70
lua_pushnilFunction · 0.70

Tested by

no test coverage detected