Browse Source

tonumber'e1' and tonumber(' ', x), for x!=10, gave 0 instead of nil.

Roberto Ierusalimschy 26 years ago
parent
commit
ae3ecc2d4a
3 changed files with 27 additions and 24 deletions
  1. 4 0
      bugs
  2. 9 7
      lbuiltin.c
  3. 14 17
      lobject.c

+ 4 - 0
bugs

@@ -118,3 +118,7 @@ Thu Sep  2 10:07:20 EST 1999
 could realloc f->consts.
 could realloc f->consts.
 (by Supratik Champati; since 3.2 beta)
 (by Supratik Champati; since 3.2 beta)
 
 
+** lobject.c / lbuiltin.c
+Wed Sep  8 17:41:54 EST 1999
+>> tonumber'e1' and tonumber('  ', x), for x!=10, gave 0 instead of nil.
+(since 3.1)

+ 9 - 7
lbuiltin.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lbuiltin.c,v 1.60 1999/07/22 19:35:41 roberto Exp roberto $
+** $Id: lbuiltin.c,v 1.61 1999/08/16 20:52:00 roberto Exp roberto $
 ** Built-in functions
 ** Built-in functions
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -146,13 +146,15 @@ static void luaB_tonumber (void) {
     else lua_pushnil();  /* not a number */
     else lua_pushnil();  /* not a number */
   }
   }
   else {
   else {
-    char *s;
-    long n;
+    const char *s1 = luaL_check_string(1);
+    char *s2;
+    real n;
     luaL_arg_check(0 <= base && base <= 36, 2, "base out of range");
     luaL_arg_check(0 <= base && base <= 36, 2, "base out of range");
-    n = strtol(luaL_check_string(1), &s, base);
-    while (isspace((unsigned char)*s)) s++;  /* skip trailing spaces */
-    if (*s) lua_pushnil();  /* invalid format: return nil */
-    else lua_pushnumber(n);
+    n = strtoul(s1, &s2, base);
+    if (s1 == s2) return;  /* no valid digits: return nil */
+    while (isspace((unsigned char)*s2)) s2++;  /* skip trailing spaces */
+    if (*s2) return;  /* invalid trailing character: return nil */
+    lua_pushnumber(n);
   }
   }
 }
 }
 
 

+ 14 - 17
lobject.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lobject.c,v 1.21 1999/09/06 13:55:09 roberto Exp roberto $
+** $Id: lobject.c,v 1.22 1999/09/06 20:19:22 roberto Exp roberto $
 ** Some generic functions over Lua objects
 ** Some generic functions over Lua objects
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -91,37 +91,34 @@ static double expten (unsigned int e) {
 int luaO_str2d (const char *s, real *result) {  /* LUA_NUMBER */
 int luaO_str2d (const char *s, real *result) {  /* LUA_NUMBER */
   double a = 0.0;
   double a = 0.0;
   int point = 0;  /* number of decimal digits */
   int point = 0;  /* number of decimal digits */
-  int sig = 1;
-  int valid = 0;  /* check whether number has at least one valid digit */
+  int sig;
   while (isspace((unsigned char)*s)) s++;
   while (isspace((unsigned char)*s)) s++;
-  if (*s == '-') {
-    s++;
-    sig = -1;
+  sig = 1;
+  switch (*s) {
+    case '-': sig = -1;  /* go through */
+    case '+': s++;
   }
   }
-  else if (*s == '+') s++;
-  while (isdigit((unsigned char)*s)) {
+  if (! (isdigit((unsigned char)*s) ||
+          (*s == '.' && isdigit((unsigned char)*(s+1)))))
+    return 0;  /* not (at least one digit before or after the point) */
+  while (isdigit((unsigned char)*s))
     a = 10.0*a + (*(s++)-'0');
     a = 10.0*a + (*(s++)-'0');
-    valid = 1;
-  }
   if (*s == '.') {
   if (*s == '.') {
     s++;
     s++;
     while (isdigit((unsigned char)*s)) {
     while (isdigit((unsigned char)*s)) {
       a = 10.0*a + (*(s++)-'0');
       a = 10.0*a + (*(s++)-'0');
       point++;
       point++;
-      valid = 1;
     }
     }
   }
   }
-  if (!valid) return 0;
   a *= sig;
   a *= sig;
   if (toupper((unsigned char)*s) == 'E') {
   if (toupper((unsigned char)*s) == 'E') {
     int e = 0;
     int e = 0;
-    sig = 1;
     s++;
     s++;
-    if (*s == '-') {
-      s++;
-      sig = -1;
+    sig = 1;
+    switch (*s) {
+      case '-': sig = -1;  /* go through */
+      case '+': s++;
     }
     }
-    else if (*s == '+') s++;
     if (!isdigit((unsigned char)*s)) return 0;  /* no digit in the exponent? */
     if (!isdigit((unsigned char)*s)) return 0;  /* no digit in the exponent? */
     do {
     do {
       e = 10*e + (*(s++)-'0');
       e = 10*e + (*(s++)-'0');