digitalmars.D.learn - Dynamic Minimum width with Format / writefln
- Chris Katko (25/25) Oct 02 2018 - First, I'm confused. The docs say 's' is "whatever it needs to
- Adam D. Ruppe (6/8) Oct 02 2018 That number certainly isn't col.width (unless you have a width of
- Chris Katko (6/14) Oct 02 2018 I'm not sure how I made this mistake. But it seems to only show
- Adam D. Ruppe (8/12) Oct 02 2018 Yeah.
- First, I'm confused. The docs say 's' is "whatever it needs to
be". ("he corresponding argument is formatted in a manner
consistent with its type:") But what if I specifically want a
STRING. Because I only see floats, ints, etc. No forced string
types.
- Second,
This works fine in D:
printf("%-*s|", col.width-1, col.name.toStringz());
It's a left-aligned, string with a minimum width from the first
argument, col.width. (-1 because I'm throwing a pipe symbol on
the end.)
Now with writefln:
writefln("%-*s|", col.width-1, col.name);
Same format specifier, except I don't need a toStringz which is
nice. Except it doesn't work and tries to decode col.width-1 into
a hexadecimal number and only prints that. ("4D6EF6")
I looked through the docs:
https://dlang.org/phobos/std_format.html
'%' Position Flags Width Separator Precision FormatChar
Width:
empty
Integer
'*'
But there are then zero examples or explanations of how to use
that option. What's going on here?
Oct 02 2018
On Wednesday, 3 October 2018 at 00:14:03 UTC, Chris Katko wrote:
Except it doesn't work and tries to decode col.width-1 into a
hexadecimal number and only prints that. ("4D6EF6")
That number certainly isn't col.width (unless you have a width of
like millions)...
It looks more like a pointer. What is the type of col.name? If it
is string, this code should work fine.
I'm guessing it is a char*...
Oct 02 2018
On Wednesday, 3 October 2018 at 00:34:33 UTC, Adam D. Ruppe wrote:On Wednesday, 3 October 2018 at 00:14:03 UTC, Chris Katko wrote:I'm not sure how I made this mistake. But it seems to only show up now if I leave .toStringz() with the writefln. writefln("%-*s<", col.width-1, col.name.toStringz() /* here */); So maybe I've been staring at code too long tonight and simply missed it?Except it doesn't work and tries to decode col.width-1 into a hexadecimal number and only prints that. ("4D6EF6")That number certainly isn't col.width (unless you have a width of like millions)... It looks more like a pointer. What is the type of col.name? If it is string, this code should work fine. I'm guessing it is a char*...
Oct 02 2018
On Wednesday, 3 October 2018 at 01:14:24 UTC, Chris Katko wrote:I'm not sure how I made this mistake. But it seems to only show up now if I leave .toStringz() with the writefln.Yeah. So what's happening here is toStringz returns the C-style char*, which printf works well with, but writef doesn't trust it and just prints the pointer value instead of trying to traverse it looking for a zero terminator (which might not be there). Just passing a D string will work consistently.So maybe I've been staring at code too long tonight and simply missed it?oh probably, it happens to everyone :)
Oct 02 2018








Adam D. Ruppe <destructionator gmail.com>