diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2016-05-20 11:13:21 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2016-05-20 11:13:21 -0300 |
commit | 707b0ba6e2dbfd58cf1167dae0e17975904b18aa (patch) | |
tree | 0f73ee0e6c1d123b6096bd96da2e4381cc1c31a7 /lstrlib.c | |
parent | 10b0b09555920ad2ac186274348e73ed61cede90 (diff) | |
download | lua-707b0ba6e2dbfd58cf1167dae0e17975904b18aa.tar.gz lua-707b0ba6e2dbfd58cf1167dae0e17975904b18aa.tar.bz2 lua-707b0ba6e2dbfd58cf1167dae0e17975904b18aa.zip |
'string.format("%q")' writes 'math.mininteger' in hexa, to ensure
it is read back as an integer
Diffstat (limited to 'lstrlib.c')
-rw-r--r-- | lstrlib.c | 33 |
1 files changed, 19 insertions, 14 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstrlib.c,v 1.249 2016/05/13 19:09:46 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.250 2016/05/18 18:19:51 roberto Exp roberto $ |
3 | ** Standard library for string operations and pattern-matching | 3 | ** Standard library for string operations and pattern-matching |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -928,20 +928,14 @@ static void addquoted (luaL_Buffer *b, const char *s, size_t len) { | |||
928 | 928 | ||
929 | 929 | ||
930 | /* | 930 | /* |
931 | ** Convert a Lua number to a string in floating-point hexadecimal | 931 | ** Ensures the 'buff' string uses a dot as the radix character. |
932 | ** format. Ensures the resulting string uses a dot as the radix | ||
933 | ** character. | ||
934 | */ | 932 | */ |
935 | static void addliteralnum (lua_State *L, luaL_Buffer *b, lua_Number n) { | 933 | static void checkdp (char *buff, int nb) { |
936 | char *buff = luaL_prepbuffsize(b, MAX_ITEM); | ||
937 | int nb = lua_number2strx(L, buff, MAX_ITEM, | ||
938 | "%" LUA_NUMBER_FRMLEN "a", n); | ||
939 | if (memchr(buff, '.', nb) == NULL) { /* no dot? */ | 934 | if (memchr(buff, '.', nb) == NULL) { /* no dot? */ |
940 | char point = lua_getlocaledecpoint(); /* try locale point */ | 935 | char point = lua_getlocaledecpoint(); /* try locale point */ |
941 | char *ppoint = memchr(buff, point, nb); | 936 | char *ppoint = memchr(buff, point, nb); |
942 | if (ppoint) *ppoint = '.'; /* change it to a dot */ | 937 | if (ppoint) *ppoint = '.'; /* change it to a dot */ |
943 | } | 938 | } |
944 | luaL_addsize(b, nb); | ||
945 | } | 939 | } |
946 | 940 | ||
947 | 941 | ||
@@ -954,12 +948,23 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { | |||
954 | break; | 948 | break; |
955 | } | 949 | } |
956 | case LUA_TNUMBER: { | 950 | case LUA_TNUMBER: { |
957 | if (!lua_isinteger(L, arg)) { /* write floats as hexa ('%a') */ | 951 | char *buff = luaL_prepbuffsize(b, MAX_ITEM); |
958 | addliteralnum(L, b, lua_tonumber(L, arg)); | 952 | int nb; |
959 | break; | 953 | if (!lua_isinteger(L, arg)) { /* float? */ |
954 | lua_Number n = lua_tonumber(L, arg); /* write as hexa ('%a') */ | ||
955 | nb = lua_number2strx(L, buff, MAX_ITEM, "%" LUA_NUMBER_FRMLEN "a", n); | ||
956 | checkdp(buff, nb); /* ensure it uses a dot */ | ||
957 | } | ||
958 | else { /* integers */ | ||
959 | lua_Integer n = lua_tointeger(L, arg); | ||
960 | const char *format = (n == LUA_MININTEGER) /* corner case? */ | ||
961 | ? "0x%" LUA_INTEGER_FRMLEN "x" /* use hexa */ | ||
962 | : LUA_INTEGER_FMT; /* else use default format */ | ||
963 | nb = l_sprintf(buff, MAX_ITEM, format, n); | ||
960 | } | 964 | } |
961 | /* else integers; write in "native" format */ | 965 | luaL_addsize(b, nb); |
962 | } /* FALLTHROUGH */ | 966 | break; |
967 | } | ||
963 | case LUA_TNIL: case LUA_TBOOLEAN: { | 968 | case LUA_TNIL: case LUA_TBOOLEAN: { |
964 | luaL_tolstring(L, arg, NULL); | 969 | luaL_tolstring(L, arg, NULL); |
965 | luaL_addvalue(b); | 970 | luaL_addvalue(b); |