Skip to content

Commit 324c990

Browse files
authored
Support CodeUnits (#60)
1 parent 2532b91 commit 324c990

9 files changed

+68
-63
lines changed

src/TranscodingStreams.jl

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ export
55
Noop,
66
NoopStream
77

8+
const ByteData = Union{Vector{UInt8},Base.CodeUnits{UInt8}}
9+
810
include("memory.jl")
911
include("buffer.jl")
1012
include("error.jl")

src/buffer.jl

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ mutable struct Buffer
3535
end
3636
end
3737

38+
function Buffer(data::Base.CodeUnits{UInt8})
39+
return Buffer(Vector{UInt8}(data))
40+
end
41+
3842
function Base.length(buf::Buffer)
3943
return length(buf.data)
4044
end

src/memory.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct Memory
1111
size::UInt
1212
end
1313

14-
function Memory(data::Vector{UInt8})
14+
function Memory(data::ByteData)
1515
return Memory(pointer(data), sizeof(data))
1616
end
1717

src/noop.jl

+6-2
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,14 @@ function Base.unsafe_write(stream::NoopStream, input::Ptr{UInt8}, nbytes::UInt)
107107
end
108108
end
109109

110-
function Base.transcode(::Noop, data::Vector{UInt8})
110+
function Base.transcode(::Type{Noop}, data::ByteData)
111+
return transcode(Noop(), data)
112+
end
113+
114+
function Base.transcode(::Noop, data::ByteData)
111115
# Copy data because the caller may expect the return object is not the same
112116
# as from the input.
113-
return copy(data)
117+
return Vector{UInt8}(data)
114118
end
115119

116120

src/stream.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ Insert `data` to the current reading position of `stream`.
361361
The next `read(stream, sizeof(data))` call will read data that are just
362362
inserted.
363363
"""
364-
function unread(stream::TranscodingStream, data::Vector{UInt8})
364+
function unread(stream::TranscodingStream, data::ByteData)
365365
unsafe_unread(stream, pointer(data), sizeof(data))
366366
end
367367

src/transcode.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ julia> String(decompressed)
2727
2828
```
2929
"""
30-
function Base.transcode(::Type{C}, data::Vector{UInt8}) where C<:Codec
30+
function Base.transcode(::Type{C}, data::ByteData) where C<:Codec
3131
codec = C()
3232
initialize(codec)
3333
try
@@ -70,7 +70,7 @@ julia> String(decompressed)
7070
7171
```
7272
"""
73-
function Base.transcode(codec::Codec, data::Vector{UInt8})
73+
function Base.transcode(codec::Codec, data::ByteData)
7474
# Add `minoutsize` because `transcode` will be called at least two times.
7575
buffer2 = Buffer(
7676
expectedsize(codec, Memory(data)) + minoutsize(codec, Memory(C_NULL, 0)))

test/codecnoop.jl

+41-41
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
source = IOBuffer("foo")
1010
stream = TranscodingStream(Noop(), source)
1111
@test !eof(stream)
12-
@test read(stream) == bb"foo"
12+
@test read(stream) == b"foo"
1313
close(stream)
1414

1515
data = rand(UInt8, 100_000)
@@ -28,15 +28,15 @@
2828
@test read(stream, UInt8) === UInt8('f')
2929
data = Vector{UInt8}(undef, 5)
3030
unsafe_read(stream, pointer(data), 5) === nothing
31-
@test data == bb"oobar"
31+
@test data == b"oobar"
3232
close(stream)
3333

3434
sink = IOBuffer()
3535
stream = TranscodingStream(Noop(), sink)
3636
@test write(stream, "foo") === 3
3737
@test occursin("mode=write", repr(stream))
3838
flush(stream)
39-
@test take!(sink) == bb"foo"
39+
@test take!(sink) == b"foo"
4040
close(stream)
4141

4242
data = rand(UInt8, 100_000)
@@ -49,7 +49,7 @@
4949
@test take!(sink) == data
5050
close(stream)
5151

52-
stream = TranscodingStream(Noop(), IOBuffer(bb"foobarbaz"))
52+
stream = TranscodingStream(Noop(), IOBuffer(b"foobarbaz"))
5353
@test position(stream) === 0
5454
read(stream, UInt8)
5555
@test position(stream) === 1
@@ -71,13 +71,13 @@
7171
@test !ismarked(stream)
7272
close(stream)
7373

74-
stream = TranscodingStream(Noop(), IOBuffer(bb"foobarbaz"))
74+
stream = TranscodingStream(Noop(), IOBuffer(b"foobarbaz"))
7575
seek(stream, 2)
76-
@test read(stream, 3) == bb"oba"
76+
@test read(stream, 3) == b"oba"
7777
seek(stream, 0)
78-
@test read(stream, 3) == bb"foo"
78+
@test read(stream, 3) == b"foo"
7979
seekstart(stream)
80-
@test read(stream, 3) == bb"foo"
80+
@test read(stream, 3) == b"foo"
8181
seekend(stream)
8282
@test eof(stream)
8383
close(stream)
@@ -116,7 +116,7 @@
116116
out = zeros(UInt8, 3)
117117
@test bytesavailable(stream) == 0
118118
@test TranscodingStreams.unsafe_read(stream, pointer(out), 10) == 3
119-
@test out == bb"foo"
119+
@test out == b"foo"
120120
close(stream)
121121

122122
data = rand(UInt8, 1999)
@@ -132,11 +132,11 @@
132132
close(stream)
133133

134134
stream = NoopStream(NoopStream(IOBuffer("foobar")))
135-
@test read(stream) == bb"foobar"
135+
@test read(stream) == b"foobar"
136136
close(stream)
137137

138138
stream = NoopStream(NoopStream(NoopStream(IOBuffer("foobar"))))
139-
@test read(stream) == bb"foobar"
139+
@test read(stream) == b"foobar"
140140
close(stream)
141141

142142
# Two buffers are the same object.
@@ -151,7 +151,7 @@
151151
@test s1.state.buffer1 === s2.state.buffer1 === s3.state.buffer1 ===
152152
s1.state.buffer2 === s2.state.buffer2 === s3.state.buffer2
153153

154-
stream = TranscodingStream(Noop(), IOBuffer(bb"foobar"))
154+
stream = TranscodingStream(Noop(), IOBuffer(b"foobar"))
155155
@test TranscodingStreams.stats(stream).in === Int64(0)
156156
@test TranscodingStreams.stats(stream).out === Int64(0)
157157
read(stream)
@@ -162,7 +162,7 @@
162162
stream = TranscodingStream(Noop(), IOBuffer())
163163
@test TranscodingStreams.stats(stream).in === Int64(0)
164164
@test TranscodingStreams.stats(stream).out === Int64(0)
165-
write(stream, bb"foobar")
165+
write(stream, b"foobar")
166166
flush(stream)
167167
@test TranscodingStreams.stats(stream).in === Int64(6)
168168
@test TranscodingStreams.stats(stream).out === Int64(6)
@@ -176,23 +176,23 @@
176176

177177
stream = NoopStream(IOBuffer("foobar"))
178178
@test bytesavailable(stream) === 0
179-
@test readavailable(stream) == bb""
179+
@test readavailable(stream) == b""
180180
@test read(stream, UInt8) === UInt8('f')
181181
@test bytesavailable(stream) === 5
182-
@test readavailable(stream) == bb"oobar"
182+
@test readavailable(stream) == b"oobar"
183183
close(stream)
184184

185-
data = bb""
185+
data = b""
186186
@test transcode(Noop, data) == data
187187
@test transcode(Noop, data) !== data
188-
data = bb"foo"
188+
data = b"foo"
189189
@test transcode(Noop, data) == data
190190
@test transcode(Noop, data) !== data
191191

192-
data = bb""
192+
data = b""
193193
@test transcode(Noop(), data) == data
194194
@test transcode(Noop(), data) !== data
195-
data = bb"foo"
195+
data = b"foo"
196196
@test transcode(Noop(), data) == data
197197
@test transcode(Noop(), data) !== data
198198

@@ -202,56 +202,56 @@
202202
TranscodingStreams.test_roundtrip_lines(NoopStream, NoopStream)
203203

204204
# switch write => read
205-
stream = NoopStream(IOBuffer(bb"foobar", read=true, write=true))
205+
stream = NoopStream(IOBuffer(b"foobar", read=true, write=true))
206206
@test_throws ArgumentError begin
207-
write(stream, bb"xyz")
207+
write(stream, b"xyz")
208208
read(stream, 3)
209209
end
210210

211211
# switch read => write
212-
stream = NoopStream(IOBuffer(bb"foobar", read=true, write=true))
212+
stream = NoopStream(IOBuffer(b"foobar", read=true, write=true))
213213
@test_throws ArgumentError begin
214214
read(stream, 3)
215-
write(stream, bb"xyz")
215+
write(stream, b"xyz")
216216
end
217217

218218
stream = NoopStream(IOBuffer(""))
219-
@test TranscodingStreams.unread(stream, bb"foo") === nothing
220-
@test read(stream, 3) == bb"foo"
219+
@test TranscodingStreams.unread(stream, b"foo") === nothing
220+
@test read(stream, 3) == b"foo"
221221
close(stream)
222222

223223
stream = NoopStream(IOBuffer("foo"))
224-
@test read(stream, 3) == bb"foo"
225-
@test TranscodingStreams.unread(stream, bb"bar") === nothing
226-
@test read(stream, 3) == bb"bar"
224+
@test read(stream, 3) == b"foo"
225+
@test TranscodingStreams.unread(stream, b"bar") === nothing
226+
@test read(stream, 3) == b"bar"
227227
close(stream)
228228

229229
stream = NoopStream(IOBuffer("foobar"))
230-
@test TranscodingStreams.unread(stream, bb"baz") === nothing
231-
@test read(stream, 3) == bb"baz"
232-
@test read(stream, 3) == bb"foo"
233-
@test read(stream, 3) == bb"bar"
230+
@test TranscodingStreams.unread(stream, b"baz") === nothing
231+
@test read(stream, 3) == b"baz"
232+
@test read(stream, 3) == b"foo"
233+
@test read(stream, 3) == b"bar"
234234
@test eof(stream)
235235
close(stream)
236236

237237
stream = NoopStream(IOBuffer("foobar"))
238-
@test read(stream, 3) == bb"foo"
239-
@test TranscodingStreams.unread(stream, bb"baz") === nothing
240-
@test read(stream, 3) == bb"baz"
241-
@test read(stream, 3) == bb"bar"
238+
@test read(stream, 3) == b"foo"
239+
@test TranscodingStreams.unread(stream, b"baz") === nothing
240+
@test read(stream, 3) == b"baz"
241+
@test read(stream, 3) == b"bar"
242242
@test eof(stream)
243243
close(stream)
244244

245245
stream = NoopStream(IOBuffer("foobar"))
246-
@test read(stream, 3) == bb"foo"
247-
@test read(stream, 3) == bb"bar"
248-
@test TranscodingStreams.unread(stream, bb"baz") === nothing
249-
@test read(stream, 3) == bb"baz"
246+
@test read(stream, 3) == b"foo"
247+
@test read(stream, 3) == b"bar"
248+
@test TranscodingStreams.unread(stream, b"baz") === nothing
249+
@test read(stream, 3) == b"baz"
250250
@test eof(stream)
251251
close(stream)
252252

253253
stream = NoopStream(IOBuffer("foobar"))
254-
@test_throws ArgumentError TranscodingStreams.unsafe_unread(stream, pointer(bb"foo"), -1)
254+
@test_throws ArgumentError TranscodingStreams.unsafe_unread(stream, pointer(b"foo"), -1)
255255
close(stream)
256256

257257
stream = NoopStream(IOBuffer(""))

test/codecquadruple.jl

+8-8
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ function TranscodingStreams.minoutsize(
2929
end
3030

3131
@testset "Quadruple Codec" begin
32-
@test transcode(QuadrupleCodec, bb"") == bb""
33-
@test transcode(QuadrupleCodec, bb"a") == bb"aaaa"
34-
@test transcode(QuadrupleCodec, bb"ab") == bb"aaaabbbb"
35-
@test transcode(QuadrupleCodec(), bb"") == bb""
36-
@test transcode(QuadrupleCodec(), bb"a") == bb"aaaa"
37-
@test transcode(QuadrupleCodec(), bb"ab") == bb"aaaabbbb"
32+
@test transcode(QuadrupleCodec, b"") == b""
33+
@test transcode(QuadrupleCodec, b"a") == b"aaaa"
34+
@test transcode(QuadrupleCodec, b"ab") == b"aaaabbbb"
35+
@test transcode(QuadrupleCodec(), b"") == b""
36+
@test transcode(QuadrupleCodec(), b"a") == b"aaaa"
37+
@test transcode(QuadrupleCodec(), b"ab") == b"aaaabbbb"
3838

3939
#=
4040
data = "x"^1024
@@ -43,11 +43,11 @@ end
4343
=#
4444

4545
stream = TranscodingStream(QuadrupleCodec(), NoopStream(IOBuffer("foo")))
46-
@test read(stream) == bb"ffffoooooooo"
46+
@test read(stream) == b"ffffoooooooo"
4747
close(stream)
4848

4949
stream = NoopStream(TranscodingStream(QuadrupleCodec(), NoopStream(IOBuffer("foo"))))
50-
@test read(stream) == bb"ffffoooooooo"
50+
@test read(stream) == b"ffffoooooooo"
5151
close(stream)
5252

5353
# Buffers are shared.

test/runtests.jl

+3-8
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,12 @@ import TranscodingStreams:
1313
#=ismarked,=# mark!, unmark!, reset!,
1414
makemargin!, emptybuffer!
1515

16-
# binary bytes
17-
macro bb_str(data)
18-
convert(Vector{UInt8}, codeunits(data))
19-
end
20-
2116
@testset "Buffer" begin
2217
buf = Buffer(1024)
2318
@test buf isa Buffer
2419
@test length(buf.data) == 1024
2520

26-
data = bb"foobar"
21+
data = Vector{UInt8}(b"foobar")
2722
buf = Buffer(data)
2823
@test buf isa Buffer
2924
@test bufferptr(buf) === pointer(data)
@@ -69,7 +64,7 @@ end
6964
end
7065

7166
@testset "Memory" begin
72-
data = bb"foobar"
67+
data = Vector{UInt8}(b"foobar")
7368
mem = TranscodingStreams.Memory(pointer(data), sizeof(data))
7469
@test mem isa TranscodingStreams.Memory
7570
@test mem.ptr === pointer(data)
@@ -90,7 +85,7 @@ end
9085
@test_throws BoundsError mem[7] = 0x00
9186
@test_throws BoundsError mem[0] = 0x00
9287

93-
data = bb"foobar"
88+
data = Vector{UInt8}(b"foobar")
9489
mem = TranscodingStreams.Memory(data)
9590
@test mem isa TranscodingStreams.Memory
9691
@test mem.ptr == pointer(data)

0 commit comments

Comments
 (0)