commit 801722825d94b3bddd94d83887b081c328772147
parent 3abc25fa5424ebc857d445792dd0689926b7ea34
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Fri, 9 Feb 1996 17:00:03 -0200
"lua_check_number" accepts strings convertible to numbers.
Diffstat:
2 files changed, 17 insertions(+), 9 deletions(-)
diff --git a/lualib.h b/lualib.h
@@ -2,7 +2,7 @@
** Libraries to be used in LUA programs
** Grupo de Tecnologia em Computacao Grafica
** TeCGraf - PUC-Rio
-** $Id: lualib.h,v 1.4 1995/11/10 17:54:31 roberto Exp roberto $
+** $Id: lualib.h,v 1.5 1996/01/22 17:38:57 roberto Exp roberto $
*/
#ifndef lualib_h
@@ -16,7 +16,7 @@ void mathlib_open (void);
/* auxiliar functions (private) */
void lua_arg_error(char *funcname);
char *lua_check_string (int numArg, char *funcname);
-float lua_check_number (int numArg, char *funcname);
+double lua_check_number (int numArg, char *funcname);
char *luaI_addchar (int c);
#endif
diff --git a/strlib.c b/strlib.c
@@ -3,7 +3,7 @@
** String library to LUA
*/
-char *rcs_strlib="$Id: strlib.c,v 1.15 1996/01/22 17:38:57 roberto Exp roberto $";
+char *rcs_strlib="$Id: strlib.c,v 1.16 1996/01/26 12:11:28 roberto Exp roberto $";
#include <string.h>
#include <stdio.h>
@@ -29,12 +29,20 @@ char *lua_check_string (int numArg, char *funcname)
return lua_getstring(o);
}
-float lua_check_number (int numArg, char *funcname)
+double lua_check_number (int numArg, char *funcname)
{
lua_Object o = lua_getparam(numArg);
- if (!lua_isnumber(o))
- lua_arg_error(funcname);
- return lua_getnumber(o);
+ if (lua_isnumber(o))
+ return lua_getnumber(o);
+ else if (lua_isstring(o))
+ {
+ float t;
+ char c;
+ if (sscanf(lua_getstring(o), "%f %c",&t, &c) == 1)
+ return t;
+ }
+ lua_arg_error(funcname);
+ return 0; /* to avoid warnings */
}
char *luaI_addchar (int c)
@@ -171,7 +179,7 @@ static void str_ascii (void)
#define MAX_CONVERTION 2000
#define MAX_FORMAT 50
-static void io_format (void)
+static void str_format (void)
{
int arg = 1;
char *strfrmt = lua_check_string(arg++, "format");
@@ -244,5 +252,5 @@ void strlib_open (void)
lua_register ("strlower", str_lower);
lua_register ("strupper", str_upper);
lua_register ("ascii", str_ascii);
- lua_register ("format", io_format);
+ lua_register ("format", str_format);
}