beta-0.89.2
[luatex.git] / source / libs / gmp / gmp-src / printf / doprnti.c
blobc4cb9c8da8deb4ba3f798fee6459bb14df351b03
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 either:
14 * the GNU Lesser General Public License as published by the Free
15 Software Foundation; either version 3 of the License, or (at your
16 option) any later version.
20 * the GNU General Public License as published by the Free Software
21 Foundation; either version 2 of the License, or (at your option) any
22 later version.
24 or both in parallel, as here.
26 The GNU MP Library is distributed in the hope that it will be useful, but
27 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
28 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 for more details.
31 You should have received copies of the GNU General Public License and the
32 GNU Lesser General Public License along with the GNU MP Library. If not,
33 see https://www.gnu.org/licenses/. */
35 #include <stdarg.h> /* for va_list and hence doprnt_funs_t */
36 #include <string.h>
37 #include <stdio.h>
38 #include <stdlib.h>
40 #include "gmp.h"
41 #include "gmp-impl.h"
44 int
45 __gmp_doprnt_integer (const struct doprnt_funs_t *funs,
46 void *data,
47 const struct doprnt_params_t *p,
48 const char *s)
50 int retval = 0;
51 int slen, justlen, showbaselen, sign, signlen, slashlen, zeros;
52 int justify, den_showbaselen;
53 const char *slash, *showbase;
55 /* '+' or ' ' if wanted, and don't already have '-' */
56 sign = p->sign;
57 if (s[0] == '-')
59 sign = s[0];
60 s++;
62 signlen = (sign != '\0');
64 /* if the precision was explicitly 0, print nothing for a 0 value */
65 if (*s == '0' && p->prec == 0)
66 s++;
68 slen = strlen (s);
69 slash = strchr (s, '/');
71 showbase = NULL;
72 showbaselen = 0;
74 if (p->showbase != DOPRNT_SHOWBASE_NO)
76 switch (p->base) {
77 case 16: showbase = "0x"; showbaselen = 2; break;
78 case -16: showbase = "0X"; showbaselen = 2; break;
79 case 8: showbase = "0"; showbaselen = 1; break;
83 den_showbaselen = showbaselen;
84 if (slash == NULL
85 || (p->showbase == DOPRNT_SHOWBASE_NONZERO && slash[1] == '0'))
86 den_showbaselen = 0;
88 if (p->showbase == DOPRNT_SHOWBASE_NONZERO && s[0] == '0')
89 showbaselen = 0;
91 /* the influence of p->prec on mpq is currently undefined */
92 zeros = MAX (0, p->prec - slen);
94 /* space left over after actual output length */
95 justlen = p->width
96 - (strlen(s) + signlen + showbaselen + den_showbaselen + zeros);
98 justify = p->justify;
99 if (justlen <= 0) /* no justifying if exceed width */
100 justify = DOPRNT_JUSTIFY_NONE;
102 if (justify == DOPRNT_JUSTIFY_RIGHT) /* pad right */
103 DOPRNT_REPS (p->fill, justlen);
105 DOPRNT_REPS_MAYBE (sign, signlen); /* sign */
107 DOPRNT_MEMORY_MAYBE (showbase, showbaselen); /* base */
109 DOPRNT_REPS_MAYBE ('0', zeros); /* zeros */
111 if (justify == DOPRNT_JUSTIFY_INTERNAL) /* pad internal */
112 DOPRNT_REPS (p->fill, justlen);
114 /* if there's a showbase on the denominator, then print the numerator
115 separately so it can be inserted */
116 if (den_showbaselen != 0)
118 ASSERT (slash != NULL);
119 slashlen = slash+1 - s;
120 DOPRNT_MEMORY (s, slashlen); /* numerator and slash */
121 slen -= slashlen;
122 s += slashlen;
123 DOPRNT_MEMORY (showbase, den_showbaselen);
126 DOPRNT_MEMORY (s, slen); /* number, or denominator */
128 if (justify == DOPRNT_JUSTIFY_LEFT) /* pad left */
129 DOPRNT_REPS (p->fill, justlen);
131 done:
132 return retval;
134 error:
135 retval = -1;
136 goto done;