Skip to content

Commit

Permalink
parsable SHA1 and UUID printing, tests (#27036)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbyrne authored and StefanKarpinski committed May 16, 2018
1 parent 6291d3e commit d3be48c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
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))

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

0 comments on commit d3be48c

Please sign in to comment.