diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-12-27 18:25:20 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-12-27 18:25:20 -0200 |
| commit | 4c94d8cc2cbeac74ae3618b1322c3f3d3ec166ea (patch) | |
| tree | fd95bdb49c335389a222547b072b60d04d23043f /lobject.c | |
| parent | d2de2d5eda5779832a6e6ce4de1f1d8aa4f01047 (diff) | |
| download | lua-4c94d8cc2cbeac74ae3618b1322c3f3d3ec166ea.tar.gz lua-4c94d8cc2cbeac74ae3618b1322c3f3d3ec166ea.tar.bz2 lua-4c94d8cc2cbeac74ae3618b1322c3f3d3ec166ea.zip | |
new function "luaO_str2d" to convert strings to numbers, because
old "lex" algorithm had aproximation errors, but strtod (and atof
and scanf) are too slow.
Diffstat (limited to 'lobject.c')
| -rw-r--r-- | lobject.c | 70 |
1 files changed, 59 insertions, 11 deletions
| @@ -1,9 +1,10 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lobject.c,v 1.12 1998/06/18 16:57:03 roberto Exp roberto $ | 2 | ** $Id: lobject.c,v 1.13 1998/06/19 16:14:09 roberto Exp $ |
| 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 | */ |
| 6 | 6 | ||
| 7 | #include <ctype.h> | ||
| 7 | #include <stdlib.h> | 8 | #include <stdlib.h> |
| 8 | 9 | ||
| 9 | #include "lobject.h" | 10 | #include "lobject.h" |
| @@ -64,20 +65,67 @@ void luaO_insertlist (GCnode *root, GCnode *node) | |||
| 64 | node->marked = 0; | 65 | node->marked = 0; |
| 65 | } | 66 | } |
| 66 | 67 | ||
| 68 | |||
| 67 | #ifdef OLD_ANSI | 69 | #ifdef OLD_ANSI |
| 68 | void luaO_memup (void *dest, void *src, int size) | 70 | void luaO_memup (void *dest, void *src, int size) { |
| 69 | { | 71 | while (size--) |
| 70 | char *d = dest; | 72 | ((char *)dest)[size]=((char *)src)[size]; |
| 71 | char *s = src; | ||
| 72 | while (size--) d[size]=s[size]; | ||
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | void luaO_memdown (void *dest, void *src, int size) | 75 | void luaO_memdown (void *dest, void *src, int size) { |
| 76 | { | ||
| 77 | char *d = dest; | ||
| 78 | char *s = src; | ||
| 79 | int i; | 76 | int i; |
| 80 | for (i=0; i<size; i++) d[i]=s[i]; | 77 | for (i=0; i<size; i++) |
| 78 | ((char *)dest)[i]=((char *)src)[i]; | ||
| 81 | } | 79 | } |
| 82 | #endif | 80 | #endif |
| 83 | 81 | ||
| 82 | |||
| 83 | |||
| 84 | static double expten (unsigned int e) { | ||
| 85 | double exp = 10.0; | ||
| 86 | double res = 1.0; | ||
| 87 | for (; e; e>>=1) { | ||
| 88 | if (e & 1) res *= exp; | ||
| 89 | exp *= exp; | ||
| 90 | } | ||
| 91 | return res; | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | double luaO_str2d (char *s) { | ||
| 96 | double a = 0.0; | ||
| 97 | int point = 0; | ||
| 98 | if (!isdigit((unsigned char)*s) && !isdigit((unsigned char)*(s+1))) | ||
| 99 | return -1; /* no digit before or after decimal point */ | ||
| 100 | while (isdigit((unsigned char)*s)) { | ||
| 101 | a = 10.0*a + (*(s++)-'0'); | ||
| 102 | } | ||
| 103 | if (*s == '.') s++; | ||
| 104 | while (isdigit((unsigned char)*s)) { | ||
| 105 | a = 10.0*a + (*(s++)-'0'); | ||
| 106 | point++; | ||
| 107 | } | ||
| 108 | if (toupper((unsigned char)*s) == 'E') { | ||
| 109 | int e = 0; | ||
| 110 | int sig = 1; | ||
| 111 | s++; | ||
| 112 | if (*s == '+') s++; | ||
| 113 | else if (*s == '-') { | ||
| 114 | s++; | ||
| 115 | sig = -1; | ||
| 116 | } | ||
| 117 | if (!isdigit((unsigned char)*s)) return -1; /* no digit in expoent part? */ | ||
| 118 | do { | ||
| 119 | e = 10*e + (*(s++)-'0'); | ||
| 120 | } while (isdigit((unsigned char)*s)); | ||
| 121 | point -= sig*e; | ||
| 122 | } | ||
| 123 | while (isspace((unsigned char)*s)) s++; | ||
| 124 | if (*s != '\0') return -1; /* invalid trailing characters? */ | ||
| 125 | if (point > 0) | ||
| 126 | a /= expten(point); | ||
| 127 | else if (point < 0) | ||
| 128 | a *= expten(-point); | ||
| 129 | return a; | ||
| 130 | } | ||
| 131 | |||
