Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #14341, rewrite bytes2hex to be more efficient #14397

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions base/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,18 @@ maxintfloat() = maxintfloat(Float64)

isinteger(x::AbstractFloat) = (trunc(x)==x)&isfinite(x)

num2hex(x::Float16) = hex(reinterpret(UInt16,x), 4)
num2hex(x::Float32) = hex(box(UInt32,unbox(Float32,x)),8)
num2hex(x::Float64) = hex(box(UInt64,unbox(Float64,x)),16)
hex(x::Float16) = hex(reinterpret(UInt16,x), 4)
hex(x::Float32) = hex(box(UInt32,unbox(Float32,x)),8)
hex(x::Float64) = hex(box(UInt64,unbox(Float64,x)),16)

num2hex{T<:Union{Float16, Float32, Float64}}(x::T) = hex(x)

function hex2num(s::AbstractString)
if length(s) <= 8
return box(Float32,unbox(UInt32,parse(UInt32,s,16)))
box(Float32,unbox(UInt32,parse(UInt32,s,16)))
else
box(Float64,unbox(UInt64,parse(UInt64,s,16)))
end
return box(Float64,unbox(UInt64,parse(UInt64,s,16)))
end

@vectorize_1arg Number abs
Expand Down
2 changes: 1 addition & 1 deletion base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function hex(x::Unsigned, pad::Int, neg::Bool)
x >>= 4
i -= 1
end
if neg; a[1]='-'; end
neg && (a[1] = '-')
ASCIIString(a)
end

Expand Down
17 changes: 16 additions & 1 deletion base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,19 @@ function hex2bytes(s::AbstractString)
return a
end

bytes2hex(arr::Vector{UInt8}) = join([hex(i,2) for i in arr])
function hex(arr::Vector{UInt8})
out = Vector{UInt8}(length(arr)<<1)
o = 0
for i = 1:length(arr)
v = arr[i]
d = v>>4
out[o += 1] = ('0' + d + 39 * (d > 9))
d = v & 0xf
out[o += 1] = ('0' + d + 39 * (d > 9))
end
ASCIIString(out)
end

hex(str::ByteString) = hex(str.data)

bytes2hex(arr::Vector{UInt8}) = hex(arr)