Permalink
Browse files

Fix space flag when Inf/NaN and width==3

While `"% 2f"` and `"% 4f"` result in `" Inf"` and `" Inf"`
respectively, `"% 3f"` results in `"Inf"` (no space).
  • Loading branch information...
nobu committed Apr 23, 2017
1 parent 809b7ca commit 7314423bd1989199c834a1a2af01df9d7f42ff17
Showing with 57 additions and 3 deletions.
  1. +1 −3 mrbgems/mruby-sprintf/src/sprintf.c
  2. +56 −0 mrbgems/mruby-sprintf/test/sprintf.rb
@@ -1048,7 +1048,7 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
expr = "Inf";
}
need = elen;
if ((!isnan(fval) && fval < 0.0) || (flags & FPLUS))
if ((!isnan(fval) && fval < 0.0) || (flags & (FPLUS|FSPACE)))
need++;
if ((flags & FWIDTH) && need < width)
need = width;
@@ -1072,8 +1072,6 @@ mrb_str_format(mrb_state *mrb, int argc, const mrb_value *argv, mrb_value fmt)
buf[blen + need - elen - 1] = '-';
else if (flags & FPLUS)
buf[blen + need - elen - 1] = '+';
else if ((flags & FSPACE) && need > width)
blen++;
memcpy(&buf[blen + need - elen], expr, elen);
}
blen += strlen(&buf[blen]);
@@ -8,6 +8,62 @@
assert_equal 15, ("%b" % (1<<14)).size
end
assert('String#% with inf') do
inf = Float::INFINITY
assert_equal "Inf", "%f" % inf
assert_equal "Inf", "%2f" % inf
assert_equal "Inf", "%3f" % inf
assert_equal " Inf", "%4f" % inf
assert_equal " Inf", "%5f" % inf
assert_equal "+Inf", "%+f" % inf
assert_equal "+Inf", "%+2f" % inf
assert_equal "+Inf", "%+3f" % inf
assert_equal "+Inf", "%+4f" % inf
assert_equal " +Inf", "%+5f" % inf
assert_equal "Inf", "%-f" % inf
assert_equal "Inf", "%-2f" % inf
assert_equal "Inf", "%-3f" % inf
assert_equal "Inf ", "%-4f" % inf
assert_equal "Inf ", "%-5f" % inf
assert_equal " Inf", "% f" % inf
assert_equal " Inf", "% 2f" % inf
assert_equal " Inf", "% 3f" % inf
assert_equal " Inf", "% 4f" % inf
assert_equal " Inf", "% 5f" % inf
end
assert('String#% with nan') do
nan = Float::NAN
assert_equal "NaN", "%f" % nan
assert_equal "NaN", "%2f" % nan
assert_equal "NaN", "%3f" % nan
assert_equal " NaN", "%4f" % nan
assert_equal " NaN", "%5f" % nan
assert_equal "+NaN", "%+f" % nan
assert_equal "+NaN", "%+2f" % nan
assert_equal "+NaN", "%+3f" % nan
assert_equal "+NaN", "%+4f" % nan
assert_equal " +NaN", "%+5f" % nan
assert_equal "NaN", "%-f" % nan
assert_equal "NaN", "%-2f" % nan
assert_equal "NaN", "%-3f" % nan
assert_equal "NaN ", "%-4f" % nan
assert_equal "NaN ", "%-5f" % nan
assert_equal " NaN", "% f" % nan
assert_equal " NaN", "% 2f" % nan
assert_equal " NaN", "% 3f" % nan
assert_equal " NaN", "% 4f" % nan
assert_equal " NaN", "% 5f" % nan
end
assert("String#% with invalid chr") do
begin
class Fixnum

0 comments on commit 7314423

Please sign in to comment.