diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-09-06 10:55:09 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-09-06 10:55:09 -0300 |
| commit | 82699d0c4f497c651fe550f73b2810facf0f0446 (patch) | |
| tree | ec21c77e9731436f9f2c62b5f2ea9e3b567b63e7 | |
| parent | 88866208f079b5ece7b7a1ea899a4d354c7695db (diff) | |
| download | lua-82699d0c4f497c651fe550f73b2810facf0f0446.tar.gz lua-82699d0c4f497c651fe550f73b2810facf0f0446.tar.bz2 lua-82699d0c4f497c651fe550f73b2810facf0f0446.zip | |
new interface for `luaO_strtod', which now checks signal, too.
| -rw-r--r-- | lobject.c | 27 |
1 files changed, 20 insertions, 7 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.c,v 1.19 1999/04/13 19:28:49 roberto Exp roberto $ | 2 | ** $Id: lobject.c,v 1.20 1999/08/16 20:52:00 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 | */ |
| @@ -88,40 +88,53 @@ static double expten (unsigned int e) { | |||
| 88 | } | 88 | } |
| 89 | 89 | ||
| 90 | 90 | ||
| 91 | double luaO_str2d (const char *s) { /* LUA_NUMBER */ | 91 | int luaO_str2d (const char *s, real *result) { /* LUA_NUMBER */ |
| 92 | double a = 0.0; | 92 | double a = 0.0; |
| 93 | int point = 0; | 93 | int point = 0; /* number of decimal digits */ |
| 94 | int sig = 1; | ||
| 95 | int valid = 0; /* check whether number has at least one valid digit */ | ||
| 96 | while (isspace((unsigned char)*s)) s++; | ||
| 97 | if (*s == '-') { | ||
| 98 | s++; | ||
| 99 | sig = -1; | ||
| 100 | } | ||
| 101 | else if (*s == '+') s++; | ||
| 94 | while (isdigit((unsigned char)*s)) { | 102 | while (isdigit((unsigned char)*s)) { |
| 95 | a = 10.0*a + (*(s++)-'0'); | 103 | a = 10.0*a + (*(s++)-'0'); |
| 104 | valid = 1; | ||
| 96 | } | 105 | } |
| 97 | if (*s == '.') { | 106 | if (*s == '.') { |
| 98 | s++; | 107 | s++; |
| 99 | while (isdigit((unsigned char)*s)) { | 108 | while (isdigit((unsigned char)*s)) { |
| 100 | a = 10.0*a + (*(s++)-'0'); | 109 | a = 10.0*a + (*(s++)-'0'); |
| 101 | point++; | 110 | point++; |
| 111 | valid = 1; | ||
| 102 | } | 112 | } |
| 103 | } | 113 | } |
| 114 | if (!valid) return 0; | ||
| 115 | a *= sig; | ||
| 104 | if (toupper((unsigned char)*s) == 'E') { | 116 | if (toupper((unsigned char)*s) == 'E') { |
| 105 | int e = 0; | 117 | int e = 0; |
| 106 | int sig = 1; | 118 | sig = 1; |
| 107 | s++; | 119 | s++; |
| 108 | if (*s == '-') { | 120 | if (*s == '-') { |
| 109 | s++; | 121 | s++; |
| 110 | sig = -1; | 122 | sig = -1; |
| 111 | } | 123 | } |
| 112 | else if (*s == '+') s++; | 124 | else if (*s == '+') s++; |
| 113 | if (!isdigit((unsigned char)*s)) return -1; /* no digit in the exponent? */ | 125 | if (!isdigit((unsigned char)*s)) return 0; /* no digit in the exponent? */ |
| 114 | do { | 126 | do { |
| 115 | e = 10*e + (*(s++)-'0'); | 127 | e = 10*e + (*(s++)-'0'); |
| 116 | } while (isdigit((unsigned char)*s)); | 128 | } while (isdigit((unsigned char)*s)); |
| 117 | point -= sig*e; | 129 | point -= sig*e; |
| 118 | } | 130 | } |
| 119 | while (isspace((unsigned char)*s)) s++; | 131 | while (isspace((unsigned char)*s)) s++; |
| 120 | if (*s != '\0') return -1; /* invalid trailing characters? */ | 132 | if (*s != '\0') return 0; /* invalid trailing characters? */ |
| 121 | if (point > 0) | 133 | if (point > 0) |
| 122 | a /= expten(point); | 134 | a /= expten(point); |
| 123 | else if (point < 0) | 135 | else if (point < 0) |
| 124 | a *= expten(-point); | 136 | a *= expten(-point); |
| 125 | return a; | 137 | *result = a; |
| 138 | return 1; | ||
| 126 | } | 139 | } |
| 127 | 140 | ||
