Bug 7341 – writefln of strings array with size formatting

Status
NEW
Severity
normal
Priority
P3
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2012-01-21T15:28:00Z
Last change time
2024-12-01T16:14:49Z
Keywords
bootcamp
Assigned to
No Owner
Creator
bearophile_hugs
See also
https://issues.dlang.org/show_bug.cgi?id=3248
Moved to GitHub: phobos#9921 →

Comments

Comment #0 by bearophile_hugs — 2012-01-21T15:28:00Z
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 by lovelydear — 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
Comment #2 by k.hara.pg — 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.
Comment #3 by smjg — 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?
Comment #4 by lovelydear — 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.
Comment #5 by bugzilla — 2019-12-12T12:06:48Z
*** Issue 9592 has been marked as a duplicate of this issue. ***
Comment #6 by robert.schadek — 2024-12-01T16:14:49Z
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