From 22093f9c6ef0e21244603883eed740d34d9fd63f Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 2 May 2016 10:58:01 -0300 Subject: 'string.format("%q", number)' ensures a dot as decimal point --- lstrlib.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'lstrlib.c') diff --git a/lstrlib.c b/lstrlib.c index f4871212..8a99a011 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.246 2016/04/19 12:34:08 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.247 2016/04/22 16:36:30 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -812,7 +813,6 @@ static int str_gsub (lua_State *L) { ** Hexadecimal floating-point formatter */ -#include #include #define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char)) @@ -927,6 +927,24 @@ static void addquoted (luaL_Buffer *b, const char *s, size_t len) { } +/* +** Convert a Lua number to a string in floating-point hexadecimal +** format. Ensures the resulting string uses a dot as the radix +** character. +*/ +static void addliteralnum (lua_State *L, luaL_Buffer *b, lua_Number n) { + 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? */ + char point = lua_getlocaledecpoint(); /* try locale point */ + char *ppoint = memchr(buff, point, nb); + if (ppoint) *ppoint = '.'; /* change it to a dot */ + } + luaL_addsize(b, nb); +} + + static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { switch (lua_type(L, arg)) { case LUA_TSTRING: { @@ -937,11 +955,7 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { } case LUA_TNUMBER: { if (!lua_isinteger(L, arg)) { /* write floats as hexa ('%a') */ - char *buff = luaL_prepbuffsize(b, MAX_ITEM); - lua_Number n = lua_tonumber(L, arg); - int nb = lua_number2strx(L, buff, MAX_ITEM, - "%" LUA_NUMBER_FRMLEN "a", n); - luaL_addsize(b, nb); + addliteralnum(L, b, lua_tonumber(L, arg)); break; } /* else integers; write in "native" format *//* FALLTHROUGH */ -- cgit v1.2.3-55-g6feb