aboutsummaryrefslogtreecommitdiff
path: root/lbaselib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lbaselib.c')
-rw-r--r--lbaselib.c15
1 files changed, 8 insertions, 7 deletions
diff --git a/lbaselib.c b/lbaselib.c
index 8b034340..a7b6c3ed 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -58,21 +58,22 @@ static int luaB_warn (lua_State *L) {
58 58
59#define SPACECHARS " \f\n\r\t\v" 59#define SPACECHARS " \f\n\r\t\v"
60 60
61static const char *b_str2int (const char *s, int base, lua_Integer *pn) { 61static const char *b_str2int (const char *s, unsigned base, lua_Integer *pn) {
62 lua_Unsigned n = 0; 62 lua_Unsigned n = 0;
63 int neg = 0; 63 int neg = 0;
64 s += strspn(s, SPACECHARS); /* skip initial spaces */ 64 s += strspn(s, SPACECHARS); /* skip initial spaces */
65 if (*s == '-') { s++; neg = 1; } /* handle sign */ 65 if (*s == '-') { s++; neg = 1; } /* handle sign */
66 else if (*s == '+') s++; 66 else if (*s == '+') s++;
67 if (!isalnum((unsigned char)*s)) /* no digit? */ 67 if (!isalnum(cast_uchar(*s))) /* no digit? */
68 return NULL; 68 return NULL;
69 do { 69 do {
70 int digit = (isdigit((unsigned char)*s)) ? *s - '0' 70 unsigned digit = cast_uint(isdigit(cast_uchar(*s))
71 : (toupper((unsigned char)*s) - 'A') + 10; 71 ? *s - '0'
72 : (toupper(cast_uchar(*s)) - 'A') + 10);
72 if (digit >= base) return NULL; /* invalid numeral */ 73 if (digit >= base) return NULL; /* invalid numeral */
73 n = n * base + digit; 74 n = n * base + digit;
74 s++; 75 s++;
75 } while (isalnum((unsigned char)*s)); 76 } while (isalnum(cast_uchar(*s)));
76 s += strspn(s, SPACECHARS); /* skip trailing spaces */ 77 s += strspn(s, SPACECHARS); /* skip trailing spaces */
77 *pn = (lua_Integer)((neg) ? (0u - n) : n); 78 *pn = (lua_Integer)((neg) ? (0u - n) : n);
78 return s; 79 return s;
@@ -102,7 +103,7 @@ static int luaB_tonumber (lua_State *L) {
102 luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */ 103 luaL_checktype(L, 1, LUA_TSTRING); /* no numbers as strings */
103 s = lua_tolstring(L, 1, &l); 104 s = lua_tolstring(L, 1, &l);
104 luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range"); 105 luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
105 if (b_str2int(s, (int)base, &n) == s + l) { 106 if (b_str2int(s, cast_uint(base), &n) == s + l) {
106 lua_pushinteger(L, n); 107 lua_pushinteger(L, n);
107 return 1; 108 return 1;
108 } /* else not a number */ 109 } /* else not a number */
@@ -159,7 +160,7 @@ static int luaB_rawlen (lua_State *L) {
159 int t = lua_type(L, 1); 160 int t = lua_type(L, 1);
160 luaL_argexpected(L, t == LUA_TTABLE || t == LUA_TSTRING, 1, 161 luaL_argexpected(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
161 "table or string"); 162 "table or string");
162 lua_pushinteger(L, lua_rawlen(L, 1)); 163 lua_pushinteger(L, l_castU2S(lua_rawlen(L, 1)));
163 return 1; 164 return 1;
164} 165}
165 166