Skip to content

Commit

Permalink
Allow boolean parsing of strings containing 0 and 1
Browse files Browse the repository at this point in the history
  • Loading branch information
kim366 committed Nov 12, 2018
1 parent 96ce5ba commit 690198b
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 690198b

Please sign in to comment.