FormatSpec struct
James Miller
james at aatch.net
Fri Apr 13 00:16:48 PDT 2012
* Paul D. Anderson <paul.d.removethis.anderson at 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.
>
> 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
>
>
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
More information about the Digitalmars-d-learn
mailing list