[前][次][番号順一覧][スレッド一覧]

ruby-changes:62141

From: Kenta <ko1@a...>
Date: Mon, 6 Jul 2020 08:56:25 +0900 (JST)
Subject: [ruby-changes:62141] 6a826eb4b0 (master): [ruby/bigdecimal] Return US-ASCII string from BigDecimal#to_s

https://git.ruby-lang.org/ruby.git/commit/?id=6a826eb4b0

From 6a826eb4b08484cb3b6279da905f5b9e03e0df5f Mon Sep 17 00:00:00 2001
From: Kenta Murata <mrkn@m...>
Date: Mon, 23 Dec 2019 11:07:23 +0900
Subject: [ruby/bigdecimal] Return US-ASCII string from BigDecimal#to_s

Fixes #159

https://github.com/ruby/bigdecimal/commit/57ee92e700

diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index 3800152..e0e1c68 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -2092,7 +2092,7 @@ BigDecimal_to_s(int argc, VALUE *argv, VALUE self) https://github.com/ruby/ruby/blob/trunk/ext/bigdecimal/bigdecimal.c#L2092
 	nc += (nc + mc - 1) / mc + 1;
     }
 
-    str = rb_str_new(0, nc);
+    str = rb_usascii_str_new(0, nc);
     psz = RSTRING_PTR(str);
 
     if (fmt) {
diff --git a/test/bigdecimal/test_bigdecimal.rb b/test/bigdecimal/test_bigdecimal.rb
index 0a190d2..4d53575 100644
--- a/test/bigdecimal/test_bigdecimal.rb
+++ b/test/bigdecimal/test_bigdecimal.rb
@@ -1534,29 +1534,34 @@ class TestBigDecimal < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/bigdecimal/test_bigdecimal.rb#L1534
     assert_equal(BigDecimal::SIGN_NEGATIVE_ZERO, (-1 / inf).sign)
   end
 
+  def assert_equal_us_ascii_string(a, b)
+    assert_equal(a, b)
+    assert_equal(Encoding::US_ASCII, b.encoding)
+  end
+
   def test_to_special_string
     BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false)
     BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false)
     nan = BigDecimal("NaN")
-    assert_equal("NaN", nan.to_s)
+    assert_equal_us_ascii_string("NaN", nan.to_s)
     inf = BigDecimal("Infinity")
-    assert_equal("Infinity", inf.to_s)
-    assert_equal(" Infinity", inf.to_s(" "))
-    assert_equal("+Infinity", inf.to_s("+"))
-    assert_equal("-Infinity", (-inf).to_s)
+    assert_equal_us_ascii_string("Infinity", inf.to_s)
+    assert_equal_us_ascii_string(" Infinity", inf.to_s(" "))
+    assert_equal_us_ascii_string("+Infinity", inf.to_s("+"))
+    assert_equal_us_ascii_string("-Infinity", (-inf).to_s)
     pzero = BigDecimal("0")
-    assert_equal("0.0", pzero.to_s)
-    assert_equal(" 0.0", pzero.to_s(" "))
-    assert_equal("+0.0", pzero.to_s("+"))
-    assert_equal("-0.0", (-pzero).to_s)
+    assert_equal_us_ascii_string("0.0", pzero.to_s)
+    assert_equal_us_ascii_string(" 0.0", pzero.to_s(" "))
+    assert_equal_us_ascii_string("+0.0", pzero.to_s("+"))
+    assert_equal_us_ascii_string("-0.0", (-pzero).to_s)
   end
 
   def test_to_string
-    assert_equal("0.01", BigDecimal("0.01").to_s("F"))
+    assert_equal_us_ascii_string("0.01", BigDecimal("0.01").to_s("F"))
     s = "0." + "0" * 100 + "1"
-    assert_equal(s, BigDecimal(s).to_s("F"))
+    assert_equal_us_ascii_string(s, BigDecimal(s).to_s("F"))
     s = "1" + "0" * 100 + ".0"
-    assert_equal(s, BigDecimal(s).to_s("F"))
+    assert_equal_us_ascii_string(s, BigDecimal(s).to_s("F"))
   end
 
   def test_ctov
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]