Skip to content

Commit

Permalink
Add specific method for Base.string to avoid extra copying
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnj committed Sep 6, 2019
1 parent d090052 commit bde0c92
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions base/ryu/Ryu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ function Base.show(io::IO, x::T) where {T <: Base.IEEEFloat}
return
end

function Base.string(x::T) where {T <: Base.IEEEFloat}
buf = Base.StringVector(neededdigits(T))
pos = writeshortest(buf, 1, x, false, false, true, -1,
x isa Float32 ? UInt8('f') : UInt8('e'), false, UInt8('.'), false)
return String(resize!(buf, pos - 1))
end

Base.print(io::IO, x::Union{Float16, Float32}) = show(IOContext(io, :compact => true), x)

end # module

0 comments on commit bde0c92

Please sign in to comment.