Bug in ruby printf/sprintf

Discussion in 'Ruby' started by paul.dlug@gmail.com, Jul 28, 2006.

  1. Guest

    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
     
    , Jul 28, 2006
    #1
    1. Advertising

  2. Tiberius Guest

    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.



    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, Jul 28, 2006
    #2
    1. Advertising

  3. Guest

    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
     
    , Jul 28, 2006
    #3
  4. Hi,]

    In message "Re: Bug in ruby printf/sprintf"
    on Sat, 29 Jul 2006 04:45:13 +0900, "" <paul.dlug@gm=
    ail.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.

    =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.
     
    Yukihiro Matsumoto, Jul 29, 2006
    #4
  5. On 7/29/06, Yukihiro Matsumoto <> wrote:
    > Hi,]
    >
    > In message "Re: Bug in ruby printf/sprintf"
    > on Sat, 29 Jul 2006 04:45:13 +0900, "" <> 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.


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

    Sorry to insult everyone's intelligence!
     
    Leslie Viljoen, Jul 29, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ben
    Replies:
    4
    Views:
    658
    Martin Ambuhl
    Jun 26, 2004
  2. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,328
  3. Replies:
    7
    Views:
    480
    Richard Heathfield
    Mar 15, 2008
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    455
  5. Jian Lin
    Replies:
    1
    Views:
    104
    Roger Pack
    May 5, 2010
Loading...

Share This Page