From owner-svn-soc-all@FreeBSD.ORG Tue Jun 10 00:33:33 2014 Return-Path: Delivered-To: svn-soc-all@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 05D04871 for ; Tue, 10 Jun 2014 00:33:33 +0000 (UTC) Received: from socsvn.freebsd.org (socsvn.freebsd.org [IPv6:2001:1900:2254:206a::50:2]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id DBCA6232F for ; Tue, 10 Jun 2014 00:33:32 +0000 (UTC) Received: from socsvn.freebsd.org ([127.0.1.124]) by socsvn.freebsd.org (8.14.8/8.14.8) with ESMTP id s5A0XW0Q087820 for ; Tue, 10 Jun 2014 00:33:32 GMT (envelope-from pedrosouza@FreeBSD.org) Received: (from www@localhost) by socsvn.freebsd.org (8.14.8/8.14.8/Submit) id s5A0XWwA087662 for svn-soc-all@FreeBSD.org; Tue, 10 Jun 2014 00:33:32 GMT (envelope-from pedrosouza@FreeBSD.org) Date: Tue, 10 Jun 2014 00:33:32 GMT Message-Id: <201406100033.s5A0XWwA087662@socsvn.freebsd.org> X-Authentication-Warning: socsvn.freebsd.org: www set sender to pedrosouza@FreeBSD.org using -f From: pedrosouza@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r269341 - soc2014/pedrosouza/lua_loader/head/sys/lua/src MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-soc-all@freebsd.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: SVN commit messages for the entire Summer of Code repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 10 Jun 2014 00:33:33 -0000 Author: pedrosouza Date: Tue Jun 10 00:33:31 2014 New Revision: 269341 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=269341 Log: Improve dtostr function Modified: soc2014/pedrosouza/lua_loader/head/sys/lua/src/lstd.c soc2014/pedrosouza/lua_loader/head/sys/lua/src/lstd.h Modified: soc2014/pedrosouza/lua_loader/head/sys/lua/src/lstd.c ============================================================================== --- soc2014/pedrosouza/lua_loader/head/sys/lua/src/lstd.c Mon Jun 9 23:11:50 2014 (r269340) +++ soc2014/pedrosouza/lua_loader/head/sys/lua/src/lstd.c Tue Jun 10 00:33:31 2014 (r269341) @@ -170,49 +170,96 @@ return ret * exp_m; } -char * dtostr(double value, char * buf) +int dtostr(double v, char * str) { - int c, i; + int exp = 0; + int i; + long long n; + double e = 1; + char * ptr; char tmp[20]; - char *ptr, *bb = buf; - long long num = (long long) value; - double frac = value - num; + char *buf = str; - tmp[19] = 0; - ptr = &tmp[19]; - while (num > 0) + if (v < 0) { - c = num % 10; - num = num / 10; - *(--ptr) = '0' + c; + *buf++ = '-'; + v = -v; } - if (ptr == &tmp[19]) - *buf++ = '0'; - else + if (v <= e) { - while (*ptr) + while (v < e) { - *buf++ = *ptr++; + --exp; + e *= 0.1; } + } else + { + while (v > e) + { + ++exp; + e *= 10; + } + --exp; + e /= 10; } - if (abs(frac) > 0.0000001) + if (exp > 9 || exp < -9) + { + v /= e; + } else + { + exp = 0; + } + + n = (long long)v; + v -= n; + ptr = &tmp[19]; + *ptr = 0; + + do { - *buf++ = 0; - return bb; - } else + i = n % 10; + n /= 10; + *(--ptr) = i + '0'; + } while (n > 0); + + while (*ptr != 0) *buf++ = *ptr++; + + if (v != 0) { + ptr = buf; *buf++ = '.'; - for (i = 0; i < 7; ++i) + for (i = 0; i < 17; ++i) + { + v *= 10; + n = (long long)v; + *buf++ = '0' + n; + ptr = n > 0 ? buf : ptr; + v -= n; + } + buf = ptr; + } + + if (exp != 0) + { + *buf++ = 'e'; + if (exp < 0) + { + *buf++ = '-'; + exp = -exp; + } + ptr = &tmp[19]; + *ptr = 0; + while (exp > 0) { - frac *= 10; - c = (long long)frac; - *buf++ = '0' + c; - frac -= c; + i = exp % 10; + exp /= 10; + *(--ptr) = '0' + i; } - *buf = 0; + while (*ptr != 0) *buf++ = *ptr++; } - return bb; + *buf = 0; + return buf - str; } #endif //BOOT_LUA \ No newline at end of file Modified: soc2014/pedrosouza/lua_loader/head/sys/lua/src/lstd.h ============================================================================== --- soc2014/pedrosouza/lua_loader/head/sys/lua/src/lstd.h Mon Jun 9 23:11:50 2014 (r269340) +++ soc2014/pedrosouza/lua_loader/head/sys/lua/src/lstd.h Tue Jun 10 00:33:31 2014 (r269341) @@ -57,7 +57,7 @@ double strtod(const char *string, char **endPtr); -char * dtostr(double value, char * buf); +int dtostr(double v, char * str); #endif #endif //LSTD_H