diff options
author | Kenta Murata <mrkn@mrkn.jp> | 2019-12-23 11:07:23 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2020-07-06 01:15:47 +0900 |
commit | 6a826eb4b08484cb3b6279da905f5b9e03e0df5f (patch) | |
tree | 000e618b343805cb4c63792b2a1aea8d54581604 | |
parent | e794d96ca439a698483b2e76d104b77a1cfdcc94 (diff) | |
download | ruby-6a826eb4b08484cb3b6279da905f5b9e03e0df5f.tar.gz |
[ruby/bigdecimal] Return US-ASCII string from BigDecimal#to_s
Fixes #159
https://github.com/ruby/bigdecimal/commit/57ee92e700
-rw-r--r-- | ext/bigdecimal/bigdecimal.c | 2 | ||||
-rw-r--r-- | test/bigdecimal/test_bigdecimal.rb | 29 |
2 files changed, 18 insertions, 13 deletions
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c index 380015248b..e0e1c683b5 100644 --- a/ext/bigdecimal/bigdecimal.c +++ b/ext/bigdecimal/bigdecimal.c @@ -2092,7 +2092,7 @@ BigDecimal_to_s(int argc, VALUE *argv, VALUE self) 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 0a190d2548..4d535757dd 100644 --- a/test/bigdecimal/test_bigdecimal.rb +++ b/test/bigdecimal/test_bigdecimal.rb @@ -1534,29 +1534,34 @@ class TestBigDecimal < Test::Unit::TestCase 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 |