sprintf bug (?)

Discussion in 'Ruby' started by Peter Szinek, Nov 11, 2006.

  1. Peter Szinek

    Peter Szinek Guest

    Hello all,

    I am wondering if I have just found a bug in (s)printf...

    According to the PickAxe, table "sprintf flag characters":

    ================= snip ==================================

    0 (zero) all Pad with zeros, not spaces.

    ================= snip ==================================

    so I think this call

    irb(main):053:0> sprintf("%05s", 123)
    => " 123"

    should correctly result in

    "00123"

    or I am getting something wrong?

    thanks,
    Peter

    __
    http://www.rubyrailways.com
    Peter Szinek, Nov 11, 2006
    #1
    1. Advertising

  2. Peter Szinek wrote:
    > Hello all,
    >
    > I am wondering if I have just found a bug in (s)printf...
    >
    > According to the PickAxe, table "sprintf flag characters":
    >
    > ================= snip ==================================
    >
    > 0 (zero) all Pad with zeros, not spaces.
    >
    > ================= snip ==================================
    >
    > so I think this call
    >
    > irb(main):053:0> sprintf("%05s", 123)
    > => " 123"
    >
    > should correctly result in
    >
    > "00123"
    >
    > or I am getting something wrong?


    I think ruby follows the C standard, which says zero padding only
    applies to numeric formats.

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Nov 11, 2006
    #2
    1. Advertising

  3. ----- Original Message -----
    From: "Peter Szinek" <>
    To: "ruby-talk ML" <>
    Sent: Saturday, November 11, 2006 1:28 PM
    Subject: sprintf bug (?)


    > Hello all,
    >
    > I am wondering if I have just found a bug in (s)printf...
    >
    > According to the PickAxe, table "sprintf flag characters":
    >
    > ================= snip ==================================
    >
    > 0 (zero) all Pad with zeros, not spaces.
    >
    > ================= snip ==================================
    >
    > so I think this call
    >
    > irb(main):053:0> sprintf("%05s", 123)
    > => " 123"
    >
    > should correctly result in
    >
    > "00123"
    >

    sprintf("%05d",123) # 'd' not 's'
    => "00123"
    Bernard Kenik, Nov 12, 2006
    #3
  4. Peter Szinek

    Peter Szinek Guest

    Bernard Kenik wrote:
    >
    > ----- Original Message ----- From: "Peter Szinek" <>
    > To: "ruby-talk ML" <>
    > Sent: Saturday, November 11, 2006 1:28 PM
    > Subject: sprintf bug (?)
    >
    >
    >> Hello all,
    >>
    >> I am wondering if I have just found a bug in (s)printf...
    >>
    >> According to the PickAxe, table "sprintf flag characters":
    >>
    >> ================= snip ==================================
    >>
    >> 0 (zero) all Pad with zeros, not spaces.
    >>
    >> ================= snip ==================================
    >>
    >> so I think this call
    >>
    >> irb(main):053:0> sprintf("%05s", 123)
    >> => " 123"
    >>
    >> should correctly result in
    >>
    >> "00123"
    >>

    > sprintf("%05d",123) # 'd' not 's'
    > => "00123"


    :) I know 'd' is not 's', it was a bad example. It should have been

    irb(main):002:0> sprintf("%10s",'hello')
    => " hello"

    (there are no zeroes...)

    Peter

    __
    http://www.rubyrailways.com
    Peter Szinek, Nov 12, 2006
    #4
  5. Peter Szinek wrote:
    > Bernard Kenik wrote:
    >
    >> ----- Original Message ----- From: "Peter Szinek" <>
    >> To: "ruby-talk ML" <>
    >> Sent: Saturday, November 11, 2006 1:28 PM
    >> Subject: sprintf bug (?)
    >>
    >>
    >>
    >>> Hello all,
    >>>
    >>> I am wondering if I have just found a bug in (s)printf...
    >>>
    >>> According to the PickAxe, table "sprintf flag characters":
    >>>
    >>> ================= snip ==================================
    >>>
    >>> 0 (zero) all Pad with zeros, not spaces.
    >>>
    >>> ================= snip ==================================
    >>>
    >>> so I think this call
    >>>
    >>> irb(main):053:0> sprintf("%05s", 123)
    >>> => " 123"
    >>>
    >>> should correctly result in
    >>>
    >>> "00123"
    >>>
    >>>

    >> sprintf("%05d",123) # 'd' not 's'
    >> => "00123"
    >>

    >
    > :) I know 'd' is not 's', it was a bad example. It should have been
    >
    > irb(main):002:0> sprintf("%10s",'hello')
    > => " hello"
    >
    > (there are no zeroes...)
    >
    > Peter
    >


    Possibly you meant "%010s" instead of "%10s"? In any case, I checked the
    output of a C program compiled with gcc on my Powerbook running OS X
    10.8. In this case, the '0' flag adds padding on the left:

    ruby$ cat testit.c
    #include <stdio.h>

    int main(int argc, char *argv[])
    {
    printf("%010s", "hello\n");
    return 0;
    }

    ruby$ ./testit
    0000hello
    ruby$
    Timothy Hunter, Nov 12, 2006
    #5
  6. Hi,

    In message "Re: sprintf bug (?)"
    on Mon, 13 Nov 2006 07:03:44 +0900, Timothy Hunter <> writes:

    |Possibly you meant "%010s" instead of "%10s"? In any case, I checked the
    |output of a C program compiled with gcc on my Powerbook running OS X
    |10.8. In this case, the '0' flag adds padding on the left:

    This is platform dependent. Linux manpage says:

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

    zero option affects some specifiers. %s is not among them.

    matz.
    Yukihiro Matsumoto, Nov 12, 2006
    #6
  7. Peter Szinek

    Peter Szinek Guest

    Yukihiro Matsumoto wrote:
    > zero option affects some specifiers. %s is not among them.


    Thank, Matz. This made it really clear at last...

    Peter
    __
    http://www.rubyrailways.com
    Peter Szinek, Nov 13, 2006
    #7
  8. ----- Original Message -----
    From: "Peter Szinek" <>
    To: "ruby-talk ML" <>
    Sent: Sunday, November 12, 2006 4:43 PM
    Subject: Re: sprintf bug (?)


    > Bernard Kenik wrote:
    >>
    >> ----- Original Message ----- From: "Peter Szinek"
    >> <>
    >> To: "ruby-talk ML" <>
    >> Sent: Saturday, November 11, 2006 1:28 PM
    >> Subject: sprintf bug (?)
    >>
    >>
    >>> Hello all,
    >>>
    >>> I am wondering if I have just found a bug in (s)printf...
    >>>
    >>> According to the PickAxe, table "sprintf flag characters":
    >>>
    >>> ================= snip ==================================
    >>>
    >>> 0 (zero) all Pad with zeros, not spaces.
    >>>
    >>> ================= snip ==================================
    >>>
    >>> so I think this call
    >>>
    >>> irb(main):053:0> sprintf("%05s", 123)
    >>> => " 123"
    >>>
    >>> should correctly result in
    >>>
    >>> "00123"
    >>>

    >> sprintf("%05d",123) # 'd' not 's'
    >> => "00123"

    >
    > :) I know 'd' is not 's', it was a bad example. It should have been
    >
    > irb(main):002:0> sprintf("%10s",'hello')
    > => " hello"
    >
    > (there are no zeroes...)
    >
    > Peter
    >


    This is the correct behavior of sprintf for strings
    Bernard Kenik, Nov 14, 2006
    #8
  9. ----- Original Message -----
    From: "Yukihiro Matsumoto" <>
    To: "ruby-talk ML" <>
    Sent: Sunday, November 12, 2006 6:34 PM
    Subject: Re: sprintf bug (?)


    > Hi,
    >
    > In message "Re: sprintf bug (?)"
    > on Mon, 13 Nov 2006 07:03:44 +0900, Timothy Hunter
    > <> writes:
    >
    > |Possibly you meant "%010s" instead of "%10s"? In any case, I checked the
    > |output of a C program compiled with gcc on my Powerbook running OS X
    > |10.8. In this case, the '0' flag adds padding on the left:
    >
    > This is platform dependent. Linux manpage says:
    >
    > 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.
    > ~~~~~~~~~
    >
    > zero option affects some specifiers. %s is not among them.
    >
    > matz.
    >

    I suggest that the OP asks ruby's description of sprintf via "ri sprintf"
    ... it is essentially as described above
    Bernard Kenik, Nov 14, 2006
    #9
    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. Mark Hubbart

    sprintf bug in 1.9.0?

    Mark Hubbart, Feb 16, 2004, in forum: Ruby
    Replies:
    5
    Views:
    217
  2. jwesley

    Sprintf bug

    jwesley, Nov 11, 2005, in forum: Ruby
    Replies:
    8
    Views:
    161
  3. Replies:
    4
    Views:
    186
    Leslie Viljoen
    Jul 29, 2006
  4. Wes Gamble

    Bug in sprintf?

    Wes Gamble, Aug 18, 2006, in forum: Ruby
    Replies:
    23
    Views:
    321
    Hal Fulton
    Aug 20, 2006
  5. Daniel DeLorme

    sprintf bug?

    Daniel DeLorme, Feb 1, 2008, in forum: Ruby
    Replies:
    1
    Views:
    91
    Robert Klemme
    Feb 1, 2008
Loading...

Share This Page