aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lapi.c12
-rw-r--r--lbaselib.c24
-rw-r--r--llex.c4
-rw-r--r--lobject.c24
-rw-r--r--lobject.h4
-rw-r--r--lua.h4
-rw-r--r--lvm.c8
7 files changed, 39 insertions, 41 deletions
diff --git a/lapi.c b/lapi.c
index a81d3ecc..27f51220 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 2.206 2014/04/29 18:14:16 roberto Exp roberto $ 2** $Id: lapi.c,v 2.207 2014/04/30 16:48:44 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -333,13 +333,11 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
333} 333}
334 334
335 335
336LUA_API int lua_strtonum (lua_State *L, const char *s, size_t len) { 336LUA_API size_t lua_strtonum (lua_State *L, const char *s) {
337 if (!luaO_str2num(s, len, L->top)) 337 size_t sz = luaO_str2num(s, L->top);
338 return 0; /* conversion failed */ 338 if (sz != 0)
339 else {
340 api_incr_top(L); 339 api_incr_top(L);
341 return 1; 340 return sz;
342 }
343} 341}
344 342
345 343
diff --git a/lbaselib.c b/lbaselib.c
index e0e6a66c..5b22a1fc 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -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
48static int b_str2int (const char *s, const char *e, int base, lua_Integer *pn) { 48static 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
71static int luaB_tonumber (lua_State *L) { 71static 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 */
diff --git a/llex.c b/llex.c
index ff3c3781..328a4833 100644
--- a/llex.c
+++ b/llex.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: llex.c,v 2.74 2014/02/14 15:23:51 roberto Exp roberto $ 2** $Id: llex.c,v 2.75 2014/04/30 16:48:44 roberto Exp roberto $
3** Lexical Analyzer 3** Lexical Analyzer
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -210,7 +210,7 @@ static void buffreplace (LexState *ls, char from, char to) {
210#endif 210#endif
211 211
212 212
213#define buff2num(b,o) luaO_str2num(luaZ_buffer(b), luaZ_bufflen(b) - 1, o) 213#define buff2num(b,o) (luaO_str2num(luaZ_buffer(b), o) != 0)
214 214
215/* 215/*
216** in case of format error, try to change decimal point separator to 216** in case of format error, try to change decimal point separator to
diff --git a/lobject.c b/lobject.c
index a95948f5..bc594482 100644
--- a/lobject.c
+++ b/lobject.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.c,v 2.82 2014/04/29 18:14:16 roberto Exp roberto $ 2** $Id: lobject.c,v 2.83 2014/04/30 16:48:44 roberto Exp roberto $
3** Some generic functions over Lua objects 3** Some generic functions over Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -254,22 +254,21 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
254/* }====================================================== */ 254/* }====================================================== */
255 255
256 256
257static int l_str2d (const char *s, size_t len, lua_Number *result) { 257static const char *l_str2d (const char *s, lua_Number *result) {
258 char *endptr; 258 char *endptr;
259 if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */ 259 if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */
260 return 0; 260 return NULL;
261 else if (strpbrk(s, "xX")) /* hexa? */ 261 else if (strpbrk(s, "xX")) /* hexa? */
262 *result = lua_strx2number(s, &endptr); 262 *result = lua_strx2number(s, &endptr);
263 else 263 else
264 *result = lua_str2number(s, &endptr); 264 *result = lua_str2number(s, &endptr);
265 if (endptr == s) return 0; /* nothing recognized */ 265 if (endptr == s) return 0; /* nothing recognized */
266 while (lisspace(cast_uchar(*endptr))) endptr++; 266 while (lisspace(cast_uchar(*endptr))) endptr++;
267 return (endptr == s + len); /* OK if no trailing characters */ 267 return (*endptr == '\0' ? endptr : NULL); /* OK if no trailing characters */
268} 268}
269 269
270 270
271static int l_str2int (const char *s, size_t len, lua_Integer *result) { 271static const char *l_str2int (const char *s, lua_Integer *result) {
272 const char *ends = s + len;
273 lua_Unsigned a = 0; 272 lua_Unsigned a = 0;
274 int empty = 1; 273 int empty = 1;
275 int neg; 274 int neg;
@@ -290,25 +289,26 @@ static int l_str2int (const char *s, size_t len, lua_Integer *result) {
290 } 289 }
291 } 290 }
292 while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */ 291 while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */
293 if (empty || s != ends) return 0; /* something wrong in the numeral */ 292 if (empty || *s != '\0') return NULL; /* something wrong in the numeral */
294 else { 293 else {
295 *result = l_castU2S((neg) ? 0u - a : a); 294 *result = l_castU2S((neg) ? 0u - a : a);
296 return 1; 295 return s;
297 } 296 }
298} 297}
299 298
300 299
301int luaO_str2num (const char *s, size_t len, TValue *o) { 300size_t luaO_str2num (const char *s, TValue *o) {
302 lua_Integer i; lua_Number n; 301 lua_Integer i; lua_Number n;
303 if (l_str2int(s, len, &i)) { /* try as an integer */ 302 const char *e;
303 if ((e = l_str2int(s, &i)) != NULL) { /* try as an integer */
304 setivalue(o, i); 304 setivalue(o, i);
305 } 305 }
306 else if (l_str2d(s, len, &n)) { /* else try as a float */ 306 else if ((e = l_str2d(s, &n)) != NULL) { /* else try as a float */
307 setfltvalue(o, n); 307 setfltvalue(o, n);
308 } 308 }
309 else 309 else
310 return 0; /* conversion failed */ 310 return 0; /* conversion failed */
311 return 1; /* success */ 311 return (e - s + 1); /* success; return string size */
312} 312}
313 313
314 314
diff --git a/lobject.h b/lobject.h
index 0653d062..e239f8c6 100644
--- a/lobject.h
+++ b/lobject.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.h,v 2.87 2014/04/29 18:14:16 roberto Exp roberto $ 2** $Id: lobject.h,v 2.88 2014/04/30 16:48:44 roberto Exp roberto $
3** Type definitions for Lua objects 3** Type definitions for Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -500,7 +500,7 @@ LUAI_FUNC int luaO_utf8esc (char *buff, unsigned int x);
500LUAI_FUNC int luaO_ceillog2 (unsigned int x); 500LUAI_FUNC int luaO_ceillog2 (unsigned int x);
501LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1, 501LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
502 const TValue *p2, TValue *res); 502 const TValue *p2, TValue *res);
503LUAI_FUNC int luaO_str2num (const char *s, size_t len, TValue *o); 503LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
504LUAI_FUNC int luaO_hexavalue (int c); 504LUAI_FUNC int luaO_hexavalue (int c);
505LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, 505LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
506 va_list argp); 506 va_list argp);
diff --git a/lua.h b/lua.h
index 76413a2d..7705421e 100644
--- a/lua.h
+++ b/lua.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lua.h,v 1.301 2014/03/12 20:57:40 roberto Exp roberto $ 2** $Id: lua.h,v 1.302 2014/03/20 19:42:35 roberto Exp roberto $
3** Lua - A Scripting Language 3** Lua - A Scripting Language
4** Lua.org, PUC-Rio, Brazil (http://www.lua.org) 4** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
5** See Copyright Notice at the end of this file 5** See Copyright Notice at the end of this file
@@ -311,7 +311,7 @@ LUA_API int (lua_next) (lua_State *L, int idx);
311LUA_API void (lua_concat) (lua_State *L, int n); 311LUA_API void (lua_concat) (lua_State *L, int n);
312LUA_API void (lua_len) (lua_State *L, int idx); 312LUA_API void (lua_len) (lua_State *L, int idx);
313 313
314LUA_API int (lua_strtonum) (lua_State *L, const char *s, size_t len); 314LUA_API size_t (lua_strtonum) (lua_State *L, const char *s);
315 315
316LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud); 316LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
317LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud); 317LUA_API void (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);
diff --git a/lvm.c b/lvm.c
index 3eed5535..026e943d 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 2.203 2014/04/30 16:50:16 roberto Exp roberto $ 2** $Id: lvm.c,v 2.204 2014/04/30 19:29:51 roberto Exp roberto $
3** Lua virtual machine 3** Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -70,7 +70,7 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
70 return 1; 70 return 1;
71 } 71 }
72 else if (ttisstring(obj) && 72 else if (ttisstring(obj) &&
73 luaO_str2num(svalue(obj), tsvalue(obj)->len, &v)) { 73 luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) {
74 obj = &v; 74 obj = &v;
75 goto again; /* convert result from 'luaO_str2num' to a float */ 75 goto again; /* convert result from 'luaO_str2num' to a float */
76 } 76 }
@@ -109,8 +109,8 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) {
109 *p = ivalue(obj); 109 *p = ivalue(obj);
110 return 1; 110 return 1;
111 } 111 }
112 if (ttisstring(obj) && 112 else if (ttisstring(obj) &&
113 luaO_str2num(svalue(obj), tsvalue(obj)->len, &v)) { 113 luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) {
114 obj = &v; 114 obj = &v;
115 goto again; /* convert result from 'luaO_str2num' to an integer */ 115 goto again; /* convert result from 'luaO_str2num' to an integer */
116 } 116 }