diff options
| -rw-r--r-- | lobject.c | 63 |
1 files changed, 7 insertions, 56 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.c,v 1.49 2000/09/29 12:42:13 roberto Exp roberto $ | 2 | ** $Id: lobject.c,v 1.50 2000/10/02 20:10:55 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 | */ |
| @@ -66,62 +66,13 @@ char *luaO_openspace (lua_State *L, size_t n) { | |||
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | 68 | ||
| 69 | static double expten (unsigned int e) { | ||
| 70 | double exp = 10.0; | ||
| 71 | double res = 1.0; | ||
| 72 | for (; e; e>>=1) { | ||
| 73 | if (e & 1) res *= exp; | ||
| 74 | exp *= exp; | ||
| 75 | } | ||
| 76 | return res; | ||
| 77 | } | ||
| 78 | |||
| 79 | |||
| 80 | int luaO_str2d (const char *s, Number *result) { /* LUA_NUMBER */ | 69 | int luaO_str2d (const char *s, Number *result) { /* LUA_NUMBER */ |
| 81 | double a = 0.0; | 70 | char *endptr; |
| 82 | int point = 0; /* number of decimal digits */ | 71 | Number res = lua_str2number(s, &endptr); |
| 83 | int sig; | 72 | if (endptr == s) return 0; /* no conversion */ |
| 84 | while (isspace((unsigned char)*s)) s++; | 73 | while (isspace((unsigned char)*endptr)) endptr++; |
| 85 | sig = 0; | 74 | if (*endptr != '\0') return 0; /* invalid trailing characters? */ |
| 86 | switch (*s) { | 75 | *result = res; |
| 87 | case '-': sig = 1; /* go through */ | ||
| 88 | case '+': s++; | ||
| 89 | } | ||
| 90 | if (! (isdigit((unsigned char)*s) || | ||
| 91 | (*s == '.' && isdigit((unsigned char)*(s+1))))) | ||
| 92 | return 0; /* not (at least one digit before or after the point) */ | ||
| 93 | while (isdigit((unsigned char)*s)) | ||
| 94 | a = 10.0*a + (*(s++)-'0'); | ||
| 95 | if (*s == '.') { | ||
| 96 | s++; | ||
| 97 | while (isdigit((unsigned char)*s)) { | ||
| 98 | a = 10.0*a + (*(s++)-'0'); | ||
| 99 | point++; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | if (sig) a = -a; | ||
| 103 | if (*s == 'e' || *s == 'E') { | ||
| 104 | int e = 0; | ||
| 105 | s++; | ||
| 106 | sig = 0; | ||
| 107 | switch (*s) { | ||
| 108 | case '-': sig = 1; /* go through */ | ||
| 109 | case '+': s++; | ||
| 110 | } | ||
| 111 | if (!isdigit((unsigned char)*s)) return 0; /* no digit in the exponent? */ | ||
| 112 | do { | ||
| 113 | e = 10*e + (*(s++)-'0'); | ||
| 114 | } while (isdigit((unsigned char)*s)); | ||
| 115 | if (sig) e = -e; | ||
| 116 | point -= e; | ||
| 117 | } | ||
| 118 | while (isspace((unsigned char)*s)) s++; | ||
| 119 | if (*s != '\0') return 0; /* invalid trailing characters? */ | ||
| 120 | if (point != 0) { | ||
| 121 | if (point > 0) a /= expten(point); | ||
| 122 | else a *= expten(-point); | ||
| 123 | } | ||
| 124 | *result = a; | ||
| 125 | return 1; | 76 | return 1; |
| 126 | } | 77 | } |
| 127 | 78 | ||
