From 78308d543d344b19baa66eb673685e0a65e23a80 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Tue, 8 May 2018 16:19:09 -0700 Subject: [PATCH 1/8] tweak SHA1 printing 1. Print quotes around byte string in show 2. Add a print method to enable string interpolation --- base/loading.jl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/base/loading.jl b/base/loading.jl index 75b9d6e7e3b69..08a55b8c01405 100644 --- a/base/loading.jl +++ b/base/loading.jl @@ -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 From 263357cd0857f01b9ee82cdf56bc78c193540cd1 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Wed, 9 May 2018 12:18:49 -0700 Subject: [PATCH 2/8] same for UUID --- base/uuid.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/base/uuid.jl b/base/uuid.jl index 72ce1aa6b293f..a1f3eb7c03f8d 100644 --- a/base/uuid.jl +++ b/base/uuid.jl @@ -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, "\")") From 6b501c7cc853161c2cb5db75a35ade76b03fa47d Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Thu, 10 May 2018 11:33:22 -0700 Subject: [PATCH 3/8] add tests for SHA1 and UUID printing --- test/loading.jl | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/loading.jl b/test/loading.jl index 3336fab4d272c..9cd45efbc7133 100644 --- a/test/loading.jl +++ b/test/loading.jl @@ -86,6 +86,18 @@ import UUIDs: UUID, uuid4, uuid_version import Random: shuffle, randstring using Test +shastr = "ab"^20 +hash = SHA1(shastr) +@test hash == eval(sprint(io -> show(io, hash))) # check show method +@test string(hash) == shastr +@test "check $hash" == "check $shastr" + +uuidstr = "ab"^4 * "-" * "ab"^2 * "-" * "ab"^2 * "-" * "ab"^2 * "-" * "ab"^6 +uuid = UUID(uuidstr) +@test uuid == eval(sprint(io -> show(io, uuid))) # check show method +@test string(uuid) == uuidstr +@test "check $uuid" == "check $uuidstr" + function subset(v::Vector{T}, m::Int) where T T[v[j] for j = 1:length(v) if ((m >>> (j - 1)) & 1) == 1] end From 1df24d9c3f5bfbc9c1d29b74f07b92baea42778b Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Fri, 11 May 2018 10:57:47 -0700 Subject: [PATCH 4/8] fix parsing --- test/loading.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/loading.jl b/test/loading.jl index 9cd45efbc7133..53a71f6baf8dd 100644 --- a/test/loading.jl +++ b/test/loading.jl @@ -88,13 +88,13 @@ using Test shastr = "ab"^20 hash = SHA1(shastr) -@test hash == eval(sprint(io -> show(io, hash))) # check show method +@test hash == eval(parse(sprint(io -> show(io, hash)))) # check show method @test string(hash) == shastr @test "check $hash" == "check $shastr" uuidstr = "ab"^4 * "-" * "ab"^2 * "-" * "ab"^2 * "-" * "ab"^2 * "-" * "ab"^6 uuid = UUID(uuidstr) -@test uuid == eval(sprint(io -> show(io, uuid))) # check show method +@test uuid == eval(parse(sprint(io -> show(io, uuid)))) # check show method @test string(uuid) == uuidstr @test "check $uuid" == "check $uuidstr" From f1af3b2a76f6c99df0174e575d396d1b9b9e8dfd Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Fri, 11 May 2018 11:12:58 -0700 Subject: [PATCH 5/8] simplify show check --- test/loading.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/loading.jl b/test/loading.jl index 53a71f6baf8dd..e75d89c016da0 100644 --- a/test/loading.jl +++ b/test/loading.jl @@ -88,13 +88,13 @@ using Test shastr = "ab"^20 hash = SHA1(shastr) -@test hash == eval(parse(sprint(io -> show(io, hash)))) # check show method +@test hash == eval(parse(sprint(show, hash))) # check show method @test string(hash) == shastr @test "check $hash" == "check $shastr" uuidstr = "ab"^4 * "-" * "ab"^2 * "-" * "ab"^2 * "-" * "ab"^2 * "-" * "ab"^6 uuid = UUID(uuidstr) -@test uuid == eval(parse(sprint(io -> show(io, uuid)))) # check show method +@test uuid == eval(parse(sprint(show, uuid))) # check show method @test string(uuid) == uuidstr @test "check $uuid" == "check $uuidstr" From b85023235fbdb794e878b4435fac982d62f02222 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Fri, 11 May 2018 11:55:36 -0700 Subject: [PATCH 6/8] use repr --- test/loading.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/loading.jl b/test/loading.jl index e75d89c016da0..a71d51ba5448d 100644 --- a/test/loading.jl +++ b/test/loading.jl @@ -88,13 +88,13 @@ using Test shastr = "ab"^20 hash = SHA1(shastr) -@test hash == eval(parse(sprint(show, hash))) # check show method +@test hash == eval(parse(repr(hash))) # check show method @test string(hash) == shastr @test "check $hash" == "check $shastr" uuidstr = "ab"^4 * "-" * "ab"^2 * "-" * "ab"^2 * "-" * "ab"^2 * "-" * "ab"^6 uuid = UUID(uuidstr) -@test uuid == eval(parse(sprint(show, uuid))) # check show method +@test uuid == eval(parse(repr(uuid))) # check show method @test string(uuid) == uuidstr @test "check $uuid" == "check $uuidstr" From fa2d946f7aaf83bb55c8df5635ec16f27e08af72 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Sun, 13 May 2018 21:58:23 -0700 Subject: [PATCH 7/8] parse -> Meta.parse --- test/loading.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/loading.jl b/test/loading.jl index a71d51ba5448d..9b1176bf0de4f 100644 --- a/test/loading.jl +++ b/test/loading.jl @@ -88,13 +88,13 @@ using Test shastr = "ab"^20 hash = SHA1(shastr) -@test hash == eval(parse(repr(hash))) # check show method +@test hash == eval(Meta.parse(repr(hash))) # check show method @test string(hash) == shastr @test "check $hash" == "check $shastr" uuidstr = "ab"^4 * "-" * "ab"^2 * "-" * "ab"^2 * "-" * "ab"^2 * "-" * "ab"^6 uuid = UUID(uuidstr) -@test uuid == eval(parse(repr(uuid))) # check show method +@test uuid == eval(Meta.parse(repr(uuid))) # check show method @test string(uuid) == uuidstr @test "check $uuid" == "check $uuidstr" From 5a93028ac71aac395c5b801a869a2ee361ffc5d3 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Wed, 16 May 2018 13:48:27 +0800 Subject: [PATCH 8/8] use let binding --- test/loading.jl | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/test/loading.jl b/test/loading.jl index 9b1176bf0de4f..aa1bd829b7396 100644 --- a/test/loading.jl +++ b/test/loading.jl @@ -86,17 +86,19 @@ import UUIDs: UUID, uuid4, uuid_version import Random: shuffle, randstring using Test -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" - -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" +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]