1 /* __gmp_doprnt_integer -- integer style formatted output.
3 THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY. THEY'RE ALMOST
4 CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
5 FUTURE GNU MP RELEASES.
7 Copyright 2001 Free Software Foundation, Inc.
9 This file is part of the GNU MP Library.
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
16 The GNU MP Library is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
19 License for more details.
21 You should have received a copy of the GNU Lesser General Public License
22 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
27 #include <stdarg.h> /* for va_list and hence doprnt_funs_t */
41 __gmp_doprnt_integer (const struct doprnt_funs_t
*funs
,
43 const struct doprnt_params_t
*p
,
47 int slen
, justlen
, showbaselen
, sign
, signlen
, slashlen
, zeros
;
48 int justify
, den_showbaselen
;
49 const char *slash
, *showbase
;
51 /* '+' or ' ' if wanted, and don't already have '-' */
58 signlen
= (sign
!= '\0');
60 /* if the precision was explicitly 0, print nothing for a 0 value */
61 if (*s
== '0' && p
->prec
== 0)
65 slash
= strchr (s
, '/');
70 if (p
->showbase
!= DOPRNT_SHOWBASE_NO
)
73 case 16: showbase
= "0x"; showbaselen
= 2; break;
74 case -16: showbase
= "0X"; showbaselen
= 2; break;
75 case 8: showbase
= "0"; showbaselen
= 1; break;
79 den_showbaselen
= showbaselen
;
81 || (p
->showbase
== DOPRNT_SHOWBASE_NONZERO
&& slash
[1] == '0'))
84 if (p
->showbase
== DOPRNT_SHOWBASE_NONZERO
&& s
[0] == '0')
87 /* the influence of p->prec on mpq is currently undefined */
88 zeros
= MAX (0, p
->prec
- slen
);
90 /* space left over after actual output length */
92 - (strlen(s
) + signlen
+ showbaselen
+ den_showbaselen
+ zeros
);
95 if (justlen
<= 0) /* no justifying if exceed width */
96 justify
= DOPRNT_JUSTIFY_NONE
;
98 if (justify
== DOPRNT_JUSTIFY_RIGHT
) /* pad right */
99 DOPRNT_REPS (p
->fill
, justlen
);
101 DOPRNT_REPS_MAYBE (sign
, signlen
); /* sign */
103 DOPRNT_MEMORY_MAYBE (showbase
, showbaselen
); /* base */
105 DOPRNT_REPS_MAYBE ('0', zeros
); /* zeros */
107 if (justify
== DOPRNT_JUSTIFY_INTERNAL
) /* pad internal */
108 DOPRNT_REPS (p
->fill
, justlen
);
110 /* if there's a showbase on the denominator, then print the numerator
111 separately so it can be inserted */
112 if (den_showbaselen
!= 0)
114 ASSERT (slash
!= NULL
);
115 slashlen
= slash
+1 - s
;
116 DOPRNT_MEMORY (s
, slashlen
); /* numerator and slash */
119 DOPRNT_MEMORY (showbase
, den_showbaselen
);
122 DOPRNT_MEMORY (s
, slen
); /* number, or denominator */
124 if (justify
== DOPRNT_JUSTIFY_LEFT
) /* pad left */
125 DOPRNT_REPS (p
->fill
, justlen
);