Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

writefln of strings array with size formatting #9921

Open
dlangBugzillaToGithub opened this issue Jan 21, 2012 · 5 comments
Open

writefln of strings array with size formatting #9921

dlangBugzillaToGithub opened this issue Jan 21, 2012 · 5 comments

Comments

@dlangBugzillaToGithub
Copy link

bearophile_hugs reported this on 2012-01-21T15:28:00Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=7341

CC List

Description

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"]
@dlangBugzillaToGithub
Copy link
Author

lovelydear commented on 2012-04-21T05:12:33Z

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

@dlangBugzillaToGithub
Copy link
Author

k.hara.pg commented on 2012-04-21T06:26:55Z

(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.

@dlangBugzillaToGithub
Copy link
Author

smjg commented on 2012-04-21T08:48:54Z

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?

@dlangBugzillaToGithub
Copy link
Author

lovelydear commented on 2012-04-21T12:07:22Z

(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.

@dlangBugzillaToGithub
Copy link
Author

bugzilla (@WalterBright) commented on 2019-12-12T12:06:48Z

*** Issue 9592 has been marked as a duplicate of this issue. ***

@thewilsonator thewilsonator removed P3 OS:Windows Issues Specific to Windows Arch:x86 Issues specific to x86 labels Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants