MCPcopy Create free account
hub / github.com/DFHack/dfhack / luaB_tonumber

Function luaB_tonumber

depends/lua/src/lbaselib.c:69–99  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

67
68
69static int luaB_tonumber (lua_State *L) {
70 if (lua_isnoneornil(L, 2)) { /* standard conversion? */
71 luaL_checkany(L, 1);
72 if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */
73 lua_settop(L, 1); /* yes; return it */
74 return 1;
75 }
76 else {
77 size_t l;
78 const char *s = lua_tolstring(L, 1, &l);
79 if (s != NULL && lua_stringtonumber(L, s) == l + 1)
80 return 1; /* successful conversion to number */
81 /* else not a number */
82 }
83 }
84 else {
85 size_t l;
86 const char *s;
87 lua_Integer n = 0; /* to avoid warnings */
88 lua_Integer base = luaL_checkinteger(L, 2);
89 luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */
90 s = lua_tolstring(L, 1, &l);
91 luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
92 if (b_str2int(s, (int)base, &n) == s + l) {
93 lua_pushinteger(L, n);
94 return 1;
95 } /* else not a number */
96 } /* else not a number */
97 lua_pushnil(L); /* not a number */
98 return 1;
99}
100
101
102static int luaB_error (lua_State *L) {

Callers

nothing calls this directly

Calls 10

luaL_checkanyFunction · 0.85
lua_typeFunction · 0.85
lua_settopFunction · 0.85
lua_tolstringFunction · 0.85
lua_stringtonumberFunction · 0.85
luaL_checkintegerFunction · 0.85
luaL_checktypeFunction · 0.85
b_str2intFunction · 0.85
lua_pushintegerFunction · 0.85
lua_pushnilFunction · 0.85

Tested by

no test coverage detected