Skip to content

Commit 4ac85ed

Browse files
authored
Fixes and tests for 1.11 (#84)
* tests and fixes for 1.11 * copy to buffer in zip_crc32 * fix CI
1 parent 1dedb46 commit 4ac85ed

File tree

6 files changed

+101
-12
lines changed

6 files changed

+101
-12
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jobs:
1616
fail-fast: false
1717
matrix:
1818
version:
19+
- '1.10'
1920
- '1'
2021
- 'pre'
2122
os:

src/reader.jl

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,27 @@ function unsafe_crc32(p::Ptr{UInt8}, nb::UInt, crc::UInt32)::UInt32
55
)
66
end
77

8-
const ByteArray = Union{
9-
Base.CodeUnits{UInt8, String},
10-
Vector{UInt8},
11-
Base.FastContiguousSubArray{UInt8,1,Base.CodeUnits{UInt8,String}},
12-
Base.FastContiguousSubArray{UInt8,1,Vector{UInt8}}
13-
}
8+
if VERSION v"1.11"
9+
const ByteArray = Union{
10+
Base.CodeUnits{UInt8, String},
11+
Vector{UInt8},
12+
Base.FastContiguousSubArray{UInt8,1,Base.CodeUnits{UInt8,String}},
13+
Base.FastContiguousSubArray{UInt8,1,Vector{UInt8}},
14+
Memory{UInt8},
15+
Base.FastContiguousSubArray{UInt8,1,Memory{UInt8}}
16+
}
17+
else
18+
const ByteArray = Union{
19+
Base.CodeUnits{UInt8, String},
20+
Vector{UInt8},
21+
Base.FastContiguousSubArray{UInt8,1,Base.CodeUnits{UInt8,String}},
22+
Base.FastContiguousSubArray{UInt8,1,Vector{UInt8}}
23+
}
24+
end
1425

1526
# version of String(v::AbstractVector{UInt8}) that works consistently.
1627
function bytes2string(v::AbstractVector{UInt8})::String
17-
String(v)
18-
end
19-
function bytes2string(v::Vector{UInt8})::String
20-
GC.@preserve v unsafe_string(pointer(v), length(v))
28+
String(view(v,:))
2129
end
2230

2331
"""
@@ -32,7 +40,17 @@ function zip_crc32(data::ByteArray, crc::UInt32=UInt32(0))::UInt32
3240
end
3341

3442
function zip_crc32(data::AbstractVector{UInt8}, crc::UInt32=UInt32(0))::UInt32
35-
zip_crc32(collect(data), crc)
43+
start::Int64 = firstindex(data)
44+
n::Int64 = length(data)
45+
offset::Int64 = 0
46+
buf = Vector{UInt8}(undef, min(n, Int64(24576)))
47+
while offset < n
48+
nb = min(n-offset, Int64(24576))
49+
copyto!(buf, 1, data, offset + start, nb)
50+
crc = zip_crc32(view(buf, 1:nb), crc)
51+
offset += nb
52+
end
53+
crc
3654
end
3755

3856
@inline readle(io::IO, ::Type{UInt64}) = UInt64(readle(io, UInt32)) | UInt64(readle(io, UInt32))<<32

test/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[sources]
2+
ZipArchives = {path = ".."}
3+
14
[deps]
25
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
36
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

test/test_bytes2string.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,24 @@ using OffsetArrays: Origin
5656
@test a == [0x62,0x62,0x63]
5757
@test c == [0x62,0x62,0x63]
5858
@test s == "abc"
59+
60+
if VERSION v"1.11"
61+
a = Base.Memory{UInt8}(undef, 3)
62+
a .= [0x41,0x42,0x43]
63+
s = ZipArchives.bytes2string(a)
64+
@test s == "ABC"
65+
@test a == [0x41,0x42,0x43]
66+
a[1] = 0x43
67+
@test s == "ABC"
68+
@test a == [0x43,0x42,0x43]
69+
70+
a = view(Base.Memory{UInt8}(undef, 3), 1:3)
71+
a .= [0x41,0x42,0x43]
72+
s = ZipArchives.bytes2string(a)
73+
@test s == "ABC"
74+
@test a == [0x41,0x42,0x43]
75+
a[1] = 0x43
76+
@test s == "ABC"
77+
@test a == [0x43,0x42,0x43]
78+
end
5979
end

test/test_reader.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,25 @@ end
306306
@test zip_readentry(r, 1) == codeunits("KYDtLOxn")
307307
end
308308

309+
if VERSION v"1.11"
310+
@testset "reading from memory" begin
311+
io = IOBuffer()
312+
ZipWriter(io) do w
313+
zip_writefile(w, "foo.txt", codeunits("KYDtLOxn"))
314+
end
315+
data = take!(io)
316+
n = length(data)
317+
a = Base.Memory{UInt8}(undef, n)
318+
a .= data
319+
r = ZipReader(a)
320+
@test zip_names(r) == ["foo.txt"]
321+
@test zip_readentry(r, 1) == codeunits("KYDtLOxn")
322+
r = ZipReader(view(a, 1:length(a)))
323+
@test zip_names(r) == ["foo.txt"]
324+
@test zip_readentry(r, 1) == codeunits("KYDtLOxn")
325+
end
326+
end
327+
309328
function rewrite_zip(old::AbstractString, new::AbstractString)
310329
d = mmap(old)
311330
try

test/test_writer.jl

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,32 @@ end
439439
end
440440
r = ZipReader(take!(out))
441441
@test zip_readentry(r, "data.txt") == 0x01:0x0f
442-
end
442+
end
443+
444+
if VERSION v"1.11"
445+
@testset "zip_writefile on memory" begin
446+
data = [0x41,0x42,0x43]
447+
a = Base.Memory{UInt8}(undef, 3)
448+
a .= data
449+
@test zip_crc32(data) == zip_crc32(a)
450+
out = IOBuffer()
451+
ZipWriter(out) do w
452+
zip_writefile(w, "data.txt", a)
453+
end
454+
r = ZipReader(take!(out))
455+
@test zip_readentry(r, "data.txt") == data
456+
end
457+
@testset "zip_writefile on view of memory" begin
458+
data = [0x41,0x42,0x43]
459+
m = Base.Memory{UInt8}(undef, 3)
460+
m .= data
461+
a = view(m, :)
462+
@test zip_crc32(data) == zip_crc32(a)
463+
out = IOBuffer()
464+
ZipWriter(out) do w
465+
zip_writefile(w, "data.txt", a)
466+
end
467+
r = ZipReader(take!(out))
468+
@test zip_readentry(r, "data.txt") == data
469+
end
470+
end

0 commit comments

Comments
 (0)