diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-04-13 13:46:43 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-04-13 13:46:43 -0300 |
| commit | ceaa97ff5b78db364b1608ede26e3dba9050be0e (patch) | |
| tree | 640cc29480b642e6302ff5f8169356d0008d1831 /lbuiltin.c | |
| parent | 2bb77cdaed74190673769d6bc32c4a091272a146 (diff) | |
| download | lua-ceaa97ff5b78db364b1608ede26e3dba9050be0e.tar.gz lua-ceaa97ff5b78db364b1608ede26e3dba9050be0e.tar.bz2 lua-ceaa97ff5b78db364b1608ede26e3dba9050be0e.zip | |
tonumber must return `nil' when convertion fails.
Diffstat (limited to 'lbuiltin.c')
| -rw-r--r-- | lbuiltin.c | 20 |
1 files changed, 13 insertions, 7 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbuiltin.c,v 1.101 2000/04/03 13:20:33 roberto Exp roberto $ | 2 | ** $Id: lbuiltin.c,v 1.102 2000/04/04 20:49:32 roberto Exp roberto $ |
| 3 | ** Built-in functions | 3 | ** Built-in functions |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -157,8 +157,10 @@ void luaB_tonumber (lua_State *L) { | |||
| 157 | int base = luaL_opt_int(L, 2, 10); | 157 | int base = luaL_opt_int(L, 2, 10); |
| 158 | if (base == 10) { /* standard conversion */ | 158 | if (base == 10) { /* standard conversion */ |
| 159 | lua_Object o = luaL_nonnullarg(L, 1); | 159 | lua_Object o = luaL_nonnullarg(L, 1); |
| 160 | if (lua_isnumber(L, o)) lua_pushnumber(L, lua_getnumber(L, o)); | 160 | if (lua_isnumber(L, o)) { |
| 161 | else lua_pushnil(L); /* not a number */ | 161 | lua_pushnumber(L, lua_getnumber(L, o)); |
| 162 | return; | ||
| 163 | } | ||
| 162 | } | 164 | } |
| 163 | else { | 165 | else { |
| 164 | const char *s1 = luaL_check_string(L, 1); | 166 | const char *s1 = luaL_check_string(L, 1); |
| @@ -166,11 +168,15 @@ void luaB_tonumber (lua_State *L) { | |||
| 166 | Number n; | 168 | Number n; |
| 167 | luaL_arg_check(L, 0 <= base && base <= 36, 2, "base out of range"); | 169 | luaL_arg_check(L, 0 <= base && base <= 36, 2, "base out of range"); |
| 168 | n = strtoul(s1, &s2, base); | 170 | n = strtoul(s1, &s2, base); |
| 169 | if (s1 == s2) return; /* no valid digits: return nil */ | 171 | if (s1 != s2) { /* at least one valid digit? */ |
| 170 | while (isspace((unsigned char)*s2)) s2++; /* skip trailing spaces */ | 172 | while (isspace((unsigned char)*s2)) s2++; /* skip trailing spaces */ |
| 171 | if (*s2) return; /* invalid trailing character: return nil */ | 173 | if (*s2 == '\0') { /* no invalid trailing characters? */ |
| 172 | lua_pushnumber(L, n); | 174 | lua_pushnumber(L, n); |
| 175 | return; | ||
| 176 | } | ||
| 177 | } | ||
| 173 | } | 178 | } |
| 179 | lua_pushnil(L); /* else not a number */ | ||
| 174 | } | 180 | } |
| 175 | 181 | ||
| 176 | 182 | ||
