Thread overview
printf/scanf extensions
Jun 30, 2004
Sean Kelly
Jun 30, 2004
Ben Hinkle
Jun 30, 2004
Walter
Jun 30, 2004
Sean Kelly
Jun 30, 2004
Walter
Jun 30, 2004
Sean Kelly
June 30, 2004
I've been working on a scanf implementation as part of my stream mods, and I've started wondering about D-specific extensions.  I've already added support for the %.*s parameter for D strings, and have started to think about complex and imaginary numbers.  Should I even bother trying to parse "A + Bi" as a single number or should the user be expected to call scanf( "%f + %fi", cd.re, cd.im ) to parse this type of thing?


Sean


June 30, 2004
Sean Kelly wrote:

> I've been working on a scanf implementation as part of my stream mods, and
> I've
> started wondering about D-specific extensions.  I've already added support
> for the %.*s parameter for D strings, and have started to think about
> complex and
> imaginary numbers.  Should I even bother trying to parse "A + Bi" as a
> single number or should the user be expected to call scanf( "%f + %fi",
> cd.re, cd.im ) to parse this type of thing?

One advantage of a builtin complex support would be handling negative values
for B and zero for A. Otherwise if the code had scanf("%f + %fi") then
users would have to enter, for example, 0+-2i instead of the more natural
-2i. So I'd say go for it. I don't know if any other printf extension has
done this already - if there were we could check out their syntax. Off the
top of my head the format character "j" could be used - and "j" is commonly
used in engineering as the imaginary unit so it is a natural choice. That
would mean "%j" would parse a cfloat, "%lj" a cdouble and "%Lj" a creal. By
"parse" it would accept numbers of the form
 %f %fi
 %fi
 %f %fj
 %fj


> 
> Sean

June 30, 2004
I'm working on a replacement for printf, so don't bother with that <g>.

"Sean Kelly" <sean@f4.ca> wrote in message news:cbt0je$d8r$1@digitaldaemon.com...
> I've been working on a scanf implementation as part of my stream mods, and
I've
> started wondering about D-specific extensions.  I've already added support
for
> the %.*s parameter for D strings, and have started to think about complex
and
> imaginary numbers.  Should I even bother trying to parse "A + Bi" as a
single
> number or should the user be expected to call scanf( "%f + %fi", cd.re,
cd.im )
> to parse this type of thing?
>
>
> Sean
>
>


June 30, 2004
In article <cbtik8$183l$1@digitaldaemon.com>, Walter says...
>
>I'm working on a replacement for printf, so don't bother with that <g>.

That's what inspired me to ask this question :)  By the way, I've been mulling over the width specifier and dealing with it seems like kind of a pain.  Assume I have a file containing this:

-123 0x456

and I do this:

scanf( "%1i%i %2i%i", &a, &b, &c, &d );

The way I read the spec, I think I would end up with this:

a: 0
b: 123
c: 0
d: 456 (base 10)

Is this correct?  It seems odd that creative use of the width specifier can cause a sign or base specifier to be lost.  I've even considered throwing some kind of exception when this happens, but haven't decided whether that would be a good idea.  On the other hand, I'm sure the optional sign and such need to be counted as part of the value width (they're not in the current stream implementation) since the specifier is meant to allow reads from fixed-width files.


Sean


June 30, 2004
"Sean Kelly" <sean@f4.ca> wrote in message news:cbuvj5$tm3$1@digitaldaemon.com...
> In article <cbtik8$183l$1@digitaldaemon.com>, Walter says...
> >
> >I'm working on a replacement for printf, so don't bother with that <g>.
>
> That's what inspired me to ask this question :)  By the way, I've been
mulling
> over the width specifier and dealing with it seems like kind of a pain.
Assume
> I have a file containing this:
>
> -123 0x456
>
> and I do this:
>
> scanf( "%1i%i %2i%i", &a, &b, &c, &d );
>
> The way I read the spec, I think I would end up with this:
>
> a: 0
> b: 123
> c: 0
> d: 456 (base 10)
>
> Is this correct?  It seems odd that creative use of the width specifier
can
> cause a sign or base specifier to be lost.  I've even considered throwing
some
> kind of exception when this happens, but haven't decided whether that
would be a
> good idea.  On the other hand, I'm sure the optional sign and such need to
be
> counted as part of the value width (they're not in the current stream implementation) since the specifier is meant to allow reads from
fixed-width
> files.

I think the 'x' where a number is expected would/should cause an error.


June 30, 2004
In article <cbv8fu$1alf$2@digitaldaemon.com>, Walter says...
>
>I think the 'x' where a number is expected would/should cause an error.

Oops, good point.  So it's only the sign char and leading '0' for octal numbers that might cause problems.  I guess then, width<=2 for %i could do weird things or width=1 for any integer type.  I'm going to ignore the problem for now as the standard doesn't seem to care, but will perhaps revisit it if I decide to start throwing exceptions out of scanf.

Sean