diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-11-08 15:38:37 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-11-08 15:38:37 -0200 |
commit | 43ec354722183e8a9f14f0c588ea3a63c8c86432 (patch) | |
tree | 61d4bd7ffaf8ac8583680a3e823deef1ceb913ee | |
parent | 700b003fb520d190fb3ba040e50b67d93b4fca3d (diff) | |
download | lua-43ec354722183e8a9f14f0c588ea3a63c8c86432.tar.gz lua-43ec354722183e8a9f14f0c588ea3a63c8c86432.tar.bz2 lua-43ec354722183e8a9f14f0c588ea3a63c8c86432.zip |
added support for conditional use of %Lg when using long double
-rw-r--r-- | lstrlib.c | 33 |
1 files changed, 25 insertions, 8 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstrlib.c,v 1.155 2010/10/25 19:01:37 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.156 2010/10/29 17:52:46 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 | */ |
@@ -722,6 +722,7 @@ static int str_gsub (lua_State *L) { | |||
722 | ** 'string.format'; LUA_INTFRM_T is the integer type corresponding to | 722 | ** 'string.format'; LUA_INTFRM_T is the integer type corresponding to |
723 | ** the previous length | 723 | ** the previous length |
724 | */ | 724 | */ |
725 | #if !defined(LUA_INTFRMLEN) /* { */ | ||
725 | #if defined(LUA_USELONGLONG) | 726 | #if defined(LUA_USELONGLONG) |
726 | 727 | ||
727 | #define LUA_INTFRMLEN "ll" | 728 | #define LUA_INTFRMLEN "ll" |
@@ -733,6 +734,20 @@ static int str_gsub (lua_State *L) { | |||
733 | #define LUA_INTFRM_T long | 734 | #define LUA_INTFRM_T long |
734 | 735 | ||
735 | #endif | 736 | #endif |
737 | #endif /* } */ | ||
738 | |||
739 | |||
740 | /* | ||
741 | ** LUA_FLTFRMLEN is the length modifier for float conversions in | ||
742 | ** 'string.format'; LUA_FLTFRM_T is the float type corresponding to | ||
743 | ** the previous length | ||
744 | */ | ||
745 | #if !defined(LUA_FLTFRMLEN) | ||
746 | |||
747 | #define LUA_FLTFRMLEN "" | ||
748 | #define LUA_FLTFRM_T double | ||
749 | |||
750 | #endif | ||
736 | 751 | ||
737 | 752 | ||
738 | /* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ | 753 | /* 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) { | |||
793 | 808 | ||
794 | 809 | ||
795 | /* | 810 | /* |
796 | ** add length modifier into integer formats | 811 | ** add length modifier into formats |
797 | */ | 812 | */ |
798 | static void addintlen (char *form) { | 813 | static void addlenmod (char *form, const char *lenmod) { |
799 | size_t l = strlen(form); | 814 | size_t l = strlen(form); |
815 | size_t lm = strlen(lenmod); | ||
800 | char spec = form[l - 1]; | 816 | char spec = form[l - 1]; |
801 | strcpy(form + l - 1, LUA_INTFRMLEN); | 817 | strcpy(form + l - 1, lenmod); |
802 | form[l + sizeof(LUA_INTFRMLEN) - 2] = spec; | 818 | form[l + lm - 1] = spec; |
803 | form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0'; | 819 | form[l + lm] = '\0'; |
804 | } | 820 | } |
805 | 821 | ||
806 | 822 | ||
@@ -834,13 +850,14 @@ static int str_format (lua_State *L) { | |||
834 | lua_Number n = luaL_checknumber(L, arg); | 850 | lua_Number n = luaL_checknumber(L, arg); |
835 | LUA_INTFRM_T r = (n < 0) ? (LUA_INTFRM_T)n : | 851 | LUA_INTFRM_T r = (n < 0) ? (LUA_INTFRM_T)n : |
836 | (LUA_INTFRM_T)(unsigned LUA_INTFRM_T)n; | 852 | (LUA_INTFRM_T)(unsigned LUA_INTFRM_T)n; |
837 | addintlen(form); | 853 | addlenmod(form, LUA_INTFRMLEN); |
838 | nb = sprintf(buff, form, r); | 854 | nb = sprintf(buff, form, r); |
839 | break; | 855 | break; |
840 | } | 856 | } |
841 | case 'e': case 'E': case 'f': | 857 | case 'e': case 'E': case 'f': |
842 | case 'g': case 'G': { | 858 | case 'g': case 'G': { |
843 | nb = sprintf(buff, form, (double)luaL_checknumber(L, arg)); | 859 | addlenmod(form, LUA_FLTFRMLEN); |
860 | nb = sprintf(buff, form, (LUA_FLTFRM_T)luaL_checknumber(L, arg)); | ||
844 | break; | 861 | break; |
845 | } | 862 | } |
846 | case 'q': { | 863 | case 'q': { |