8000 Return US-ASCII string from BigDecimal#to_s · ruby/bigdecimal@57ee92e · GitHub
[go: up one dir, main page]

Skip to content

Commit 57ee92e

Browse files
committed
Return US-ASCII string from BigDecimal#to_s
Fixes #159
1 parent 77c64be commit 57ee92e

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

ext/bigdecimal/bigdecimal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2070,7 +2070,7 @@ BigDecimal_to_s(int argc, VALUE *argv, VALUE self)
20702070
nc += (nc + mc - 1) / mc + 1;
20712071
}
20722072

2073-
str = rb_str_new(0, nc);
2073+
str = rb_usascii_str_new(0, nc);
20742074
psz = RSTRING_PTR(str);
20752075

20762076
if (fmt) {

test/bigdecimal/test_bigdecimal.rb

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,29 +1524,34 @@ def test_inf
15241524
assert_equal(BigDecimal::SIGN_NEGATIVE_ZERO, (-1 / inf).sign)
15251525
end
15261526

1527+
def assert_equal_us_ascii_string(a, b)
1528+
assert_equal(a, b)
1529+
assert_equal(Encoding::US_ASCII, b.encoding)
1530+
end
1531+
15271532
def test_to_special_string
15281533
BigDecimal.mode(BigDecimal::EXCEPTION_OVERFLOW, false)
15291534
BigDecimal.mode(BigDecimal::EXCEPTION_NaN, false)
15301535
nan = BigDecimal("NaN")
1531-
assert_equal("NaN", nan.to_s)
1536+
assert_equal_us_ascii_string("NaN", nan.to_s)
15321537
inf = BigDecimal("Infinity")
1533-
assert_equal("Infinity", inf.to_s)
1534-
assert_equal(" Infinity", inf.to_s(" "))
1535-
assert_equal("+Infinity", inf.to_s("+"))
1536-
assert_equal("-Infinity", (-inf).to_s)
1538+
assert_equal_us_ascii_string("Infinity", inf.to_s)
1539+
assert_equal_us_ascii_string(" Infinity", inf.to_s(" "))
1540+
assert_equal_us_ascii_string("+Infinity", inf.to_s("+"))
1541+
assert_equal_us_ascii_string("-Infinity", (-inf).to_s)
15371542
pzero = BigDecimal("0")
1538-
assert_equal("0.0", pzero.to_s)
1539-
assert_equal(" 0.0", pzero.to_s(" "))
1540-
assert_equal("+0.0", pzero.to_s("+"))
1541-
assert_equal("-0.0", (-pzero).to_s)
1543+
assert_equal_us_ascii_string("0.0", pzero.to_s)
1544+
assert_equal_us_ascii_string(" 0.0", pzero.to_s(" "))
1545+
assert_equal_us_ascii_string("+0.0", pzero.to_s("+"))
1546+
assert_equal_us_ascii_string("-0.0", (-pzero).to_s)
15421547
end
15431548

15441549
def test_to_string
1545-
assert_equal("0.01", BigDecimal("0.01").to_s("F"))
1550+
assert_equal_us_ascii_string("0.01", BigDecimal("0.01").to_s("F"))
15461551
s = "0." + "0" * 100 + "1"
1547-
assert_equal(s, BigDecimal(s).to_s("F"))
1552+
assert_equal_us_ascii_string(s, BigDecimal(s).to_s("F"))
15481553
s = "1" + "0" * 100 + ".0"
1549-
assert_equal(s, BigDecimal(s).to_s("F"))
1554+
assert_equal_us_ascii_string(s, BigDecimal(s).to_s("F"))
15501555
end
15511556

15521557
def test_ctov

0 commit comments

Comments
 (0)
0