Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Unistring and toUtf8()

Moderators: kris

Posted: 09/13/07 20:06:46 Modified: 09/14/07 19:07:09

Hello ! Not sure if its actually a bug, but its definitively unexpected behaviour. this code:

private import tango.io.Console, tango.text.String;

alias String!(char) str;

int main(char[][] args) {

        str str = new str("Test");      
        Cout(str.toUtf8()).newline;
        Object o = str;
        Cout(o.toUtf8()).newline;
        return 0;
}

Produces the following Output:

$ ./Main
Test
tango.text.String.String!(char).String

But you would expect:

$ ./Main
Test
Test

This is because the class UniString?? only has a toUtf8 method with a default argument. It should probably be changed I guess....

Author Message

Posted: 09/13/07 22:09:01

Yep, you are right.

UniString? should have this instead:

xchar[] toUtfX()
{
   return toUtfX(null);
}

abstract xchar[] toUtfX(xchar[] dst);

And String(T) should have:

alias toUtfX UniString.toUtfX;

BTW, Once that all works, you should be able to do:

Cout(str).newline;
Cout(o).newline;

-Steve

Posted: 09/14/07 06:48:16 -- Modified: 09/14/07 19:08:13 by
ptriller

Sorry if I screwed up some D best practices, I only wrote about 100 Lines of D code and try to get along with soem reading and my C++ and Java experience

Posted: 09/14/07 21:20:18

I think you may have misunderstood my reply.

You are correct in that this is a bug. My messages about putting alias and splitting the implementation of toUtf8 in UniString? were for the tango developers to see how to fix the problem in tango.

My suggestion to use Cout(o) instead of Cout(o.toUtf8) was just an FYI for you, that once the tango guys fix the code, you should be able to do this :)

-Steve

Posted: 09/14/07 22:48:27

ptriller wrote:

Sorry if I screwed up some D best practices, I only wrote about 100 Lines of D code and try to get along with soem reading and my C++ and Java experience

Thanks for pointing this out, ptriller ... it'll get fixed in the next release :)