www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8730] New: writeln stops on a nul character, even if passed a D string

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

           Summary: writeln stops on a nul character, even if passed a D
                    string
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: minor
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: destructionator gmail.com



13:12:00 PDT ---
import std.stdio;

void main() {
        writeln("test\0gone");
}


only prints "test". writefln("%s") prints the whole thing.

$ ./test | xxd
0000000: 7465 7374 0a

as you can see the data is indeed not being printed; it isn't just invisible on
my screen.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 26 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8730


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

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



17:11:33 PDT ---
I'm just guessing, but:

void writeln(T...)(T args)
{
    static if (T.length == 0)
    {
        enforce(fputc('\n', .stdout.p.handle) == '\n');
    }
    else static if (T.length == 1 &&
                    is(typeof(args[0]) : const(char)[]) &&
                    !is(typeof(args[0]) == enum) && !is(typeof(args[0]) ==
typeof(null)) &&
                    !isAggregateType!(typeof(args[0])))
    {
        // Specialization for strings - a very frequent case
        enforce(fprintf(.stdout.p.handle, "%.*s\n",
                        cast(int) args[0].length, args[0].ptr) >= 0);
    }
    else
    {
        // Most general instance
        stdout.write(args, '\n');
    }
}

The specialization is probably to blame. I think 'args[0].length' probably sets
the max limit rather than min, but I don't know enough about fprintf internals.
:)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 04 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8730




19:24:49 PDT ---

 The specialization is probably to blame. I think 'args[0].length' probably sets
 the max limit rather than min, but I don't know enough about fprintf internals.
 :)
Yeah, you're right. The man page for printf says "the maximum number of characters to be printed from a string for s and S conversions." I'm not sure what is best here. I really think it should work, but the specialization has got to be there for a reason too. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Oct 09 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8730




19:38:15 PDT ---


 The specialization is probably to blame. I think 'args[0].length' probably sets
 the max limit rather than min, but I don't know enough about fprintf internals.
 :)
Yeah, you're right. The man page for printf says "the maximum number of characters to be printed from a string for s and S conversions." I'm not sure what is best here. I really think it should work, but the specialization has got to be there for a reason too.
It could me off guard just recently. E.g. git uses two strings separated by nul in it's object format, and when I've tried to print out the contents as a char[] using writeln it would only print out a small portion of it, even though printing it as a byte[] would print much more. Anyway this *is* a bug. We can't have it both ways: writeln("bla\0bla"); // bla writefln("%s", "bla\0bla"); // bla[NUL]bla -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Oct 09 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8730


Brad Roberts <braddr puremagic.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |braddr puremagic.com



---
Shouldn't this be trivial to fix:

Replace:
fprintf(.stdout.p.handle, "%.*s\n",
                        cast(int) args[0].length, args[0].ptr)

with:
fwrite(args[0].ptr, 1, args[0].length, .stdout.p.handle)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 09 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8730




---
oops, followed by the same code as in the length == 0 code to get the \n.  At
which point it's questionable that specialization is all that special.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 09 2012