8ca061
From 4fdfb28e7d2e3eefc0df1e1d034fbfc932c0d2a1 Mon Sep 17 00:00:00 2001
8ca061
From: usa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>
8ca061
Date: Thu, 14 Sep 2017 11:35:52 +0000
8ca061
Subject: [PATCH] merge revision(s) 58453,58454: [Backport #13499]
8ca061
8ca061
	Fix space flag when Inf/NaN and width==3
8ca061
8ca061
	* sprintf.c (rb_str_format): while `"% 2f"` and `"% 4f"` result in
8ca061
	  `" Inf"` and `" Inf"` respectively, `"% 3f"` results in
8ca061
	  `"Inf"` (no space).
8ca061
	Refactor "%f" % Inf/NaN
8ca061
8ca061
	* sprintf.c (rb_str_format): as for non-finite float, calculate
8ca061
	  the exact needed size with the space flag.
8ca061
8ca061
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@59901 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
8ca061
---
8ca061
 ChangeLog                 |   12 ++++++++++
8ca061
 sprintf.c                 |   37 +++++++++++++++-----------------
8ca061
 test/ruby/test_sprintf.rb |   52 ++++++++++++++++++++++++++++++++++++++++++++++
8ca061
 3 files changed, 81 insertions(+), 20 deletions(-)
8ca061
8ca061
diff --git a/ChangeLog b/ChangeLog
8ca061
index ef36ffbd1552..a4594f678f8c 100644
8ca061
--- a/ChangeLog
8ca061
+++ b/ChangeLog
8ca061
@@ -4,6 +4,18 @@
8ca061
 	  protocol list.
8ca061
 	  The protocol list from OpenSSL is not null-terminated.
8ca061
 	  patched by Kazuki Yamaguchi [Bug #11810] [ruby-core:72082]
8ca061
+
8ca061
+Thu Sep 14 20:33:52 2017  Nobuyoshi Nakada  <nobu@ruby-lang.org>
8ca061
+
8ca061
+	Fix space flag when Inf/NaN and width==3
8ca061
+
8ca061
+	* sprintf.c (rb_str_format): while "% 2f" and "% 4f" result in " Inf"
8ca061
+	  and " Inf" respectively, "% 3f" results in "Inf" (no space).
8ca061
+
8ca061
+	Refactor "%f" % Inf/NaN
8ca061
+
8ca061
+	* sprintf.c (rb_str_format): as for non-finite float, calculate the
8ca061
+	  exact needed size with the space flag.
8ca061
 
8ca061
 Sun Sep 10 10:10:05 2017  SHIBATA Hiroshi  <hsbt@ruby-lang.org>
8ca061
 
8ca061
diff --git a/sprintf.c b/sprintf.c
8ca061
index 70c7cceb14d6..db7499979dc3 100644
8ca061
--- a/sprintf.c
8ca061
+++ b/sprintf.c
8ca061
@@ -1025,6 +1025,8 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
8ca061
 		fval = RFLOAT_VALUE(rb_Float(val));
8ca061
 		if (isnan(fval) || isinf(fval)) {
8ca061
 		    const char *expr;
8ca061
+		    int elen;
8ca061
+		    char sign = '\0';
8ca061
 
8ca061
 		    if (isnan(fval)) {
8ca061
 			expr = "NaN";
8ca061
@@ -1033,33 +1035,28 @@ rb_str_format(int argc, const VALUE *argv, VALUE fmt)
8ca061
 			expr = "Inf";
8ca061
 		    }
8ca061
 		    need = (int)strlen(expr);
8ca061
-		    if ((!isnan(fval) && fval < 0.0) || (flags & FPLUS))
8ca061
-			need++;
8ca061
+		    elen = need;
8ca061
+		    i = 0;
8ca061
+		    if (!isnan(fval) && fval < 0.0)
8ca061
+			sign = '-';
8ca061
+		    else if (flags & (FPLUS|FSPACE))
8ca061
+			sign = (flags & FPLUS) ? '+' : ' ';
8ca061
+		    if (sign)
8ca061
+			++need;
8ca061
 		    if ((flags & FWIDTH) && need < width)
8ca061
 			need = width;
8ca061
 
8ca061
-		    CHECK(need + 1);
8ca061
-		    snprintf(&buf[blen], need + 1, "%*s", need, "");
8ca061
+		    FILL(' ', need);
8ca061
 		    if (flags & FMINUS) {
8ca061
-			if (!isnan(fval) && fval < 0.0)
8ca061
-			    buf[blen++] = '-';
8ca061
-			else if (flags & FPLUS)
8ca061
-			    buf[blen++] = '+';
8ca061
-			else if (flags & FSPACE)
8ca061
-			    blen++;
8ca061
-			memcpy(&buf[blen], expr, strlen(expr));
8ca061
+			if (sign)
8ca061
+			    buf[blen - need--] = sign;
8ca061
+			memcpy(&buf[blen - need], expr, elen);
8ca061
 		    }
8ca061
 		    else {
8ca061
-			if (!isnan(fval) && fval < 0.0)
8ca061
-			    buf[blen + need - strlen(expr) - 1] = '-';
8ca061
-			else if (flags & FPLUS)
8ca061
-			    buf[blen + need - strlen(expr) - 1] = '+';
8ca061
-			else if ((flags & FSPACE) && need > width)
8ca061
-			    blen++;
8ca061
-			memcpy(&buf[blen + need - strlen(expr)], expr,
8ca061
-			       strlen(expr));
8ca061
+			if (sign)
8ca061
+			    buf[blen - elen - 1] = sign;
8ca061
+			memcpy(&buf[blen - elen], expr, elen);
8ca061
 		    }
8ca061
-		    blen += strlen(&buf[blen]);
8ca061
 		    break;
8ca061
 		}
8ca061
 
8ca061
diff --git a/test/ruby/test_sprintf.rb b/test/ruby/test_sprintf.rb
8ca061
index 3fd4736a54b0..ab3037a8f267 100644
8ca061
--- a/test/ruby/test_sprintf.rb
8ca061
+++ b/test/ruby/test_sprintf.rb
8ca061
@@ -84,6 +84,18 @@ def test_nan
8ca061
     assert_equal("NaN", sprintf("%-f", nan))
8ca061
     assert_equal("+NaN", sprintf("%+f", nan))
8ca061
 
8ca061
+    assert_equal("NaN", sprintf("%3f", nan))
8ca061
+    assert_equal("NaN", sprintf("%-3f", nan))
8ca061
+    assert_equal("+NaN", sprintf("%+3f", nan))
8ca061
+
8ca061
+    assert_equal(" NaN", sprintf("% 3f", nan))
8ca061
+    assert_equal(" NaN", sprintf("%- 3f", nan))
8ca061
+    assert_equal("+NaN", sprintf("%+ 3f", nan))
8ca061
+
8ca061
+    assert_equal(" NaN", sprintf("% 03f", nan))
8ca061
+    assert_equal(" NaN", sprintf("%- 03f", nan))
8ca061
+    assert_equal("+NaN", sprintf("%+ 03f", nan))
8ca061
+
8ca061
     assert_equal("     NaN", sprintf("%8f", nan))
8ca061
     assert_equal("NaN     ", sprintf("%-8f", nan))
8ca061
     assert_equal("    +NaN", sprintf("%+8f", nan))
8ca061
@@ -107,6 +119,26 @@ def test_inf
8ca061
     assert_equal("Inf", sprintf("%-f", inf))
8ca061
     assert_equal("+Inf", sprintf("%+f", inf))
8ca061
 
8ca061
+    assert_equal(" Inf", sprintf("% f", inf))
8ca061
+    assert_equal(" Inf", sprintf("%- f", inf))
8ca061
+    assert_equal("+Inf", sprintf("%+ f", inf))
8ca061
+
8ca061
+    assert_equal(" Inf", sprintf("% 0f", inf))
8ca061
+    assert_equal(" Inf", sprintf("%- 0f", inf))
8ca061
+    assert_equal("+Inf", sprintf("%+ 0f", inf))
8ca061
+
8ca061
+    assert_equal("Inf", sprintf("%3f", inf))
8ca061
+    assert_equal("Inf", sprintf("%-3f", inf))
8ca061
+    assert_equal("+Inf", sprintf("%+3f", inf))
8ca061
+
8ca061
+    assert_equal(" Inf", sprintf("% 3f", inf))
8ca061
+    assert_equal(" Inf", sprintf("%- 3f", inf))
8ca061
+    assert_equal("+Inf", sprintf("%+ 3f", inf))
8ca061
+
8ca061
+    assert_equal(" Inf", sprintf("% 03f", inf))
8ca061
+    assert_equal(" Inf", sprintf("%- 03f", inf))
8ca061
+    assert_equal("+Inf", sprintf("%+ 03f", inf))
8ca061
+
8ca061
     assert_equal("     Inf", sprintf("%8f", inf))
8ca061
     assert_equal("Inf     ", sprintf("%-8f", inf))
8ca061
     assert_equal("    +Inf", sprintf("%+8f", inf))
8ca061
@@ -127,6 +159,26 @@ def test_inf
8ca061
     assert_equal("-Inf", sprintf("%-f", -inf))
8ca061
     assert_equal("-Inf", sprintf("%+f", -inf))
8ca061
 
8ca061
+    assert_equal("-Inf", sprintf("% f", -inf))
8ca061
+    assert_equal("-Inf", sprintf("%- f", -inf))
8ca061
+    assert_equal("-Inf", sprintf("%+ f", -inf))
8ca061
+
8ca061
+    assert_equal("-Inf", sprintf("% 0f", -inf))
8ca061
+    assert_equal("-Inf", sprintf("%- 0f", -inf))
8ca061
+    assert_equal("-Inf", sprintf("%+ 0f", -inf))
8ca061
+
8ca061
+    assert_equal("-Inf", sprintf("%4f", -inf))
8ca061
+    assert_equal("-Inf", sprintf("%-4f", -inf))
8ca061
+    assert_equal("-Inf", sprintf("%+4f", -inf))
8ca061
+
8ca061
+    assert_equal("-Inf", sprintf("% 4f", -inf))
8ca061
+    assert_equal("-Inf", sprintf("%- 4f", -inf))
8ca061
+    assert_equal("-Inf", sprintf("%+ 4f", -inf))
8ca061
+
8ca061
+    assert_equal("-Inf", sprintf("% 04f", -inf))
8ca061
+    assert_equal("-Inf", sprintf("%- 04f", -inf))
8ca061
+    assert_equal("-Inf", sprintf("%+ 04f", -inf))
8ca061
+
8ca061
     assert_equal("    -Inf", sprintf("%8f", -inf))
8ca061
     assert_equal("-Inf    ", sprintf("%-8f", -inf))
8ca061
     assert_equal("    -Inf", sprintf("%+8f", -inf))