aboutsummaryrefslogtreecommitdiff
path: root/lstrlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2016-05-02 10:58:01 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2016-05-02 10:58:01 -0300
commit22093f9c6ef0e21244603883eed740d34d9fd63f (patch)
tree8cf6829cedc10058f20f3bc68bcb23b9c9cc247a /lstrlib.c
parent792ffaccf2b28880f19564f6be63ecad03d4863e (diff)
downloadlua-22093f9c6ef0e21244603883eed740d34d9fd63f.tar.gz
lua-22093f9c6ef0e21244603883eed740d34d9fd63f.tar.bz2
lua-22093f9c6ef0e21244603883eed740d34d9fd63f.zip
'string.format("%q", number)' ensures a dot as decimal point
Diffstat (limited to 'lstrlib.c')
-rw-r--r--lstrlib.c28
1 files changed, 21 insertions, 7 deletions
diff --git a/lstrlib.c b/lstrlib.c
index f4871212..8a99a011 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.246 2016/04/19 12:34:08 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.247 2016/04/22 16:36:30 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*/
@@ -13,6 +13,7 @@
13#include <ctype.h> 13#include <ctype.h>
14#include <float.h> 14#include <float.h>
15#include <limits.h> 15#include <limits.h>
16#include <locale.h>
16#include <stddef.h> 17#include <stddef.h>
17#include <stdio.h> 18#include <stdio.h>
18#include <stdlib.h> 19#include <stdlib.h>
@@ -812,7 +813,6 @@ static int str_gsub (lua_State *L) {
812** Hexadecimal floating-point formatter 813** Hexadecimal floating-point formatter
813*/ 814*/
814 815
815#include <locale.h>
816#include <math.h> 816#include <math.h>
817 817
818#define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char)) 818#define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char))
@@ -927,6 +927,24 @@ static void addquoted (luaL_Buffer *b, const char *s, size_t len) {
927} 927}
928 928
929 929
930/*
931** Convert a Lua number to a string in floating-point hexadecimal
932** format. Ensures the resulting string uses a dot as the radix
933** character.
934*/
935static void addliteralnum (lua_State *L, luaL_Buffer *b, lua_Number n) {
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? */
940 char point = lua_getlocaledecpoint(); /* try locale point */
941 char *ppoint = memchr(buff, point, nb);
942 if (ppoint) *ppoint = '.'; /* change it to a dot */
943 }
944 luaL_addsize(b, nb);
945}
946
947
930static void addliteral (lua_State *L, luaL_Buffer *b, int arg) { 948static void addliteral (lua_State *L, luaL_Buffer *b, int arg) {
931 switch (lua_type(L, arg)) { 949 switch (lua_type(L, arg)) {
932 case LUA_TSTRING: { 950 case LUA_TSTRING: {
@@ -937,11 +955,7 @@ static void addliteral (lua_State *L, luaL_Buffer *b, int arg) {
937 } 955 }
938 case LUA_TNUMBER: { 956 case LUA_TNUMBER: {
939 if (!lua_isinteger(L, arg)) { /* write floats as hexa ('%a') */ 957 if (!lua_isinteger(L, arg)) { /* write floats as hexa ('%a') */
940 char *buff = luaL_prepbuffsize(b, MAX_ITEM); 958 addliteralnum(L, b, lua_tonumber(L, arg));
941 lua_Number n = lua_tonumber(L, arg);
942 int nb = lua_number2strx(L, buff, MAX_ITEM,
943 "%" LUA_NUMBER_FRMLEN "a", n);
944 luaL_addsize(b, nb);
945 break; 959 break;
946 } 960 }
947 /* else integers; write in "native" format *//* FALLTHROUGH */ 961 /* else integers; write in "native" format *//* FALLTHROUGH */