diff options
| -rw-r--r-- | lauxlib.c | 24 |
1 files changed, 11 insertions, 13 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lauxlib.c,v 1.35 2000/09/11 20:29:27 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.36 2000/09/12 13:48:22 roberto Exp roberto $ |
| 3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -66,38 +66,36 @@ void luaL_checktype(lua_State *L, int narg, const char *tname) { | |||
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | 68 | ||
| 69 | static const char *checkstr (lua_State *L, int narg, size_t *len) { | 69 | const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) { |
| 70 | const char *s = lua_tostring(L, narg); | 70 | const char *s = lua_tostring(L, narg); |
| 71 | if (!s) type_error(L, narg, "string"); | 71 | if (!s) type_error(L, narg, "string"); |
| 72 | if (len) *len = lua_strlen(L, narg); | 72 | if (len) *len = lua_strlen(L, narg); |
| 73 | return s; | 73 | return s; |
| 74 | } | 74 | } |
| 75 | 75 | ||
| 76 | const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) { | ||
| 77 | return checkstr(L, narg, len); | ||
| 78 | } | ||
| 79 | 76 | ||
| 80 | const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, | 77 | const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, |
| 81 | size_t *len) { | 78 | size_t *len) { |
| 82 | if (lua_isnull(L, narg)) { | 79 | if (lua_isnull(L, narg)) { |
| 83 | if (len) *len = def ? strlen(def) : 0; | 80 | if (len) |
| 81 | *len = (def ? strlen(def) : 0); | ||
| 84 | return def; | 82 | return def; |
| 85 | } | 83 | } |
| 86 | else return checkstr(L, narg, len); | 84 | else return luaL_check_lstr(L, narg, len); |
| 87 | } | 85 | } |
| 88 | 86 | ||
| 87 | |||
| 89 | double luaL_check_number (lua_State *L, int narg) { | 88 | double luaL_check_number (lua_State *L, int narg) { |
| 90 | if (!lua_isnumber(L, narg)) type_error(L, narg, "number"); | 89 | double d = lua_tonumber(L, narg); |
| 91 | return lua_tonumber(L, narg); | 90 | if (d == 0 && !lua_isnumber(L, narg)) /* avoid extra test when d is not 0 */ |
| 91 | type_error(L, narg, "number"); | ||
| 92 | return d; | ||
| 92 | } | 93 | } |
| 93 | 94 | ||
| 94 | 95 | ||
| 95 | double luaL_opt_number (lua_State *L, int narg, double def) { | 96 | double luaL_opt_number (lua_State *L, int narg, double def) { |
| 96 | if (lua_isnull(L, narg)) return def; | 97 | if (lua_isnull(L, narg)) return def; |
| 97 | else { | 98 | else return luaL_check_number(L, narg); |
| 98 | if (!lua_isnumber(L, narg)) type_error(L, narg, "number"); | ||
| 99 | return lua_tonumber(L, narg); | ||
| 100 | } | ||
| 101 | } | 99 | } |
| 102 | 100 | ||
| 103 | 101 | ||
