aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-09-08 17:45:18 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-09-08 17:45:18 -0300
commitae3ecc2d4a5b108efbb2bd3db9f8e902f28de21d (patch)
tree7841fe86bc11cb386d8b51c7020ce308dc0ec175
parent2e13cd77ab3b3719ef139e4786328be813fb10e0 (diff)
downloadlua-ae3ecc2d4a5b108efbb2bd3db9f8e902f28de21d.tar.gz
lua-ae3ecc2d4a5b108efbb2bd3db9f8e902f28de21d.tar.bz2
lua-ae3ecc2d4a5b108efbb2bd3db9f8e902f28de21d.zip
tonumber'e1' and tonumber(' ', x), for x!=10, gave 0 instead of nil.
-rw-r--r--bugs4
-rw-r--r--lbuiltin.c16
-rw-r--r--lobject.c31
3 files changed, 27 insertions, 24 deletions
diff --git a/bugs b/bugs
index 210bd9b2..114f3ef6 100644
--- a/bugs
+++ b/bugs
@@ -118,3 +118,7 @@ Thu Sep 2 10:07:20 EST 1999
118could realloc f->consts. 118could realloc f->consts.
119(by Supratik Champati; since 3.2 beta) 119(by Supratik Champati; since 3.2 beta)
120 120
121** lobject.c / lbuiltin.c
122Wed Sep 8 17:41:54 EST 1999
123>> tonumber'e1' and tonumber(' ', x), for x!=10, gave 0 instead of nil.
124(since 3.1)
diff --git a/lbuiltin.c b/lbuiltin.c
index 384f77c0..b7365f66 100644
--- a/lbuiltin.c
+++ b/lbuiltin.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbuiltin.c,v 1.60 1999/07/22 19:35:41 roberto Exp roberto $ 2** $Id: lbuiltin.c,v 1.61 1999/08/16 20:52:00 roberto Exp roberto $
3** Built-in functions 3** Built-in functions
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -146,13 +146,15 @@ static void luaB_tonumber (void) {
146 else lua_pushnil(); /* not a number */ 146 else lua_pushnil(); /* not a number */
147 } 147 }
148 else { 148 else {
149 char *s; 149 const char *s1 = luaL_check_string(1);
150 long n; 150 char *s2;
151 real n;
151 luaL_arg_check(0 <= base && base <= 36, 2, "base out of range"); 152 luaL_arg_check(0 <= base && base <= 36, 2, "base out of range");
152 n = strtol(luaL_check_string(1), &s, base); 153 n = strtoul(s1, &s2, base);
153 while (isspace((unsigned char)*s)) s++; /* skip trailing spaces */ 154 if (s1 == s2) return; /* no valid digits: return nil */
154 if (*s) lua_pushnil(); /* invalid format: return nil */ 155 while (isspace((unsigned char)*s2)) s2++; /* skip trailing spaces */
155 else lua_pushnumber(n); 156 if (*s2) return; /* invalid trailing character: return nil */
157 lua_pushnumber(n);
156 } 158 }
157} 159}
158 160
diff --git a/lobject.c b/lobject.c
index 2eaeb91d..926c8c6c 100644
--- a/lobject.c
+++ b/lobject.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.c,v 1.21 1999/09/06 13:55:09 roberto Exp roberto $ 2** $Id: lobject.c,v 1.22 1999/09/06 20:19:22 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*/
@@ -91,37 +91,34 @@ static double expten (unsigned int e) {
91int luaO_str2d (const char *s, real *result) { /* LUA_NUMBER */ 91int luaO_str2d (const char *s, real *result) { /* LUA_NUMBER */
92 double a = 0.0; 92 double a = 0.0;
93 int point = 0; /* number of decimal digits */ 93 int point = 0; /* number of decimal digits */
94 int sig = 1; 94 int sig;
95 int valid = 0; /* check whether number has at least one valid digit */
96 while (isspace((unsigned char)*s)) s++; 95 while (isspace((unsigned char)*s)) s++;
97 if (*s == '-') { 96 sig = 1;
98 s++; 97 switch (*s) {
99 sig = -1; 98 case '-': sig = -1; /* go through */
99 case '+': s++;
100 } 100 }
101 else if (*s == '+') s++; 101 if (! (isdigit((unsigned char)*s) ||
102 while (isdigit((unsigned char)*s)) { 102 (*s == '.' && isdigit((unsigned char)*(s+1)))))
103 return 0; /* not (at least one digit before or after the point) */
104 while (isdigit((unsigned char)*s))
103 a = 10.0*a + (*(s++)-'0'); 105 a = 10.0*a + (*(s++)-'0');
104 valid = 1;
105 }
106 if (*s == '.') { 106 if (*s == '.') {
107 s++; 107 s++;
108 while (isdigit((unsigned char)*s)) { 108 while (isdigit((unsigned char)*s)) {
109 a = 10.0*a + (*(s++)-'0'); 109 a = 10.0*a + (*(s++)-'0');
110 point++; 110 point++;
111 valid = 1;
112 } 111 }
113 } 112 }
114 if (!valid) return 0;
115 a *= sig; 113 a *= sig;
116 if (toupper((unsigned char)*s) == 'E') { 114 if (toupper((unsigned char)*s) == 'E') {
117 int e = 0; 115 int e = 0;
118 sig = 1;
119 s++; 116 s++;
120 if (*s == '-') { 117 sig = 1;
121 s++; 118 switch (*s) {
122 sig = -1; 119 case '-': sig = -1; /* go through */
120 case '+': s++;
123 } 121 }
124 else if (*s == '+') s++;
125 if (!isdigit((unsigned char)*s)) return 0; /* no digit in the exponent? */ 122 if (!isdigit((unsigned char)*s)) return 0; /* no digit in the exponent? */
126 do { 123 do {
127 e = 10*e + (*(s++)-'0'); 124 e = 10*e + (*(s++)-'0');