digitalmars.D - printf/scanf extensions
- Sean Kelly (7/7) Jun 29 2004 I've been working on a scanf implementation as part of my stream mods, a...
- Ben Hinkle (14/24) Jun 29 2004 One advantage of a builtin complex support would be handling negative va...
-
Walter
(8/15)
Jun 29 2004
I'm working on a replacement for printf, so don't bother with that
. - Sean Kelly (20/21) Jun 30 2004 That's what inspired me to ask this question :) By the way, I've been m...
- Walter (10/31) Jun 30 2004 mulling
- Sean Kelly (7/8) Jun 30 2004 Oops, good point. So it's only the sign char and leading '0' for octal ...
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
Jun 29 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 %fjSean
Jun 29 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, andI'vestarted wondering about D-specific extensions. I've already added supportforthe %.*s parameter for D strings, and have started to think about complexandimaginary numbers. Should I even bother trying to parse "A + Bi" as asinglenumber or should the user be expected to call scanf( "%f + %fi", cd.re,cd.im )to parse this type of thing? Sean
Jun 29 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
Jun 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...mullingI'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 beenover the width specifier and dealing with it seems like kind of a pain.AssumeI 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 specifiercancause a sign or base specifier to be lost. I've even considered throwingsomekind of exception when this happens, but haven't decided whether thatwould be agood idea. On the other hand, I'm sure the optional sign and such need tobecounted as part of the value width (they're not in the current stream implementation) since the specifier is meant to allow reads fromfixed-widthfiles.I think the 'x' where a number is expected would/should cause an error.
Jun 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
Jun 30 2004