commit 43ec354722183e8a9f14f0c588ea3a63c8c86432
parent 700b003fb520d190fb3ba040e50b67d93b4fca3d
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Mon, 8 Nov 2010 15:38:13 -0200
added support for conditional use of %Lg when using long double
Diffstat:
M | lstrlib.c | | | 33 | +++++++++++++++++++++++++-------- |
1 file changed, 25 insertions(+), 8 deletions(-)
diff --git a/lstrlib.c b/lstrlib.c
@@ -1,5 +1,5 @@
/*
-** $Id: lstrlib.c,v 1.155 2010/10/25 19:01:37 roberto Exp roberto $
+** $Id: lstrlib.c,v 1.156 2010/10/29 17:52:46 roberto Exp roberto $
** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h
*/
@@ -722,6 +722,7 @@ static int str_gsub (lua_State *L) {
** 'string.format'; LUA_INTFRM_T is the integer type corresponding to
** the previous length
*/
+#if !defined(LUA_INTFRMLEN) /* { */
#if defined(LUA_USELONGLONG)
#define LUA_INTFRMLEN "ll"
@@ -733,6 +734,20 @@ static int str_gsub (lua_State *L) {
#define LUA_INTFRM_T long
#endif
+#endif /* } */
+
+
+/*
+** LUA_FLTFRMLEN is the length modifier for float conversions in
+** 'string.format'; LUA_FLTFRM_T is the float type corresponding to
+** the previous length
+*/
+#if !defined(LUA_FLTFRMLEN)
+
+#define LUA_FLTFRMLEN ""
+#define LUA_FLTFRM_T double
+
+#endif
/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
@@ -793,14 +808,15 @@ static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
/*
-** add length modifier into integer formats
+** add length modifier into formats
*/
-static void addintlen (char *form) {
+static void addlenmod (char *form, const char *lenmod) {
size_t l = strlen(form);
+ size_t lm = strlen(lenmod);
char spec = form[l - 1];
- strcpy(form + l - 1, LUA_INTFRMLEN);
- form[l + sizeof(LUA_INTFRMLEN) - 2] = spec;
- form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0';
+ strcpy(form + l - 1, lenmod);
+ form[l + lm - 1] = spec;
+ form[l + lm] = '\0';
}
@@ -834,13 +850,14 @@ static int str_format (lua_State *L) {
lua_Number n = luaL_checknumber(L, arg);
LUA_INTFRM_T r = (n < 0) ? (LUA_INTFRM_T)n :
(LUA_INTFRM_T)(unsigned LUA_INTFRM_T)n;
- addintlen(form);
+ addlenmod(form, LUA_INTFRMLEN);
nb = sprintf(buff, form, r);
break;
}
case 'e': case 'E': case 'f':
case 'g': case 'G': {
- nb = sprintf(buff, form, (double)luaL_checknumber(L, arg));
+ addlenmod(form, LUA_FLTFRMLEN);
+ nb = sprintf(buff, form, (LUA_FLTFRM_T)luaL_checknumber(L, arg));
break;
}
case 'q': {