mirror of
https://github.com/lua/lua
synced 2025-03-25 15:12:51 +03:00
new API function 'lua_cvtonum' to convert a value (number or string)
to a number, following the rules of the language to create integers or floats
This commit is contained in:
parent
36e8771076
commit
b5ec26101f
24
lapi.c
24
lapi.c
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lapi.c,v 2.178 2013/04/29 17:12:50 roberto Exp roberto $
|
** $Id: lapi.c,v 2.179 2013/05/02 12:37:24 roberto Exp roberto $
|
||||||
** Lua API
|
** Lua API
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@ -331,6 +331,28 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
LUA_API int lua_cvtonum (lua_State *L, int idx) {
|
||||||
|
TValue *o = index2addr(L, idx);
|
||||||
|
if (ttisnumber(o)) return 1; /* already a number? */
|
||||||
|
else if (!ttisstring(o)) return 0; /* only strings can be converted */
|
||||||
|
else {
|
||||||
|
lua_Integer i; lua_Number n;
|
||||||
|
const char *s = svalue(o);
|
||||||
|
size_t len = tsvalue(o)->len;
|
||||||
|
if (luaO_str2int(s, len, &i)) {
|
||||||
|
setivalue(o, i);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else if (luaO_str2d(s, len, &n)) {
|
||||||
|
setivalue(o, i);
|
||||||
|
setnvalue(o, n);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
else return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
|
LUA_API lua_Number lua_tonumberx (lua_State *L, int idx, int *pisnum) {
|
||||||
lua_Number n;
|
lua_Number n;
|
||||||
const TValue *o = index2addr(L, idx);
|
const TValue *o = index2addr(L, idx);
|
||||||
|
4
lua.h
4
lua.h
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lua.h,v 1.287 2013/04/26 13:07:53 roberto Exp roberto $
|
** $Id: lua.h,v 1.288 2013/04/26 15:39:25 roberto Exp roberto $
|
||||||
** Lua - A Scripting Language
|
** Lua - A Scripting Language
|
||||||
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
|
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
|
||||||
** See Copyright Notice at the end of this file
|
** See Copyright Notice at the end of this file
|
||||||
@ -307,6 +307,8 @@ LUA_API int (lua_next) (lua_State *L, int idx);
|
|||||||
LUA_API void (lua_concat) (lua_State *L, int n);
|
LUA_API void (lua_concat) (lua_State *L, int n);
|
||||||
LUA_API void (lua_len) (lua_State *L, int idx);
|
LUA_API void (lua_len) (lua_State *L, int idx);
|
||||||
|
|
||||||
|
LUA_API int (lua_cvtonum) (lua_State *L, int idx);
|
||||||
|
|
||||||
LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
|
LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
|
||||||
LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
|
LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user