Thread overview
swritef()
Jan 08, 2005
Sebastian Beschke
Jan 08, 2005
h3r3tic
Jan 08, 2005
Sebastian Beschke
Jan 08, 2005
teqDruid
Jan 09, 2005
Sebastian Beschke
Re: swritef() (OT)
Jan 09, 2005
h3r3tic
Jan 09, 2005
Sebastian Beschke
Jan 09, 2005
Sebastian Beschke
January 08, 2005
I've copied and pasted this little snippet so much now that I thought I'd post it:

char[] swritef(...)
{
    char[] result;

	void putc(dchar c)
	{
        char[4] buf;
        char[] b;

        b = std.utf.toUTF8(buf, c);
        for (size_t i = 0; i < b.length; i++) {
            result ~= b[i];
	    }
	}

	std.format.doFormat(&putc, _arguments, _argptr);

    return result;
}

Dunno if it's very efficient or anything, but I think such a thing is really needed, and as far as I can tell, phobos doesn't contain it yet.

-Sebastian
January 08, 2005
Sebastian Beschke wrote:
> I've copied and pasted this little snippet so much now that I thought I'd post it:
> 
> char[] swritef(...)
> {
>     char[] result;
> 
>     void putc(dchar c)
>     {
>         char[4] buf;
>         char[] b;
> 
>         b = std.utf.toUTF8(buf, c);
>         for (size_t i = 0; i < b.length; i++) {
>             result ~= b[i];
>         }
>     }
> 
>     std.format.doFormat(&putc, _arguments, _argptr);
> 
>     return result;
> }
> 
> Dunno if it's very efficient or anything, but I think such a thing is really needed, and as far as I can tell, phobos doesn't contain it yet.
> 
> -Sebastian


How bout this ?


char[] str(...)  // or call it swritef; this name's from Python
{
	char[] result;

	void putc(dchar c)
	{
		std.utf.encode(result, c);
	}

	std.format.doFormat(&putc, _arguments, _argptr);
	return result;
}


Tom
January 08, 2005
h3r3tic schrieb:
> Sebastian Beschke wrote:
> 
>> I've copied and pasted this little snippet so much now that I thought I'd post it:
>>
>> char[] swritef(...)
>> {
>>     char[] result;
>>
>>     void putc(dchar c)
>>     {
>>         char[4] buf;
>>         char[] b;
>>
>>         b = std.utf.toUTF8(buf, c);
>>         for (size_t i = 0; i < b.length; i++) {
>>             result ~= b[i];
>>         }
>>     }
>>
>>     std.format.doFormat(&putc, _arguments, _argptr);
>>
>>     return result;
>> }
>>
>> Dunno if it's very efficient or anything, but I think such a thing is really needed, and as far as I can tell, phobos doesn't contain it yet.
>>
>> -Sebastian
> 
> 
> 
> How bout this ?
> 
> 
> char[] str(...)  // or call it swritef; this name's from Python
> {
>     char[] result;
> 
>     void putc(dchar c)
>     {
>         std.utf.encode(result, c);
>     }
> 
>     std.format.doFormat(&putc, _arguments, _argptr);
>     return result;
> }
> 
> 
> Tom


You're right, I overlooked the encode() function. Thanks :)

-Sebastian
January 08, 2005
I think std.string.format() provides this functionality.

John

On Sat, 08 Jan 2005 16:07:25 +0100, Sebastian Beschke wrote:

> I've copied and pasted this little snippet so much now that I thought I'd post it:
> 
> char[] swritef(...)
> {
>      char[] result;
> 
> 	void putc(dchar c)
> 	{
>          char[4] buf;
>          char[] b;
> 
>          b = std.utf.toUTF8(buf, c);
>          for (size_t i = 0; i < b.length; i++) {
>              result ~= b[i];
> 	    }
> 	}
> 
> 	std.format.doFormat(&putc, _arguments, _argptr);
> 
>      return result;
> }
> 
> Dunno if it's very efficient or anything, but I think such a thing is really needed, and as far as I can tell, phobos doesn't contain it yet.
> 
> -Sebastian

January 09, 2005
teqDruid schrieb:
> I think std.string.format() provides this functionality.

Not only that, but it's identic to what h3r3tic posted...

This should be in the docs! ;)

I still prefer the name swritef, though.

-Sebastian
January 09, 2005
Sebastian Beschke wrote:
> teqDruid schrieb:
> 
>> I think std.string.format() provides this functionality.
> 
> 
> Not only that, but it's identic to what h3r3tic posted...

Wow, I've just reinvented the wheel :D
I'll have to patent it <g>


> This should be in the docs! ;)

Definitely...


> I still prefer the name swritef, though.

As for std.string.format ... It's quite scary... Say I want to make a string with the drive letter in it. Shall I do it with 'format("C:")' ? ;)


Tom
January 09, 2005
h3r3tic schrieb:
> Sebastian Beschke wrote:
>> I still prefer the name swritef, though.
> 
> 
> As for std.string.format ... It's quite scary... Say I want to make a string with the drive letter in it. Shall I do it with 'format("C:")' ? ;)

LOL, good one :)

> Tom
January 09, 2005
Sebastian Beschke schrieb:
> 
> This should be in the docs! ;)
> 

I added a small section in the documentation amendments in Wiki4d.

http://www.prowiki.org/wiki4d/wiki.cgi?DocComments/Phobos

-Sebastian