summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2013-04-25 10:52:13 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2013-04-25 10:52:13 -0300
commit944709c77b8471c4cf95e3a76d812c6e643338a0 (patch)
treef21f6d28b75b3c80ec7f0f1f448e612b8ff794e3
parent52cb90ec759e3569e1edf1f73549d5a07043727d (diff)
downloadlua-944709c77b8471c4cf95e3a76d812c6e643338a0.tar.gz
lua-944709c77b8471c4cf95e3a76d812c6e643338a0.tar.bz2
lua-944709c77b8471c4cf95e3a76d812c6e643338a0.zip
more definitions to support integer formatting
-rw-r--r--lstrlib.c68
-rw-r--r--luaconf.h34
2 files changed, 36 insertions, 66 deletions
diff --git a/lstrlib.c b/lstrlib.c
index 831f4571..d76e4c13 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.177 2012/07/31 17:48:42 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.178 2012/08/14 18:12:34 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*/
@@ -786,48 +786,17 @@ static int str_gsub (lua_State *L) {
786** ======================================================= 786** =======================================================
787*/ 787*/
788 788
789/*
790** LUA_INTFRMLEN is the length modifier for integer conversions in
791** 'string.format'; LUA_INTFRM_T is the integer type corresponding to
792** the previous length
793*/
794#if !defined(LUA_INTFRMLEN) /* { */
795#if defined(LUA_USE_LONGLONG)
796
797#define LUA_INTFRMLEN "ll"
798#define LUA_INTFRM_T long long
799
800#else
801
802#define LUA_INTFRMLEN "l"
803#define LUA_INTFRM_T long
804
805#endif
806#endif /* } */
807
808
809/*
810** LUA_FLTFRMLEN is the length modifier for float conversions in
811** 'string.format'; LUA_FLTFRM_T is the float type corresponding to
812** the previous length
813*/
814#if !defined(LUA_FLTFRMLEN)
815
816#define LUA_FLTFRMLEN ""
817#define LUA_FLTFRM_T double
818
819#endif
820
821
822/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ 789/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
823#define MAX_ITEM 512 790#define MAX_ITEM 512
791
824/* valid flags in a format specification */ 792/* valid flags in a format specification */
825#define FLAGS "-+ #0" 793#define FLAGS "-+ #0"
794
826/* 795/*
827** maximum size of each format specification (such as '%-099.99d') 796** maximum size of each format specification (such as "%-099.99d")
828** (+10 accounts for %99.99x plus margin of error) 797** (+2 for length modifiers; +10 accounts for %99.99x plus margin of error)
829*/ 798*/
830#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10) 799#define MAX_FORMAT (sizeof(FLAGS) + 2 + 10)
831 800
832 801
833static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { 802static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
@@ -914,24 +883,11 @@ static int str_format (lua_State *L) {
914 nb = sprintf(buff, form, luaL_checkint(L, arg)); 883 nb = sprintf(buff, form, luaL_checkint(L, arg));
915 break; 884 break;
916 } 885 }
917 case 'd': case 'i': { 886 case 'd': case 'i':
918 lua_Number n = luaL_checknumber(L, arg);
919 LUA_INTFRM_T ni = (LUA_INTFRM_T)n;
920 lua_Number diff = n - (lua_Number)ni;
921 luaL_argcheck(L, -1 < diff && diff < 1, arg,
922 "not a number in proper range");
923 addlenmod(form, LUA_INTFRMLEN);
924 nb = sprintf(buff, form, ni);
925 break;
926 }
927 case 'o': case 'u': case 'x': case 'X': { 887 case 'o': case 'u': case 'x': case 'X': {
928 lua_Number n = luaL_checknumber(L, arg); 888 lua_Integer n = luaL_checkinteger(L, arg);
929 unsigned LUA_INTFRM_T ni = (unsigned LUA_INTFRM_T)n; 889 addlenmod(form, LUA_INTEGER_FRMLEN);
930 lua_Number diff = n - (lua_Number)ni; 890 nb = sprintf(buff, form, n);
931 luaL_argcheck(L, -1 < diff && diff < 1, arg,
932 "not a non-negative number in proper range");
933 addlenmod(form, LUA_INTFRMLEN);
934 nb = sprintf(buff, form, ni);
935 break; 891 break;
936 } 892 }
937 case 'e': case 'E': case 'f': 893 case 'e': case 'E': case 'f':
@@ -939,8 +895,8 @@ static int str_format (lua_State *L) {
939 case 'a': case 'A': 895 case 'a': case 'A':
940#endif 896#endif
941 case 'g': case 'G': { 897 case 'g': case 'G': {
942 addlenmod(form, LUA_FLTFRMLEN); 898 addlenmod(form, LUA_NUMBER_FRMLEN);
943 nb = sprintf(buff, form, (LUA_FLTFRM_T)luaL_checknumber(L, arg)); 899 nb = sprintf(buff, form, luaL_checknumber(L, arg));
944 break; 900 break;
945 } 901 }
946 case 'q': { 902 case 'q': {
diff --git a/luaconf.h b/luaconf.h
index c7b72b92..bb68289e 100644
--- a/luaconf.h
+++ b/luaconf.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: luaconf.h,v 1.175 2013/01/29 16:00:40 roberto Exp roberto $ 2** $Id: luaconf.h,v 1.176 2013/03/16 21:10:18 roberto Exp roberto $
3** Configuration file for Lua 3** Configuration file for Lua
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -394,13 +394,15 @@
394 394
395 395
396/* 396/*
397@@ LUA_NUMBER_SCAN is the format for reading numbers. 397@@ LUA_NUMBER_FRMLEN is the length modifier for writing floats.
398@@ LUA_NUMBER_FMT is the format for writing numbers. 398@@ LUA_NUMBER_SCAN is the format for reading floats.
399@@ lua_number2str converts a number to a string. 399@@ LUA_NUMBER_FMT is the format for writing floats.
400@@ lua_number2str converts a floats to a string.
400@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion. 401@@ LUAI_MAXNUMBER2STR is maximum size of previous conversion.
401*/ 402*/
403#define LUA_NUMBER_FRMLEN ""
402#define LUA_NUMBER_SCAN "%lf" 404#define LUA_NUMBER_SCAN "%lf"
403#define LUA_NUMBER_FMT "%.14g" 405#define LUA_NUMBER_FMT "%.14" LUA_NUMBER_FRMLEN "g"
404#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n)) 406#define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
405#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */ 407#define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */
406 408
@@ -457,13 +459,25 @@
457** CHANGE that if ptrdiff_t is not adequate on your machine. (On most 459** CHANGE that if ptrdiff_t is not adequate on your machine. (On most
458** machines, ptrdiff_t gives a good choice between int or long.) 460** machines, ptrdiff_t gives a good choice between int or long.)
459*/ 461*/
460#define LUA_INTEGER ptrdiff_t 462#define LUA_INTEGER long long
461 463
462/* 464/*
463@@ LUA_UNSIGNED is the integral type used by lua_pushunsigned/lua_tounsigned. 465@@ LUA_UNSIGNED is the unsigned version of LUA_INTEGER.
464** It must have at least 32 bits.
465*/ 466*/
466#define LUA_UNSIGNED unsigned LUA_INT32 467#define LUA_UNSIGNED unsigned long long
468
469/*
470@@ LUA_INTEGER_FRMLEN is the length modifier for writing integers.
471@@ LUA_INTEGER_SCAN is the format for reading integers.
472@@ LUA_INTEGER_FMT is the format for writing integers.
473@@ lua_integer2str converts an integer to a string.
474@@ LUAI_MAXINTEGER2STR is maximum size of previous conversion.
475*/
476#define LUA_INTEGER_FRMLEN "ll"
477#define LUA_INTEGER_SCAN "%Ld"
478#define LUA_INTEGER_FMT "%" LUA_INTEGER_FRMLEN "d"
479#define lua_integer2str(s,n) sprintf((s), LUA_INTEGER_FMT, (n))
480#define LUA_MAXINTEGER2STR 32
467 481
468 482
469 483
@@ -471,7 +485,7 @@
471** Some tricks with doubles 485** Some tricks with doubles
472*/ 486*/
473 487
474#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) /* { */ 488#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && 0 /* { */
475/* 489/*
476** The next definitions activate some tricks to speed up the 490** The next definitions activate some tricks to speed up the
477** conversion from doubles to integer types, mainly to LUA_UNSIGNED. 491** conversion from doubles to integer types, mainly to LUA_UNSIGNED.