www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - swritef()

reply Sebastian Beschke <s.beschke gmx.de> writes:
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
Jan 08 2005
next sibling parent reply h3r3tic <foo bar.baz> writes:
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
Jan 08 2005
parent Sebastian Beschke <s.beschke gmx.de> writes:
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
Jan 08 2005
prev sibling parent reply teqDruid <me teqdruid.com> writes:
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
Jan 08 2005
parent reply Sebastian Beschke <s.beschke gmx.de> writes:
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
Jan 09 2005
next sibling parent reply h3r3tic <foo bar.baz> writes:
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
Jan 09 2005
parent Sebastian Beschke <s.beschke gmx.de> writes:
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
Jan 09 2005
prev sibling parent Sebastian Beschke <s.beschke gmx.de> writes:
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
Jan 09 2005