bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: printf (_("...%zu..."), X) where X is of type size_t


From: Eric Blake
Subject: Re: printf (_("...%zu..."), X) where X is of type size_t
Date: Fri, 30 Sep 2005 23:05:52 +0000

> >> > +/* Determine a printf conversion specifier that is appropriate for 
> >> > size_t.
> >> > +   Ideally, we'd just use the c99-specified `z' length modifier, 
> >> > defining
> >> > +   PRI_z to "zu", but that's not portable.  */
> >> ...
> >> I thought about doing that a while ago but gave it up because it
> >> appears that this sort of approach will run afoul of gettext.
> > ...
> > Maybe it's time that we provide a gnulib module for printf 
> 
> Personally I'd rather not head down that path.  printf is the biggest
> cesspool in the C library, and lots of things that C likes to sweep
> under the rug finds its way into printf somehow: floating-point
> accuracy, cancellation points, multibyte encoding errors, signal
> handling, storage allocation, wrong-sized types for the job, etc.,
> etc.  I suppose we could drag glibc's printf into gnulib, but I
> wouldn't envy the task of doing that, or maintaining the result.

When I wrote my suggestion, I was thinking more along the
lines of a thin wrapper, whose sole job in life is to translate the
format string into something that the system's vprintf can already
handle, without ever unwrapping the va_args itself.  For that
matter, since the translation does not need to know what the
va_args are, maybe my suggestion is better written like this:

printf (printf_format (_("The size is %zu.")), size);

where we only provide printf_format, and its job is
to turn the "%z" into "%l" or "%ll" in the translation,
as appropriate.

> Perhaps GNU extensions PRIoSIZE, PRIuSIZE, PRIxSIZE, PRIXSIZE could be
> added to GNU gettext.  That would mean we could write this instead:
> 
>      printf (_("The size is %"PRIuSIZE".\n"), size);
> 
> which would be nicer than either of the above.

But your idea of formalizing PRI?SIZE as part of <gettext.h> is
by far the nicest approach. 

> 
> It is curious that <inttypes.h> lacks the PRI* macros for size_t.
> I wonder why they left them out?

They also left out PRI* macros for ptrdiff_t (%t), since PRIdPTR
applies to intptr_t instead.  Plus, it might be nice to have PRIdSIZE
and PRIiSIZE for ssize_t (although that assumes that ssize_t
and size_t map to the same integer type - %zd is only specified
to operate on the corresponding signed type matching size_t,
which is not necessarily ssize_t).

--
Eric Blake






reply via email to

[Prev in Thread] Current Thread [Next in Thread]