Skip to content

Commit

Permalink
Allow leading + in number strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija authored and asterite committed Jan 16, 2018
1 parent 80cbe66 commit 244da57
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions spec/std/big/big_decimal_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ describe BigDecimal do
it "can be converted from other types" do
1.to_big_d.should eq (BigDecimal.new(1))
"1.5".to_big_d.should eq (BigDecimal.new(15, 1))
"+1.5".to_big_d.should eq (BigDecimal.new(15, 1))
BigInt.new(15).to_big_d.should eq (BigDecimal.new(15, 0))
1.5.to_big_d.should eq (BigDecimal.new(15, 1))
1.5.to_big_f.to_big_d.should eq (BigDecimal.new(15, 1))
Expand Down
2 changes: 2 additions & 0 deletions spec/std/big/big_float_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ describe "BigFloat" do
it "new(String)" do
bigfloat_of_integer_value.to_s.should eq(string_of_integer_value)
bigfloat_of_float_value.to_s.should eq(string_of_float_value)
BigFloat.new("+#{string_of_integer_value}").to_s.should eq(string_of_integer_value)
BigFloat.new("-#{string_of_integer_value}").to_s.should eq("-#{string_of_integer_value}")
end

it "new(BigInt)" do
Expand Down
2 changes: 2 additions & 0 deletions spec/std/big/big_int_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ describe "BigInt" do

it "creates from string" do
BigInt.new("12345678").to_s.should eq("12345678")
BigInt.new("+12345678").to_s.should eq("12345678")
BigInt.new("-12345678").to_s.should eq("-12345678")
end

it "raises if creates from string but invalid" do
Expand Down
3 changes: 3 additions & 0 deletions src/big/big_decimal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ struct BigDecimal < Number
#
# Allows only valid number strings with an optional negative sign.
def initialize(str : String)
# Strip leading '+' char to smooth out cases with strings like "+123"
str = str.lchop('+')

raise InvalidBigDecimalException.new(str, "Zero size") if str.bytesize == 0

# Check str's validity and find index of .
Expand Down
2 changes: 2 additions & 0 deletions src/big/big_float.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ struct BigFloat < Float
end

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

Expand Down
2 changes: 2 additions & 0 deletions src/big/big_int.cr
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ struct BigInt < Int
# BigInt.new("1234567890ABCDEF", base: 16) # => 1311768467294899695
# ```
def initialize(str : String, base = 10)
# Strip leading '+' char to smooth out cases with strings like "+123"
str = str.lchop('+')
err = LibGMP.init_set_str(out @mpz, str, base)
if err == -1
raise ArgumentError.new("Invalid BigInt: #{str}")
Expand Down

0 comments on commit 244da57

Please sign in to comment.