Issue 7341 - writefln of strings array with size formatting
Summary: writefln of strings array with size formatting
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P3 normal
Assignee: No Owner
URL:
Keywords: bootcamp
: 9592 (view as issue list)
Depends on:
Blocks:
 
Reported: 2012-01-21 15:28 UTC by bearophile_hugs
Modified: 2024-12-01 16:14 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2012-01-21 15:28:00 UTC
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"]
Comment 1 SomeDude 2012-04-21 05:12:33 UTC
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
Comment 2 Kenji Hara 2012-04-21 06:26:55 UTC
(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.
Comment 3 Stewart Gordon 2012-04-21 08:48:54 UTC
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?
Comment 4 SomeDude 2012-04-21 12:07:22 UTC
(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.
Comment 5 berni44 2019-12-12 12:06:48 UTC
*** Issue 9592 has been marked as a duplicate of this issue. ***
Comment 6 dlangBugzillaToGithub 2024-12-01 16:14:49 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/9921

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB