commit 36e8771076705d33261a88a3c144967b1840671a
parent 27f09415e38b00f16013649d3d91c430a6cc33ff
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Tue, 14 May 2013 12:58:39 -0300
'luaO_str2int' more generic: accepts white spaces around the numeral
and handles signal
Diffstat:
3 files changed, 27 insertions(+), 15 deletions(-)
diff --git a/llex.c b/llex.c
@@ -1,5 +1,5 @@
/*
-** $Id: llex.c,v 2.64 2013/04/16 18:46:28 roberto Exp roberto $
+** $Id: llex.c,v 2.65 2013/04/26 13:07:53 roberto Exp roberto $
** Lexical Analyzer
** See Copyright Notice in lua.h
*/
@@ -248,7 +248,8 @@ static int read_numeral (LexState *ls, SemInfo *seminfo, int isf) {
}
save(ls, '\0');
if (!isf) {
- if (!luaO_str2int(luaZ_buffer(ls->buff), &seminfo->i))
+ if (!luaO_str2int(luaZ_buffer(ls->buff), luaZ_bufflen(ls->buff) - 1,
+ &seminfo->i))
lexerror(ls, "malformed number", TK_INT);
return TK_INT;
}
diff --git a/lobject.c b/lobject.c
@@ -1,5 +1,5 @@
/*
-** $Id: lobject.c,v 2.61 2013/04/29 16:57:28 roberto Exp roberto $
+** $Id: lobject.c,v 2.62 2013/05/02 12:37:24 roberto Exp roberto $
** Some generic functions over Lua objects
** See Copyright Notice in lua.h
*/
@@ -133,11 +133,6 @@ int luaO_hexavalue (int c) {
}
-#if !defined(lua_strx2number)
-
-#include <math.h>
-
-
static int isneg (const char **s) {
if (**s == '-') { (*s)++; return 1; }
else if (**s == '+') (*s)++;
@@ -145,6 +140,11 @@ static int isneg (const char **s) {
}
+#if !defined(lua_strx2number)
+
+#include <math.h>
+
+
static lua_Number readhexa (const char **s, lua_Number r, int *count) {
for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */
r = (r * cast_num(16.0)) + cast_num(luaO_hexavalue(cast_uchar(**s)));
@@ -212,21 +212,32 @@ int luaO_str2d (const char *s, size_t len, lua_Number *result) {
}
-int luaO_str2int (const char *s, lua_Integer *result) {
+int luaO_str2int (const char *s, size_t len, lua_Integer *result) {
+ const char *ends = s + len;
lua_Unsigned a = 0;
+ int empty = 1;
+ int neg;
+ while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
+ neg = isneg(&s);
if (s[0] == '0' &&
(s[1] == 'x' || s[1] == 'X')) { /* hexa? */
s += 2; /* skip '0x' */
- for (; lisxdigit(cast_uchar(*s)); s++)
+ for (; lisxdigit(cast_uchar(*s)); s++) {
a = a * 16 + luaO_hexavalue(cast_uchar(*s));
+ empty = 0;
+ }
}
else { /* decimal */
- for (; lisdigit(cast_uchar(*s)); s++)
+ for (; lisdigit(cast_uchar(*s)); s++) {
a = a * 10 + luaO_hexavalue(cast_uchar(*s));
+ empty = 0;
+ }
}
- if (*s != '\0') return 0; /* something wrong in the numeral */
+ while (lisspace(cast_uchar(*s))) s++; /* skip trailing spaces */
+ if (empty || s != ends) return 0; /* something wrong in the numeral */
else {
- *result = cast(lua_Integer, a);
+ if (neg) *result = -cast(lua_Integer, a);
+ else *result = cast(lua_Integer, a);
return 1;
}
}
diff --git a/lobject.h b/lobject.h
@@ -1,5 +1,5 @@
/*
-** $Id: lobject.h,v 2.76 2013/05/02 12:37:24 roberto Exp roberto $
+** $Id: lobject.h,v 2.77 2013/05/06 17:17:09 roberto Exp roberto $
** Type definitions for Lua objects
** See Copyright Notice in lua.h
*/
@@ -496,7 +496,7 @@ LUAI_FUNC int luaO_ceillog2 (unsigned int x);
LUAI_FUNC void luaO_arith (lua_State *L, int op, const TValue *p1,
const TValue *p2, TValue *res);
LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);
-LUAI_FUNC int luaO_str2int (const char *s, lua_Integer *result);
+LUAI_FUNC int luaO_str2int (const char *s, size_t len, lua_Integer *result);
LUAI_FUNC int luaO_hexavalue (int c);
LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
va_list argp);