Bug in ruby printf/sprintf

P

paul.dlug

I have encountered what appears to be a bug in ruby's printf/sprintf

This code:

str = "test"
printf "|%010s|", str

Generates this output:
| test|

However in C and perl, the same code generates this output:
|000000test|

The latter output is correct according to the man pages, according to
the ruby doc for printf/sprintf the behavior should be the same.

Has anyone else encountered this? Is there something I'm missing here
or is there a workaround for it if it is a bug?


Thanks,
Paul
 
T

Tiberius

The equivalent C program under RedHat Linux 3 update 6 x86_64 produces
| test|

Also, I ran your ruby code in ruby 1.8.4 (2006-12-24) and it also
produces the same results as the C code does.
 
P

paul.dlug

Tiberius said:
The equivalent C program under RedHat Linux 3 update 6 x86_64 produces
| test|

Also, I ran your ruby code in ruby 1.8.4 (2006-12-24) and it also
produces the same results as the C code does.

That's interesting. I just tried the same on linux (Gentoo) and you're
correct, printf in C behaves the same way there. In Mac OS X (10.4) and
FreeBSD (6.1) it does zero pad it as does perl on all platforms. I
wonder why I haven't hit this before and what the reason for the
difference is. Time to take a look at the doc's for the linux version.


--Paul
 
Y

Yukihiro Matsumoto

Hi,]

In message "Re: Bug in ruby printf/sprintf"
|That's interesting. I just tried the same on linux (Gentoo) and you're
|correct, printf in C behaves the same way there. In Mac OS X (10.4) and
|FreeBSD (6.1) it does zero pad it as does perl on all platforms. I
|wonder why I haven't hit this before and what the reason for the
|difference is. Time to take a look at the doc's for the linux version.

=46rom Linux man page printf(3):

0 The value should be zero padded. For d, i, o, u, x,
X, a, A, e, E, f, F, g, and G conversions, the converted
value is padded on the left with zeros rather than
blanks. If the 0 and - flags both appear, the 0 flag is
ignored. If a precision is given with a numeric
conversion (d, i, o, u, x, and X), the 0 flag is
ignored. For other conversions, the behavior is
undefined.

It's behavior is undefined, so that some fills with zeros and others
just ignore. Don't use zero with %s specifier if you want portable
behavior.

matz.
 
L

Leslie Viljoen

Hi,]

In message "Re: Bug in ruby printf/sprintf"

|That's interesting. I just tried the same on linux (Gentoo) and you're
|correct, printf in C behaves the same way there. In Mac OS X (10.4) and
|FreeBSD (6.1) it does zero pad it as does perl on all platforms. I
|wonder why I haven't hit this before and what the reason for the
|difference is. Time to take a look at the doc's for the linux version.

From Linux man page printf(3):

0 The value should be zero padded. For d, i, o, u, x,
X, a, A, e, E, f, F, g, and G conversions, the converted
value is padded on the left with zeros rather than
blanks. If the 0 and - flags both appear, the 0 flag is
ignored. If a precision is given with a numeric
conversion (d, i, o, u, x, and X), the 0 flag is
ignored. For other conversions, the behavior is
undefined.

It's behavior is undefined, so that some fills with zeros and others
just ignore. Don't use zero with %s specifier if you want portable
behavior.

So here's portable:
print "|#{str.rjust(10, "0")}|"

Sorry to insult everyone's intelligence!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

sprintf bug (?) 8
sprintf behavies different than printf 7
sprintf 15
sprintf percent sign problem 2
Bug in sprintf? 23
Named sprintf parameters 14
BUG IN test/unit!?!? 0
Unexpected sprintf/String % Behavior 3

Members online

Forum statistics

Threads
473,999
Messages
2,570,244
Members
46,838
Latest member
KandiceChi

Latest Threads

Top