diff --git a/ChangeLog b/ChangeLog index 10a7922520..96b8fd5093 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Thu Oct 14 09:46:28 2010 NARUSE, Yui + + * sprintf.c (rb_str_format): fix: sprintf with hex format and + precision includes wrong dots. + Fri Oct 15 16:40:37 2010 NARUSE, Yui * pack.c (pack_pack): fix more than one modifiers appear in the diff --git a/sprintf.c b/sprintf.c index 21509eafdd..0e97955161 100644 --- a/sprintf.c +++ b/sprintf.c @@ -844,7 +844,7 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) } else { s = nbuf; - if (v < 0) { + if (v < 0 && !(flags & FPREC0)) { dots = 1; } snprintf(fbuf, sizeof(fbuf), "%%l%c", *p == 'X' ? 'x' : *p); @@ -892,7 +892,8 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) tmp1 = tmp = rb_big2str0(val, base, RBIGNUM_SIGN(val)); s = RSTRING_PTR(tmp); if (*s == '-') { - dots = 1; + if (!(flags & FPREC0)) + dots = 1; if (base == 10) { rb_warning("negative number for %%u specifier"); } @@ -925,14 +926,11 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt) } } if (prefix && !prefix[1]) { /* octal */ - if (dots) { - prefix = 0; - } - else if (len == 1 && *s == '0') { + if (len == 1 && *s == '0') { len = 0; if (flags & FPREC) prec--; } - else if ((flags & FPREC) && (prec > len)) { + else if ((flags & FPREC) && (prec > len) && v >= 0) { prefix = 0; } } diff --git a/test/ruby/test_sprintf.rb b/test/ruby/test_sprintf.rb index 96a1b62bb7..24b6a34b3a 100644 --- a/test/ruby/test_sprintf.rb +++ b/test/ruby/test_sprintf.rb @@ -24,12 +24,12 @@ class TestSprintf < Test::Unit::TestCase assert_equal("0000", sprintf("%.4b", 0)) assert_equal("0001", sprintf("%.4b", 1)) assert_equal("0010", sprintf("%.4b", 2)) - assert_equal("..11", sprintf("%.4b", -1)) + assert_equal("1111", sprintf("%.4b", -1)) assert_equal(" 0000", sprintf("%6.4b", 0)) assert_equal(" 0001", sprintf("%6.4b", 1)) assert_equal(" 0010", sprintf("%6.4b", 2)) - assert_equal(" ..11", sprintf("%6.4b", -1)) + assert_equal(" 1111", sprintf("%6.4b", -1)) assert_equal(" 0", sprintf("%#4b", 0)) assert_equal(" 0b1", sprintf("%#4b", 1)) @@ -44,12 +44,12 @@ class TestSprintf < Test::Unit::TestCase assert_equal("0000", sprintf("%#.4b", 0)) assert_equal("0b0001", sprintf("%#.4b", 1)) assert_equal("0b0010", sprintf("%#.4b", 2)) - assert_equal("0b..11", sprintf("%#.4b", -1)) + assert_equal("0b1111", sprintf("%#.4b", -1)) assert_equal(" 0000", sprintf("%#6.4b", 0)) assert_equal("0b0001", sprintf("%#6.4b", 1)) assert_equal("0b0010", sprintf("%#6.4b", 2)) - assert_equal("0b..11", sprintf("%#6.4b", -1)) + assert_equal("0b1111", sprintf("%#6.4b", -1)) assert_equal("+0", sprintf("%+b", 0)) assert_equal("+1", sprintf("%+b", 1)) @@ -288,6 +288,8 @@ class TestSprintf < Test::Unit::TestCase b1 = (/\.\./ =~ s1) != nil b2 = (/\.\./ =~ s2) != nil assert(b1 == b2, "[ruby-dev:33224]") + + assert_equal("ffffffff", sprintf("%.8x", -1)) end def test_named diff --git a/test/ruby/test_sprintf_comb.rb b/test/ruby/test_sprintf_comb.rb index 261732bcbc..3105127046 100644 --- a/test/ruby/test_sprintf_comb.rb +++ b/test/ruby/test_sprintf_comb.rb @@ -190,7 +190,7 @@ class TestSprintfComb < Test::Unit::TestCase if digits.last != radix-1 digits << (radix-1) end - sign = '..' + sign = '..' unless precision else sign = '-' end @@ -222,8 +222,8 @@ class TestSprintfComb < Test::Unit::TestCase end end if type == 'o' && hs - if digits.empty? || digits.last != d - digits << d + if digits.empty? || digits.last != 0 + prefix = '0' end end