Thread overview
compile time printf -> .NET style format string conversion template.
Jan 03, 2008
Neal Alexander
Jan 03, 2008
Neal Alexander
Jan 04, 2008
Matti Niemenmaa
January 03, 2008
Alright the basic idea behind this was:

- During tango porting i want all the stdlib wrapping done in 1 module.
- Any function that takes a format string cant be wrapped transparently without converting the string or doing something half baked (right?).

So i wrote this compile-time conversion template:

http://paste.dprogramming.com/dpjq2ug6 (See the FMT template for discussion purposes. The rest is just conversion/parsing).

Now for the questions:

- Why cant i do return x ~ FMT!(s[i .. $]); at line 106? This would avoid having to call skip() to count the fmt string length a 2nd time. The compiler complains s[i .. $] is an invalid argument or somethig.

- How do i template the print function so that the compiler retains the   immediateness of the format string arg.

with something like this you'd have to do:
---------------------------
void print(T...)(T t)
{
    version (Tango) Stdout.formatln(t);
    else            writefln(t);
}
print(FMT!("whatever %d"), 0);


where ideally you would want something like this:
---------------------------
void print(T...)(char[] fmt, T t)
{
    version (Tango) Stdout.formatln(FMT!(fmt), t);
    else            writefln(fmt, t);
}
print("whatever %d", 0);
January 03, 2008
Neal Alexander wrote:
> Alright the basic idea behind this was:
> 
> - During tango porting i want all the stdlib wrapping done in 1 module.
> - Any function that takes a format string cant be wrapped transparently without converting the string or doing something half baked (right?).
> 
> So i wrote this compile-time conversion template:
> 
> http://paste.dprogramming.com/dpjq2ug6 (See the FMT template for discussion purposes. The rest is just conversion/parsing).
> 
> Now for the questions:
> 
> - Why cant i do return x ~ FMT!(s[i .. $]); at line 106? This would avoid having to call skip() to count the fmt string length a 2nd time. The compiler complains s[i .. $] is an invalid argument or somethig.
> 
> - How do i template the print function so that the compiler retains the   immediateness of the format string arg.
> 
> with something like this you'd have to do:
> ---------------------------
> void print(T...)(T t)
> {
>     version (Tango) Stdout.formatln(t);
>     else            writefln(t);
> }
> print(FMT!("whatever %d"), 0);
> 
> 
> where ideally you would want something like this:
> ---------------------------
> void print(T...)(char[] fmt, T t)
> {
>     version (Tango) Stdout.formatln(FMT!(fmt), t);
>     else            writefln(fmt, t);
> }
> print("whatever %d", 0);

figured out one of the questions at least haha:

template print(char[] fmt, T...)
{
    void print(){ Stdout.formatln(FMT!(fmt), T); }
}

print!(...);
January 04, 2008
Neal Alexander wrote:

In case you're interested, attached is my (command-line tool) attempt at doing the same thing: converting writef's formatting strings to Tango's. I soon gave up as doing a character-by-character exact conversion leads to too many special cases.

It's a couple months old and thus probably doesn't even compile with the current Tango. On top of that, the code is fairly ugly as I didn't expect it to blow up to such a length. But if you want to support everything writef parses (what I tried) there might be something of use in there.

-- 
E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi