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 boolean parsing of strings containing 0 and 1 #29997

Merged
merged 1 commit into from
Nov 28, 2018
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
7 changes: 7 additions & 0 deletions base/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ function tryparse_internal(::Type{Bool}, sbuff::Union{String,SubString{String}},
return nothing
end

if isnumeric(sbuff[1])
intres = tryparse_internal(UInt8, sbuff, startpos, endpos, base, false)
(intres == 1) && return true
(intres == 0) && return false
raise && throw(ArgumentError("invalid Bool representation: $(repr(sbuff))"))
end

orig_start = startpos
orig_end = endpos

Expand Down
11 changes: 11 additions & 0 deletions test/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,14 @@ end
@test eltype([tryparse(Float64, s) for s in String[]]) == Union{Nothing, Float64}
@test eltype([tryparse(Complex{Int}, s) for s in String[]]) == Union{Nothing, Complex{Int}}
end

@testset "isssue #29980" begin
@test parse(Bool, "1") === true
@test parse(Bool, "01") === true
@test parse(Bool, "0") === false
@test parse(Bool, "000000000000000000000000000000000000000000000000001") === true
@test parse(Bool, "000000000000000000000000000000000000000000000000000") === false
@test_throws ArgumentError parse(Bool, "1000000000000000000000000000000000000000000000000000")
@test_throws ArgumentError parse(Bool, "2")
@test_throws ArgumentError parse(Bool, "02")
end