Thread overview
[Issue 7341] New: writefln of strings array with size formatting
Apr 21, 2012
SomeDude
Apr 21, 2012
Kenji Hara
Apr 21, 2012
Stewart Gordon
Apr 21, 2012
SomeDude
January 21, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7341

           Summary: writefln of strings array with size formatting
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2012-01-21 15:28:00 PST ---
D2 code:

import std.stdio;
void main() {
    int[] a1 = [1, 10, 5];
    writefln("%12d", a1);
    string[] a2 = ["red", "yellow", "ya"];
    writefln("%12s", a2);
}



Output:
[           1,           10,            5]
["red", "yellow", "ya"]



But I expect an output more like:
[           1,           10,            5]
[       "red",     "yellow",         "ya"]

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 21, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7341


SomeDude <lovelydear@mailmetrash.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lovelydear@mailmetrash.com


--- Comment #1 from SomeDude <lovelydear@mailmetrash.com> 2012-04-21 05:12:33 PDT ---
Was writefln supposed to work or arrays ? Here is what I get on 2.059:

PS E:\DigitalMars\dmd2\samples> rdmd bug.d object.Exception@E:\DigitalMars\dmd2\windows\bin\..\..\src\phobos\std\format.d(1886): Incorrect format specifier for range: %d

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 21, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7341



--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2012-04-21 06:26:55 PDT ---
(In reply to comment #0)
> D2 code:
> 
> import std.stdio;
> void main() {
>     int[] a1 = [1, 10, 5];
>     writefln("%12d", a1);
>     string[] a2 = ["red", "yellow", "ya"];
>     writefln("%12s", a2);
> }
> 
> Output:
> [           1,           10,            5]
> ["red", "yellow", "ya"]
> 
> But I expect an output more like:
> [           1,           10,            5]
> [       "red",     "yellow",         "ya"]

If you want to give format specifier for elements explicitly, you should use
compound format specifier (It is %( and %).)

    writefln("[%(%12d, %)]", [1, 10, 5]);
    writefln("[%(%12s, %)]", ["red", "yellow", "ya"]);

But, this code output:
[           1,           10,            5]
["red", "yellow", "ya"]

Because, string elements and character elements are treated specially. They are quoted, and other specifications are ignored.

https://github.com/D-Programming-Language/phobos/blob/master/std/format.d#L1930

But, I agree it is debatable thing.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 21, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7341


Stewart Gordon <smjg@iname.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg@iname.com


--- Comment #3 from Stewart Gordon <smjg@iname.com> 2012-04-21 08:48:54 PDT ---
The reason for the original behaviour seems to be that the width specified for %s is taken to be the width to which the whole argument is formatted, not the width to which each element of the array is formatted.

But I'm getting the same error as SomeDude (DMD 2.059, Win32).  Kenji - what setup do you have that's giving a different result?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
April 21, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7341



--- Comment #4 from SomeDude <lovelydear@mailmetrash.com> 2012-04-21 12:07:22 PDT ---
(In reply to comment #3)
> But I'm getting the same error as SomeDude (DMD 2.059, Win32).  Kenji - what setup do you have that's giving a different result?

He wrote it. The code that's supposed to work is:

import std.stdio;
void main() {
    writefln("[%(%12d, %)]", [1, 10, 5]);
    writefln("[%(%12s, %)]", ["red", "yellow", "ya"]);
}

Not the original test example.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------