1 /* Floating point output for `printf'.
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3 Written by Ulrich Drepper.
5 This file is part of the GNU C Library.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If
19 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 Cambridge, MA 02139, USA. */
22 /* The gmp headers need some configuration frobs. */
34 #include <gmp-mparam.h>
35 #include "../stdlib/gmp.h"
36 #include "../stdlib/gmp-impl.h"
37 #include "../stdlib/longlong.h"
38 #include "../stdlib/fpioconst.h"
39 #include "../locale/localeinfo.h"
47 #define NDEBUG /* Undefine this for debugging assertions. */
50 /* This defines make it possible to use the same code for GNU C library and
51 the GNU I/O library. */
53 # define PUT(f, s, n) _IO_sputn (f, s, n)
54 # define PAD(f, c, n) _IO_padn (f, c, n)
55 /* We use this file GNU C library and GNU I/O library. So make
58 # define putc(c, f) _IO_putc (c, f)
59 # define size_t _IO_size_t
60 # define FILE _IO_FILE
61 #else /* ! USE_IN_LIBIO */
62 # define PUT(f, s, n) fwrite (s, 1, n, f)
63 # define PAD(f, c, n) __printf_pad (f, c, n)
64 ssize_t __printf_pad
__P ((FILE *, char pad
, int n
)); /* In vfprintf.c. */
65 #endif /* USE_IN_LIBIO */
67 /* Macros for doing the actual output. */
72 register CONST int outc = (ch); \
73 if (putc (outc, fp) == EOF) \
78 #define PRINT(ptr, len) \
81 register size_t outlen = (len); \
84 if (PUT (fp, ptr, outlen) != outlen) \
91 while (outlen-- > 0) \
96 #define PADN(ch, len) \
99 if (PAD (fp, ch, len) != len) \
105 /* We use the GNU MP library to handle large numbers.
107 An MP variable occupies a varying number of entries in its array. We keep
108 track of this number for efficiency reasons. Otherwise we would always
109 have to process the whole array. */
110 #define MPN_VAR(name) mp_limb *name; mp_size_t name##size
112 #define MPN_ASSIGN(dst,src) \
113 memcpy (dst, src, (dst##size = src##size) * sizeof (mp_limb))
114 #define MPN_GE(u,v) \
115 (u##size > v##size || (u##size == v##size && __mpn_cmp (u, v, u##size) >= 0))
117 extern int __isinfl (long double), __isnanl (long double);
119 extern mp_size_t
__mpn_extract_double (mp_ptr res_ptr
, mp_size_t size
,
120 int *expt
, int *is_neg
,
122 extern mp_size_t
__mpn_extract_long_double (mp_ptr res_ptr
, mp_size_t size
,
123 int *expt
, int *is_neg
,
127 static unsigned int guess_grouping (unsigned int intdig_max
,
128 const char *grouping
, wchar_t sepchar
);
129 static char *group_number (char *buf
, char *bufend
, unsigned int intdig_no
,
130 const char *grouping
, wchar_t thousands_sep
);
134 __printf_fp (FILE *fp
,
135 const struct printf_info
*info
,
136 const void *const *args
)
138 /* The floating-point value to output. */
146 /* Locale-dependent representation of decimal point. */
149 /* Locale-dependent thousands separator and grouping specification. */
150 wchar_t thousands_sep
;
151 const char *grouping
;
153 /* "NaN" or "Inf" for the special cases. */
154 CONST
char *special
= NULL
;
156 /* We need just a few limbs for the input before shifting to the right
158 mp_limb fp_input
[(LDBL_MANT_DIG
+ BITS_PER_MP_LIMB
- 1) / BITS_PER_MP_LIMB
];
159 /* We need to shift the contents of fp_input by this amount of bits. */
162 /* The significant of the floting-point value in question */
164 /* and the exponent. */
166 /* Sign of the exponent. */
168 /* Sign of float number. */
171 /* Scaling factor. */
174 /* Temporary bignum value. */
177 /* Digit which is result of last hack_digit() call. */
180 /* The type of output format that will be used: 'e'/'E' or 'f'. */
183 /* Counter for number of written characters. */
186 /* General helper (carry limb). */
189 char hack_digit (void)
193 if (expsign
!= 0 && type
== 'f' && exponent
-- > 0)
195 else if (scalesize
== 0)
197 hi
= frac
[fracsize
- 1];
198 cy
= __mpn_mul_1 (frac
, frac
, fracsize
- 1, 10);
199 frac
[fracsize
- 1] = cy
;
203 if (fracsize
< scalesize
)
207 hi
= mpn_divmod (tmp
, frac
, fracsize
, scale
, scalesize
);
208 tmp
[fracsize
- scalesize
] = hi
;
211 fracsize
= scalesize
;
212 while (fracsize
!= 0 && frac
[fracsize
- 1] == 0)
216 /* We're not prepared for an mpn variable with zero
223 cy
= __mpn_mul_1 (frac
, frac
, fracsize
, 10);
225 frac
[fracsize
++] = cy
;
232 /* Figure out the decimal point character. */
233 if (mbtowc (&decimal
, _NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
),
234 strlen (_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
))) <= 0)
235 decimal
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, DECIMAL_POINT
);
240 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
241 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
245 /* Figure out the thousands seperator character. */
246 if (mbtowc (&thousands_sep
, _NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
),
247 strlen (_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
))) <= 0)
248 thousands_sep
= (wchar_t) *_NL_CURRENT (LC_NUMERIC
, THOUSANDS_SEP
);
249 if (thousands_sep
== L
'\0')
256 /* Fetch the argument value. */
257 if (info
->is_long_double
&& sizeof (long double) > sizeof (double))
259 fpnum
.ldbl
= *(const long double *) args
[0];
261 /* Check for special values: not a number or infinity. */
262 if (__isnanl (fpnum
.ldbl
))
267 else if (__isinfl (fpnum
.ldbl
))
270 is_neg
= fpnum
.ldbl
< 0;
274 fracsize
= __mpn_extract_long_double (fp_input
,
276 sizeof (fp_input
[0])),
279 to_shift
= 1 + fracsize
* BITS_PER_MP_LIMB
- LDBL_MANT_DIG
;
284 fpnum
.dbl
= *(const double *) args
[0];
286 /* Check for special values: not a number or infinity. */
287 if (__isnan (fpnum
.dbl
))
292 else if (__isinf (fpnum
.dbl
))
295 is_neg
= fpnum
.dbl
< 0;
299 fracsize
= __mpn_extract_double (fp_input
,
301 / sizeof (fp_input
[0])),
302 &exponent
, &is_neg
, fpnum
.dbl
);
303 to_shift
= 1 + fracsize
* BITS_PER_MP_LIMB
- DBL_MANT_DIG
;
309 int width
= info
->prec
> info
->width
? info
->prec
: info
->width
;
311 if (is_neg
|| info
->showsign
|| info
->space
)
315 if (!info
->left
&& width
> 0)
320 else if (info
->showsign
)
322 else if (info
->space
)
327 if (info
->left
&& width
> 0)
334 /* We need three multiprecision variables. Now that we have the exponent
335 of the number we can allocate the needed memory. It would be more
336 efficient to use variables of the fixed maximum size but because this
337 would be really big it could lead to memory problems. */
339 mp_size_t bignum_size
= ((ABS (exponent
) + BITS_PER_MP_LIMB
- 1)
340 / BITS_PER_MP_LIMB
+ 4) * sizeof (mp_limb
);
341 frac
= (mp_limb
*) alloca (bignum_size
);
342 tmp
= (mp_limb
*) alloca (bignum_size
);
343 scale
= (mp_limb
*) alloca (bignum_size
);
346 /* We now have to distinguish between numbers with positive and negative
347 exponents because the method used for the one is not applicable/efficient
354 int explog
= LDBL_MAX_10_EXP_LOG
;
356 const struct mp_power
*tens
= &_fpioconst_pow10
[explog
+ 1];
359 if ((exponent
+ to_shift
) % BITS_PER_MP_LIMB
== 0)
361 MPN_COPY_DECR (frac
+ (exponent
+ to_shift
) / BITS_PER_MP_LIMB
,
363 fracsize
+= (exponent
+ to_shift
) / BITS_PER_MP_LIMB
;
367 cy
= __mpn_lshift (frac
+ (exponent
+ to_shift
) / BITS_PER_MP_LIMB
,
369 (exponent
+ to_shift
) % BITS_PER_MP_LIMB
);
370 fracsize
+= (exponent
+ to_shift
) / BITS_PER_MP_LIMB
;
372 frac
[fracsize
++] = cy
;
374 MPN_ZERO (frac
, (exponent
+ to_shift
) / BITS_PER_MP_LIMB
);
376 assert (tens
> &_fpioconst_pow10
[0]);
381 /* The number of the product of two binary numbers with n and m
382 bits respectively has m+n or m+n-1 bits. */
383 if (exponent
>= scaleexpo
+ tens
->p_expo
- 1)
386 MPN_ASSIGN (tmp
, tens
->array
);
389 cy
= __mpn_mul (tmp
, scale
, scalesize
,
390 &tens
->array
[_FPIO_CONST_OFFSET
],
391 tens
->arraysize
- _FPIO_CONST_OFFSET
);
392 tmpsize
= scalesize
+ tens
->arraysize
- _FPIO_CONST_OFFSET
;
397 if (MPN_GE (frac
, tmp
))
400 MPN_ASSIGN (scale
, tmp
);
401 count_leading_zeros (cnt
, scale
[scalesize
- 1]);
402 scaleexpo
= (scalesize
- 2) * BITS_PER_MP_LIMB
- cnt
- 1;
403 exp10
|= 1 << explog
;
408 while (tens
> &_fpioconst_pow10
[0]);
411 /* Optimize number representations. We want to represent the numbers
412 with the lowest number of bytes possible without losing any
413 bytes. Also the highest bit in the scaling factor has to be set
414 (this is a requirement of the MPN division routines). */
417 /* Determine minimum number of zero bits at the end of
419 for (i
= 0; scale
[i
] == 0 && frac
[i
] == 0; i
++)
422 /* Determine number of bits the scaling factor is misplaced. */
423 count_leading_zeros (cnt_h
, scale
[scalesize
- 1]);
427 /* The highest bit of the scaling factor is already set. So
428 we only have to remove the trailing empty limbs. */
431 MPN_COPY_INCR (scale
, scale
+ i
, scalesize
- i
);
433 MPN_COPY_INCR (frac
, frac
+ i
, fracsize
- i
);
441 count_trailing_zeros (cnt_l
, scale
[i
]);
445 count_trailing_zeros (cnt_l2
, frac
[i
]);
451 count_trailing_zeros (cnt_l
, frac
[i
]);
453 /* Now shift the numbers to their optimal position. */
454 if (i
== 0 && BITS_PER_MP_LIMB
- cnt_h
> cnt_l
)
456 /* We cannot save any memory. So just roll both numbers
457 so that the scaling factor has its highest bit set. */
459 (void) __mpn_lshift (scale
, scale
, scalesize
, cnt_h
);
460 cy
= __mpn_lshift (frac
, frac
, fracsize
, cnt_h
);
462 frac
[fracsize
++] = cy
;
464 else if (BITS_PER_MP_LIMB
- cnt_h
<= cnt_l
)
466 /* We can save memory by removing the trailing zero limbs
467 and by packing the non-zero limbs which gain another
470 (void) __mpn_rshift (scale
, scale
+ i
, scalesize
- i
,
471 BITS_PER_MP_LIMB
- cnt_h
);
473 (void) __mpn_rshift (frac
, frac
+ i
, fracsize
- i
,
474 BITS_PER_MP_LIMB
- cnt_h
);
475 fracsize
-= frac
[fracsize
- i
- 1] == 0 ? i
+ 1 : i
;
479 /* We can only save the memory of the limbs which are zero.
480 The non-zero parts occupy the same number of limbs. */
482 (void) __mpn_rshift (scale
, scale
+ (i
- 1),
484 BITS_PER_MP_LIMB
- cnt_h
);
486 (void) __mpn_rshift (frac
, frac
+ (i
- 1),
488 BITS_PER_MP_LIMB
- cnt_h
);
489 fracsize
-= frac
[fracsize
- (i
- 1) - 1] == 0 ? i
: i
- 1;
494 else if (exponent
< 0)
498 int explog
= LDBL_MAX_10_EXP_LOG
;
499 const struct mp_power
*tens
= &_fpioconst_pow10
[explog
+ 1];
500 mp_size_t used_limbs
= fracsize
- 1;
502 /* Now shift the input value to its right place. */
503 cy
= __mpn_lshift (frac
, fp_input
, fracsize
, to_shift
);
504 frac
[fracsize
++] = cy
;
505 assert (cy
== 1 || (frac
[fracsize
- 2] == 0 && frac
[0] == 0));
508 exponent
= -exponent
;
510 assert (tens
!= &_fpioconst_pow10
[0]);
515 if (exponent
>= tens
->m_expo
)
517 int i
, incr
, cnt_h
, cnt_l
;
520 /* The __mpn_mul function expects the first argument to be
521 bigger than the second. */
522 if (fracsize
< tens
->arraysize
- _FPIO_CONST_OFFSET
)
523 cy
= __mpn_mul (tmp
, &tens
->array
[_FPIO_CONST_OFFSET
],
524 tens
->arraysize
- _FPIO_CONST_OFFSET
,
527 cy
= __mpn_mul (tmp
, frac
, fracsize
,
528 &tens
->array
[_FPIO_CONST_OFFSET
],
529 tens
->arraysize
- _FPIO_CONST_OFFSET
);
530 tmpsize
= fracsize
+ tens
->arraysize
- _FPIO_CONST_OFFSET
;
534 count_leading_zeros (cnt_h
, tmp
[tmpsize
- 1]);
535 incr
= (tmpsize
- fracsize
) * BITS_PER_MP_LIMB
536 + BITS_PER_MP_LIMB
- 1 - cnt_h
;
538 assert (incr
<= tens
->p_expo
);
540 /* If we increased the exponent by exactly 3 we have to test
541 for overflow. This is done by comparing with 10 shifted
542 to the right position. */
543 if (incr
== exponent
+ 3)
544 if (cnt_h
<= BITS_PER_MP_LIMB
- 4)
548 = ((mp_limb
) 10) << (BITS_PER_MP_LIMB
- 4 - cnt_h
);
552 topval
[0] = ((mp_limb
) 10) << (BITS_PER_MP_LIMB
- 4);
554 (void) __mpn_lshift (topval
, topval
, 2,
555 BITS_PER_MP_LIMB
- cnt_h
);
558 /* We have to be careful when multiplying the last factor.
559 If the result is greater than 1.0 be have to test it
560 against 10.0. If it is greater or equal to 10.0 the
561 multiplication was not valid. This is because we cannot
562 determine the number of bits in the result in advance. */
563 if (incr
< exponent
+ 3
564 || (incr
== exponent
+ 3 &&
565 (tmp
[tmpsize
- 1] < topval
[1]
566 || (tmp
[tmpsize
- 1] == topval
[1]
567 && tmp
[tmpsize
- 2] < topval
[0]))))
569 /* The factor is right. Adapt binary and decimal
572 exp10
|= 1 << explog
;
574 /* If this factor yields a number greater or equal to
575 1.0, we must not shift the non-fractional digits down. */
579 /* Now we optimize the number representation. */
580 for (i
= 0; tmp
[i
] == 0; ++i
);
581 if (cnt_h
== BITS_PER_MP_LIMB
- 1)
583 MPN_COPY (frac
, tmp
+ i
, tmpsize
- i
);
584 fracsize
= tmpsize
- i
;
588 count_trailing_zeros (cnt_l
, tmp
[i
]);
590 /* Now shift the numbers to their optimal position. */
591 if (i
== 0 && BITS_PER_MP_LIMB
- 1 - cnt_h
> cnt_l
)
593 /* We cannot save any memory. Just roll the
594 number so that the leading digit is in a
597 cy
= __mpn_lshift (frac
, tmp
, tmpsize
, cnt_h
+ 1);
598 fracsize
= tmpsize
+ 1;
599 frac
[fracsize
- 1] = cy
;
601 else if (BITS_PER_MP_LIMB
- 1 - cnt_h
<= cnt_l
)
603 (void) __mpn_rshift (frac
, tmp
+ i
, tmpsize
- i
,
604 BITS_PER_MP_LIMB
- 1 - cnt_h
);
605 fracsize
= tmpsize
- i
;
609 /* We can only save the memory of the limbs which
610 are zero. The non-zero parts occupy the same
613 (void) __mpn_rshift (frac
, tmp
+ (i
- 1),
615 BITS_PER_MP_LIMB
- 1 - cnt_h
);
616 fracsize
= tmpsize
- (i
- 1);
619 used_limbs
= fracsize
- 1;
624 while (tens
!= &_fpioconst_pow10
[1] && exponent
> 0);
625 /* All factors but 10^-1 are tested now. */
630 cy
= __mpn_mul_1 (tmp
, frac
, fracsize
, 10);
632 assert (cy
== 0 || tmp
[tmpsize
- 1] < 20);
634 count_trailing_zeros (cnt_l
, tmp
[0]);
635 if (cnt_l
< MIN (4, exponent
))
637 cy
= __mpn_lshift (frac
, tmp
, tmpsize
,
638 BITS_PER_MP_LIMB
- MIN (4, exponent
));
640 frac
[tmpsize
++] = cy
;
643 (void) __mpn_rshift (frac
, tmp
, tmpsize
, MIN (4, exponent
));
646 assert (frac
[fracsize
- 1] < 10);
652 /* This is a special case. We don't need a factor because the
653 numbers are in the range of 0.0 <= fp < 8.0. We simply
654 shift it to the right place and divide it by 1.0 to get the
655 leading digit. (Of course this division is not really made.) */
656 assert (0 <= exponent
&& exponent
< 3 &&
657 exponent
+ to_shift
< BITS_PER_MP_LIMB
);
659 /* Now shift the input value to its right place. */
660 cy
= __mpn_lshift (frac
, fp_input
, fracsize
, (exponent
+ to_shift
));
661 frac
[fracsize
++] = cy
;
666 int width
= info
->width
;
667 char *buffer
, *startp
, *cp
;
670 int intdig_max
, intdig_no
= 0;
671 int fracdig_min
, fracdig_max
, fracdig_no
= 0;
675 if (tolower (info
->spec
) == 'e')
679 fracdig_min
= fracdig_max
= info
->prec
< 0 ? 6 : info
->prec
;
680 chars_needed
= 1 + 1 + fracdig_max
+ 1 + 1 + 4;
681 /* d . ddd e +- ddd */
682 dig_max
= INT_MAX
; /* Unlimited. */
683 significant
= 1; /* Does not matter here. */
685 else if (info
->spec
== 'f')
688 fracdig_min
= fracdig_max
= info
->prec
< 0 ? 6 : info
->prec
;
691 intdig_max
= exponent
+ 1;
692 /* This can be really big! */ /* XXX Maybe malloc if too big? */
693 chars_needed
= exponent
+ 1 + 1 + fracdig_max
;
698 chars_needed
= 1 + 1 + fracdig_max
;
700 dig_max
= INT_MAX
; /* Unlimited. */
701 significant
= 1; /* Does not matter here. */
705 dig_max
= info
->prec
< 0 ? 6 : (info
->prec
== 0 ? 1 : info
->prec
);
706 if ((expsign
== 0 && exponent
>= dig_max
)
707 || (expsign
!= 0 && exponent
> 4))
709 type
= isupper (info
->spec
) ? 'E' : 'e';
710 fracdig_max
= dig_max
- 1;
712 chars_needed
= 1 + 1 + fracdig_max
+ 1 + 1 + 4;
717 intdig_max
= expsign
== 0 ? exponent
+ 1 : 0;
718 fracdig_max
= dig_max
- intdig_max
;
719 /* We need space for the significant digits and perhaps for
720 leading zeros when < 1.0. Pessimistic guess: dig_max. */
721 chars_needed
= dig_max
+ dig_max
+ 1;
723 fracdig_min
= info
->alt
? fracdig_max
: 0;
724 significant
= 0; /* We count significant digits. */
728 /* Guess the number of groups we will make, and thus how
729 many spaces we need for separator characters. */
730 chars_needed
+= guess_grouping (intdig_max
, grouping
, thousands_sep
);
732 /* Allocate buffer for output. We need two more because while rounding
733 it is possible that we need two more characters in front of all the
735 buffer
= alloca (2 + chars_needed
);
736 cp
= startp
= buffer
+ 2; /* Let room for rounding. */
738 /* Do the real work: put digits in allocated buffer. */
739 if (expsign
== 0 || type
!= 'f')
741 assert (expsign
== 0 || intdig_max
== 1);
742 while (intdig_no
< intdig_max
)
745 *cp
++ = hack_digit ();
750 || (fracdig_max
> 0 && (fracsize
> 1 || frac
[0] != 0)))
755 /* |fp| < 1.0 and the selected type is 'f', so put "0."
762 /* Generate the needed number of fractional digits. */
763 while (fracdig_no
< fracdig_min
764 || (fracdig_no
< fracdig_max
&& (fracsize
> 1 || frac
[0] != 0)))
770 else if (significant
== 0)
780 digit
= hack_digit ();
786 /* This is the critical case. */
787 if (fracsize
== 1 && frac
[0] == 0)
788 /* Rest of the number is zero -> round to even.
789 (IEEE 754-1985 4.1 says this is the default rounding.) */
790 if ((*(cp
- 1) & 1) == 0)
795 /* Process fractional digits. Terminate if not rounded or
796 radix character is reached. */
797 while (*--tp
!= decimal
&& *tp
== '9')
804 if (fracdig_no
== 0 || *tp
== decimal
)
806 /* Round the integer digits. */
807 if (*(tp
- 1) == decimal
)
810 while (--tp
>= startp
&& *tp
== '9')
817 /* It is more citical. All digits were 9's. */
822 exponent
+= expsign
== 0 ? 1 : -1;
824 else if (intdig_no
== dig_max
)
826 /* This is the case where for type %g the number fits
827 really in the range for %f output but after rounding
828 the number of digits is too big. */
832 if (info
->alt
|| fracdig_no
> 0)
834 /* Overwrite the old radix character. */
835 startp
[intdig_no
+ 2] = '0';
839 fracdig_no
+= intdig_no
;
841 fracdig_max
= intdig_max
- intdig_no
;
843 /* Now we must print the exponent. */
844 type
= isupper (info
->spec
) ? 'E' : 'e';
848 /* We can simply add another another digit before the
854 /* While rounding the number of digits can change.
855 If the number now exceeds the limits remove some
856 fractional digits. */
857 if (intdig_no
+ fracdig_no
> dig_max
)
859 cp
-= intdig_no
+ fracdig_no
- dig_max
;
860 fracdig_no
-= intdig_no
+ fracdig_no
- dig_max
;
867 /* Now remove unnecessary '0' at the end of the string. */
868 while (fracdig_no
> fracdig_min
&& *(cp
- 1) == '0')
873 /* If we eliminate all fractional digits we perhaps also can remove
874 the radix character. */
875 if (fracdig_no
== 0 && !info
->alt
&& *(cp
- 1) == decimal
)
879 /* Add in separator characters, overwriting the same buffer. */
880 cp
= group_number (startp
, cp
, intdig_no
, grouping
, thousands_sep
);
882 /* Write the exponent if it is needed. */
886 *cp
++ = expsign
? '-' : '+';
888 /* Find the magnitude of the exponent. */
890 while (expscale
<= exponent
)
894 /* Exponent always has at least two digits. */
900 *cp
++ = '0' + (exponent
/ expscale
);
901 exponent
%= expscale
;
903 while (expscale
> 10);
904 *cp
++ = '0' + exponent
;
907 /* Compute number of characters which must be filled with the padding
909 if (is_neg
|| info
->showsign
|| info
->space
)
911 width
-= cp
- startp
;
913 if (!info
->left
&& info
->pad
!= '0' && width
> 0)
914 PADN (info
->pad
, width
);
918 else if (info
->showsign
)
920 else if (info
->space
)
923 if (!info
->left
&& info
->pad
== '0' && width
> 0)
926 PRINT (startp
, cp
- startp
);
928 if (info
->left
&& width
> 0)
929 PADN (info
->pad
, width
);
934 /* Return the number of extra grouping characters that will be inserted
935 into a number with INTDIG_MAX integer digits. */
938 guess_grouping (unsigned int intdig_max
, const char *grouping
, wchar_t sepchar
)
942 /* We treat all negative values like CHAR_MAX. */
944 if (*grouping
== CHAR_MAX
|| *grouping
<= 0)
945 /* No grouping should be done. */
949 while (intdig_max
> (unsigned int) *grouping
)
952 intdig_max
-= *grouping
++;
954 if (*grouping
== CHAR_MAX
|| *grouping
< 0)
955 /* No more grouping should be done. */
957 else if (*grouping
== 0)
959 /* Same grouping repeats. */
960 groups
+= intdig_max
/ grouping
[-1];
968 /* Group the INTDIG_NO integer digits of the number in [BUF,BUFEND).
969 There is guaranteed enough space past BUFEND to extend it.
970 Return the new end of buffer. */
973 group_number (char *buf
, char *bufend
, unsigned int intdig_no
,
974 const char *grouping
, wchar_t thousands_sep
)
976 unsigned int groups
= guess_grouping (intdig_no
, grouping
, thousands_sep
);
982 /* Move the fractional part down. */
983 memmove (buf
+ intdig_no
+ groups
, buf
+ intdig_no
,
984 bufend
- (buf
+ intdig_no
));
986 p
= buf
+ intdig_no
+ groups
- 1;
989 unsigned int len
= *grouping
++;
991 *p
-- = buf
[--intdig_no
];
993 *p
-- = thousands_sep
;
995 if (*grouping
== CHAR_MAX
|| *grouping
< 0)
996 /* No more grouping should be done. */
998 else if (*grouping
== 0)
999 /* Same grouping repeats. */
1001 } while (intdig_no
> (unsigned int) *grouping
);
1003 /* Copy the remaining ungrouped digits. */
1005 *p
-- = buf
[--intdig_no
];
1008 return bufend
+ groups
;