Bug in ruby printf/sprintf

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

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.

···

paul.dlug@gmail.com wrote:

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

Tiberius wrote:

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

Hi,]

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.

              matz.

···

In message "Re: Bug in ruby printf/sprintf" on Sat, 29 Jul 2006 04:45:13 +0900, "paul.dlug@gmail.com" <paul.dlug@gmail.com> writes:

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

Sorry to insult everyone's intelligence!

···

On 7/29/06, Yukihiro Matsumoto <matz@ruby-lang.org> wrote:

Hi,]

In message "Re: Bug in ruby printf/sprintf" > on Sat, 29 Jul 2006 04:45:13 +0900, "paul.dlug@gmail.com" <paul.dlug@gmail.com> writes:

>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.