aboutsummaryrefslogtreecommitdiffstats
path: root/src/editfns.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/editfns.c')
-rw-r--r--src/editfns.c40
1 files changed, 14 insertions, 26 deletions
diff --git a/src/editfns.c b/src/editfns.c
index 56ad99d199f..577263c5aea 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -3504,22 +3504,6 @@ usage: (propertize STRING &rest PROPERTIES) */)
3504 RETURN_UNGCPRO (string); 3504 RETURN_UNGCPRO (string);
3505} 3505}
3506 3506
3507/* pWIDE is a conversion for printing large decimal integers (possibly with a
3508 trailing "d" that is ignored). pWIDElen is its length. signed_wide and
3509 unsigned_wide are signed and unsigned types for printing them. Use widest
3510 integers if available so that more floating point values can be converted. */
3511#ifdef PRIdMAX
3512# define pWIDE PRIdMAX
3513enum { pWIDElen = sizeof PRIdMAX - 2 }; /* Don't count trailing "d". */
3514typedef intmax_t signed_wide;
3515typedef uintmax_t unsigned_wide;
3516#else
3517# define pWIDE pI
3518enum { pWIDElen = sizeof pI - 1 };
3519typedef EMACS_INT signed_wide;
3520typedef EMACS_UINT unsigned_wide;
3521#endif
3522
3523DEFUN ("format", Fformat, Sformat, 1, MANY, 0, 3507DEFUN ("format", Fformat, Sformat, 1, MANY, 0,
3524 doc: /* Format a string out of a format-string and arguments. 3508 doc: /* Format a string out of a format-string and arguments.
3525The first argument is a format control string. 3509The first argument is a format control string.
@@ -3901,7 +3885,11 @@ usage: (format STRING &rest OBJECTS) */)
3901 precision is no more than DBL_USEFUL_PRECISION_MAX. 3885 precision is no more than DBL_USEFUL_PRECISION_MAX.
3902 On all practical hosts, %f is the worst case. */ 3886 On all practical hosts, %f is the worst case. */
3903 SPRINTF_BUFSIZE = 3887 SPRINTF_BUFSIZE =
3904 sizeof "-." + (DBL_MAX_10_EXP + 1) + USEFUL_PRECISION_MAX 3888 sizeof "-." + (DBL_MAX_10_EXP + 1) + USEFUL_PRECISION_MAX,
3889
3890 /* Length of pM (that is, of pMd without the
3891 trailing "d"). */
3892 pMlen = sizeof pMd - 2
3905 }; 3893 };
3906 verify (0 < USEFUL_PRECISION_MAX); 3894 verify (0 < USEFUL_PRECISION_MAX);
3907 3895
@@ -3914,7 +3902,7 @@ usage: (format STRING &rest OBJECTS) */)
3914 3902
3915 /* Copy of conversion specification, modified somewhat. 3903 /* Copy of conversion specification, modified somewhat.
3916 At most three flags F can be specified at once. */ 3904 At most three flags F can be specified at once. */
3917 char convspec[sizeof "%FFF.*d" + pWIDElen]; 3905 char convspec[sizeof "%FFF.*d" + pMlen];
3918 3906
3919 /* Avoid undefined behavior in underlying sprintf. */ 3907 /* Avoid undefined behavior in underlying sprintf. */
3920 if (conversion == 'd' || conversion == 'i') 3908 if (conversion == 'd' || conversion == 'i')
@@ -3922,7 +3910,7 @@ usage: (format STRING &rest OBJECTS) */)
3922 3910
3923 /* Create the copy of the conversion specification, with 3911 /* Create the copy of the conversion specification, with
3924 any width and precision removed, with ".*" inserted, 3912 any width and precision removed, with ".*" inserted,
3925 and with pWIDE inserted for integer formats. */ 3913 and with pM inserted for integer formats. */
3926 { 3914 {
3927 char *f = convspec; 3915 char *f = convspec;
3928 *f++ = '%'; 3916 *f++ = '%';
@@ -3937,8 +3925,8 @@ usage: (format STRING &rest OBJECTS) */)
3937 || conversion == 'o' || conversion == 'x' 3925 || conversion == 'o' || conversion == 'x'
3938 || conversion == 'X') 3926 || conversion == 'X')
3939 { 3927 {
3940 memcpy (f, pWIDE, pWIDElen); 3928 memcpy (f, pMd, pMlen);
3941 f += pWIDElen; 3929 f += pMlen;
3942 zero_flag &= ~ precision_given; 3930 zero_flag &= ~ precision_given;
3943 } 3931 }
3944 *f++ = conversion; 3932 *f++ = conversion;
@@ -3978,7 +3966,7 @@ usage: (format STRING &rest OBJECTS) */)
3978 /* For float, maybe we should use "%1.0f" 3966 /* For float, maybe we should use "%1.0f"
3979 instead so it also works for values outside 3967 instead so it also works for values outside
3980 the integer range. */ 3968 the integer range. */
3981 signed_wide x; 3969 printmax_t x;
3982 if (INTEGERP (args[n])) 3970 if (INTEGERP (args[n]))
3983 x = XINT (args[n]); 3971 x = XINT (args[n]);
3984 else 3972 else
@@ -3986,13 +3974,13 @@ usage: (format STRING &rest OBJECTS) */)
3986 double d = XFLOAT_DATA (args[n]); 3974 double d = XFLOAT_DATA (args[n]);
3987 if (d < 0) 3975 if (d < 0)
3988 { 3976 {
3989 x = TYPE_MINIMUM (signed_wide); 3977 x = TYPE_MINIMUM (printmax_t);
3990 if (x < d) 3978 if (x < d)
3991 x = d; 3979 x = d;
3992 } 3980 }
3993 else 3981 else
3994 { 3982 {
3995 x = TYPE_MAXIMUM (signed_wide); 3983 x = TYPE_MAXIMUM (printmax_t);
3996 if (d < x) 3984 if (d < x)
3997 x = d; 3985 x = d;
3998 } 3986 }
@@ -4002,7 +3990,7 @@ usage: (format STRING &rest OBJECTS) */)
4002 else 3990 else
4003 { 3991 {
4004 /* Don't sign-extend for octal or hex printing. */ 3992 /* Don't sign-extend for octal or hex printing. */
4005 unsigned_wide x; 3993 uprintmax_t x;
4006 if (INTEGERP (args[n])) 3994 if (INTEGERP (args[n]))
4007 x = XUINT (args[n]); 3995 x = XUINT (args[n]);
4008 else 3996 else
@@ -4012,7 +4000,7 @@ usage: (format STRING &rest OBJECTS) */)
4012 x = 0; 4000 x = 0;
4013 else 4001 else
4014 { 4002 {
4015 x = TYPE_MAXIMUM (unsigned_wide); 4003 x = TYPE_MAXIMUM (uprintmax_t);
4016 if (d < x) 4004 if (d < x)
4017 x = d; 4005 x = d;
4018 } 4006 }