Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #555 (closed defect: wontfix)

Opened 12 years ago

Last modified 12 years ago

Format specifiers in format strings only work as the first character

Reported by: Deewiant Assigned to: sean
Priority: major Milestone: 1.0
Component: Core Functionality Version: trunk
Keywords: Cc:

Description

import tango.io.Stdout;

void main() {
	Stdout.formatln("{:3x}", 100);
	Stdout.formatln("{:4e}", 100.0);
}

Expected results are "064" and "1.00000e+02".

The actual results are "100" and "100.0000".

Clearly, the 'x' and 'e' are being ignored. This can be traced to the tango.text.convert.Layout.parse2 function:

        private T parse2 (T[] format, inout uint width, T def=T.init)
        {
                uint number;
                foreach (c; format)
                         if (c >= '0' && c <= '9')
                             number = number * 10 + c - '0';

                width = number;
                return format.length > 0 ? format[0] : def;
        }

Which always returns format[0] as the specifier for a valid format string. Here's a somewhat modified working version:

        private T parse2 (T[] format, inout uint width, T def=T.init)
        {
                size_t i = 0;

                foreach (c; format)
                if (c >= '0' && c <= '9') {
                        width = width * 10 + c - '0';
                        ++i;
                }

                return i < format.length ? format[i] : def;
        }

Change History

08/01/07 16:33:39 changed by kris

  • status changed from new to closed.
  • resolution set to wontfix.

the optional format specifier is the first char after the ':' and not elsewhere

08/01/07 16:42:34 changed by Deewiant

How would you duplicate the "{:4e}" case then, for a wanted precision of 4?

08/01/07 16:55:36 changed by kris

would {:e4} do it?

08/01/07 17:00:17 changed by Deewiant

<slaps head>

It would, wouldn't it, since this isn't printf formatting. Sorry, I'm too used to typing stuff like %4d.

08/01/07 17:14:18 changed by kris

hehe ... no worries ... I thought perhaps you were onto something, but wasn't sure :)