@@ -38,16 +38,38 @@ julia> parse(Complex{Float64}, "3.2e-1 + 4.5im")
3838parse (T:: Type , str; base = Int)
3939parse (:: Type{Union{}} , slurp... ; kwargs... ) = error (" cannot parse a value as Union{}" )
4040
41- function parse (:: Type{T} , c:: AbstractChar ; base:: Integer = 10 ) where T<: Integer
42- a:: Int = (base <= 36 ? 10 : 36 )
43- 2 <= base <= 62 || throw (ArgumentError (" invalid base: base must be 2 ≤ base ≤ 62, got $base " ))
44- d = ' 0' <= c <= ' 9' ? c- ' 0' :
45- ' A' <= c <= ' Z' ? c- ' A' + 10 :
46- ' a' <= c <= ' z' ? c- ' a' + a : throw (ArgumentError (" invalid digit: $(repr (c)) " ))
47- d < base || throw (ArgumentError (" invalid base $base digit $(repr (c)) " ))
48- convert (T, d)
41+ @noinline function _invalid_base (base)
42+ throw (ArgumentError (" invalid base: base must be 2 ≤ base ≤ 62, got $base " ))
43+ end
44+
45+ @noinline _invalid_digit (base, char) = throw (ArgumentError (" invalid base $base digit $(repr (char)) " ))
46+
47+ function parse_char (:: Type{T} , c:: AbstractChar , base:: Integer , throw:: Bool ) where T
48+ a:: UInt8 = (base <= 36 ? 10 : 36 )
49+ (2 <= base <= 62 ) || _invalid_base (base)
50+ base = base % UInt8
51+ cp = codepoint (c)
52+ cp = cp > 0x7a ? 0xff : cp % UInt8
53+ d = UInt8 (' 0' ) ≤ cp ≤ UInt8 (' 9' ) ? cp - UInt8 (' 0' ) :
54+ UInt8 (' A' ) ≤ cp ≤ UInt8 (' Z' ) ? cp - UInt8 (' A' ) + UInt8 (10 ) :
55+ UInt8 (' a' ) ≤ cp ≤ UInt8 (' z' ) ? cp - UInt8 (' a' ) + a :
56+ 0xff
57+ d < base || (throw ? _invalid_digit (base, c) : return nothing )
58+ convert (T, d):: T
59+ end
60+
61+ function parse (:: Type{T} , c:: AbstractChar ; base:: Integer = 10 ) where {T <: Integer }
62+ @inline parse_char (T, c, base, true )
4963end
5064
65+ function tryparse (:: Type{T} , c:: AbstractChar ; base:: Integer = 10 ) where {T <: Integer }
66+ @inline parse_char (T, c, base, false )
67+ end
68+
69+ # For consistency with parse(t, AbstractString), support a `base` argument only when T<:Integer
70+ parse (:: Type{T} , c:: AbstractChar ) where T = @inline parse_char (T, c, 10 , true )
71+ tryparse (:: Type{T} , c:: AbstractChar ) where T = @inline parse_char (T, c, 10 , false )
72+
5173function parseint_iterate (s:: AbstractString , startpos:: Int , endpos:: Int )
5274 (0 < startpos <= endpos) || (return Char (0 ), 0 , 0 )
5375 j = startpos
@@ -116,8 +138,7 @@ function tryparse_internal(::Type{T}, s::AbstractString, startpos::Int, endpos::
116138 return nothing
117139 end
118140 if ! (2 <= base <= 62 )
119- raise && throw (ArgumentError (LazyString (" invalid base: base must be 2 ≤ base ≤ 62, got " , base)))
120- return nothing
141+ raise ? _invalid_base (base) : return nothing
121142 end
122143 if i == 0
123144 raise && throw (ArgumentError (" premature end of integer: $(repr (SubString (s,startpos,endpos))) " ))
236257 if 2 <= base <= 62
237258 return base
238259 end
239- throw ( ArgumentError ( " invalid base: base must be 2 ≤ base ≤ 62, got $base " ) )
260+ _invalid_base ( base)
240261end
241262
242263"""
0 commit comments