some details in 'luaO_int2fb' + more consistent use of the locale

decimal point
This commit is contained in:
Roberto Ierusalimschy 2015-04-11 15:30:08 -03:00
parent ae76c39712
commit 69b5f7a410
1 changed files with 20 additions and 10 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lobject.c,v 2.102 2015/02/05 17:15:33 roberto Exp roberto $ ** $Id: lobject.c,v 2.103 2015/03/28 19:14:47 roberto Exp roberto $
** Some generic functions over Lua objects ** Some generic functions over Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -10,6 +10,7 @@
#include "lprefix.h" #include "lprefix.h"
#include <locale.h>
#include <math.h> #include <math.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
@ -40,8 +41,12 @@ LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT};
int luaO_int2fb (unsigned int x) { int luaO_int2fb (unsigned int x) {
int e = 0; /* exponent */ int e = 0; /* exponent */
if (x < 8) return x; if (x < 8) return x;
while (x >= 0x10) { while (x >= (8 << 4)) { /* coarse steps */
x = (x+1) >> 1; x = (x + 0xf) >> 4; /* x = ceil(x / 16) */
e += 4;
}
while (x >= (8 << 1)) { /* fine steps */
x = (x + 1) >> 1; /* x = ceil(x / 2) */
e++; e++;
} }
return ((e+1) << 3) | (cast_int(x) - 8); return ((e+1) << 3) | (cast_int(x) - 8);
@ -56,8 +61,11 @@ int luaO_fb2int (int x) {
} }
/*
** Computes ceil(log2(x))
*/
int luaO_ceillog2 (unsigned int x) { int luaO_ceillog2 (unsigned int x) {
static const lu_byte log_2[256] = { static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */
0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
@ -173,6 +181,7 @@ static int isneg (const char **s) {
** Lua's implementation for 'lua_strx2number' ** Lua's implementation for 'lua_strx2number'
** =================================================================== ** ===================================================================
*/ */
#if !defined(lua_strx2number) #if !defined(lua_strx2number)
/* maximum number of significant digits to read (to avoid overflows /* maximum number of significant digits to read (to avoid overflows
@ -184,21 +193,22 @@ static int isneg (const char **s) {
** C99 specification for 'strtod' ** C99 specification for 'strtod'
*/ */
static lua_Number lua_strx2number (const char *s, char **endptr) { static lua_Number lua_strx2number (const char *s, char **endptr) {
int dot = lua_getlocaledecpoint();
lua_Number r = 0.0; /* result (accumulator) */ lua_Number r = 0.0; /* result (accumulator) */
int sigdig = 0; /* number of significant digits */ int sigdig = 0; /* number of significant digits */
int nosigdig = 0; /* number of non-significant digits */ int nosigdig = 0; /* number of non-significant digits */
int e = 0; /* exponent correction */ int e = 0; /* exponent correction */
int neg; /* 1 if number is negative */ int neg; /* 1 if number is negative */
int dot = 0; /* true after seen a dot */ int hasdot = 0; /* true after seen a dot */
*endptr = cast(char *, s); /* nothing is valid yet */ *endptr = cast(char *, s); /* nothing is valid yet */
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */ while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
neg = isneg(&s); /* check signal */ neg = isneg(&s); /* check signal */
if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */ if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
return 0.0; /* invalid format (no '0x') */ return 0.0; /* invalid format (no '0x') */
for (s += 2; ; s++) { /* skip '0x' and read numeral */ for (s += 2; ; s++) { /* skip '0x' and read numeral */
if (*s == '.') { if (*s == dot) {
if (dot) break; /* second dot? stop loop */ if (hasdot) break; /* second dot? stop loop */
else dot = 1; else hasdot = 1;
} }
else if (lisxdigit(cast_uchar(*s))) { else if (lisxdigit(cast_uchar(*s))) {
if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */ if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */
@ -206,7 +216,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */ else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */
r = (r * cast_num(16.0)) + luaO_hexavalue(*s); r = (r * cast_num(16.0)) + luaO_hexavalue(*s);
else e++; /* too many digits; ignore, but still count for exponent */ else e++; /* too many digits; ignore, but still count for exponent */
if (dot) e--; /* decimal digit? correct exponent */ if (hasdot) e--; /* decimal digit? correct exponent */
} }
else break; /* neither a dot nor a digit */ else break; /* neither a dot nor a digit */
} }
@ -328,7 +338,7 @@ void luaO_tostring (lua_State *L, StkId obj) {
len = lua_number2str(buff, fltvalue(obj)); len = lua_number2str(buff, fltvalue(obj));
#if !defined(LUA_COMPAT_FLOATSTRING) #if !defined(LUA_COMPAT_FLOATSTRING)
if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */ if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
buff[len++] = '.'; buff[len++] = lua_getlocaledecpoint();
buff[len++] = '0'; /* adds '.0' to result */ buff[len++] = '0'; /* adds '.0' to result */
} }
#endif #endif