Projects

Ticket #663 (closed defect: fixed)

Opened 22 months ago

Last modified 18 months ago

Kernel#sprintf formatting broken on trunk

Reported by: jordan.breeding@… Owned by: lsansonetti@…
Priority: blocker Milestone: MacRuby 0.7
Component: MacRuby Keywords:
Cc:

Description

11 jordan@thetourist ~ > rvm use system
Now using system ruby.
12 jordan@thetourist ~ > ruby -e 'puts("%.2f" % [5.fdiv(3)]); puts("%6.2f" % [5.fdiv(3)]);'
1.67
  1.67
13 jordan@thetourist ~ > rvm use ruby-1.9.1                                                
Using ruby 1.9.1 p378
14 jordan@thetourist ~ > ruby -e 'puts("%.2f" % [5.fdiv(3)]); puts("%6.2f" % [5.fdiv(3)]);'
1.67
  1.67
15 jordan@thetourist ~ > macruby -e 'puts("%.2f" % [5.fdiv(3)]); puts("%6.2f" % [5.fdiv(3)]);'
1.67
1.666667
16 jordan@thetourist ~ > macruby -e 'p RUBY_VERSION'
"1.9.0"
17 jordan@thetourist ~ > macruby -e 'p MACRUBY_VERSION'
"0.6"
18 jordan@thetourist ~ > macruby -e 'p MACRUBY_REVISION'
"git commit c4dbbe33596e9fe150ddcc0ac1b253c566191ba4"

Change History

Changed 18 months ago by watson1978@…

I think this problem to be a bug of the rb_uchar_strtol().
When parsed the "6.2", rb_uchar_strtol() returns

  • returns = 6
  • end_offset = 3

But, expected

  • returns = 6
  • end_offset = 1

Too big end_offset so that does not parse ".2".
Actually the "%6.2f" is treated with "%6f".

Changed 18 months ago by watson1978@…

Crashes When calls log10(0). Sorry. X(

Changed 18 months ago by watson1978@…

I did not consider as following x(

$ macruby -e 'puts("%6.02f" % [5.fdiv(3)]);' 
1.67
$ ruby -e 'puts("%6.02f" % [5.fdiv(3)]);' 
  1.67

Changed 18 months ago by watson1978@…

I rewrote a patch.

Test Code:

p sprintf("%06.4d", 2)
p sprintf("%06.0d", 2)
p sprintf("%06.04d", 2)
p sprintf("%+06.04d", 2)
p sprintf("%-06.04d", 2)
p sprintf("%25.12d", 2)
begin
  p sprintf("%06.-4d", 2)
rescue => e
  p e
end

begin
  p sprintf("%06.+4d", 2)
rescue => e
  p e
end

Result

$ ruby test.rb
"  0002"
"     2"
"  0002"
" +0002"
"0002  "
"             000000000002"
#<ArgumentError: flag after width>
#<ArgumentError: flag after width>

$ DYLD_LIBRARY_PATH=. ./macruby -I./lib test.rb
"  0002"
"     2"
"  0002"
" +0002"
"0002  "
"             000000000002"
#<ArgumentError: invalid precision>
#<ArgumentError: invalid precision>

Changed 18 months ago by lsansonetti@…

  • status changed from new to closed
  • resolution set to fixed
  • milestone changed from MacRuby 0.6 to MacRuby 0.7

The patch looks good and all specs pass :) Therefore, I merged it as r4428. Thanks for working on this.

Note: See TracTickets for help on using tickets.