1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/bigdecimal] Return US-ASCII string from BigDecimal#to_s

Fixes #159

https://github.com/ruby/bigdecimal/commit/57ee92e700
This commit is contained in:
Kenta Murata 2019-12-23 11:07:23 +09:00 committed by Nobuyoshi Nakada
parent e794d96ca4
commit 6a826eb4b0
No known key found for this signature in database
GPG key ID: 7CD2805BFA3770C6
Notes: git 2020-07-06 08:56:27 +09:00
2 changed files with 18 additions and 13 deletions

View file

@ -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) {

View file

@ -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