From a5fd7d722c2c15ca954ecd69bfe72b8fe5c4c384 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 30 Dec 1997 15:57:45 -0200 Subject: opctional "base" in "tonumber" convertion. --- lbuiltin.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'lbuiltin.c') diff --git a/lbuiltin.c b/lbuiltin.c index da2c915c..2a2ed4d8 100644 --- a/lbuiltin.c +++ b/lbuiltin.c @@ -1,11 +1,13 @@ /* -** $Id: lbuiltin.c,v 1.18 1997/12/17 20:48:58 roberto Exp roberto $ +** $Id: lbuiltin.c,v 1.19 1997/12/18 18:32:39 roberto Exp roberto $ ** Built-in functions ** See Copyright Notice in lua.h */ +#include #include +#include #include #include "lapi.h" @@ -188,11 +190,23 @@ static void luaI_type (void) } -static void lua_obj2number (void) +static void tonumber (void) { - lua_Object o = lua_getparam(1); - if (lua_isnumber(o)) - lua_pushnumber(lua_getnumber(o)); + int base = luaL_opt_number(2, 10); + if (base == 10) { /* standard convertion */ + lua_Object o = lua_getparam(1); + if (lua_isnumber(o)) + lua_pushnumber(lua_getnumber(o)); + } + else { + char *s = luaL_check_string(1); + unsigned long n; + luaL_arg_check(0 <= base && base <= 36, 2, "base out of range"); + n = strtol(s, &s, base); + while (isspace(*s)) s++; /* skip trailing spaces */ + if (*s) return; /* invalid format: return nil */ + lua_pushnumber(n); + } } @@ -481,7 +495,7 @@ static struct luaL_reg int_funcs[] = { {"settagmethod", settagmethod}, {"gettagmethod", gettagmethod}, {"settag", settag}, - {"tonumber", lua_obj2number}, + {"tonumber", tonumber}, {"tostring", bi_tostring}, {"tag", luatag}, {"type", luaI_type} -- cgit v1.2.3-55-g6feb