www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7341] New: writefln of strings array with size formatting

reply d-bugmail puremagic.com writes:
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



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: -------
Jan 21 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7341


SomeDude <lovelydear mailmetrash.com> changed:

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



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: -------
Apr 21 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7341





 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: -------
Apr 21 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7341


Stewart Gordon <smjg iname.com> changed:

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



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: -------
Apr 21 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7341




PDT ---

 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: -------
Apr 21 2012