浏览代码

'lua_strtonum' (and 'luaO_str2num') now return string size, instead of
receiving it

Roberto Ierusalimschy 11 年之前
父节点
当前提交
c549d4fe64
共有 7 个文件被更改,包括 39 次插入41 次删除
  1. 5 7
      lapi.c
  2. 12 12
      lbaselib.c
  3. 2 2
      llex.c
  4. 12 12
      lobject.c
  5. 2 2
      lobject.h
  6. 2 2
      lua.h
  7. 4 4
      lvm.c

+ 5 - 7
lapi.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lapi.c,v 2.206 2014/04/29 18:14:16 roberto Exp roberto $
+** $Id: lapi.c,v 2.207 2014/04/30 16:48:44 roberto Exp roberto $
 ** Lua API
 ** See Copyright Notice in lua.h
 */
@@ -333,13 +333,11 @@ LUA_API int lua_compare (lua_State *L, int index1, int index2, int op) {
 }
 
 
-LUA_API int lua_strtonum (lua_State *L, const char *s, size_t len) {
-  if (!luaO_str2num(s, len, L->top))
-    return 0;  /* conversion failed */
-  else {
+LUA_API size_t lua_strtonum (lua_State *L, const char *s) {
+  size_t sz = luaO_str2num(s, L->top);
+  if (sz != 0)
     api_incr_top(L);
-    return 1;
-  }
+  return sz;
 }
 
 

+ 12 - 12
lbaselib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lbaselib.c,v 1.284 2014/02/14 16:45:38 roberto Exp roberto $
+** $Id: lbaselib.c,v 1.285 2014/03/12 20:57:40 roberto Exp roberto $
 ** Basic library
 ** See Copyright Notice in lua.h
 */
@@ -45,31 +45,31 @@ static int luaB_print (lua_State *L) {
 
 #define SPACECHARS	" \f\n\r\t\v"
 
-static int b_str2int (const char *s, const char *e, int base, lua_Integer *pn) {
+static const char *b_str2int (const char *s, int base, lua_Integer *pn) {
   lua_Unsigned n = 0;
   int neg = 0;
   s += strspn(s, SPACECHARS);  /* skip initial spaces */
   if (*s == '-') { s++; neg = 1; }  /* handle signal */
   else if (*s == '+') s++;
   if (!isalnum((unsigned char)*s))  /* no digit? */
-    return 0;
+    return NULL;
   do {
     int digit = (isdigit((unsigned char)*s)) ? *s - '0'
                    : toupper((unsigned char)*s) - 'A' + 10;
-    if (digit >= base) return 0;  /* invalid numeral */
+    if (digit >= base) return NULL;  /* invalid numeral */
     n = n * base + digit;
     s++;
   } while (isalnum((unsigned char)*s));
   s += strspn(s, SPACECHARS);  /* skip trailing spaces */
-  if (s != e)  /* invalid trailing characters? */
-    return 0;
+  if (*s != '\0')  /* invalid trailing characters? */
+    return NULL;
   *pn = (lua_Integer)((neg) ? (0u - n) : n);
-  return 1;
+  return s;
 }
 
 
 static int luaB_tonumber (lua_State *L) {
-  if (lua_isnoneornil(L, 2)) {  /* standard conversion */
+  if (lua_isnoneornil(L, 2)) {  /* standard conversion? */
     luaL_checkany(L, 1);
     if (lua_type(L, 1) == LUA_TNUMBER) {  /* already a number? */
       lua_settop(L, 1);  /* yes; return it */
@@ -78,20 +78,20 @@ static int luaB_tonumber (lua_State *L) {
     else {
       size_t l;
       const char *s = lua_tolstring(L, 1, &l);
-      if (s != NULL && lua_strtonum(L, s, l))  /* can convert to a number? */
-        return 1;
+      if (s != NULL && lua_strtonum(L, s) == l + 1)
+        return 1;  /* successful conversion to number */
       /* else not a number */
     }
   }
   else {
     size_t l;
     const char *s;
-    lua_Integer n;
+    lua_Integer n = 0;  /* to avoid warnings */
     int base = luaL_checkint(L, 2);
     luaL_checktype(L, 1, LUA_TSTRING);  /* before 'luaL_checklstring'! */
     s = luaL_checklstring(L, 1, &l);
     luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
-    if (b_str2int(s, s + l, base, &n)) {
+    if (b_str2int(s, base, &n) == s + l) {
       lua_pushinteger(L, n);
       return 1;
     }  /* else not a number */

+ 2 - 2
llex.c

@@ -1,5 +1,5 @@
 /*
-** $Id: llex.c,v 2.74 2014/02/14 15:23:51 roberto Exp roberto $
+** $Id: llex.c,v 2.75 2014/04/30 16:48:44 roberto Exp roberto $
 ** Lexical Analyzer
 ** See Copyright Notice in lua.h
 */
@@ -210,7 +210,7 @@ static void buffreplace (LexState *ls, char from, char to) {
 #endif
 
 
-#define buff2num(b,o)	luaO_str2num(luaZ_buffer(b), luaZ_bufflen(b) - 1, o)
+#define buff2num(b,o)	(luaO_str2num(luaZ_buffer(b), o) != 0)
 
 /*
 ** in case of format error, try to change decimal point separator to

+ 12 - 12
lobject.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lobject.c,v 2.82 2014/04/29 18:14:16 roberto Exp roberto $
+** $Id: lobject.c,v 2.83 2014/04/30 16:48:44 roberto Exp roberto $
 ** Some generic functions over Lua objects
 ** See Copyright Notice in lua.h
 */
@@ -254,22 +254,21 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
 /* }====================================================== */
 
 
-static int l_str2d (const char *s, size_t len, lua_Number *result) {
+static const char *l_str2d (const char *s, lua_Number *result) {
   char *endptr;
   if (strpbrk(s, "nN"))  /* reject 'inf' and 'nan' */
-    return 0;
+    return NULL;
   else if (strpbrk(s, "xX"))  /* hexa? */
     *result = lua_strx2number(s, &endptr);
   else
     *result = lua_str2number(s, &endptr);
   if (endptr == s) return 0;  /* nothing recognized */
   while (lisspace(cast_uchar(*endptr))) endptr++;
-  return (endptr == s + len);  /* OK if no trailing characters */
+  return (*endptr == '\0' ? endptr : NULL);  /* OK if no trailing characters */
 }
 
 
-static int l_str2int (const char *s, size_t len, lua_Integer *result) {
-  const char *ends = s + len;
+static const char *l_str2int (const char *s, lua_Integer *result) {
   lua_Unsigned a = 0;
   int empty = 1;
   int neg;
@@ -290,25 +289,26 @@ static int l_str2int (const char *s, size_t len, lua_Integer *result) {
     }
   }
   while (lisspace(cast_uchar(*s))) s++;  /* skip trailing spaces */
-  if (empty || s != ends) return 0;  /* something wrong in the numeral */
+  if (empty || *s != '\0') return NULL;  /* something wrong in the numeral */
   else {
     *result = l_castU2S((neg) ? 0u - a : a);
-    return 1;
+    return s;
   }
 }
 
 
-int luaO_str2num (const char *s, size_t len, TValue *o) {
+size_t luaO_str2num (const char *s, TValue *o) {
   lua_Integer i; lua_Number n;
-  if (l_str2int(s, len, &i)) {  /* try as an integer */
+  const char *e;
+  if ((e = l_str2int(s, &i)) != NULL) {  /* try as an integer */
     setivalue(o, i);
   }
-  else if (l_str2d(s, len, &n)) {  /* else try as a float */
+  else if ((e = l_str2d(s, &n)) != NULL) {  /* else try as a float */
     setfltvalue(o, n);
   }
   else
     return 0;  /* conversion failed */
-  return 1;  /* success */
+  return (e - s + 1);  /* success; return string size */
 }
 
 

+ 2 - 2
lobject.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lobject.h,v 2.87 2014/04/29 18:14:16 roberto Exp roberto $
+** $Id: lobject.h,v 2.88 2014/04/30 16:48:44 roberto Exp roberto $
 ** Type definitions for Lua objects
 ** See Copyright Notice in lua.h
 */
@@ -500,7 +500,7 @@ LUAI_FUNC int luaO_utf8esc (char *buff, unsigned int x);
 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_str2num (const char *s, size_t len, TValue *o);
+LUAI_FUNC size_t luaO_str2num (const char *s, TValue *o);
 LUAI_FUNC int luaO_hexavalue (int c);
 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
                                                        va_list argp);

+ 2 - 2
lua.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lua.h,v 1.301 2014/03/12 20:57:40 roberto Exp roberto $
+** $Id: lua.h,v 1.302 2014/03/20 19:42:35 roberto Exp roberto $
 ** Lua - A Scripting Language
 ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
 ** See Copyright Notice at the end of this file
@@ -311,7 +311,7 @@ LUA_API int   (lua_next) (lua_State *L, int idx);
 LUA_API void  (lua_concat) (lua_State *L, int n);
 LUA_API void  (lua_len)    (lua_State *L, int idx);
 
-LUA_API int   (lua_strtonum) (lua_State *L, const char *s, size_t len);
+LUA_API size_t   (lua_strtonum) (lua_State *L, const char *s);
 
 LUA_API lua_Alloc (lua_getallocf) (lua_State *L, void **ud);
 LUA_API void      (lua_setallocf) (lua_State *L, lua_Alloc f, void *ud);

+ 4 - 4
lvm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lvm.c,v 2.203 2014/04/30 16:50:16 roberto Exp roberto $
+** $Id: lvm.c,v 2.204 2014/04/30 19:29:51 roberto Exp roberto $
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 */
@@ -70,7 +70,7 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
     return 1;
   }
   else if (ttisstring(obj) &&
-            luaO_str2num(svalue(obj), tsvalue(obj)->len, &v)) {
+            luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) {
     obj = &v;
     goto again;  /* convert result from 'luaO_str2num' to a float */
   }
@@ -109,8 +109,8 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) {
     *p = ivalue(obj);
     return 1;
   }
-  if (ttisstring(obj) &&
-       luaO_str2num(svalue(obj), tsvalue(obj)->len, &v)) {
+  else if (ttisstring(obj) &&
+            luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) {
     obj = &v;
     goto again;  /* convert result from 'luaO_str2num' to an integer */
   }