diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-12-30 15:57:45 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-12-30 15:57:45 -0200 |
| commit | a5fd7d722c2c15ca954ecd69bfe72b8fe5c4c384 (patch) | |
| tree | fb0a69f6d86ea4a0fc779ff67f62fa94ec2b0419 | |
| parent | 4e0bf95622151cf09a2708b465d6df682ad43284 (diff) | |
| download | lua-a5fd7d722c2c15ca954ecd69bfe72b8fe5c4c384.tar.gz lua-a5fd7d722c2c15ca954ecd69bfe72b8fe5c4c384.tar.bz2 lua-a5fd7d722c2c15ca954ecd69bfe72b8fe5c4c384.zip | |
opctional "base" in "tonumber" convertion.
| -rw-r--r-- | lbuiltin.c | 26 |
1 files changed, 20 insertions, 6 deletions
| @@ -1,11 +1,13 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lbuiltin.c,v 1.18 1997/12/17 20:48:58 roberto Exp roberto $ | 2 | ** $Id: lbuiltin.c,v 1.19 1997/12/18 18:32:39 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 | */ |
| 6 | 6 | ||
| 7 | 7 | ||
| 8 | #include <ctype.h> | ||
| 8 | #include <stdio.h> | 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> | ||
| 9 | #include <string.h> | 11 | #include <string.h> |
| 10 | 12 | ||
| 11 | #include "lapi.h" | 13 | #include "lapi.h" |
| @@ -188,11 +190,23 @@ static void luaI_type (void) | |||
| 188 | } | 190 | } |
| 189 | 191 | ||
| 190 | 192 | ||
| 191 | static void lua_obj2number (void) | 193 | static void tonumber (void) |
| 192 | { | 194 | { |
| 193 | lua_Object o = lua_getparam(1); | 195 | int base = luaL_opt_number(2, 10); |
| 194 | if (lua_isnumber(o)) | 196 | if (base == 10) { /* standard convertion */ |
| 195 | lua_pushnumber(lua_getnumber(o)); | 197 | lua_Object o = lua_getparam(1); |
| 198 | if (lua_isnumber(o)) | ||
| 199 | lua_pushnumber(lua_getnumber(o)); | ||
| 200 | } | ||
| 201 | else { | ||
| 202 | char *s = luaL_check_string(1); | ||
| 203 | unsigned long n; | ||
| 204 | luaL_arg_check(0 <= base && base <= 36, 2, "base out of range"); | ||
| 205 | n = strtol(s, &s, base); | ||
| 206 | while (isspace(*s)) s++; /* skip trailing spaces */ | ||
| 207 | if (*s) return; /* invalid format: return nil */ | ||
| 208 | lua_pushnumber(n); | ||
| 209 | } | ||
| 196 | } | 210 | } |
| 197 | 211 | ||
| 198 | 212 | ||
| @@ -481,7 +495,7 @@ static struct luaL_reg int_funcs[] = { | |||
| 481 | {"settagmethod", settagmethod}, | 495 | {"settagmethod", settagmethod}, |
| 482 | {"gettagmethod", gettagmethod}, | 496 | {"gettagmethod", gettagmethod}, |
| 483 | {"settag", settag}, | 497 | {"settag", settag}, |
| 484 | {"tonumber", lua_obj2number}, | 498 | {"tonumber", tonumber}, |
| 485 | {"tostring", bi_tostring}, | 499 | {"tostring", bi_tostring}, |
| 486 | {"tag", luatag}, | 500 | {"tag", luatag}, |
| 487 | {"type", luaI_type} | 501 | {"type", luaI_type} |
