diff options
Diffstat (limited to 'lbaselib.c')
-rw-r--r-- | lbaselib.c | 24 |
1 files changed, 12 insertions, 12 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.284 2014/02/14 16:45:38 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.285 2014/03/12 20:57:40 roberto Exp roberto $ |
3 | ** Basic library | 3 | ** Basic library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -45,31 +45,31 @@ static int luaB_print (lua_State *L) { | |||
45 | 45 | ||
46 | #define SPACECHARS " \f\n\r\t\v" | 46 | #define SPACECHARS " \f\n\r\t\v" |
47 | 47 | ||
48 | static int b_str2int (const char *s, const char *e, int base, lua_Integer *pn) { | 48 | static const char *b_str2int (const char *s, int base, lua_Integer *pn) { |
49 | lua_Unsigned n = 0; | 49 | lua_Unsigned n = 0; |
50 | int neg = 0; | 50 | int neg = 0; |
51 | s += strspn(s, SPACECHARS); /* skip initial spaces */ | 51 | s += strspn(s, SPACECHARS); /* skip initial spaces */ |
52 | if (*s == '-') { s++; neg = 1; } /* handle signal */ | 52 | if (*s == '-') { s++; neg = 1; } /* handle signal */ |
53 | else if (*s == '+') s++; | 53 | else if (*s == '+') s++; |
54 | if (!isalnum((unsigned char)*s)) /* no digit? */ | 54 | if (!isalnum((unsigned char)*s)) /* no digit? */ |
55 | return 0; | 55 | return NULL; |
56 | do { | 56 | do { |
57 | int digit = (isdigit((unsigned char)*s)) ? *s - '0' | 57 | int digit = (isdigit((unsigned char)*s)) ? *s - '0' |
58 | : toupper((unsigned char)*s) - 'A' + 10; | 58 | : toupper((unsigned char)*s) - 'A' + 10; |
59 | if (digit >= base) return 0; /* invalid numeral */ | 59 | if (digit >= base) return NULL; /* invalid numeral */ |
60 | n = n * base + digit; | 60 | n = n * base + digit; |
61 | s++; | 61 | s++; |
62 | } while (isalnum((unsigned char)*s)); | 62 | } while (isalnum((unsigned char)*s)); |
63 | s += strspn(s, SPACECHARS); /* skip trailing spaces */ | 63 | s += strspn(s, SPACECHARS); /* skip trailing spaces */ |
64 | if (s != e) /* invalid trailing characters? */ | 64 | if (*s != '\0') /* invalid trailing characters? */ |
65 | return 0; | 65 | return NULL; |
66 | *pn = (lua_Integer)((neg) ? (0u - n) : n); | 66 | *pn = (lua_Integer)((neg) ? (0u - n) : n); |
67 | return 1; | 67 | return s; |
68 | } | 68 | } |
69 | 69 | ||
70 | 70 | ||
71 | static int luaB_tonumber (lua_State *L) { | 71 | static int luaB_tonumber (lua_State *L) { |
72 | if (lua_isnoneornil(L, 2)) { /* standard conversion */ | 72 | if (lua_isnoneornil(L, 2)) { /* standard conversion? */ |
73 | luaL_checkany(L, 1); | 73 | luaL_checkany(L, 1); |
74 | if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */ | 74 | if (lua_type(L, 1) == LUA_TNUMBER) { /* already a number? */ |
75 | lua_settop(L, 1); /* yes; return it */ | 75 | lua_settop(L, 1); /* yes; return it */ |
@@ -78,20 +78,20 @@ static int luaB_tonumber (lua_State *L) { | |||
78 | else { | 78 | else { |
79 | size_t l; | 79 | size_t l; |
80 | const char *s = lua_tolstring(L, 1, &l); | 80 | const char *s = lua_tolstring(L, 1, &l); |
81 | if (s != NULL && lua_strtonum(L, s, l)) /* can convert to a number? */ | 81 | if (s != NULL && lua_strtonum(L, s) == l + 1) |
82 | return 1; | 82 | return 1; /* successful conversion to number */ |
83 | /* else not a number */ | 83 | /* else not a number */ |
84 | } | 84 | } |
85 | } | 85 | } |
86 | else { | 86 | else { |
87 | size_t l; | 87 | size_t l; |
88 | const char *s; | 88 | const char *s; |
89 | lua_Integer n; | 89 | lua_Integer n = 0; /* to avoid warnings */ |
90 | int base = luaL_checkint(L, 2); | 90 | int base = luaL_checkint(L, 2); |
91 | luaL_checktype(L, 1, LUA_TSTRING); /* before 'luaL_checklstring'! */ | 91 | luaL_checktype(L, 1, LUA_TSTRING); /* before 'luaL_checklstring'! */ |
92 | s = luaL_checklstring(L, 1, &l); | 92 | s = luaL_checklstring(L, 1, &l); |
93 | luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); | 93 | luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); |
94 | if (b_str2int(s, s + l, base, &n)) { | 94 | if (b_str2int(s, base, &n) == s + l) { |
95 | lua_pushinteger(L, n); | 95 | lua_pushinteger(L, n); |
96 | return 1; | 96 | return 1; |
97 | } /* else not a number */ | 97 | } /* else not a number */ |