Skip to content

Commit

Permalink
improve performance for parsing Floats (#27764)
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Jun 26, 2018
1 parent c500caa commit 0961212
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions base/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,26 @@ tryparse_internal(::Type{T}, s::AbstractString, startpos::Int, endpos::Int) wher
function tryparse_internal(::Type{T}, s::AbstractString, startpos::Int, endpos::Int, raise::Bool) where T<:Real
result = tryparse_internal(T, s, startpos, endpos)
if raise && result === nothing
throw(ArgumentError("cannot parse $(repr(s[startpos:endpos])) as $T"))
_parse_failure(T, s, startpos, endpos)
end
return result
end
function tryparse_internal(::Type{T}, s::AbstractString, raise::Bool) where T<:Real
result = tryparse(T, s)
if raise && result === nothing
_parse_failure(T, s)
end
return result
end
@noinline _parse_failure(T, s::AbstractString, startpos = firstindex(s), endpos = lastindex(s)) =
throw(ArgumentError("cannot parse $(repr(s[startpos:endpos])) as $T"))

tryparse_internal(::Type{T}, s::AbstractString, startpos::Int, endpos::Int, raise::Bool) where T<:Integer =
tryparse_internal(T, s, startpos, endpos, 10, raise)

parse(::Type{T}, s::AbstractString) where T<:Union{Real,Complex} =
parse(::Type{T}, s::AbstractString) where T<:Real =
convert(T, tryparse_internal(T, s, true))
parse(::Type{T}, s::AbstractString) where T<:Complex =
convert(T, tryparse_internal(T, s, firstindex(s), lastindex(s), true))

tryparse(T::Type{Complex{S}}, s::AbstractString) where S<:Real =
Expand Down

0 comments on commit 0961212

Please sign in to comment.