@@ -13,36 +13,46 @@ maybe_ensure_room(::IO, n) = nothing
13
13
# 3. come back to beginning and encode the length
14
14
# 4. shift the encoded data in case we didn't use all 5 bytes allocated for length
15
15
@inline function _with_size (f, io:: IOBuffer , sink, x)
16
- MAX_LENGTH_VARINT_BYTES = 5 # max size of a UInt32 as vbyte
17
- initpos = position (io)
18
- truncate (io, initpos + MAX_LENGTH_VARINT_BYTES) # 1.
19
- seek (io, initpos + MAX_LENGTH_VARINT_BYTES)
20
- f (sink, x) # e.g. _encode(io, x) # 2.
21
- endpos = position (io)
22
- data_len = endpos - initpos - MAX_LENGTH_VARINT_BYTES
23
- seek (io, initpos) # 3.
24
- vbyte_encode (io, UInt32 (data_len)) # --||--
25
- lenght_len = position (io) - initpos
26
- unsafe_copyto! (io. data, initpos + lenght_len + 1 , io. data, initpos + MAX_LENGTH_VARINT_BYTES + 1 , data_len) # 4.
27
- seek (io, initpos + lenght_len + data_len)
28
- truncate (io, initpos + lenght_len + data_len)
16
+ if io. seekable
17
+ MAX_LENGTH_VARINT_BYTES = 5 # max size of a UInt32 as vbyte
18
+ initpos = position (io)
19
+ truncate (io, initpos + MAX_LENGTH_VARINT_BYTES) # 1.
20
+ seek (io, initpos + MAX_LENGTH_VARINT_BYTES)
21
+ f (sink, x) # e.g. _encode(io, x) # 2.
22
+ endpos = position (io)
23
+ data_len = endpos - initpos - MAX_LENGTH_VARINT_BYTES
24
+ seek (io, initpos) # 3.
25
+ vbyte_encode (io, UInt32 (data_len)) # --||--
26
+ lenght_len = position (io) - initpos
27
+ unsafe_copyto! (io. data, initpos + lenght_len + 1 , io. data, initpos + MAX_LENGTH_VARINT_BYTES + 1 , data_len) # 4.
28
+ seek (io, initpos + lenght_len + data_len)
29
+ truncate (io, initpos + lenght_len + data_len)
30
+ else
31
+ vbyte_encode (io, UInt32 (_encoded_size (x)))
32
+ f (sink, x)
33
+ end
29
34
return nothing
30
35
end
31
36
32
37
@inline function _with_size (f, io:: IOBuffer , sink, x, V)
33
- MAX_LENGTH_VARINT_BYTES = 5 # max size of a UInt32 as vbyte
34
- initpos = position (io)
35
- truncate (io, initpos + MAX_LENGTH_VARINT_BYTES) # 1.
36
- seek (io, initpos + MAX_LENGTH_VARINT_BYTES)
37
- f (sink, x, V) # e.g. _encode(io, x, Val{:zigzag}) # 2.
38
- endpos = position (io)
39
- data_len = endpos - initpos - MAX_LENGTH_VARINT_BYTES
40
- seek (io, initpos) # 3.
41
- vbyte_encode (io, UInt32 (data_len)) # --||--
42
- lenght_len = position (io) - initpos
43
- unsafe_copyto! (io. data, initpos + lenght_len + 1 , io. data, initpos + MAX_LENGTH_VARINT_BYTES + 1 , data_len) # 4.
44
- seek (io, initpos + lenght_len + data_len)
45
- truncate (io, initpos + lenght_len + data_len)
38
+ if io. seekable
39
+ MAX_LENGTH_VARINT_BYTES = 5 # max size of a UInt32 as vbyte
40
+ initpos = position (io)
41
+ truncate (io, initpos + MAX_LENGTH_VARINT_BYTES) # 1.
42
+ seek (io, initpos + MAX_LENGTH_VARINT_BYTES)
43
+ f (sink, x, V) # e.g. _encode(io, x, Val{:zigzag}) # 2.
44
+ endpos = position (io)
45
+ data_len = endpos - initpos - MAX_LENGTH_VARINT_BYTES
46
+ seek (io, initpos) # 3.
47
+ vbyte_encode (io, UInt32 (data_len)) # --||--
48
+ lenght_len = position (io) - initpos
49
+ unsafe_copyto! (io. data, initpos + lenght_len + 1 , io. data, initpos + MAX_LENGTH_VARINT_BYTES + 1 , data_len) # 4.
50
+ seek (io, initpos + lenght_len + data_len)
51
+ truncate (io, initpos + lenght_len + data_len)
52
+ else
53
+ vbyte_encode (io, UInt32 (_encoded_size (x, V)))
54
+ f (sink, x, V)
55
+ end
46
56
return nothing
47
57
end
48
58
0 commit comments