summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenta Murata <mrkn@mrkn.jp>2019-12-23 11:07:23 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2020-07-06 01:15:47 +0900
commit6a826eb4b08484cb3b6279da905f5b9e03e0df5f (patch)
tree000e618b343805cb4c63792b2a1aea8d54581604
parente794d96ca439a698483b2e76d104b77a1cfdcc94 (diff)
[ruby/bigdecimal] Return US-ASCII string from BigDecimal#to_s
Fixes #159 https://github.com/ruby/bigdecimal/commit/57ee92e700
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/3295
-rw-r--r--ext/bigdecimal/bigdecimal.c2
-rw-r--r--test/bigdecimal/test_bigdecimal.rb29
2 files changed, 18 insertions, 13 deletions
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)
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
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