diff --git a/NEWS.md b/NEWS.md index 2df1ea68b030a..000df9c2f22ee 100644 --- a/NEWS.md +++ b/NEWS.md @@ -456,6 +456,9 @@ This section lists changes that do not have deprecation warnings. * `indexin` now returns the first rather than the last matching index ([#25998]). + * `parse(::Type, ::Char)` now uses a default base of 10, like other number parsing + methods, instead of 36 ([#26576]). + Library improvements -------------------- diff --git a/base/parse.jl b/base/parse.jl index 7c1f51117af5d..0abb7e623980f 100644 --- a/base/parse.jl +++ b/base/parse.jl @@ -33,7 +33,7 @@ julia> parse(Complex{Float64}, "3.2e-1 + 4.5im") """ parse(T::Type, str; base = Int) -function parse(::Type{T}, c::AbstractChar; base::Integer = 36) where T<:Integer +function parse(::Type{T}, c::AbstractChar; base::Integer = 10) where T<:Integer a::Int = (base <= 36 ? 10 : 36) 2 <= base <= 62 || throw(ArgumentError("invalid base: base must be 2 ≤ base ≤ 62, got $base")) d = '0' <= c <= '9' ? c-'0' : diff --git a/test/parse.jl b/test/parse.jl index 077d0fb508b66..4107fd3171f18 100644 --- a/test/parse.jl +++ b/test/parse.jl @@ -23,8 +23,12 @@ @test parse(Int,"-10") == -10 @test parse(Int64,"3830974272") == 3830974272 @test parse(Int64,"-3830974272") == -3830974272 + @test parse(Int,'3') == 3 @test parse(Int,'3', base = 8) == 3 +@test parse(Int, 'a', base=16) == 10 +@test_throws ArgumentError parse(Int, 'a') +@test_throws ArgumentError parse(Int,typemax(Char)) # Issue 20587 for T in Any[BigInt, Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8] @@ -173,9 +177,6 @@ parsehex(s) = parse(Int,s, base = 16) @test parse(Int, "3\u2003\u202F") == 3 @test_throws ArgumentError parse(Int, "3\u2003\u202F,") -@test parse(Int,'a') == 10 -@test_throws ArgumentError parse(Int,typemax(Char)) - @test parse(Int,"1234") == 1234 @test parse(Int,"0x1234") == 0x1234 @test parse(Int,"0o1234") == 0o1234