diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-05-16 15:35:57 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2013-05-16 15:35:57 -0300 |
commit | 5ca5086c191259dd88cc51161a82e93759136486 (patch) | |
tree | 73fb52c9aa501b53e90c64b0e505bfc5f53a3a79 /lbaselib.c | |
parent | 2d6a0ae1493b130e47fba6ff76a8866d32b5acde (diff) | |
download | lua-5ca5086c191259dd88cc51161a82e93759136486.tar.gz lua-5ca5086c191259dd88cc51161a82e93759136486.tar.bz2 lua-5ca5086c191259dd88cc51161a82e93759136486.zip |
'tonumber' now works with integers too
Diffstat (limited to 'lbaselib.c')
-rw-r--r-- | lbaselib.c | 18 |
1 files changed, 8 insertions, 10 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.275 2012/12/03 20:18:02 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.276 2013/02/21 13:44:53 roberto Exp roberto $ |
3 | ** Basic library | 3 | ** Basic library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -47,13 +47,11 @@ static int luaB_print (lua_State *L) { | |||
47 | 47 | ||
48 | static int luaB_tonumber (lua_State *L) { | 48 | static int luaB_tonumber (lua_State *L) { |
49 | if (lua_isnoneornil(L, 2)) { /* standard conversion */ | 49 | if (lua_isnoneornil(L, 2)) { /* standard conversion */ |
50 | int isnum; | ||
51 | lua_Number n = lua_tonumberx(L, 1, &isnum); | ||
52 | if (isnum) { | ||
53 | lua_pushnumber(L, n); | ||
54 | return 1; | ||
55 | } /* else not a number; must be something */ | ||
56 | luaL_checkany(L, 1); | 50 | luaL_checkany(L, 1); |
51 | if (lua_cvtonum(L, 1)) { /* can convert to a number? */ | ||
52 | lua_settop(L, 1); /* yes; return converted value */ | ||
53 | return 1; | ||
54 | } /* else not a number */ | ||
57 | } | 55 | } |
58 | else { | 56 | else { |
59 | size_t l; | 57 | size_t l; |
@@ -66,17 +64,17 @@ static int luaB_tonumber (lua_State *L) { | |||
66 | if (*s == '-') { s++; neg = 1; } /* handle signal */ | 64 | if (*s == '-') { s++; neg = 1; } /* handle signal */ |
67 | else if (*s == '+') s++; | 65 | else if (*s == '+') s++; |
68 | if (isalnum((unsigned char)*s)) { | 66 | if (isalnum((unsigned char)*s)) { |
69 | lua_Number n = 0; | 67 | lua_Integer n = 0; |
70 | do { | 68 | do { |
71 | int digit = (isdigit((unsigned char)*s)) ? *s - '0' | 69 | int digit = (isdigit((unsigned char)*s)) ? *s - '0' |
72 | : toupper((unsigned char)*s) - 'A' + 10; | 70 | : toupper((unsigned char)*s) - 'A' + 10; |
73 | if (digit >= base) break; /* invalid numeral; force a fail */ | 71 | if (digit >= base) break; /* invalid numeral; force a fail */ |
74 | n = n * (lua_Number)base + (lua_Number)digit; | 72 | n = n * base + digit; |
75 | s++; | 73 | s++; |
76 | } while (isalnum((unsigned char)*s)); | 74 | } while (isalnum((unsigned char)*s)); |
77 | s += strspn(s, SPACECHARS); /* skip trailing spaces */ | 75 | s += strspn(s, SPACECHARS); /* skip trailing spaces */ |
78 | if (s == e) { /* no invalid trailing characters? */ | 76 | if (s == e) { /* no invalid trailing characters? */ |
79 | lua_pushnumber(L, (neg) ? -n : n); | 77 | lua_pushinteger(L, (neg) ? -n : n); |
80 | return 1; | 78 | return 1; |
81 | } /* else not a number */ | 79 | } /* else not a number */ |
82 | } /* else not a number */ | 80 | } /* else not a number */ |