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 inobjectsare 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
objectsare of type immutable bytevector or mutable bytevector respectively.
Applicative
make-bytevectorconstructs and returns a new mutable bytevector of lengthk. Ifu8is specified, then all bytes in the returned bytevector areobj, otherwise the content of the bytevector is unspecified.
Applicative
bytevector-lengthreturns the length ofbytevector.
Applicative
bytevector-refreturns the byte ofbytevectorat positionk. Ifkis out of bounds (i.e. less than0or greater or equal than(bytevector-length bytevector)) an error is signaled.
Applicative
bytevector-set!replaces the byte with indexkinbytevectorwith byteu8. Ifkis out of bounds, orbytevectoris immutable, an error is signaled. The result returned bybytevector-set!is inert.
Applicative
bytevectorcontructs 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->bytevectorcontains an object that isn’t a byte, an error is signaled. The objects returned by these applicatives are always mutable.
Applicative
bytevector-copyconstructs 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&k2should be valid indexes inbytevector. Also it should be the case thatk1 <= k2.Applicative
bytevector-copy-partialconstructs 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-1should be valid indexes inbytevector1. Also it should be the case thatk1 <= k2. Bothk3&k3 + (k2-k1) - 1should be valid indexes inbytevector2.Applicative
bytevector-copy-partial!copies bytes k1 (inclusive) through k2 (exclusive) frombytevector1to thek2-k1positions inbytevector2starting atk3. Ifbytevector2is an immutable bytevector, an error is signaled. The result returned bybytevector-copy-partial!is inert.