mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
merge revision(s) 58453,58454: [Backport #13499]
Fix space flag when Inf/NaN and width==3 * sprintf.c (rb_str_format): while `"% 2f"` and `"% 4f"` result in `" Inf"` and `" Inf"` respectively, `"% 3f"` results in `"Inf"` (no space). Refactor "%f" % Inf/NaN * sprintf.c (rb_str_format): as for non-finite float, calculate the exact needed size with the space flag. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@59901 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
0b572d70bc
commit
4fdfb28e7d
4 changed files with 84 additions and 23 deletions
12
ChangeLog
12
ChangeLog
|
@ -1,3 +1,15 @@
|
|||
Thu Sep 14 20:33:52 2017 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
Fix space flag when Inf/NaN and width==3
|
||||
|
||||
* sprintf.c (rb_str_format): while "% 2f" and "% 4f" result in " Inf"
|
||||
and " Inf" respectively, "% 3f" results in "Inf" (no space).
|
||||
|
||||
Refactor "%f" % Inf/NaN
|
||||
|
||||
* sprintf.c (rb_str_format): as for non-finite float, calculate the
|
||||
exact needed size with the space flag.
|
||||
|
||||
Sun Sep 10 10:10:05 2017 SHIBATA Hiroshi <hsbt@ruby-lang.org>
|
||||
|
||||
* lib/rubygems: fix several vulnerabilities in RubyGems; bump to version
|
||||
|
|
37
sprintf.c
37
sprintf.c
|
@ -1135,6 +1135,8 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
|
|||
fval = RFLOAT_VALUE(rb_Float(val));
|
||||
if (isnan(fval) || isinf(fval)) {
|
||||
const char *expr;
|
||||
int elen;
|
||||
char sign = '\0';
|
||||
|
||||
if (isnan(fval)) {
|
||||
expr = "NaN";
|
||||
|
@ -1143,33 +1145,28 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
|
|||
expr = "Inf";
|
||||
}
|
||||
need = (int)strlen(expr);
|
||||
if ((!isnan(fval) && fval < 0.0) || (flags & FPLUS))
|
||||
need++;
|
||||
elen = need;
|
||||
i = 0;
|
||||
if (!isnan(fval) && fval < 0.0)
|
||||
sign = '-';
|
||||
else if (flags & (FPLUS|FSPACE))
|
||||
sign = (flags & FPLUS) ? '+' : ' ';
|
||||
if (sign)
|
||||
++need;
|
||||
if ((flags & FWIDTH) && need < width)
|
||||
need = width;
|
||||
|
||||
CHECK(need + 1);
|
||||
snprintf(&buf[blen], need + 1, "%*s", need, "");
|
||||
FILL(' ', need);
|
||||
if (flags & FMINUS) {
|
||||
if (!isnan(fval) && fval < 0.0)
|
||||
buf[blen++] = '-';
|
||||
else if (flags & FPLUS)
|
||||
buf[blen++] = '+';
|
||||
else if (flags & FSPACE)
|
||||
blen++;
|
||||
memcpy(&buf[blen], expr, strlen(expr));
|
||||
if (sign)
|
||||
buf[blen - need--] = sign;
|
||||
memcpy(&buf[blen - need], expr, elen);
|
||||
}
|
||||
else {
|
||||
if (!isnan(fval) && fval < 0.0)
|
||||
buf[blen + need - strlen(expr) - 1] = '-';
|
||||
else if (flags & FPLUS)
|
||||
buf[blen + need - strlen(expr) - 1] = '+';
|
||||
else if ((flags & FSPACE) && need > width)
|
||||
blen++;
|
||||
memcpy(&buf[blen + need - strlen(expr)], expr,
|
||||
strlen(expr));
|
||||
if (sign)
|
||||
buf[blen - elen - 1] = sign;
|
||||
memcpy(&buf[blen - elen], expr, elen);
|
||||
}
|
||||
blen += strlen(&buf[blen]);
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
@ -83,6 +83,18 @@ class TestSprintf < Test::Unit::TestCase
|
|||
assert_equal("NaN", sprintf("%-f", nan))
|
||||
assert_equal("+NaN", sprintf("%+f", nan))
|
||||
|
||||
assert_equal("NaN", sprintf("%3f", nan))
|
||||
assert_equal("NaN", sprintf("%-3f", nan))
|
||||
assert_equal("+NaN", sprintf("%+3f", nan))
|
||||
|
||||
assert_equal(" NaN", sprintf("% 3f", nan))
|
||||
assert_equal(" NaN", sprintf("%- 3f", nan))
|
||||
assert_equal("+NaN", sprintf("%+ 3f", nan))
|
||||
|
||||
assert_equal(" NaN", sprintf("% 03f", nan))
|
||||
assert_equal(" NaN", sprintf("%- 03f", nan))
|
||||
assert_equal("+NaN", sprintf("%+ 03f", nan))
|
||||
|
||||
assert_equal(" NaN", sprintf("%8f", nan))
|
||||
assert_equal("NaN ", sprintf("%-8f", nan))
|
||||
assert_equal(" +NaN", sprintf("%+8f", nan))
|
||||
|
@ -106,6 +118,26 @@ class TestSprintf < Test::Unit::TestCase
|
|||
assert_equal("Inf", sprintf("%-f", inf))
|
||||
assert_equal("+Inf", sprintf("%+f", inf))
|
||||
|
||||
assert_equal(" Inf", sprintf("% f", inf))
|
||||
assert_equal(" Inf", sprintf("%- f", inf))
|
||||
assert_equal("+Inf", sprintf("%+ f", inf))
|
||||
|
||||
assert_equal(" Inf", sprintf("% 0f", inf))
|
||||
assert_equal(" Inf", sprintf("%- 0f", inf))
|
||||
assert_equal("+Inf", sprintf("%+ 0f", inf))
|
||||
|
||||
assert_equal("Inf", sprintf("%3f", inf))
|
||||
assert_equal("Inf", sprintf("%-3f", inf))
|
||||
assert_equal("+Inf", sprintf("%+3f", inf))
|
||||
|
||||
assert_equal(" Inf", sprintf("% 3f", inf))
|
||||
assert_equal(" Inf", sprintf("%- 3f", inf))
|
||||
assert_equal("+Inf", sprintf("%+ 3f", inf))
|
||||
|
||||
assert_equal(" Inf", sprintf("% 03f", inf))
|
||||
assert_equal(" Inf", sprintf("%- 03f", inf))
|
||||
assert_equal("+Inf", sprintf("%+ 03f", inf))
|
||||
|
||||
assert_equal(" Inf", sprintf("%8f", inf))
|
||||
assert_equal("Inf ", sprintf("%-8f", inf))
|
||||
assert_equal(" +Inf", sprintf("%+8f", inf))
|
||||
|
@ -126,6 +158,26 @@ class TestSprintf < Test::Unit::TestCase
|
|||
assert_equal("-Inf", sprintf("%-f", -inf))
|
||||
assert_equal("-Inf", sprintf("%+f", -inf))
|
||||
|
||||
assert_equal("-Inf", sprintf("% f", -inf))
|
||||
assert_equal("-Inf", sprintf("%- f", -inf))
|
||||
assert_equal("-Inf", sprintf("%+ f", -inf))
|
||||
|
||||
assert_equal("-Inf", sprintf("% 0f", -inf))
|
||||
assert_equal("-Inf", sprintf("%- 0f", -inf))
|
||||
assert_equal("-Inf", sprintf("%+ 0f", -inf))
|
||||
|
||||
assert_equal("-Inf", sprintf("%4f", -inf))
|
||||
assert_equal("-Inf", sprintf("%-4f", -inf))
|
||||
assert_equal("-Inf", sprintf("%+4f", -inf))
|
||||
|
||||
assert_equal("-Inf", sprintf("% 4f", -inf))
|
||||
assert_equal("-Inf", sprintf("%- 4f", -inf))
|
||||
assert_equal("-Inf", sprintf("%+ 4f", -inf))
|
||||
|
||||
assert_equal("-Inf", sprintf("% 04f", -inf))
|
||||
assert_equal("-Inf", sprintf("%- 04f", -inf))
|
||||
assert_equal("-Inf", sprintf("%+ 04f", -inf))
|
||||
|
||||
assert_equal(" -Inf", sprintf("%8f", -inf))
|
||||
assert_equal("-Inf ", sprintf("%-8f", -inf))
|
||||
assert_equal(" -Inf", sprintf("%+8f", -inf))
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
#define RUBY_VERSION "2.2.8"
|
||||
#define RUBY_RELEASE_DATE "2017-09-10"
|
||||
#define RUBY_PATCHLEVEL 473
|
||||
#define RUBY_RELEASE_DATE "2017-09-14"
|
||||
#define RUBY_PATCHLEVEL 474
|
||||
|
||||
#define RUBY_RELEASE_YEAR 2017
|
||||
#define RUBY_RELEASE_MONTH 9
|
||||
#define RUBY_RELEASE_DAY 10
|
||||
#define RUBY_RELEASE_DAY 14
|
||||
|
||||
#include "ruby/version.h"
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue