That prints "Bob Smith" in a field with 25 characters as you'd expect, but if the string has any non-printable characters in it, each non-printable character subtracts 1 from the field length when the string is printed out. CTRL-A color codes have this effect when used with printf.
non-printable character subtracts 1 from the field length when the
string is printed out. CTRL-A color codes have this effect when used
with printf.
i'm pretty sure that's intentional and certainly seems like it should be.
who's name or other variable would have color codes in it, though?
Re: printf and color codes
By: Mro to Nightfox on Sun Jan 20 2013 21:43:04
non-printable character subtracts 1 from the field length when the
string is printed out. CTRL-A color codes have this effect when used
with printf.
i'm pretty sure that's intentional and certainly seems like it should b
It's not what I would expect. If you specify a field length for printf(), i should fill that entire field up to that length. Otherwise, the results can unexpected and ugly. The main uses for specifying a field length are to ali text and filling a space when overwriting existing text on the screen. I wo expect printf() to only count visible/printable characters in the length of string you give it.
who's name or other variable would have color codes in it, though?
Anything where color is desired.. :P
I think you're right about this.. if printf is able to handle ctrl-A codes it likely should be able to format them correctly.. you can work around it though.
var diff = str.length - console.strlen(str);
printf("%-*s",25+diff,str);
Hi DM -
I'm not sure if this is a bug or is intentional, but the JavaScript printf function doesn't seem to account for color codes (or perhaps non-printable characters in general) when printing strings with a specific field length. For instance:
var name = "Bob Smith";
printf("%-25s", name);
That prints "Bob Smith" in a field with 25 characters as you'd expect, but if the string has any non-printable characters in it, each non-printable character subtracts 1 from the field length when the string is printed out. CTRL-A color codes have this effect when used with printf.
Re: printf and color codes
By: Mro to Nightfox on Sun Jan 20 2013 21:43:04
non-printable character subtracts 1 from the field length when the
string is printed out. CTRL-A color codes have this effect when used
with printf.
i'm pretty sure that's intentional and certainly seems like it should be.
It's not what I would expect. If you specify a field length for printf(), it should fill that entire field up to that length. Otherwise, the results can be unexpected and ugly. The main uses for specifying a field length
are to align text and filling a space when overwriting existing text on the screen. I would expect printf() to only count visible/printable characters in the length of a string you give it.
That's not how printf (and the related formatting functions) in C work however. The width modifier to the %s specifier does not specify the number of "printable" characters, just the number of characters, period (which includes non-printable characters, like \a, \b, and so on).
Re: printf and color codes
By: Digital Man to Nightfox on Mon Jan 21 2013 23:56:55
That's not how printf (and the related formatting functions) in C work however. The width modifier to the %s specifier does not specify the number of "printable" characters, just the number of characters, period (which includes non-printable characters, like \a, \b, and so on).
Coming from a C/C++ background, I suspected that was so. Even then, it seem somewhat odd.. But oh well, we deal with it. :)
Sysop: | Chris Crash |
---|---|
Location: | Huntington Beach, CA. |
Users: | 578 |
Nodes: | 8 (0 / 8) |
Uptime: | 29:00:50 |
Calls: | 10,736 |
Calls today: | 1 |
Files: | 5 |
Messages: | 443,190 |
Posted today: | 1 |