19 Bytevectors
A bytevector is an object that contains a sequence of bytes, that is, exact integers between 0 and 255 inclusive. A bytevector has a length that is fixed at creation time, and as many bytes, indexed from 0
to length-1
. Compared to vectors, bytevectors use less size for each element.
Bytevectors may be mutable or immutable. If an attempt is made to mutate an immutable bytevector, an error is signaled. Two immutable bytevectors are “eq?” iff they are “equal?”. Two mutable bytevectors are “eq?” if they were created by the same constructor call. Two mutable bytevectors are “equal?” iff they have the same length and have “equal?” bytes in each position. There is only one empty bytevector (that is, a bytevector of length 0) and that bytevector is immutable. The bytevector type is encapsulated.
SOURCE NOTE: The report doesn’t currently include bytevectors. They are taken from r7rs scheme.
The primitive type predicate for type bytevector.
bytevector?
returns true iff all the objects inobjects
are of type bytevector.
— Applicative: mutable-bytevector? (mutable-bytevector? objects)
The primitive type predicates for types immutable bytevector and mutable bytevector. These return true iff all the objects in
objects
are of type immutable bytevector or mutable bytevector respectively.
Applicative
make-bytevector
constructs and returns a new mutable bytevector of lengthk
. Ifu8
is specified, then all bytes in the returned bytevector areobj
, otherwise the content of the bytevector is unspecified.
Applicative
bytevector-length
returns the length ofbytevector
.
Applicative
bytevector-ref
returns the byte ofbytevector
at positionk
. Ifk
is out of bounds (i.e. less than0
or greater or equal than(bytevector-length bytevector)
) an error is signaled.
Applicative
bytevector-set!
replaces the byte with indexk
inbytevector
with byteu8
. Ifk
is out of bounds, orbytevector
is immutable, an error is signaled. The result returned bybytevector-set!
is inert.
Applicative
bytevector
contructs and return a new mutable bytevector composed of the byte arguments.
— Applicative: list->bytevector (list->bytevector u8s)
These applicatives convert between bytevectors and lists of bytes. If the list passed to
list->bytevector
contains an object that isn’t a byte, an error is signaled. The objects returned by these applicatives are always mutable.
Applicative
bytevector-copy
constructs and returns a new mutable bytevector with the same length and bytes asbytevector
.
— Applicative: vector->bytevector (vector->bytevector vector)
These applicatives convert between bytevectors and vectors. If a vector containing objects other than bytes (exact integers between 0 and 255 inclusive) is passed to
vector->bytevector
, an error is signaled. The objects returned by these applicatives are always mutable.
bytevector2 should have a length greater than or equal to that of bytevector1.
Copies the bytes in bytevector1 to the corresponding positions in bytevector2. If bytevector2 is immutable, an error is signaled. The result returned by
bytevector-copy!
is inert.
Both
k1
&k2
should be valid indexes inbytevector
. Also it should be the case thatk1 <= k2
.Applicative
bytevector-copy-partial
constructs and returns a new mutable bytevector with lengthk2 - k1
, with the bytes frombytevector
, starting at indexk1
(inclusive) and ending at indexk2
(exclusive).
Both
k1
&k2-1
should be valid indexes inbytevector1
. Also it should be the case thatk1 <= k2
. Bothk3
&k3 + (k2-k1) - 1
should be valid indexes inbytevector2
.Applicative
bytevector-copy-partial!
copies bytes k1 (inclusive) through k2 (exclusive) frombytevector1
to thek2-k1
positions inbytevector2
starting atk3
. Ifbytevector2
is an immutable bytevector, an error is signaled. The result returned bybytevector-copy-partial!
is inert.