File tree Expand file tree Collapse file tree 3 files changed +52
-0
lines changed Expand file tree Collapse file tree 3 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -255,3 +255,7 @@ TranscodingStreams.finalize
255
255
TranscodingStreams.startproc
256
256
TranscodingStreams.process
257
257
```
258
+
259
+ ``` @docs
260
+ TranscodingStreams.Memory
261
+ ```
Original file line number Diff line number Diff line change 1
1
# Memory
2
2
# ======
3
3
4
+ """
5
+ A contiguous memory.
6
+
7
+ This type works like a `Vector` method.
8
+ """
4
9
struct Memory
5
10
ptr:: Ptr{UInt8}
6
11
size:: UInt
9
14
function Base. length (mem:: Memory )
10
15
return mem. size
11
16
end
17
+
18
+ function Base. endof (mem:: Memory )
19
+ return Int (mem. size)
20
+ end
21
+
22
+ function Base. checkbounds (mem:: Memory , i:: Integer )
23
+ if ! (1 ≤ i ≤ endof (mem))
24
+ throw (BoundsError (mem, i))
25
+ end
26
+ end
27
+
28
+ function Base. getindex (mem:: Memory , i:: Integer )
29
+ @boundscheck checkbounds (mem, i)
30
+ return unsafe_load (mem. ptr, i)
31
+ end
32
+
33
+ function Base. setindex! (mem:: Memory , val:: UInt8 , i:: Integer )
34
+ @boundscheck checkbounds (mem, i)
35
+ return unsafe_store! (mem. ptr, val, i)
36
+ end
Original file line number Diff line number Diff line change @@ -2,6 +2,29 @@ using TranscodingStreams
2
2
using TranscodingStreams. CodecIdentity
3
3
using Base. Test
4
4
5
+ @testset " Memory" begin
6
+ data = b " foobar"
7
+ mem = TranscodingStreams. Memory (pointer (data), sizeof (data))
8
+ @test mem isa TranscodingStreams. Memory
9
+ @test mem. ptr === pointer (data)
10
+ @test mem. size === length (mem) === UInt (sizeof (data))
11
+ @test endof (mem) === 6
12
+ @test mem[1 ] === UInt8 (' f' )
13
+ @test mem[2 ] === UInt8 (' o' )
14
+ @test mem[3 ] === UInt8 (' o' )
15
+ @test mem[4 ] === UInt8 (' b' )
16
+ @test mem[5 ] === UInt8 (' a' )
17
+ @test mem[6 ] === UInt8 (' r' )
18
+ @test_throws BoundsError mem[7 ]
19
+ @test_throws BoundsError mem[0 ]
20
+ mem[1 ] = UInt8 (' z' )
21
+ @test mem[1 ] === UInt8 (' z' )
22
+ mem[3 ] = UInt8 (' !' )
23
+ @test mem[3 ] === UInt8 (' !' )
24
+ @test_throws BoundsError mem[7 ] = 0x00
25
+ @test_throws BoundsError mem[0 ] = 0x00
26
+ end
27
+
5
28
@testset " Identity Codec" begin
6
29
source = IOBuffer (" " )
7
30
stream = TranscodingStream (Identity (), source)
You can’t perform that action at this time.
0 commit comments