2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Copyright (c) 2011 The FreeBSD Foundation
10 * Portions of this software were developed by David Chisnall
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * $FreeBSD: head/lib/libc/stdio/printfcommon.h 227753 2011-11-20 14:45:42Z theraven $
41 * This file defines common routines used by both printf and wprintf.
42 * You must define CHAR to either char or wchar_t prior to including this.
46 #ifndef NO_FLOATING_POINT
49 #define freedtoa __freedtoa
58 static int exponent(CHAR *, int, CHAR);
60 #endif /* !NO_FLOATING_POINT */
62 static CHAR *__ujtoa(uintmax_t, CHAR *, int, int, const char *);
63 static CHAR *__ultoa(u_long, CHAR *, int, int, const char *);
68 struct __suio uio; /* output information: summary */
69 struct __siov iov[NIOV];/* ... and individual io vectors */
73 io_init(struct io_state *iop, FILE *fp)
76 iop->uio.uio_iov = iop->iov;
77 iop->uio.uio_resid = 0;
78 iop->uio.uio_iovcnt = 0;
83 * WARNING: The buffer passed to io_print() is not copied immediately; it must
84 * remain valid until io_flush() is called.
87 io_print(struct io_state *iop, const CHAR * __restrict ptr, int len, locale_t locale)
90 iop->iov[iop->uio.uio_iovcnt].iov_base = (char *)ptr;
91 iop->iov[iop->uio.uio_iovcnt].iov_len = len;
92 iop->uio.uio_resid += len;
93 if (++iop->uio.uio_iovcnt >= NIOV)
94 return (__sprint(iop->fp, &iop->uio, locale));
100 * Choose PADSIZE to trade efficiency vs. size. If larger printf
101 * fields occur frequently, increase PADSIZE and make the initialisers
104 #define PADSIZE 16 /* pad chunk size */
105 static const CHAR blanks[PADSIZE] =
106 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
107 static const CHAR zeroes[PADSIZE] =
108 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
111 * Pad with blanks or zeroes. 'with' should point to either the blanks array
112 * or the zeroes array.
115 io_pad(struct io_state *iop, int howmany, const CHAR * __restrict with,
120 while (howmany > 0) {
121 n = (howmany >= PADSIZE) ? PADSIZE : howmany;
122 if (io_print(iop, with, n, locale))
130 * Print exactly len characters of the string spanning p to ep, truncating
131 * or padding with 'with' as necessary.
134 io_printandpad(struct io_state *iop, const CHAR *p, const CHAR *ep,
135 int len, const CHAR * __restrict with, locale_t locale)
143 if (io_print(iop, p, p_len, locale))
148 return (io_pad(iop, len - p_len, with, locale));
152 io_flush(struct io_state *iop, locale_t locale)
155 return (__sprint(iop->fp, &iop->uio, locale));
159 * Convert an unsigned long to ASCII for printf purposes, returning
160 * a pointer to the first character of the string representation.
161 * Octal numbers can be forced to have a leading zero; hex numbers
162 * use the given digits.
165 __ultoa(u_long val, CHAR *endp, int base, int octzero, const char *xdigs)
171 * Handle the three cases separately, in the hope of getting
172 * better/faster code.
176 if (val < 10) { /* many numbers are 1 digit */
177 *--cp = to_char(val);
181 * On many machines, unsigned arithmetic is harder than
182 * signed arithmetic, so we do at most one unsigned mod and
183 * divide; this is sufficient to reduce the range of
184 * the incoming value to where signed arithmetic works.
186 if (val > LONG_MAX) {
187 *--cp = to_char(val % 10);
192 *--cp = to_char(sval % 10);
199 *--cp = to_char(val & 7);
202 if (octzero && *cp != '0')
208 *--cp = xdigs[val & 15];
219 /* Identical to __ultoa, but for intmax_t. */
221 __ujtoa(uintmax_t val, CHAR *endp, int base, int octzero, const char *xdigs)
226 /* quick test for small values; __ultoa is typically much faster */
227 /* (perhaps instead we should run until small, then call __ultoa?) */
228 if (val <= ULONG_MAX)
229 return (__ultoa((u_long)val, endp, base, octzero, xdigs));
232 if (val > INTMAX_MAX) {
233 *--cp = to_char(val % 10);
238 *--cp = to_char(sval % 10);
245 *--cp = to_char(val & 7);
248 if (octzero && *cp != '0')
254 *--cp = xdigs[val & 15];
265 #ifndef NO_FLOATING_POINT
268 exponent(CHAR *p0, int exp, CHAR fmtch)
271 CHAR expbuf[MAXEXPDIG];
281 t = expbuf + MAXEXPDIG;
284 *--t = to_char(exp % 10);
285 } while ((exp /= 10) > 9);
287 for (; t < expbuf + MAXEXPDIG; *p++ = *t++);
291 * Exponents for decimal floating point conversions
292 * (%[eEgG]) must be at least two characters long,
293 * whereas exponents for hexadecimal conversions can
294 * be only one character long.
296 if (fmtch == 'e' || fmtch == 'E')
303 #endif /* !NO_FLOATING_POINT */