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).
MODIFY
mrbgems/mruby-sprintf/src/sprintf.c
mrbgems/mruby-sprintf/src/sprintf.c
@@ -1048,7 +1048,7 @@ retry:
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 @@ retry:
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]);

MODIFY
mrbgems/mruby-sprintf/test/sprintf.rb
mrbgems/mruby-sprintf/test/sprintf.rb
@@ -8,6 +8,62 @@ assert('String#%') do
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

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