Skip to content

Commit 26fcb98

Browse files
committed
add getindex for Buffer
1 parent df0bb5e commit 26fcb98

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/buffer.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ function Base.length(buf::Buffer)
3232
return length(buf.data)
3333
end
3434

35+
function Base.endof(buf::Buffer)
36+
return buffersize(buf)
37+
end
38+
39+
function Base.getindex(buf::Buffer, i::Integer)
40+
@boundscheck checkbounds(buf, i)
41+
@inbounds return buf.data[i+buf.bufferpos-1]
42+
end
43+
44+
function Base.checkbounds(buf::Buffer, i::Integer)
45+
if !(1 i endof(buf))
46+
throw(BoundsError(buf, i))
47+
end
48+
end
49+
3550
function bufferptr(buf::Buffer)
3651
return pointer(buf.data, buf.bufferpos)
3752
end

test/runtests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ using Base.Test
5757
@test read(stream, UInt8) == data[3]
5858
skip(stream, 5)
5959
@test read(stream, UInt8) == data[9]
60+
61+
s = TranscodingStream(Identity(), IOBuffer(b"baz"))
62+
@test endof(s.state.buffer1) == 0
63+
read(s, UInt8)
64+
@test endof(s.state.buffer1) == 2
65+
@test s.state.buffer1[1] === UInt8('a')
66+
@test s.state.buffer1[2] === UInt8('z')
67+
@test_throws BoundsError s.state.buffer1[0]
68+
@test_throws BoundsError s.state.buffer1[3]
6069
end
6170

6271
Pkg.test("CodecZlib")

0 commit comments

Comments
 (0)