Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 69b5f7a

Browse files
committed
some details in 'luaO_int2fb' + more consistent use of the locale
decimal point
1 parent ae76c39 commit 69b5f7a

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

lobject.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lobject.c,v 2.102 2015/02/05 17:15:33 roberto Exp roberto $
2+
** $Id: lobject.c,v 2.103 2015/03/28 19:14:47 roberto Exp roberto $
33
** Some generic functions over Lua objects
44
** See Copyright Notice in lua.h
55
*/
@@ -10,6 +10,7 @@
1010
#include "lprefix.h"
1111

1212

13+
#include <locale.h>
1314
#include <math.h>
1415
#include <stdarg.h>
1516
#include <stdio.h>
@@ -40,8 +41,12 @@ LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT};
4041
int luaO_int2fb (unsigned int x) {
4142
int e = 0; /* exponent */
4243
if (x < 8) return x;
43-
while (x >= 0x10) {
44-
x = (x+1) >> 1;
44+
while (x >= (8 << 4)) { /* coarse steps */
45+
x = (x + 0xf) >> 4; /* x = ceil(x / 16) */
46+
e += 4;
47+
}
48+
while (x >= (8 << 1)) { /* fine steps */
49+
x = (x + 1) >> 1; /* x = ceil(x / 2) */
4550
e++;
4651
}
4752
return ((e+1) << 3) | (cast_int(x) - 8);
@@ -56,8 +61,11 @@ int luaO_fb2int (int x) {
5661
}
5762

5863

64+
/*
65+
** Computes ceil(log2(x))
66+
*/
5967
int luaO_ceillog2 (unsigned int x) {
60-
static const lu_byte log_2[256] = {
68+
static const lu_byte log_2[256] = { /* log_2[i] = ceil(log2(i - 1)) */
6169
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,
6270
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,
6371
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) {
173181
** Lua's implementation for 'lua_strx2number'
174182
** ===================================================================
175183
*/
184+
176185
#if !defined(lua_strx2number)
177186

178187
/* maximum number of significant digits to read (to avoid overflows
@@ -184,29 +193,30 @@ static int isneg (const char **s) {
184193
** C99 specification for 'strtod'
185194
*/
186195
static lua_Number lua_strx2number (const char *s, char **endptr) {
196+
int dot = lua_getlocaledecpoint();
187197
lua_Number r = 0.0; /* result (accumulator) */
188198
int sigdig = 0; /* number of significant digits */
189199
int nosigdig = 0; /* number of non-significant digits */
190200
int e = 0; /* exponent correction */
191201
int neg; /* 1 if number is negative */
192-
int dot = 0; /* true after seen a dot */
202+
int hasdot = 0; /* true after seen a dot */
193203
*endptr = cast(char *, s); /* nothing is valid yet */
194204
while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
195205
neg = isneg(&s); /* check signal */
196206
if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
197207
return 0.0; /* invalid format (no '0x') */
198208
for (s += 2; ; s++) { /* skip '0x' and read numeral */
199-
if (*s == '.') {
200-
if (dot) break; /* second dot? stop loop */
201-
else dot = 1;
209+
if (*s == dot) {
210+
if (hasdot) break; /* second dot? stop loop */
211+
else hasdot = 1;
202212
}
203213
else if (lisxdigit(cast_uchar(*s))) {
204214
if (sigdig == 0 && *s == '0') /* non-significant digit (zero)? */
205215
nosigdig++;
206216
else if (++sigdig <= MAXSIGDIG) /* can read it without overflow? */
207217
r = (r * cast_num(16.0)) + luaO_hexavalue(*s);
208218
else e++; /* too many digits; ignore, but still count for exponent */
209-
if (dot) e--; /* decimal digit? correct exponent */
219+
if (hasdot) e--; /* decimal digit? correct exponent */
210220
}
211221
else break; /* neither a dot nor a digit */
212222
}
@@ -328,7 +338,7 @@ void luaO_tostring (lua_State *L, StkId obj) {
328338
len = lua_number2str(buff, fltvalue(obj));
329339
#if !defined(LUA_COMPAT_FLOATSTRING)
330340
if (buff[strspn(buff, "-0123456789")] == '\0') { /* looks like an int? */
331-
buff[len++] = '.';
341+
buff[len++] = lua_getlocaledecpoint();
332342
buff[len++] = '0'; /* adds '.0' to result */
333343
}
334344
#endif

0 commit comments

Comments
 (0)