Skip to content

Commit 244da57

Browse files
Sijaasterite
authored andcommitted
Allow leading + in number strings
1 parent 80cbe66 commit 244da57

File tree

6 files changed

+12
-0
lines changed

6 files changed

+12
-0
lines changed

spec/std/big/big_decimal_spec.cr

+1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ describe BigDecimal do
177177
it "can be converted from other types" do
178178
1.to_big_d.should eq (BigDecimal.new(1))
179179
"1.5".to_big_d.should eq (BigDecimal.new(15, 1))
180+
"+1.5".to_big_d.should eq (BigDecimal.new(15, 1))
180181
BigInt.new(15).to_big_d.should eq (BigDecimal.new(15, 0))
181182
1.5.to_big_d.should eq (BigDecimal.new(15, 1))
182183
1.5.to_big_f.to_big_d.should eq (BigDecimal.new(15, 1))

spec/std/big/big_float_spec.cr

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ describe "BigFloat" do
1111
it "new(String)" do
1212
bigfloat_of_integer_value.to_s.should eq(string_of_integer_value)
1313
bigfloat_of_float_value.to_s.should eq(string_of_float_value)
14+
BigFloat.new("+#{string_of_integer_value}").to_s.should eq(string_of_integer_value)
15+
BigFloat.new("-#{string_of_integer_value}").to_s.should eq("-#{string_of_integer_value}")
1416
end
1517

1618
it "new(BigInt)" do

spec/std/big/big_int_spec.cr

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ describe "BigInt" do
2222

2323
it "creates from string" do
2424
BigInt.new("12345678").to_s.should eq("12345678")
25+
BigInt.new("+12345678").to_s.should eq("12345678")
26+
BigInt.new("-12345678").to_s.should eq("-12345678")
2527
end
2628

2729
it "raises if creates from string but invalid" do

src/big/big_decimal.cr

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ struct BigDecimal < Number
4545
#
4646
# Allows only valid number strings with an optional negative sign.
4747
def initialize(str : String)
48+
# Strip leading '+' char to smooth out cases with strings like "+123"
49+
str = str.lchop('+')
50+
4851
raise InvalidBigDecimalException.new(str, "Zero size") if str.bytesize == 0
4952

5053
# Check str's validity and find index of .

src/big/big_float.cr

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ struct BigFloat < Float
1313
end
1414

1515
def initialize(str : String)
16+
# Strip leading '+' char to smooth out cases with strings like "+123"
17+
str = str.lchop('+')
1618
LibGMP.mpf_init_set_str(out @mpf, str, 10)
1719
end
1820

src/big/big_int.cr

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ struct BigInt < Int
2828
# BigInt.new("1234567890ABCDEF", base: 16) # => 1311768467294899695
2929
# ```
3030
def initialize(str : String, base = 10)
31+
# Strip leading '+' char to smooth out cases with strings like "+123"
32+
str = str.lchop('+')
3133
err = LibGMP.init_set_str(out @mpz, str, base)
3234
if err == -1
3335
raise ArgumentError.new("Invalid BigInt: #{str}")

0 commit comments

Comments
 (0)