Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow underscores in big"[float]" #43346

Merged
merged 1 commit into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these tests belong in int.jl? I guess it doesn't really matter...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point. It's a consequence of the big"[float]" code being in base/int.jl, which was true already before this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough. Not worth fixing.

@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