From 4c94d8cc2cbeac74ae3618b1322c3f3d3ec166ea Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Sun, 27 Dec 1998 18:25:20 -0200 Subject: 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. --- lobject.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 59 insertions(+), 11 deletions(-) (limited to 'lobject.c') diff --git a/lobject.c b/lobject.c index 8e1742dd..5c0e3dd8 100644 --- a/lobject.c +++ b/lobject.c @@ -1,9 +1,10 @@ /* -** $Id: lobject.c,v 1.12 1998/06/18 16:57:03 roberto Exp roberto $ +** $Id: lobject.c,v 1.13 1998/06/19 16:14:09 roberto Exp $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ +#include #include #include "lobject.h" @@ -64,20 +65,67 @@ void luaO_insertlist (GCnode *root, GCnode *node) node->marked = 0; } + #ifdef OLD_ANSI -void luaO_memup (void *dest, void *src, int size) -{ - char *d = dest; - char *s = src; - while (size--) d[size]=s[size]; +void luaO_memup (void *dest, void *src, int size) { + while (size--) + ((char *)dest)[size]=((char *)src)[size]; } -void luaO_memdown (void *dest, void *src, int size) -{ - char *d = dest; - char *s = src; +void luaO_memdown (void *dest, void *src, int size) { int i; - for (i=0; i>=1) { + if (e & 1) res *= exp; + exp *= exp; + } + return res; +} + + +double luaO_str2d (char *s) { + double a = 0.0; + int point = 0; + if (!isdigit((unsigned char)*s) && !isdigit((unsigned char)*(s+1))) + return -1; /* no digit before or after decimal point */ + while (isdigit((unsigned char)*s)) { + a = 10.0*a + (*(s++)-'0'); + } + if (*s == '.') s++; + while (isdigit((unsigned char)*s)) { + a = 10.0*a + (*(s++)-'0'); + point++; + } + if (toupper((unsigned char)*s) == 'E') { + int e = 0; + int sig = 1; + s++; + if (*s == '+') s++; + else if (*s == '-') { + s++; + sig = -1; + } + if (!isdigit((unsigned char)*s)) return -1; /* no digit in expoent part? */ + do { + e = 10*e + (*(s++)-'0'); + } while (isdigit((unsigned char)*s)); + point -= sig*e; + } + while (isspace((unsigned char)*s)) s++; + if (*s != '\0') return -1; /* invalid trailing characters? */ + if (point > 0) + a /= expten(point); + else if (point < 0) + a *= expten(-point); + return a; +} + -- cgit v1.2.3-55-g6feb