diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-02-09 17:00:23 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1996-02-09 17:00:23 -0200 |
commit | 801722825d94b3bddd94d83887b081c328772147 (patch) | |
tree | a76eedfe7e3e9105c0adcfbeb5d861167bc4dcce /strlib.c | |
parent | 3abc25fa5424ebc857d445792dd0689926b7ea34 (diff) | |
download | lua-801722825d94b3bddd94d83887b081c328772147.tar.gz lua-801722825d94b3bddd94d83887b081c328772147.tar.bz2 lua-801722825d94b3bddd94d83887b081c328772147.zip |
"lua_check_number" accepts strings convertible to numbers.
Diffstat (limited to 'strlib.c')
-rw-r--r-- | strlib.c | 22 |
1 files changed, 15 insertions, 7 deletions
@@ -3,7 +3,7 @@ | |||
3 | ** String library to LUA | 3 | ** String library to LUA |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_strlib="$Id: strlib.c,v 1.15 1996/01/22 17:38:57 roberto Exp roberto $"; | 6 | char *rcs_strlib="$Id: strlib.c,v 1.16 1996/01/26 12:11:28 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <string.h> | 8 | #include <string.h> |
9 | #include <stdio.h> | 9 | #include <stdio.h> |
@@ -29,12 +29,20 @@ char *lua_check_string (int numArg, char *funcname) | |||
29 | return lua_getstring(o); | 29 | return lua_getstring(o); |
30 | } | 30 | } |
31 | 31 | ||
32 | float lua_check_number (int numArg, char *funcname) | 32 | double lua_check_number (int numArg, char *funcname) |
33 | { | 33 | { |
34 | lua_Object o = lua_getparam(numArg); | 34 | lua_Object o = lua_getparam(numArg); |
35 | if (!lua_isnumber(o)) | 35 | if (lua_isnumber(o)) |
36 | lua_arg_error(funcname); | 36 | return lua_getnumber(o); |
37 | return lua_getnumber(o); | 37 | else if (lua_isstring(o)) |
38 | { | ||
39 | float t; | ||
40 | char c; | ||
41 | if (sscanf(lua_getstring(o), "%f %c",&t, &c) == 1) | ||
42 | return t; | ||
43 | } | ||
44 | lua_arg_error(funcname); | ||
45 | return 0; /* to avoid warnings */ | ||
38 | } | 46 | } |
39 | 47 | ||
40 | char *luaI_addchar (int c) | 48 | char *luaI_addchar (int c) |
@@ -171,7 +179,7 @@ static void str_ascii (void) | |||
171 | #define MAX_CONVERTION 2000 | 179 | #define MAX_CONVERTION 2000 |
172 | #define MAX_FORMAT 50 | 180 | #define MAX_FORMAT 50 |
173 | 181 | ||
174 | static void io_format (void) | 182 | static void str_format (void) |
175 | { | 183 | { |
176 | int arg = 1; | 184 | int arg = 1; |
177 | char *strfrmt = lua_check_string(arg++, "format"); | 185 | char *strfrmt = lua_check_string(arg++, "format"); |
@@ -244,5 +252,5 @@ void strlib_open (void) | |||
244 | lua_register ("strlower", str_lower); | 252 | lua_register ("strlower", str_lower); |
245 | lua_register ("strupper", str_upper); | 253 | lua_register ("strupper", str_upper); |
246 | lua_register ("ascii", str_ascii); | 254 | lua_register ("ascii", str_ascii); |
247 | lua_register ("format", io_format); | 255 | lua_register ("format", str_format); |
248 | } | 256 | } |