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

How to format some data ala vnsprintf?

Moderators: larsivi kris

Posted: 03/05/08 23:21:52

Hi,

I am looking for a way to format data like the (almost) std C function vnsprintf does, eg:

int vsnprintf(char *restrict s, size_t n, const char *restrict format, va_list ap);

And something like this:

static char buffer[1024]; vsnprintf(buffer, sizeof(buffer)/sizeof(char), format, va_list);

I found the Layout class seems to do that :

public T[] format(T) (T[] frm, ...) {
  return (new Layout!(T)()).convert(_arguments, _argptr, frm);
}

But I do not like the fact I'm creating a plain new object just to dump some garbage into a wchar array (ultimately, I must be able to do "foobar{1}".format(1) or "foobar{1:s}".format("é")).

Is there a way to avoid creating a new object? (As I understand it now, the new operator works like in C++: it will create on the heap instead of the stack?)

http://www.bbnwn.eu

Author Message

Posted: 03/06/08 04:30:58

you can use tango.io.convert.Format for this if you want, which is a Layout instance. If you're already using Stdout, then you could use Stdout.sprint() and friends to achieve the same end-result?

Posted: 03/06/08 09:23:09

I see :) I'll get a try of it in that case.

http://www.bbnwn.eu