sprintf() bug 
Author Message
 sprintf() bug

    I have run across a discrepancy between the documentation and ruby
1.6.8.  According to the pickaxe book, the flag character '0' changes
padding from spaces (' ') to zeros ('0').  This works for numbers, but does
not work for stings:

irb(main):001:0> sprintf "%05d,%05s", 1, '1'
=> "00001,    1"

    A simple fix (from the 1.6.8 version) would be:
--------------------------------
--- sprintf.c.old       Thu Dec 12 03:16:44 2002

                        CHECK(width);
                        width -= len;
                        if (!(flags&FMINUS)) {
+                           char c = (flags & FZERO) ? '0' : ' ';
                            while (width--) {
-                               buf[blen++] = ' ';
+                               buf[blen++] = c;
                            }
                        }
--------------------------------

    To get around this problem, I ended up using the combination
mystring.rjust(5).tr(' ','0').  This worked for me since my string was
guaranteed not to have any spaces, but if that were not the case, this would
get messy.

    It occurred to me that String#ljust and String#rjust really seem like
they should have an optional second parameter that defaults to ' ', but
could be any character (or any string?).  Anyone else think this is a good
idea?

    - Warren Brown



Mon, 01 Aug 2005 02:25:45 GMT  
 sprintf() bug

Quote:

>     I have run across a discrepancy between the documentation and ruby
> 1.6.8.  According to the pickaxe book, the flag character '0' changes
> padding from spaces (' ') to zeros ('0').  This works for numbers, but does
> not work for stings:

man printf

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

i think the pickaxe is simply incorrect - the printf function just delegates
to stdlib printf after all.

Quote:
>     To get around this problem, I ended up using the combination
> mystring.rjust(5).tr(' ','0').  This worked for me since my string was
> guaranteed not to have any spaces, but if that were not the case, this would
> get messy.

>     It occurred to me that String#ljust and String#rjust really seem like
> they should have an optional second parameter that defaults to ' ', but
> could be any character (or any string?).  Anyone else think this is a good
> idea?

agreed.

-a

--

 ====================================
 | Ara Howard
 | NOAA Forecast Systems Laboratory
 | Information and Technology Services
 | Data Systems Group
 | R/FST 325 Broadway
 | Boulder, CO 80305-3328

 | Phone:  303-497-7238
 | Fax:    303-497-7259
 ====================================



Mon, 01 Aug 2005 02:52:36 GMT  
 sprintf() bug
Hi,

In message "sprintf() bug"

|    I have run across a discrepancy between the documentation and ruby
|1.6.8.  According to the pickaxe book, the flag character '0' changes
|padding from spaces (' ') to zeros ('0').  This works for numbers, but does
|not work for stings:

Pickaxe is great, but not perfect (nor me).

|    It occurred to me that String#ljust and String#rjust really seem like
|they should have an optional second parameter that defaults to ' ', but
|could be any character (or any string?).  Anyone else think this is a good
|idea?

This sounds nice.  In fact, I have a plan to do so.

                                                        matz.



Mon, 01 Aug 2005 07:14:41 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Patch for bug in sprintf

2. BUGS, BUGS, BUGS, BUGS, C4 BUGS

3. sprintf formatted output

4. Single quote in sprintf / printf statements

5. sprintf()-like formatting in VW?

6. Problem with using SPrintf function in a cpp file

7. RCR: key value mapping for sprintf/%

8. My sprintf methods and props for FXRuby

9. sprintf in Modelsim

10. What is the fortran90 equivalent of sprintf?

11. formating string like sprintf

12. sprintf?

 

 
Powered by phpBB® Forum Software