1 /* __gmp_doprnt -- printf 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-2003 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
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
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 #define _GNU_SOURCE /* for DECIMAL_POINT in glibc langinfo.h */
37 #include "config.h" /* needed for the HAVE_, could also move gmp incls */
40 #include <ctype.h> /* for isdigit */
41 #include <stddef.h> /* for ptrdiff_t */
43 #include <stdio.h> /* for NULL */
47 # include <inttypes.h> /* for intmax_t */
55 #include <langinfo.h> /* for nl_langinfo */
59 #include <locale.h> /* for localeconv */
63 #include <sys/types.h> /* for quad_t */
70 /* change this to "#define TRACE(x) x" for diagnostics */
74 /* Should be portable, but in any case this is only used under some ASSERTs. */
75 #define va_equal(x, y) \
76 (memcmp (&(x), &(y), sizeof(va_list)) == 0)
79 /* printf is convenient because it allows various types to be printed in one
80 fairly compact call, so having gmp_printf support the standard types as
81 well as the gmp ones is important. This ends up meaning all the standard
82 parsing must be duplicated, to get a new routine recognising the gmp
85 With the currently favoured handling of mpz etc as Z, Q and F type
86 markers, it's not possible to use glibc register_printf_function since
87 that only accepts new conversion characters, not new types. If Z was a
88 conversion there'd be no way to specify hex, decimal or octal, or
89 similarly with F no way to specify fixed point or scientific format.
91 It seems wisest to pass conversions %f, %e and %g of float, double and
92 long double over to the standard printf. It'd be hard to be sure of
93 getting the right handling for NaNs, rounding, etc. Integer conversions
94 %d etc and string conversions %s on the other hand could be easily enough
95 handled within gmp_doprnt, but if floats are going to libc then it's just
96 as easy to send all non-gmp types there.
98 "Z" was a type marker for size_t in old glibc, but there seems no need to
99 provide access to that now "z" is standard.
101 In GMP 4.1.1 we documented "ll" and "L" as being equivalent, but in C99
102 in fact "ll" is just for long long and "L" just for long double.
103 Apparently GLIBC allows "L" for long long though. This doesn't affect
104 us as such, since both are passed through to the C library. To be
105 consistent with what we said before, the two are treated equivalently
106 here, and it's left to the C library to do what it thinks with them.
110 "b" might be nice for binary output, and could even be supported for the
111 standard C types too if desired.
113 POSIX style "%n$" parameter numbering would be possible, but would need
114 to be handled completely within gmp_doprnt, since the numbering will be
115 all different once the format string it cut into pieces.
117 Some options for mpq formatting would be good. Perhaps a non-zero
118 precision field could give a width for the denominator and mean always
119 put a "/". A form "n+p/q" might interesting too, though perhaps that's
120 better left to applications.
122 Right now there's no way for an application to know whether types like
123 intmax_t are supported here. If configure is doing its job and the same
124 compiler is used for gmp as for the application then there shouldn't be
125 any problem, but perhaps gmp.h should have some preprocessor symbols to
126 say what libgmp can do. */
130 /* If a gmp format is the very first thing or there are two gmp formats with
131 nothing in between then we'll reach here with this_fmt == last_fmt and we
132 can do nothing in that case.
134 last_ap is always replaced after a FLUSH, so it doesn't matter if va_list
135 is a call-by-reference and the funs->format routine modifies it. */
139 if (this_fmt == last_fmt) \
141 TRACE (printf ("nothing to flush\n")); \
142 ASSERT (va_equal (this_ap, last_ap)); \
146 ASSERT (*this_fmt == '%'); \
148 TRACE (printf ("flush \"%s\"\n", last_fmt)); \
149 DOPRNT_FORMAT (last_fmt, last_ap); \
154 /* Parse up the given format string and do the appropriate output using the
155 given "funs" routines. The data parameter is passed through to those
159 __gmp_doprnt (const struct doprnt_funs_t
*funs
, void *data
,
160 const char *orig_fmt
, va_list orig_ap
)
162 va_list ap
, this_ap
, last_ap
;
163 size_t alloc_fmt_size
;
164 char *fmt
, *alloc_fmt
, *last_fmt
, *this_fmt
, *gmp_str
;
166 int type
, fchar
, *value
, seen_precision
;
167 struct doprnt_params_t param
;
169 TRACE (printf ("gmp_doprnt \"%s\"\n", orig_fmt
));
171 /* Don't modify orig_ap, if va_list is actually an array and hence call by
172 reference. It could be argued that it'd be more efficient to leave the
173 caller to make a copy if it cared, but doing so here is going to be a
174 very small part of the total work, and we may as well keep applications
176 va_copy (ap
, orig_ap
);
178 /* The format string is chopped up into pieces to be passed to
179 funs->format. Unfortunately that means it has to be copied so each
180 piece can be null-terminated. We're not going to be very fast here, so
181 use __gmp_allocate_func rather than TMP_ALLOC, to avoid overflowing the
182 stack if a long output string is given. */
183 alloc_fmt_size
= strlen (orig_fmt
) + 1;
185 /* for a long long limb we change %Mx to %llx, so could need an extra 1
186 char for every 3 existing */
187 alloc_fmt_size
+= alloc_fmt_size
/ 3;
189 alloc_fmt
= __GMP_ALLOCATE_FUNC_TYPE (alloc_fmt_size
, char);
191 memcpy (fmt
, orig_fmt
, alloc_fmt_size
);
193 /* last_fmt and last_ap are just after the last output, and hence where
194 the next output will begin, when that's done */
196 va_copy (last_ap
, ap
);
200 TRACE (printf ("next: \"%s\"\n", fmt
));
202 fmt
= strchr (fmt
, '%');
206 /* this_fmt and this_ap are the current '%' sequence being considered */
208 va_copy (this_ap
, ap
);
209 fmt
++; /* skip the '%' */
211 TRACE (printf ("considering\n");
212 printf (" last: \"%s\"\n", last_fmt
);
213 printf (" this: \"%s\"\n", this_fmt
));
216 value
= ¶m
.width
;
220 param
.expfmt
= "e%c%02ld";
223 param
.justify
= DOPRNT_JUSTIFY_RIGHT
;
225 param
.showbase
= DOPRNT_SHOWBASE_NO
;
227 param
.showtrailing
= 1;
232 /* This loop parses a single % sequence. "break" from the switch
233 means continue with this %, "goto next" means the conversion
234 character has been seen and a new % should be sought. */
244 /* %a behaves like %e, but defaults to all significant digits,
245 and there's no leading zeros on the exponent (which is in
248 param
.expfmt
= "p%c%ld";
252 param
.expfmt
= "P%c%ld";
254 param
.conv
= DOPRNT_CONV_SCIENTIFIC
;
256 if (! seen_precision
)
257 param
.prec
= -1; /* default to all digits */
258 param
.showbase
= DOPRNT_SHOWBASE_YES
;
259 param
.showtrailing
= 1;
263 /* Let's assume wchar_t will be promoted to "int" in the call,
264 the same as char will be. */
265 (void) va_arg (ap
, int);
272 TRACE (printf ("integer, base=%d\n", param
.base
));
273 if (! seen_precision
)
277 /* Let's assume uintmax_t is the same size as intmax_t. */
279 (void) va_arg (ap
, intmax_t);
281 ASSERT_FAIL (intmax_t not available
);
285 (void) va_arg (ap
, long);
289 (void) va_arg (ap
, long long);
291 ASSERT_FAIL (long long not available
);
297 mp_size_t xsize
, abs_xsize
;
300 xp
= va_arg (ap
, mp_ptr
);
302 xsize
= (int) va_arg (ap
, mp_size_t
);
303 abs_xsize
= ABS (xsize
);
304 MPN_NORMALIZE (xp
, abs_xsize
);
305 SIZ(z
) = (xsize
>= 0 ? abs_xsize
: -abs_xsize
);
306 ASSERT_CODE (ALLOC(z
) = abs_xsize
);
307 gmp_str
= mpz_get_str (NULL
, param
.base
, z
);
312 /* quad_t is probably the same as long long, but let's treat
313 it separately just to be sure. Also let's assume u_quad_t
314 will be the same size as quad_t. */
316 (void) va_arg (ap
, quad_t
);
318 ASSERT_FAIL (quad_t
not available
);
323 gmp_str
= mpq_get_str (NULL
, param
.base
, va_arg(ap
, mpq_srcptr
));
327 (void) va_arg (ap
, ptrdiff_t);
329 ASSERT_FAIL (ptrdiff_t not available
);
333 (void) va_arg (ap
, size_t);
339 gmp_str
= mpz_get_str (NULL
, param
.base
,
340 va_arg (ap
, mpz_srcptr
));
342 ret
= __gmp_doprnt_integer (funs
, data
, ¶m
, gmp_str
);
343 (*__gmp_free_func
) (gmp_str
, strlen(gmp_str
)+1);
344 DOPRNT_ACCUMULATE (ret
);
345 va_copy (last_ap
, ap
);
350 /* default is an "int", and this includes h=short and hh=char
351 since they're promoted to int in a function call */
352 (void) va_arg (ap
, int);
359 param
.expfmt
= "E%c%02ld";
362 param
.conv
= DOPRNT_CONV_SCIENTIFIC
;
364 if (param
.showbase
== DOPRNT_SHOWBASE_NONZERO
)
366 /* # in %e, %f and %g */
368 param
.showtrailing
= 1;
374 DOPRNT_ACCUMULATE (__gmp_doprnt_mpf (funs
, data
, ¶m
,
376 va_arg (ap
, mpf_srcptr
)));
377 va_copy (last_ap
, ap
);
382 (void) va_arg (ap
, long double);
384 ASSERT_FAIL (long double not available
);
388 (void) va_arg (ap
, double);
394 param
.conv
= DOPRNT_CONV_FIXED
;
397 case 'F': /* mpf_t */
398 case 'j': /* intmax_t */
399 case 'L': /* long long */
401 case 'q': /* quad_t */
402 case 'Q': /* mpq_t */
403 case 't': /* ptrdiff_t */
404 case 'z': /* size_t */
405 case 'Z': /* mpz_t */
412 param
.expfmt
= "E%c%02ld";
415 param
.conv
= DOPRNT_CONV_GENERAL
;
416 param
.showtrailing
= 0;
422 type
= 'H'; /* internal code for "hh" */
428 type
= 'L'; /* "ll" means "L" */
432 /* glibc strerror(errno), no argument */
435 case 'M': /* mp_limb_t */
436 /* mung format string to l or ll and let plain printf handle it */
438 memmove (fmt
+1, fmt
, strlen (fmt
)+1);
453 p
= va_arg (ap
, void *);
455 case '\0': * (int *) p
= retval
; break;
456 case 'F': mpf_set_si ((mpf_ptr
) p
, (long) retval
); break;
457 case 'H': * (char *) p
= retval
; break;
458 case 'h': * (short *) p
= retval
; break;
460 case 'j': * (intmax_t *) p
= retval
; break;
462 case 'j': ASSERT_FAIL (intmax_t not available
); break;
464 case 'l': * (long *) p
= retval
; break;
465 #if HAVE_QUAD_T && HAVE_LONG_LONG
467 ASSERT_ALWAYS (sizeof (quad_t
) == sizeof (long long));
470 case 'q': ASSERT_FAIL (quad_t
not available
); break;
473 case 'L': * (long long *) p
= retval
; break;
475 case 'L': ASSERT_FAIL (long long not available
); break;
480 n
= va_arg (ap
, mp_size_t
);
484 * (mp_ptr
) p
= retval
;
485 MPN_ZERO ((mp_ptr
) p
+ 1, n
- 1);
489 case 'Q': mpq_set_si ((mpq_ptr
) p
, (long) retval
, 1L); break;
491 case 't': * (ptrdiff_t *) p
= retval
; break;
493 case 't': ASSERT_FAIL (ptrdiff_t not available
); break;
495 case 'z': * (size_t *) p
= retval
; break;
496 case 'Z': mpz_set_si ((mpz_ptr
) p
, (long) retval
); break;
499 va_copy (last_ap
, ap
);
509 /* "void *" will be good enough for "char *" or "wchar_t *", no
510 need for separate code. */
511 (void) va_arg (ap
, const void *);
525 param
.showbase
= DOPRNT_SHOWBASE_NONZERO
;
529 /* glibc digit grouping, just pass it through, no support for it
539 param
.justify
= DOPRNT_JUSTIFY_LEFT
;
543 param
.prec
= -1; /* "." alone means all necessary digits */
549 int n
= va_arg (ap
, int);
551 if (value
== ¶m
.width
)
553 /* negative width means left justify */
556 param
.justify
= DOPRNT_JUSTIFY_LEFT
;
563 /* don't allow negative precision */
564 param
.prec
= MAX (0, n
);
570 if (value
== ¶m
.width
)
572 /* in width field, set fill */
575 /* for right justify, put the fill after any minus sign */
576 if (param
.justify
== DOPRNT_JUSTIFY_RIGHT
)
577 param
.justify
= DOPRNT_JUSTIFY_INTERNAL
;
581 /* in precision field, set value */
586 case '1': case '2': case '3': case '4': case '5':
587 case '6': case '7': case '8': case '9':
588 /* process all digits to form a value */
592 n
= n
* 10 + (fchar
-'0');
594 } while (isascii (fchar
) && isdigit (fchar
));
595 fmt
--; /* unget the non-digit */
601 /* something invalid */
608 /* Stop parsing the current "%" format, look for a new one. */
612 TRACE (printf ("remainder: \"%s\"\n", last_fmt
));
613 if (*last_fmt
!= '\0')
614 DOPRNT_FORMAT (last_fmt
, last_ap
);
616 if (funs
->final
!= NULL
)
617 if ((*funs
->final
) (data
) == -1)
621 (*__gmp_free_func
) (alloc_fmt
, alloc_fmt_size
);