diff options
Diffstat (limited to 'lstrlib.c')
-rw-r--r-- | lstrlib.c | 68 |
1 files changed, 12 insertions, 56 deletions
@@ -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 | ||
833 | static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { | 802 | static 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': { |