Skip to content

Commit d4c6d16

Browse files
kim366JeffBezanson
authored andcommitted
Allow boolean parsing of strings containing 0 and 1 (#29997)
1 parent f1a41e2 commit d4c6d16

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

base/parse.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ function tryparse_internal(::Type{Bool}, sbuff::Union{String,SubString{String}},
175175
return nothing
176176
end
177177

178+
if isnumeric(sbuff[1])
179+
intres = tryparse_internal(UInt8, sbuff, startpos, endpos, base, false)
180+
(intres == 1) && return true
181+
(intres == 0) && return false
182+
raise && throw(ArgumentError("invalid Bool representation: $(repr(sbuff))"))
183+
end
184+
178185
orig_start = startpos
179186
orig_end = endpos
180187

test/parse.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,14 @@ end
321321
@test eltype([tryparse(Float64, s) for s in String[]]) == Union{Nothing, Float64}
322322
@test eltype([tryparse(Complex{Int}, s) for s in String[]]) == Union{Nothing, Complex{Int}}
323323
end
324+
325+
@testset "isssue #29980" begin
326+
@test parse(Bool, "1") === true
327+
@test parse(Bool, "01") === true
328+
@test parse(Bool, "0") === false
329+
@test parse(Bool, "000000000000000000000000000000000000000000000000001") === true
330+
@test parse(Bool, "000000000000000000000000000000000000000000000000000") === false
331+
@test_throws ArgumentError parse(Bool, "1000000000000000000000000000000000000000000000000000")
332+
@test_throws ArgumentError parse(Bool, "2")
333+
@test_throws ArgumentError parse(Bool, "02")
334+
end

0 commit comments

Comments
 (0)