www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4266] New: add support for structs in std.format.doFormat

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4266

           Summary: add support for structs in std.format.doFormat
           Product: D
           Version: 2.000
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: mrmocool gmx.de



Currently there is a check if a struct has a toString member and it fails if
not.
It is really cumbersome to code a toString() by hand for every little struct.

if (tis.xtoString is null)
    throw new FormatError("Can't convert " ~ tis.toString() ~ " to string:
\"string toString()\" not defined");
s = tis.xtoString(argptr);


Especially if you are dealing with external headers (in my case Windows/DirectX
headers) you have to face lots of structures that don't have a toString method.


Please add support for a default struct toString that uses the "{membername:
value, member2:value, member3:{mm1:v, ....}}" style just like arrays are
formatted as "[a, b, c, d,...]" and AAs as "[k1:v1, k2:v2, ..]" by default.


==>


if (tis.xtoString is null)
    putStruct(....);
else
    s = tis.xtoString(argptr);

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 03 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4266




This is an (inefficient) workaround, but it does its job until this is fixed.


import std.string : format;
string myformat(T...)(T ts)
{
    string res;

    foreach (t; ts)
    {
        static if (is (typeof(t) U : U*) && !is(U == void)) // is a pointer
        {
            res ~= myformat(*t);
        }
        else static if (is (typeof(t) == struct))
        {
            res ~= "{";
            foreach(i, el; t.tupleof)
            {
                res ~= t.tupleof[i].stringof[2..$] ~ ":" ~ myformat(el); // the
slice cuts off the "t." that is always prepended
                static if (i < t.tupleof.length-1)
                    res ~= ", ";
            }
            res ~= "}";
        }
        else
            res ~= format(t);
    }

    return res;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 03 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4266




add this branch (+ import std.traits;)

else static if (isSomeString!(typeof(t))) // enclose a string in ""
    res ~= `"` ~ t ~ `"`;


and it nearly matches the C struct intialization style (except for pointers,
then you need to remove the first branch):


struct S
{
    int a;
    S2* p;

    S2 adad;
}

struct S2
{
    byte[4] aaaa = [0,1,2,3];
    string ddd = "asd";
}

void main()
{
    S2 sss = S2([5,4,5,4], "foo");
    S s = S(2, &sss);
    write(myformat(s));
}


gives:
{a:2, p:{aaaa:[5,4,5,4], ddd:"foo"}, adad:{aaaa:[0,1,2,3], ddd:"asd"}}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 03 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4266


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com



10:08:30 PST ---
Did the code used to just fail to compile before? Now it prints the values:

import std.string;
import std.stdio;

struct S
{
    int x;
    float y;
    string z;
}

void main()
{
    string s = format("%s", S());
    writeln(s);
}

 S(0, nan, "")
If that's good enough you can close with "WORKSFORME", thanks. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 18 2013