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 /lvm.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 'lvm.c')
-rw-r--r-- | lvm.c | 43 |
1 files changed, 25 insertions, 18 deletions
@@ -1,11 +1,12 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 1.32 1998/12/03 15:45:15 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.33 1998/12/24 14:57:23 roberto Exp $ |
3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
6 | 6 | ||
7 | 7 | ||
8 | #include <ctype.h> | 8 | #include <ctype.h> |
9 | #include <limits.h> | ||
9 | #include <stdio.h> | 10 | #include <stdio.h> |
10 | #include <stdlib.h> | 11 | #include <stdlib.h> |
11 | #include <string.h> | 12 | #include <string.h> |
@@ -15,6 +16,7 @@ | |||
15 | #include "lfunc.h" | 16 | #include "lfunc.h" |
16 | #include "lgc.h" | 17 | #include "lgc.h" |
17 | #include "lmem.h" | 18 | #include "lmem.h" |
19 | #include "lobject.h" | ||
18 | #include "lopcodes.h" | 20 | #include "lopcodes.h" |
19 | #include "lstate.h" | 21 | #include "lstate.h" |
20 | #include "lstring.h" | 22 | #include "lstring.h" |
@@ -26,7 +28,6 @@ | |||
26 | 28 | ||
27 | #ifdef OLD_ANSI | 29 | #ifdef OLD_ANSI |
28 | #define strcoll(a,b) strcmp(a,b) | 30 | #define strcoll(a,b) strcmp(a,b) |
29 | double strtod(); | ||
30 | #endif | 31 | #endif |
31 | 32 | ||
32 | 33 | ||
@@ -40,11 +41,10 @@ double strtod(); | |||
40 | 41 | ||
41 | 42 | ||
42 | 43 | ||
43 | static TaggedString *strconc (TaggedString *l, TaggedString *r) | 44 | static TaggedString *strconc (TaggedString *l, TaggedString *r) { |
44 | { | 45 | long nl = l->u.s.len; |
45 | size_t nl = l->u.s.len; | 46 | long nr = r->u.s.len; |
46 | size_t nr = r->u.s.len; | 47 | char *buffer = luaL_openspace(nl+nr); |
47 | char *buffer = luaL_openspace(nl+nr+1); | ||
48 | memcpy(buffer, l->str, nl); | 48 | memcpy(buffer, l->str, nl); |
49 | memcpy(buffer+nl, r->str, nr); | 49 | memcpy(buffer+nl, r->str, nr); |
50 | return luaS_newlstr(buffer, nl+nr); | 50 | return luaS_newlstr(buffer, nl+nr); |
@@ -56,29 +56,36 @@ int luaV_tonumber (TObject *obj) { | |||
56 | if (ttype(obj) != LUA_T_STRING) | 56 | if (ttype(obj) != LUA_T_STRING) |
57 | return 1; | 57 | return 1; |
58 | else { | 58 | else { |
59 | char *e; | 59 | double t; |
60 | double t = strtod(svalue(obj), &e); | 60 | char *e = svalue(obj); |
61 | while (isspace(*e)) e++; | 61 | int sig = 1; |
62 | if (*e != '\0') return 2; | 62 | while (isspace((unsigned char)*e)) e++; |
63 | nvalue(obj) = (real)t; | 63 | if (*e == '+') e++; |
64 | else if (*e == '-') { | ||
65 | e++; | ||
66 | sig = -1; | ||
67 | } | ||
68 | t = luaO_str2d(e); | ||
69 | if (t<0) return 2; | ||
70 | nvalue(obj) = (real)t*sig; | ||
64 | ttype(obj) = LUA_T_NUMBER; | 71 | ttype(obj) = LUA_T_NUMBER; |
65 | return 0; | 72 | return 0; |
66 | } | 73 | } |
67 | } | 74 | } |
68 | 75 | ||
69 | 76 | ||
70 | int luaV_tostring (TObject *obj) | 77 | int luaV_tostring (TObject *obj) { |
71 | { /* LUA_NUMBER */ | 78 | /* LUA_NUMBER */ |
72 | if (ttype(obj) != LUA_T_NUMBER) | 79 | if (ttype(obj) != LUA_T_NUMBER) |
73 | return 1; | 80 | return 1; |
74 | else { | 81 | else { |
75 | char s[60]; | 82 | char s[60]; |
76 | real f = nvalue(obj); | 83 | real f = nvalue(obj); |
77 | int i; | 84 | long i; |
78 | if ((real)(-MAX_INT) <= f && f <= (real)MAX_INT && (real)(i=(int)f) == f) | 85 | if ((real)LONG_MIN <= f && f <= (real)LONG_MAX && (real)(i=(long)f) == f) |
79 | sprintf (s, "%d", i); | 86 | sprintf(s, "%ld", i); |
80 | else | 87 | else |
81 | sprintf (s, NUMBER_FMT, nvalue(obj)); | 88 | sprintf(s, "%g", (double)nvalue(obj)); |
82 | tsvalue(obj) = luaS_new(s); | 89 | tsvalue(obj) = luaS_new(s); |
83 | ttype(obj) = LUA_T_STRING; | 90 | ttype(obj) = LUA_T_STRING; |
84 | return 0; | 91 | return 0; |