more definitions to support integer formatting

This commit is contained in:
Roberto Ierusalimschy 2013-04-25 10:52:13 -03:00
parent 52cb90ec75
commit 944709c77b
2 changed files with 36 additions and 66 deletions

View File

@ -1,5 +1,5 @@
/*
** $Id: lstrlib.c,v 1.177 2012/07/31 17:48:42 roberto Exp roberto $
** $Id: lstrlib.c,v 1.178 2012/08/14 18:12:34 roberto Exp roberto $
** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h
*/
@ -786,48 +786,17 @@ static int str_gsub (lua_State *L) {
** =======================================================
*/
/*
** LUA_INTFRMLEN is the length modifier for integer conversions in
** 'string.format'; LUA_INTFRM_T is the integer type corresponding to
** the previous length
*/
#if !defined(LUA_INTFRMLEN) /* { */
#if defined(LUA_USE_LONGLONG)
#define LUA_INTFRMLEN "ll"
#define LUA_INTFRM_T long long
#else
#define LUA_INTFRMLEN "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))) */
#define MAX_ITEM 512
/* valid flags in a format specification */
#define FLAGS "-+ #0"
/*
** maximum size of each format specification (such as '%-099.99d')
** (+10 accounts for %99.99x plus margin of error)
** maximum size of each format specification (such as "%-099.99d")
** (+2 for length modifiers; +10 accounts for %99.99x plus margin of error)
*/
#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
#define MAX_FORMAT (sizeof(FLAGS) + 2 + 10)
static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
@ -914,24 +883,11 @@ static int str_format (lua_State *L) {
nb = sprintf(buff, form, luaL_checkint(L, arg));
break;
}
case 'd': case 'i': {
lua_Number n = luaL_checknumber(L, arg);
LUA_INTFRM_T ni = (LUA_INTFRM_T)n;
lua_Number diff = n - (lua_Number)ni;
luaL_argcheck(L, -1 < diff && diff < 1, arg,
"not a number in proper range");
addlenmod(form, LUA_INTFRMLEN);
nb = sprintf(buff, form, ni);
break;
}
case 'd': case 'i':
case 'o': case 'u': case 'x': case 'X': {
lua_Number n = luaL_checknumber(L, arg);
unsigned LUA_INTFRM_T ni = (unsigned LUA_INTFRM_T)n;
lua_Number diff = n - (lua_Number)ni;
luaL_argcheck(L, -1 < diff && diff < 1, arg,
"not a non-negative number in proper range");
addlenmod(form, LUA_INTFRMLEN);
nb = sprintf(buff, form, ni);
lua_Integer n = luaL_checkinteger(L, arg);
addlenmod(form, LUA_INTEGER_FRMLEN);
nb = sprintf(buff, form, n);
break;
}
case 'e': case 'E': case 'f':
@ -939,8 +895,8 @@ static int str_format (lua_State *L) {
case 'a': case 'A':
#endif
case 'g': case 'G': {
addlenmod(form, LUA_FLTFRMLEN);
nb = sprintf(buff, form, (LUA_FLTFRM_T)luaL_checknumber(L, arg));
addlenmod(form, LUA_NUMBER_FRMLEN);
nb = sprintf(buff, form, luaL_checknumber(L, arg));
break;
}
case 'q': {

View File

@ -1,5 +1,5 @@
/*
** $Id: luaconf.h,v 1.175 2013/01/29 16:00:40 roberto Exp roberto $
** $Id: luaconf.h,v 1.176 2013/03/16 21:10:18 roberto Exp roberto $
** Configuration file for Lua
** See Copyright Notice in lua.h
*/
@ -394,13 +394,15 @@
/*
@@ LUA_NUMBER_SCAN is the format for reading numbers.
@@ LUA_NUMBER_FMT is the format for writing numbers.
@@ lua_number2str converts a number to a string.
@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats.
@@ LUA_NUMBER_SCAN is the format for reading floats.
@@ LUA_NUMBER_FMT is the format for writing floats.
@@ lua_number2str converts a floats to a string.
@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion.
*/
#define LUA_NUMBER_FRMLEN ""
#define LUA_NUMBER_SCAN "%lf"
#define LUA_NUMBER_FMT "%.14g"
#define LUA_NUMBER_FMT "%.14" LUA_NUMBER_FRMLEN "g"
#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */
@ -457,13 +459,25 @@
** CHANGE that if ptrdiff_t is not adequate on your machine. (On most
** machines, ptrdiff_t gives a good choice between int or long.)
*/
#define LUA_INTEGER ptrdiff_t
#define LUA_INTEGER long long
/*
@@ LUA_UNSIGNED is the integral type used by lua_pushunsigned/lua_tounsigned.
** It must have at least 32 bits.
@@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER.
*/
#define LUA_UNSIGNED unsigned LUA_INT32
#define LUA_UNSIGNED unsigned long long
/*
@@ LUA_INTEGER_FRMLEN is the length modifier for writing integers.
@@ LUA_INTEGER_SCAN is the format for reading integers.
@@ LUA_INTEGER_FMT is the format for writing integers.
@@ lua_integer2str converts an integer to a string.
@@ LUAI_MAXINTEGER2STR is maximum size of previous conversion.
*/
#define LUA_INTEGER_FRMLEN "ll"
#define LUA_INTEGER_SCAN "%Ld"
#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d"
#define lua_integer2str(s,n) sprintf((s), LUA_INTEGER_FMT, (n))
#define LUA_MAXINTEGER2STR 32
@ -471,7 +485,7 @@
** Some tricks with doubles
*/
#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) /* { */
#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && 0 /* { */
/*
** The next definitions activate some tricks to speed up the
** conversion from doubles to integer types, mainly to LUA_UNSIGNED.