1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* sprintf.c (rb_str_format): fix: sprintf with hex format and

precision includes wrong dots.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@29502 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
naruse 2010-10-15 08:28:12 +00:00
parent ccaf61d5af
commit a160986d90
4 changed files with 19 additions and 14 deletions

View file

@ -1,3 +1,8 @@
Thu Oct 14 09:46:28 2010 NARUSE, Yui <naruse@ruby-lang.org>
* sprintf.c (rb_str_format): fix: sprintf with hex format and
precision includes wrong dots.
Fri Oct 15 16:40:37 2010 NARUSE, Yui <naruse@ruby-lang.org>
* pack.c (pack_pack): fix more than one modifiers appear in the

View file

@ -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,6 +892,7 @@ 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 == '-') {
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;
}
}

View file

@ -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

View file

@ -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