'string.format("%q")' writes 'math.mininteger' in hexa, to ensure
it is read back as an integer
This commit is contained in:
31
lstrlib.c
31
lstrlib.c
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
** $Id: lstrlib.c,v 1.249 2016/05/13 19:09:46 roberto Exp roberto $
|
** $Id: lstrlib.c,v 1.250 2016/05/18 18:19:51 roberto Exp roberto $
|
||||||
** Standard library for string operations and pattern-matching
|
** Standard library for string operations and pattern-matching
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
@@ -928,20 +928,14 @@ static void addquoted (luaL_Buffer *b, const char *s, size_t len) {
|
|||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Convert a Lua number to a string in floating-point hexadecimal
|
** Ensures the 'buff' string uses a dot as the radix character.
|
||||||
** format. Ensures the resulting string uses a dot as the radix
|
|
||||||
** character.
|
|
||||||
*/
|
*/
|
||||||
static void addliteralnum (lua_State *L, luaL_Buffer *b, lua_Number n) {
|
static void checkdp (char *buff, int nb) {
|
||||||
char *buff = luaL_prepbuffsize(b, MAX_ITEM);
|
|
||||||
int nb = lua_number2strx(L, buff, MAX_ITEM,
|
|
||||||
"%" LUA_NUMBER_FRMLEN "a", n);
|
|
||||||
if (memchr(buff, '.', nb) == NULL) { /* no dot? */
|
if (memchr(buff, '.', nb) == NULL) { /* no dot? */
|
||||||
char point = lua_getlocaledecpoint(); /* try locale point */
|
char point = lua_getlocaledecpoint(); /* try locale point */
|
||||||
char *ppoint = memchr(buff, point, nb);
|
char *ppoint = memchr(buff, point, nb);
|
||||||
if (ppoint) *ppoint = '.'; /* change it to a dot */
|
if (ppoint) *ppoint = '.'; /* change it to a dot */
|
||||||
}
|
}
|
||||||
luaL_addsize(b, nb);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -954,12 +948,23 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case LUA_TNUMBER: {
|
case LUA_TNUMBER: {
|
||||||
if (!lua_isinteger(L, arg)) { /* write floats as hexa ('%a') */
|
char *buff = luaL_prepbuffsize(b, MAX_ITEM);
|
||||||
addliteralnum(L, b, lua_tonumber(L, arg));
|
int nb;
|
||||||
|
if (!lua_isinteger(L, arg)) { /* float? */
|
||||||
|
lua_Number n = lua_tonumber(L, arg); /* write as hexa ('%a') */
|
||||||
|
nb = lua_number2strx(L, buff, MAX_ITEM, "%" LUA_NUMBER_FRMLEN "a", n);
|
||||||
|
checkdp(buff, nb); /* ensure it uses a dot */
|
||||||
|
}
|
||||||
|
else { /* integers */
|
||||||
|
lua_Integer n = lua_tointeger(L, arg);
|
||||||
|
const char *format = (n == LUA_MININTEGER) /* corner case? */
|
||||||
|
? "0x%" LUA_INTEGER_FRMLEN "x" /* use hexa */
|
||||||
|
: LUA_INTEGER_FMT; /* else use default format */
|
||||||
|
nb = l_sprintf(buff, MAX_ITEM, format, n);
|
||||||
|
}
|
||||||
|
luaL_addsize(b, nb);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
/* else integers; write in "native" format */
|
|
||||||
} /* FALLTHROUGH */
|
|
||||||
case LUA_TNIL: case LUA_TBOOLEAN: {
|
case LUA_TNIL: case LUA_TBOOLEAN: {
|
||||||
luaL_tolstring(L, arg, NULL);
|
luaL_tolstring(L, arg, NULL);
|
||||||
luaL_addvalue(b);
|
luaL_addvalue(b);
|
||||||
|
|||||||
Reference in New Issue
Block a user