Re: Why doesn't the standard allow reuse of arguments in printf() ?



"David R Tribble" <david@xxxxxxxxxxx> wrote in message
news:1153004809.505381.156810@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Wojtek Lerch wrote:
Instead, POSIX/XSI chose to require that all arguments from 1$ to N$
must
be specified in the format (and don't have to be the same type), and
that
you can't mix numbered specifications with unnumbered ones in the same
call (which makes advancing the pointer a non-issue when numbered
specifications are used). Do you think that's still underspecified?


P.J. Plauger wrote:
I should have clarified that my web search turned up three (count 'em 3)
different descriptions of the N$ mechanism. Two described themselves as
official and were mutually contradictory. The third was simply vague.
Since the customer wanted our library to "do what everyone else does"
I concluded that none of these specs was a safe guide to actual practice.

Admittedly, the Open Group spec might be self consistent. (The reason
for the "might be" appears below.) It simply requires printf to scan the
format string N times to obtain the Nth argument, or to stack up all N
arguments in one pass by some unspecified but cumbrous mechanism. That's
the only way to safely skip over a heterogeneous sequence of arguments.

The "cumbrous" method I used was a single scan that kept a stack
of descriptors for each $n specifier found in a left-to-right scan of
the format string. Each descriptor contained the format specifier
type (e.g., 'd', 's', 'x', etc.). This provides enough information to
extract the arguments from the call stack. This approach also
allows you to verify that all format specifiers $1 through $n are
actually present in the format string.

Our printf family has 29 different calls to va_arg, each effectively
fetching a different type of argument. By the time you duplicate the
logic for determining which of those 29 argument types you want to
fetch, and the 29-way switch for getting the argument, it starts
looking cumbrous to me. YMMV.

The only trick is preallocating enough $n descriptors for the
worst-case format string. If all you need for each descriptor is
the % datatype, they can be implemented as a character array,
perhaps 80 or so chars in length (where it's assumed that 80
should be enough for most users). Or more elaborate dynamic
allocations can be used to handle an unlimited number of $n
specifiers.

Any library function that requires an open-ended amount of storage,
or a guess at a safe upper bound, starts looking cumbrous to me.
Admittedly, five bits per argument ain't all that bad, but the
code duplication I cited above is at best graceless and bloated,
at worst a maintenance nightmare.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com


.



Relevant Pages

  • Re: Fundamental lexical tokens and format specifications
    ... the "fundamental lexical tokens of a program unit". ... How do those relate to the bits and pieces inside the format ... specifications. ... change is that is just allows you to pop in _extra_ blanks, ...
    (comp.lang.fortran)
  • Re: Why doesnt the standard allow reuse of arguments in printf() ?
    ... you can't mix numbered specifications with unnumbered ones in the same ... format string N times to obtain the Nth argument, or to stack up all N ... The only trick is preallocating enough $n descriptors for the ...
    (comp.std.c)
  • Re: silly question
    ... There are many other options using the printf format String that contains the text to be printed. ... the other flags, width, .precision and modifiers sub-parameters are optional and follow these specifications: ... The width is not specified in the format string, it is specified by an integer value preceding the argument thas has to be formatted. ...
    (comp.lang.smalltalk.dolphin)
  • Re: IC v. Employee question
    ... Anyone typing that work, IC or employee, would have been ... have specifications for format, style, etc., and should you choose to submit ... the document in another format, it might end up in the circular file. ... imagine JAMA and other such journals expect their contributors will be able to ...
    (sci.med.transcription)
  • Re: main
    ... most code you'll find out there you have this format of main ... You're assuming that the "grep" command is available and does what you ... int main(int argc, char **argv) ... i've specified, also, that the command ONLY works for specifications ...
    (comp.lang.c)