6.6.12.10 Bytevector Slices

As an extension to the R6RS specification, the (rnrs bytevectors gnu) module provides the bytevector-slice procedure, which returns a bytevector aliasing part of an existing bytevector.

Scheme Procedure: bytevector-slice bv offset [size]
C Function: scm_bytevector_slice (bv, offset, size)

Return the slice of bv starting at offset and counting size bytes. When size is omitted, the slice covers all of bv starting from offset. The returned slice shares storage with bv: changes to the slice are visible in bv and vice-versa.

When bv is actually a SRFI-4 uniform vector, its element type is preserved unless offset and size are not aligned on its element type size.

Here is an example showing how to use it:

(use-modules (rnrs bytevectors)
             (rnrs bytevectors gnu))

(define bv (u8-list->bytevector (iota 10)))
(define slice (bytevector-slice bv 2 3))

slice
⇒ #vu8(2 3 4)

(bytevector-u8-set! slice 0 77)
slice
⇒ #vu8(77 3 4)

bv
⇒ #vu8(0 1 77 3 4 5 6 7 8 9)