Skip to content

Commit

Permalink
Allow underscores in big"[float]" (JuliaLang#43346)
Browse files Browse the repository at this point in the history
  • Loading branch information
ettersi authored and LilithHafner committed Mar 8, 2022
1 parent efbfcee commit ab52b88
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
27 changes: 16 additions & 11 deletions base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -689,25 +689,30 @@ julia> big"7891.5"
```
"""
macro big_str(s)
message = "invalid number format $s for BigInt or BigFloat"
throw_error = :(throw(ArgumentError($message)))
if '_' in s
# remove _ in s[2:end-1]
bf = IOBuffer(maxsize=lastindex(s))
print(bf, s[1])
c = s[1]
print(bf, c)
is_prev_underscore = (c == '_')
is_prev_dot = (c == '.')
for c in SubString(s, 2, lastindex(s)-1)
c != '_' && print(bf, c)
c == '_' && is_prev_dot && return throw_error
c == '.' && is_prev_underscore && return throw_error
is_prev_underscore = (c == '_')
is_prev_dot = (c == '.')
end
print(bf, s[end])
seekstart(bf)
n = tryparse(BigInt, String(take!(bf)))
n === nothing || return n
else
n = tryparse(BigInt, s)
n === nothing || return n
n = tryparse(BigFloat, s)
n === nothing || return n
s = String(take!(bf))
end
message = "invalid number format $s for BigInt or BigFloat"
return :(throw(ArgumentError($message)))
n = tryparse(BigInt, s)
n === nothing || return n
n = tryparse(BigFloat, s)
n === nothing || return n
return throw_error
end

## integer promotions ##
Expand Down
8 changes: 7 additions & 1 deletion test/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,16 @@ end
end
end

@testset "issue #21092" begin
@testset "Underscores in big_str" begin
@test big"1_0_0_0" == BigInt(1000)
@test_throws ArgumentError big"1_0_0_0_"
@test_throws ArgumentError big"_1_0_0_0"

@test big"1_0.2_5" == BigFloat(10.25)
@test_throws ArgumentError big"_1_0.2_5"
@test_throws ArgumentError big"1_0.2_5_"
@test_throws ArgumentError big"1_0_.2_5"
@test_throws ArgumentError big"1_0._2_5"
end

# issue #26779
Expand Down

0 comments on commit ab52b88

Please sign in to comment.