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 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/lib/libc/stdio/printfcommon.h,v 1.4 2009/01/22 08:14:28 das Exp $
36 * This file defines common routines used by both printf and wprintf.
37 * You must define CHAR to either char or wchar_t prior to including this.
41 #ifndef NO_FLOATING_POINT
44 #define freedtoa __freedtoa
53 static int exponent(CHAR *, int, CHAR);
55 #endif /* !NO_FLOATING_POINT */
57 static CHAR *__ujtoa(uintmax_t, CHAR *, int, int, const char *);
58 static CHAR *__ultoa(u_long, CHAR *, int, int, const char *);
63 struct __suio uio; /* output information: summary */
64 struct __siov iov[NIOV];/* ... and individual io vectors */
68 io_init(struct io_state *iop, FILE *fp)
71 iop->uio.uio_iov = iop->iov;
72 iop->uio.uio_resid = 0;
73 iop->uio.uio_iovcnt = 0;
78 * WARNING: The buffer passed to io_print() is not copied immediately; it must
79 * remain valid until io_flush() is called.
82 io_print(struct io_state *iop, const CHAR * __restrict ptr, int len)
85 iop->iov[iop->uio.uio_iovcnt].iov_base = __DECONST(char *, ptr);
86 iop->iov[iop->uio.uio_iovcnt].iov_len = len;
87 iop->uio.uio_resid += len;
88 if (++iop->uio.uio_iovcnt >= NIOV)
89 return (__sprint(iop->fp, &iop->uio));
95 * Choose PADSIZE to trade efficiency vs. size. If larger printf
96 * fields occur frequently, increase PADSIZE and make the initialisers
99 #define PADSIZE 16 /* pad chunk size */
100 static const CHAR blanks[PADSIZE] =
101 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
102 static const CHAR zeroes[PADSIZE] =
103 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
106 * Pad with blanks or zeroes. 'with' should point to either the blanks array
107 * or the zeroes array.
110 io_pad(struct io_state *iop, int howmany, const CHAR * __restrict with)
114 while (howmany > 0) {
115 n = (howmany >= PADSIZE) ? PADSIZE : howmany;
116 if (io_print(iop, with, n))
124 * Print exactly len characters of the string spanning p to ep, truncating
125 * or padding with 'with' as necessary.
128 io_printandpad(struct io_state *iop, const CHAR *p, const CHAR *ep,
129 int len, const CHAR * __restrict with)
137 if (io_print(iop, p, p_len))
142 return (io_pad(iop, len - p_len, with));
146 io_flush(struct io_state *iop)
149 return (__sprint(iop->fp, &iop->uio));
153 * Convert an unsigned long to ASCII for printf purposes, returning
154 * a pointer to the first character of the string representation.
155 * Octal numbers can be forced to have a leading zero; hex numbers
156 * use the given digits.
159 __ultoa(u_long val, CHAR *endp, int base, int octzero, const char *xdigs)
165 * Handle the three cases separately, in the hope of getting
166 * better/faster code.
170 if (val < 10) { /* many numbers are 1 digit */
171 *--cp = to_char(val);
175 * On many machines, unsigned arithmetic is harder than
176 * signed arithmetic, so we do at most one unsigned mod and
177 * divide; this is sufficient to reduce the range of
178 * the incoming value to where signed arithmetic works.
180 if (val > LONG_MAX) {
181 *--cp = to_char(val % 10);
186 *--cp = to_char(sval % 10);
193 *--cp = to_char(val & 7);
196 if (octzero && *cp != '0')
202 *--cp = xdigs[val & 15];
213 /* Identical to __ultoa, but for intmax_t. */
215 __ujtoa(uintmax_t val, CHAR *endp, int base, int octzero, const char *xdigs)
220 /* quick test for small values; __ultoa is typically much faster */
221 /* (perhaps instead we should run until small, then call __ultoa?) */
222 if (val <= ULONG_MAX)
223 return (__ultoa((u_long)val, endp, base, octzero, xdigs));
227 *--cp = to_char(val % 10);
230 if (val > INTMAX_MAX) {
231 *--cp = to_char(val % 10);
236 *--cp = to_char(sval % 10);
243 *--cp = to_char(val & 7);
246 if (octzero && *cp != '0')
252 *--cp = xdigs[val & 15];
263 #ifndef NO_FLOATING_POINT
266 exponent(CHAR *p0, int exp, CHAR fmtch)
269 CHAR expbuf[MAXEXPDIG];
279 t = expbuf + MAXEXPDIG;
282 *--t = to_char(exp % 10);
283 } while ((exp /= 10) > 9);
285 for (; t < expbuf + MAXEXPDIG; *p++ = *t++);
289 * Exponents for decimal floating point conversions
290 * (%[eEgG]) must be at least two characters long,
291 * whereas exponents for hexadecimal conversions can
292 * be only one character long.
294 if (fmtch == 'e' || fmtch == 'E')
301 #endif /* !NO_FLOATING_POINT */