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

tweak SHA1 printing #27036

Merged
merged 8 commits into from
May 16, 2018
Merged
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
5 changes: 3 additions & 2 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ struct SHA1
return new(bytes)
end
end
SHA1(s::Union{String,SubString{String}}) = SHA1(hex2bytes(s))
SHA1(s::AbstractString) = SHA1(hex2bytes(s))
string(hash::SHA1) = bytes2hex(hash.bytes)
print(io::IO, hash::SHA1) = print(io, string(hash))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems suboptimal: It seems like we should have a bytes2hex(io, bytes) method that writes the hex digits to a stream rather than allocating a string. That way you can print the hex digits directly instead of allocating a string first.

Copy link
Member

@StefanKarpinski StefanKarpinski May 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's get this merged and tweak for optimality in the future; issue: #27121.


show(io::IO, hash::SHA1) = print(io, "SHA1(", string(hash), ")")
show(io::IO, hash::SHA1) = print(io, "SHA1(\"", string(hash), "\")")
isless(a::SHA1, b::SHA1) = lexless(a.bytes, b.bytes)
hash(a::SHA1, h::UInt) = hash((SHA1, a.bytes), h)
==(a::SHA1, b::SHA1) = a.bytes == b.bytes
Expand Down
3 changes: 2 additions & 1 deletion base/uuid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,5 @@ let groupings = [36:-1:25; 23:-1:20; 18:-1:15; 13:-1:10; 8:-1:1]
end
end

show(io::IO, u::UUID) = write(io, string(u))
print(io::IO, u::UUID) = print(io, string(u))
show(io::IO, u::UUID) = print(io, "UUID(\"", u, "\")")
14 changes: 14 additions & 0 deletions test/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ import UUIDs: UUID, uuid4, uuid_version
import Random: shuffle, randstring
using Test

let shastr = "ab"^20
hash = SHA1(shastr)
@test hash == eval(Meta.parse(repr(hash))) # check show method
@test string(hash) == shastr
@test "check $hash" == "check $shastr"
end

let uuidstr = "ab"^4 * "-" * "ab"^2 * "-" * "ab"^2 * "-" * "ab"^2 * "-" * "ab"^6
uuid = UUID(uuidstr)
@test uuid == eval(Meta.parse(repr(uuid))) # check show method
@test string(uuid) == uuidstr
@test "check $uuid" == "check $uuidstr"
end

function subset(v::Vector{T}, m::Int) where T
T[v[j] for j = 1:length(v) if ((m >>> (j - 1)) & 1) == 1]
end
Expand Down