Skip to content

Commit

Permalink
Allow boolean parsing of strings containing 0 and 1 (#29997)
Browse files Browse the repository at this point in the history
  • Loading branch information
kim366 authored and JeffBezanson committed Nov 28, 2018
1 parent f1a41e2 commit d4c6d16
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
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

0 comments on commit d4c6d16

Please sign in to comment.