| 28 | #define SPACECHARS " \f\n\r\t\v" |
| 29 | |
| 30 | static 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 | |
| 71 | static int luaB_error (lua_State *L) { |
nothing calls this directly
no test coverage detected