www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - FormatSpec struct

reply "Paul D. Anderson" <paul.d.removethis.anderson comcast.andthis.net> writes:
I'm trying to add formatted output to my decimal arithmetic 
module. Decimals should format like floating point, using 'E', 
'F' and 'G', etc.

I would expect a format string like "%9.6e" to parse as width = 
9, precision = 6, using exponential notation.

In std.format there is a FormatSpec struct that looks as if it 
will do the parsing for me. As far as I can tell the usage is:

      auto spec = std.format.FormatSpec!char("9.6e");
      writeln("fmtspec = ", fmtspec);

But it doesn't do what I think it should do.

The output of the method is:

fmtspec = address = 1637116
width = 0
precision = 2147483646
spec = s
indexStart = 0
indexEnd = 0
flDash = false
flZero = false
flSpace = false
flPlus = false
flHash = false
nested =
trailing = 9.6e

The width field should be 9, the precision field should be 6, and 
the spec field should be 'e'. Instead it seems to disregard the 
input string and return a default FormatSpec, with only the 
'trailing' field populated, containing the input.

What am I missing here? I've tried variations -- "%9.6e", "s", 
"%s", etc, but the input is always relegated to the trailing 
field.

Paul
Apr 12 2012
next sibling parent James Miller <james aatch.net> writes:
* James Miller <james aatch.net> [2012-04-13 19:16:48 +1200]:
 * Paul D. Anderson <paul.d.removethis.anderson comcast.andthis.net>
[2012-04-13 07:50:31 +0200]:
 I'm trying to add formatted output to my decimal arithmetic module.
 Decimals should format like floating point, using 'E', 'F' and 'G',
 etc.
 
 I would expect a format string like "%9.6e" to parse as width = 9,
 precision = 6, using exponential notation.
 

Hey Paul, so some investigation has led me to believe that FormatSpec is really just for internal usage. The documentation is a bit misleading (to the point of being possibly completely false). FormatSpec, AFAICT, is essentially just a parser for the standard format specifier, but its not very clear as to proper usage. I'm going to try to improve it and submit a pull request, until then looking at the source code for std.format should give you some idea of how to best use it. -- James Miller

So I made the pull request, the documentation you need to read is here: https://github.com/Aatch/phobos/commit/cda3c079ee32d98a017f88949c10097840baa075 Hopefully it helps. -- James Miller
Apr 13 2012
prev sibling parent "Paul D. Anderson" <paul.d.removethis.anderson comcast.andthis.net> writes:
On Friday, 13 April 2012 at 09:10:37 UTC, James Miller wrote:
<snip/>
 So I made the pull request, the documentation you need to read 
 is here:

 https://github.com/Aatch/phobos/commit/cda3c079ee32d98a017f88949c10097840baa075

 Hopefully it helps.

 --
 James Miller

Thanks. That did the trick. Paul
Apr 13 2012