Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 08, 2005 swritef() | ||||
---|---|---|---|---|
| ||||
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 Re: swritef() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sebastian Beschke | 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 Re: swritef() | ||||
---|---|---|---|---|
| ||||
Posted in reply to h3r3tic | 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 Re: swritef() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sebastian Beschke | 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 Re: swritef() | ||||
---|---|---|---|---|
| ||||
Posted in reply to teqDruid | 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 Re: swritef() (OT) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sebastian Beschke | 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 Re: swritef() (OT) | ||||
---|---|---|---|---|
| ||||
Posted in reply to h3r3tic | 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 Re: swritef() | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sebastian Beschke | 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 |
Copyright © 1999-2021 by the D Language Foundation